## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment  = "#>"
)
library(iDIFr)

## ----simulate-----------------------------------------------------------------
set.seed(42)
dat <- simulate_dif(
  n_persons  = 600,
  n_items    = 20,
  n_groups   = 2,
  dif_items  = c(3, 7, 12),  # these items have DIF
  dif_effect = 0.9,
  dif_type   = "uniform"
)

head(dat[c(1:5, 21)])  # first 5 items + group column

## ----check-groups, eval = FALSE-----------------------------------------------
# check_groups(dat, group = ~ group)

## ----check-intersectional, eval = FALSE---------------------------------------
# # Add nationality variable for illustration
# dat$nationality <- sample(c("UK", "DE", "FR"), 600, replace = TRUE)
# dat$age_band    <- sample(c("18-30", "31-45", "46+"), 600, replace = TRUE)
# 
# check_groups(dat, group = ~ group * nationality * age_band)

## ----run-idifr, eval = FALSE--------------------------------------------------
# result <- idifr(
#   data   = dat,
#   items  = 1:20,
#   group  = ~ group,
#   method = c("LR", "LRT")
# )

## ----explore, eval = FALSE----------------------------------------------------
# # Flagged items with effect sizes
# print(result)
# 
# # Full breakdown by method
# summary(result)
# 
# # Effect size heatmap
# plot(result)
# 
# # Method concordance
# plot(result, type = "concordance")
# 
# # Flat data frame for your own analysis
# df <- tidy(result)

## ----intersectional, eval = FALSE---------------------------------------------
# result_intersectional <- idifr(
#   data   = dat,
#   items  = 1:20,
#   group  = ~ group * nationality * age_band,  # crossing all three variables
#   method = c("LR", "LRT")
# )
# 
# print(result_intersectional)

## ----merge, eval = FALSE------------------------------------------------------
# grp <- check_groups(dat, group = ~ group * nationality * age_band)
# 
# merged_dat <- merge_groups(
#   grp,
#   age_band = list("18-45" = c("18-30", "31-45"))  # combine two age bands
# )
# 
# # Re-run with merged groups
# result_merged <- idifr(merged_dat, 1:20,
#                        group  = ~ group * nationality * age_band,
#                        method = c("LR", "LRT"))

## ----ica-example, eval = FALSE------------------------------------------------
# ica_res <- idifr(
#   data   = dat,
#   items  = 1:20,
#   group  = ~ group * nationality * age_band,
#   method = "LR",
#   ica    = TRUE
# )
# 
# print(ica_res)                 # includes the ICA classification section
# tidy(ica_res, table = "ica")   # flat ICA classification table

