Differential Item Functioning & Explanatory IRT (R)¶
difR for DIF detection, lme4 for the explanatory (LLTM) model¶
The R counterpart to dif_python.ipynb. Where the Python notebook builds the DIF-plus-impact model from scratch (Albert–Chib augmentation) and cross-checks in PyMC, this notebook uses the field-standard tools: difR — the standard package for differential item functioning — flags biased items by gender with a logistic-regression test, and lme4 fits the explanatory (linear-logistic-test-model) IRT that predicts item difficulty from item features. Data: the verbal-aggression benchmark (do items function the same for men and women?).
options(repr.plot.width=11, repr.plot.height=5)
.libPaths(c("C:/Users/user/R/win-library/4.6", .libPaths())); suppressMessages({library(difR); library(lme4)})
BLUE<-"#2b6cb0"; RED<-"#c53030"; GREEN<-"#2f855a"; GREY<-"#718096"
V<-read.csv("verbagg.csv"); grp<-V$Gender; X<-V[,-1]
cat(nrow(X), "respondents x", ncol(X), "items;", sum(grp=="M"), "men /", sum(grp=="F"), "women\n")
Warning message: "package 'difR' was built under R version 4.6.1"
316 respondents x 24 items; 73 men / 243 women
1. DIF detection with difR¶
difLogistic fits, for each item, a logistic regression of the response on the total score and the group, and tests whether the group term is needed — the standard logistic-regression DIF procedure (Swaminathan & Rogers). Items where it is needed function differently for men and women at the same aggression level.
dl <- difLogistic(as.data.frame(X), group=grp, focal.name="M", type="udif", p.adjust.method="BH")
difidx <- dl$DIFitems
cat("items flagged with gender DIF (BH-adjusted):", if(length(difidx)) paste(colnames(X)[difidx], collapse=", ") else "none", "\n")
cat(sprintf("(%d of %d items)\n", length(difidx), ncol(X)))
par(mar=c(8,4,3,1)); st<-dl$Logistik # DIF statistic per item
bcol<-rep(GREY, ncol(X)); bcol[difidx]<-RED
barplot(st, names.arg=colnames(X), las=2, col=bcol, cex.names=.55, ylab="logistic DIF statistic",
main="Gender DIF in verbal-aggression items (red = flagged)")
abline(h=qchisq(0.95,1), lty=2)
cat("Several items show gender DIF: at equal overall aggression, men and women endorse them differently. As the\n")
cat("Python notebook shows, women more readily endorse 'want to shout' items, men 'do curse/scold' items.\n")
items flagged with gender DIF (BH-adjusted): S2WantShout, S2DoCurse, S2DoScold, S3DoCurse
(4 of 24 items)
Several items show gender DIF: at equal overall aggression, men and women endorse them differently. As the
Python notebook shows, women more readily endorse 'want to shout' items, men 'do curse/scold' items.
2. Explanatory IRT — the linear logistic test model with lme4¶
Instead of a free difficulty per item, explanatory IRT predicts difficulty from item features. A Rasch model is a logistic mixed model (person random intercept = ability), so lme4::glmer fits the linear logistic test model directly: the response depends on the item's behaviour type, whether it is want or do, and whether the situation blames self or other, plus a person random intercept.
data(VerbAgg) # long format with item features
m <- glmer(r2 ~ btype + situ + mode + (1|id), data=VerbAgg, family=binomial)
fe <- fixef(m); print(round(summary(m)$coefficients[,c(1,2,4)], 3))
cat(sprintf("\nperson random-intercept SD (spread of the aggression trait) = %.2f\n", sqrt(as.numeric(VarCorr(m)$id))))
par(mar=c(4,8,3,1)); b<-fe[-1]
barplot(b, horiz=TRUE, las=1, col=ifelse(b>0, GREEN, RED), xlab="effect on log-odds of endorsing",
main="What makes a verbal-aggression act easy to endorse (LLTM)")
abline(v=0)
cat("Wanting is far easier to endorse than doing; cursing easier than scolding, shouting hardest of all; blaming\n")
cat("others easier than oneself. Item features explain the difficulties -- the explanatory (linear-logistic-test-model) view.\n")
Estimate Std. Error Pr(>|z|) (Intercept) 1.744 0.102 0 btypescold -1.055 0.069 0 btypeshout -2.042 0.075 0 situself -1.028 0.058 0 modedo -0.672 0.057 0
person random-intercept SD (spread of the aggression trait) = 1.34
Wanting is far easier to endorse than doing; cursing easier than scolding, shouting hardest of all; blaming
others easier than oneself. Item features explain the difficulties -- the explanatory (linear-logistic-test-model) view.
3. Summary¶
The standard R tools reproduce the Python results. difR's logistic-regression procedure flags the verbal-aggression items that function differently by gender — the same items the from-scratch DIF model isolates — showing that men and women, at equal aggression, endorse different acts. lme4 fits the explanatory IRT / linear logistic test model as a logistic GLMM: item difficulty is predicted from behaviour type, want-vs-do and self-vs-other, and those features explain most of the variation, with a person random intercept standing in for the ability.
difR is the field-standard DIF package and the Rasch-as-GLMM connection lets lme4 fit explanatory IRT directly — both the frequentist cross-check on the Bayesian from-scratch DIF-plus-impact model and PyMC fit in dif_python.ipynb. DIF is a group×item interaction on the 2PL; the LLTM is a regression on item features — the same explanatory move as latent class regression. Next, the optional capstone: IRT model comparison (1PL vs 2PL vs 3PL).