dat <- read.table("rasch-data.txt")
m <- nrow(dat)
n <- ncol(dat)
names(dat) <- sprintf("I%.2i", 1:10)
head(dat)## I01 I02 I03 I04 I05 I06 I07 I08 I09 I10
## 1 1 1 0 0 1 0 1 1 1 1
## 2 0 1 1 0 1 0 1 0 1 1
## 3 1 1 1 1 1 0 1 0 1 1
## 4 0 0 0 0 0 0 0 0 1 1
## 5 1 0 1 0 1 1 1 1 1 1
## 6 0 1 0 0 1 0 1 1 0 1
library(mirt)
m1 <- mirt(dat, model = 1, itemtype = "Rasch", verbose = FALSE)
## Parameters and estimates
pars <- coef(m1, IRTpars = TRUE, as.data.frame = TRUE)
m1.epsi <- pars[paste0(names(dat), ".b"), ] # item difficulty
m1.theta <- fscores(m1, method = "MAP")[, 1] # ability (empirical Bayes, MAP)library(lme4)
## Ability as random intercept
m2 <- glmer(response ~ 0 + item + (1 | subj), dat2, binomial)
m2.epsi <- -lme4::fixef(m2) # item difficulty
m2.theta <- ranef(m2)$subj[, 1] # ability estimates
## Comparing MML vs. GLMM
cbind( mml = m1.epsi - mean(m1.epsi),
glmm = m2.epsi - mean(m2.epsi))## mml glmm
## I01.b -0.01170977 -0.0118220
## I02.b -0.53094115 -0.5312392
## I03.b 0.15104384 0.1512207
## I04.b 1.53685130 1.5351476
## I05.b -0.85850602 -0.8584868
## I06.b 0.60410637 0.6046813
## I07.b -0.99228054 -0.9919966
## I08.b 0.80601046 0.8063718
## I09.b 0.31513898 0.3155574
## I10.b -1.01971347 -1.0194340
## mml glmm
## [1,] 0.56506075 0.55534053
## [2,] 0.27353079 0.26889143
## [3,] 0.86690569 0.85141168
## [4,] -0.88847350 -0.87297411
## [5,] 0.86690569 0.85141168
## [6,] -0.01279103 -0.01268411
## [7,] -0.58880892 -0.57896997
## [8,] 0.27353079 0.26889143
## [9,] 1.18493971 1.16251506
## [10,] -0.01279103 -0.01268411
## [11,] -0.88847350 -0.87297411
## [12,] 0.27353079 0.26889143
See: https://cran.r-project.org/web/packages/difR/refman/difR.html#verbal
aggregate(r2 == "Y" ~ btype + mode + situ, VerbAgg, mean) |>
lattice::xyplot(
`r2 == "Y"` ~ btype | situ, data = _, groups = mode, type = c("g", "b"),
pch = 16, auto.key = TRUE,
xlab = "Behavior type", ylab = "Percent 'Yes' responses"
)## Anger Gender item resp id
## Min. :11 F:5832 S1WantCurse: 316 no :3973 1 : 24
## 1st Qu.:17 M:1752 S1WantScold: 316 perhaps:2081 2 : 24
## Median :19 S1WantShout: 316 yes :1530 3 : 24
## Mean :20 S2WantCurse: 316 4 : 24
## 3rd Qu.:23 S2WantScold: 316 5 : 24
## Max. :39 S2WantShout: 316 6 : 24
## (Other) :5688 (Other):7440
## btype situ mode r2
## curse:2528 other:3792 want:3792 N:3973
## scold:2528 self :3792 do :3792 Y:3611
## shout:2528
##
##
##
##
## situ other self
## substr(item, 1, 2) S1 S2 S3 S4 S1 S2 S3 S4
## btype mode
## curse want 316 316 0 0 0 0 316 316
## do 316 316 0 0 0 0 316 316
## scold want 316 316 0 0 0 0 316 316
## do 316 316 0 0 0 0 316 316
## shout want 316 316 0 0 0 0 316 316
## do 316 316 0 0 0 0 316 316
## Rasch model (1PL, one-hot coded item indicator)
m1 <- glmer(r2 ~ 0 + item + (1 | id), VerbAgg, binomial)
print(m1)## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: binomial ( logit )
## Formula: r2 ~ 0 + item + (1 | id)
## Data: VerbAgg
## AIC BIC logLik -2*log(L) df.resid
## 8128.519 8301.863 -4039.259 8078.519 7559
## Random effects:
## Groups Name Std.Dev.
## id (Intercept) 1.379
## Number of obs: 7584, groups: id, 316
## Fixed Effects:
## itemS1WantCurse itemS1WantScold itemS1WantShout itemS2WantCurse
## 1.22932 0.57264 0.08828 1.75706
## itemS2WantScold itemS2WantShout itemS3WantCurse itemS3WantScold
## 0.71566 0.01875 0.53682 -0.67784
## itemS3WantShout itemS4wantCurse itemS4WantScold itemS4WantShout
## -1.51889 1.09012 -0.34092 -1.03664
## itemS1DoCurse itemS1DoScold itemS1DoShout itemS2DoCurse
## 1.22897 0.39648 -0.86308 0.87918
## itemS2DoScold itemS2DoShout itemS3DoCurse itemS3DoScold
## -0.04913 -1.47350 -0.20184 -1.49716
## itemS3DoShout itemS4DoCurse itemS4DoScold itemS4DoShout
## -2.96666 0.71532 -0.37672 -1.99209
## Linear logistic test model (LLTM)
m2 <- glmer(r2 ~ 0 + btype + mode + situ + (1 | id), VerbAgg, binomial)
print(m2)## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: binomial ( logit )
## Formula: r2 ~ 0 + btype + mode + situ + (1 | id)
## Data: VerbAgg
## AIC BIC logLik -2*log(L) df.resid
## 8249.935 8291.537 -4118.967 8237.935 7578
## Random effects:
## Groups Name Std.Dev.
## id (Intercept) 1.339
## Number of obs: 7584, groups: id, 316
## Fixed Effects:
## btypecurse btypescold btypeshout modedo situself
## 1.7437 0.6885 -0.2985 -0.6716 -1.0279
## Random persons, fixed items
m1
## Random persons, random items
m5 <- glmer(r2 ~ 1 + (1 | id) + (1 | item), VerbAgg, binomial)
## Fixed persons, fixed items (joint ML)
# m6 <- glmer(r2 ~ 0 + id + item + (1 | item), VerbAgg, binomial)
## Fixed persons, random items
# m7 <- glmer(r2 ~ 0 + id + (1 | item), VerbAgg, binomial)
## Latent regression 1PL model (person property covariates)
m8 <- glmer(r2 ~ 0 + item + Anger + Gender + (1 | id), VerbAgg, binomial)
## Heteroscedasticity on gender
m9 <- glmer(r2 ~ 0 + item + Anger + Gender
+ (0 + Gender | id), VerbAgg, binomial)
## Multilevel model (person partition covariates)
# m10 <- glmer(r2 ~ 0 + item + (1 | id) + (1 | group), VerbAgg, binomial)## DIF w.r.t. gender for 8 items
dif <- with(VerbAgg,
factor(0 + (Gender == "F" & mode == "do" & btype != "shout")))
m11 <- glmer(r2 ~ 0 + item + dif + Gender + (1 | id), VerbAgg, binomial)
print(m11)## Generalized linear mixed model fit by maximum likelihood (Laplace
## Approximation) [glmerMod]
## Family: binomial ( logit )
## Formula: r2 ~ 0 + item + dif + Gender + (1 | id)
## Data: VerbAgg
## AIC BIC logLik -2*log(L) df.resid
## 8081.231 8268.443 -4013.615 8027.231 7557
## Random effects:
## Groups Name Std.Dev.
## id (Intercept) 1.383
## Number of obs: 7584, groups: id, 316
## Fixed Effects:
## itemS1WantCurse itemS1WantScold itemS1WantShout itemS2WantCurse
## 1.23448 0.57737 0.09174 1.76190
## itemS2WantScold itemS2WantShout itemS3WantCurse itemS3WantScold
## 0.72104 0.02319 0.54222 -0.67662
## itemS3WantShout itemS4wantCurse itemS4WantScold itemS4WantShout
## -1.51904 1.09523 -0.33846 -1.03517
## itemS1DoCurse itemS1DoScold itemS1DoShout itemS2DoCurse
## 2.02903 1.18123 -0.86170 1.67362
## itemS2DoScold itemS2DoShout itemS3DoCurse itemS3DoScold
## 0.72547 -1.47401 0.56823 -0.75056
## itemS3DoShout itemS4DoCurse itemS4DoScold itemS4DoShout
## -2.97022 1.50577 0.39194 -1.99313
## dif1 GenderM
## -1.00556 -0.03796
## Data: VerbAgg
## Models:
## m11a: r2 ~ 0 + item + Gender + (1 | id)
## m11: r2 ~ 0 + item + dif + Gender + (1 | id)
## npar AIC BIC logLik -2*log(L) Chisq Df Pr(>Chisq)
## m11a 26 8128.1 8308.3 -4038.0 8076.1
## m11 27 8081.2 8268.4 -4013.6 8027.2 48.827 1 2.796e-12 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1