# \donttest{
# Load required package
library("dplyr")
## Uniform DIF Detection
###############################################
# (1) Simulate data with true uniform DIF
###############################################
# Import the "-prm.txt" output file from flexMIRT
flex_sam <- system.file("extdata", "flexmirt_sample-prm.txt", package = "irtQ")
# Select 36 3PLM items that are non-DIF
par_nstd <-
bring.flexmirt(file = flex_sam, "par")$Group1$full_df %>%
dplyr::filter(.data$model == "3PLM") %>%
dplyr::filter(dplyr::row_number() %in% 1:36) %>%
dplyr::select(1:6)
par_nstd$id <- paste0("nondif", 1:36)
# Generate four new items to contain uniform DIF
difpar_ref <-
shape_df(
par.drm = list(a = c(0.8, 1.5, 0.8, 1.5), b = c(0.0, 0.0, -0.5, -0.5), g = 0.15),
item.id = paste0("dif", 1:4), cats = 2, model = "3PLM"
)
# Introduce uniform DIF in the focal group by shifting b-parameters
difpar_foc <-
difpar_ref %>%
dplyr::mutate_at(.vars = "par.2", .funs = function(x) x + rep(0.7, 4))
# Combine the 4 DIF and 36 non-DIF items for both reference and focal groups
# Threfore, the first four items now exhibit uniform DIF
par_ref <- rbind(difpar_ref, par_nstd)
par_foc <- rbind(difpar_foc, par_nstd)
# Generate true theta values
set.seed(123)
theta_ref <- rnorm(500, 0.0, 1.0)
theta_foc <- rnorm(500, 0.0, 1.0)
# Simulate response data
resp_ref <- simdat(par_ref, theta = theta_ref, D = 1)
resp_foc <- simdat(par_foc, theta = theta_foc, D = 1)
data <- rbind(resp_ref, resp_foc)
###############################################
# (2) Estimate item and ability parameters
# using the aggregated data
###############################################
# Estimate item parameters
est_mod <- est_irt(data = data, D = 1, model = "3PLM")
est_par <- est_mod$par.est
# Estimate ability parameters using ML
theta_est <- est_score(x = est_par, data = data, method = "ML")
score <- theta_est$est.theta
se <- theta_est$se.theta
###############################################
# (3) Conduct DIF analysis
###############################################
# Create a vector of group membership indicators
# where '1' indicates the focal group
group <- c(rep(0, 500), rep(1, 500))
# (a)-1 Compute the CATSIB statistic using provided scores,
# without purification
dif_1 <- catsib(
x = NULL, data = data, D = 1, score = score, se = se, group = group, focal.name = 1,
weight.group = "comb", alpha = 0.05, missing = NA, purify = FALSE
)
print(dif_1)
# (a)-2 Compute the CATSIB statistic using provided scores,
# with purification
dif_2 <- catsib(
x = est_par, data = data, D = 1, score = score, se = se, group = group, focal.name = 1,
weight.group = "comb", alpha = 0.05, missing = NA, purify = TRUE
)
print(dif_2)
# }
Run the code above in your browser using DataLab