Martijn Wieling
Computational Linguistics Research Group
R
family="binomial"R: plogis(x)
head(eye)
#   Subject   Item TargetDefinite TargetNeuter TargetColor TargetPlace CompColor CompPlace TrialID
# 1    S300   boom              1            0       green           3     brown         2       1
# 2    S300  bloem              1            0         red           4     green         2       2
# 3    S300  anker              1            1      yellow           3    yellow         2       3
# 4    S300   auto              1            0       green           3     brown         2       4
# 5    S300   boek              1            1        blue           4      blue         3       5
# 6    S300 varken              1            1       brown           1     green         3       6
#   Age IsMale Edulevel SameColor SameGender TargetFocus CompFocus
# 1  52      0        1         0          1          43        41
# 2  52      0        1         0          0         100         0
# 3  52      0        1         1          1          73        27
# 4  52      0        1         0          0         100         0
# 5  52      0        1         1          0          12        21
# 6  52      0        1         0          0           0        51
  lme4 version 1.1.12)library(lme4)
summary(model <- glmer(cbind(TargetFocus, CompFocus) ~ (1 | Subject) + (1 | Item), data = eye, family = "binomial"))  # intercept-only model
# Generalized linear mixed model fit by maximum likelihood (Laplace Approximation) ['glmerMod']
#  Family: binomial  ( logit )
# Formula: cbind(TargetFocus, CompFocus) ~ (1 | Subject) + (1 | Item)
#    Data: eye
# 
#      AIC      BIC   logLik deviance df.resid 
#   125387   125404   -62690   125381     2263 
# 
# Scaled residuals: 
#    Min     1Q Median     3Q    Max 
# -42.14  -4.12   2.37   5.17  11.40 
# 
# Random effects:
#  Groups  Name        Variance Std.Dev.
#  Item    (Intercept) 0.106    0.326   
#  Subject (Intercept) 0.345    0.588   
# Number of obs: 2266, groups:  Item, 48; Subject, 28
# 
# Fixed effects:
#             Estimate Std. Error z value Pr(>|z|)    
# (Intercept)    0.848      0.121    7.02  2.3e-12 ***
# ---
# Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  summary(model)$coef
#             Estimate Std. Error z value Pr(>|z|)
# (Intercept)    0.848      0.121    7.02 2.28e-12
plogis(fixef(model)["(Intercept)"])
# (Intercept) 
#         0.7
# In the Gaussian case, the REML parameter needs to be set to TRUE when comparing models only
# differing in the random effects; For glmer, this parameter is absent, as it only allows ML fitting.
model1 <- glmer(cbind(TargetFocus, CompFocus) ~ (1 | Subject), data = eye, family = "binomial")
model2 <- glmer(cbind(TargetFocus, CompFocus) ~ (1 | Subject) + (1 | Item), data = eye, family = "binomial")
AIC(model1) - AIC(model2)
# [1] 2917
model3 <- glmer(cbind(TargetFocus, CompFocus) ~ SameColor + (1 | Subject) + (1 | Item), data = eye, family = "binomial")
summary(model3)$coef
#             Estimate Std. Error z value Pr(>|z|)
# (Intercept)     1.68     0.1209    13.9 7.44e-44
# SameColor      -1.48     0.0118  -125.5 0.00e+00
SameColor is highly significantSameColor varies per subject model4 <- glmer(cbind(TargetFocus, CompFocus) ~ SameColor + (1 + SameColor | Subject) + (1 | Item), data = eye, 
    family = "binomial")  # the random slope SameColor needs to be correlated with the random intercept
AIC(model3) - AIC(model4)
# [1] 2218
summary(model4)$varcor
#  Groups  Name        Std.Dev. Corr 
#  Item    (Intercept) 0.359         
#  Subject (Intercept) 1.251         
#          SameColor   0.949    -0.95
summary(model4)$coef
#             Estimate Std. Error z value Pr(>|z|)
# (Intercept)     1.89      0.245     7.7 1.36e-14
# SameColor      -1.71      0.184    -9.3 1.40e-20
SameColor is still highly significantmodel5 <- glmer(cbind(TargetFocus, CompFocus) ~ SameColor + SameGender + (1 + SameColor | Subject) + 
    (1 | Item), data = eye, family = "binomial")
summary(model5)$coef
#             Estimate Std. Error z value Pr(>|z|)
# (Intercept)   1.8536     0.2462    7.53 5.08e-14
# SameColor    -1.7124     0.1846   -9.28 1.74e-20
# SameGender    0.0742     0.0115    6.47 9.97e-11
model6 <- glmer(cbind(TargetFocus, CompFocus) ~ SameColor + SameGender + TargetNeuter + (1 + SameColor | 
    Subject) + (1 | Item), data = eye, family = "binomial")
summary(model6)$coef
#              Estimate Std. Error z value Pr(>|z|)
# (Intercept)    1.9398     0.2509    7.73 1.06e-14
# SameColor     -1.7125     0.1845   -9.28 1.65e-20
# SameGender     0.0742     0.0115    6.47 9.92e-11
# TargetNeuter  -0.1723     0.1015   -1.70 8.96e-02
  model7 <- glmer(cbind(TargetFocus, CompFocus) ~ SameColor + SameGender * TargetNeuter + (1 + SameColor | 
    Subject) + (1 | Item), data = eye, family = "binomial")
summary(model7)$coef
#                         Estimate Std. Error z value  Pr(>|z|)
# (Intercept)                2.067     0.2512    8.23  1.88e-16
# SameColor                 -1.716     0.1846   -9.30  1.43e-20
# SameGender                -0.174     0.0164  -10.63  2.27e-26
# TargetNeuter              -0.416     0.1026   -4.05  5.13e-05
# SameGender:TargetNeuter    0.488     0.0230   21.24 4.35e-100
# As glmer has only a single fitting option (ML), we can immediately compare model7 (SameColor +
# SameGender * TargetNeuter) to the best previous model model5 (SameColor + SameGender)
AIC(model5) - AIC(model7)
# [1] 451
# set a reference level for the factor
eye$TargetColor <- relevel(eye$TargetColor, "brown")
model8 <- glmer(cbind(TargetFocus, CompFocus) ~ SameColor + SameGender * TargetNeuter + TargetColor + 
    (1 + SameColor | Subject) + (1 | Item), data = eye, family = "binomial")
summary(model8)$coef
#                         Estimate Std. Error z value  Pr(>|z|)
# (Intercept)                1.707     0.2672    6.39  1.65e-10
# SameColor                 -1.717     0.1848   -9.29  1.59e-20
# SameGender                -0.174     0.0164  -10.63  2.16e-26
# TargetNeuter              -0.415     0.0880   -4.72  2.32e-06
# TargetColorblue            0.275     0.1433    1.92  5.50e-02
# TargetColorgreen           0.494     0.1435    3.44  5.78e-04
# TargetColorred             0.456     0.1434    3.18  1.47e-03
# TargetColoryellow          0.502     0.1434    3.50  4.67e-04
# SameGender:TargetNeuter    0.488     0.0230   21.24 3.76e-100
  library(multcomp)
summary(glht(model8, linfct = mcp(TargetColor = "Tukey")))
# 
#    Simultaneous Tests for General Linear Hypotheses
# 
# Multiple Comparisons of Means: Tukey Contrasts
# 
# 
# Fit: glmer(formula = cbind(TargetFocus, CompFocus) ~ SameColor + SameGender * 
#     TargetNeuter + TargetColor + (1 + SameColor | Subject) + 
#     (1 | Item), data = eye, family = "binomial")
# 
# Linear Hypotheses:
#                     Estimate Std. Error z value Pr(>|z|)   
# blue - brown == 0    0.27510    0.14335    1.92   0.3067   
# green - brown == 0   0.49375    0.14346    3.44   0.0051 **
# red - brown == 0     0.45611    0.14338    3.18   0.0129 * 
# yellow - brown == 0  0.50161    0.14335    3.50   0.0042 **
# green - blue == 0    0.21865    0.13518    1.62   0.4857   
# red - blue == 0      0.18101    0.13508    1.34   0.6657   
# yellow - blue == 0   0.22651    0.13507    1.68   0.4479   
# red - green == 0    -0.03764    0.13518   -0.28   0.9987   
# yellow - green == 0  0.00786    0.13517    0.06   1.0000   
# yellow - red == 0    0.04550    0.13507    0.34   0.9972   
# ---
# Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# (Adjusted p values reported -- single-step method)
  eye$TargetBrown <- (eye$TargetColor == "brown") * 1
model9 <- glmer(cbind(TargetFocus, CompFocus) ~ SameColor + SameGender * TargetNeuter + TargetBrown + 
    (1 + SameColor | Subject) + (1 | Item), data = eye, family = "binomial")
summary(model9)$coef
#                         Estimate Std. Error z value  Pr(>|z|)
# (Intercept)                2.139     0.2502    8.55  1.23e-17
# SameColor                 -1.717     0.1849   -9.28  1.62e-20
# SameGender                -0.174     0.0164  -10.63  2.14e-26
# TargetNeuter              -0.415     0.0913   -4.55  5.36e-06
# TargetBrown               -0.432     0.1215   -3.55  3.82e-04
# SameGender:TargetNeuter    0.488     0.0230   21.24 3.96e-100
AIC(model9) - AIC(model8)  # N.B. model8 is more complex
# [1] -2.43
  # chance to focus on target
# when there is a color
# competitor and a gender
# competitor, while the target
# is common and not brown
(logit <- fixef(model9)["(Intercept)"] + 
    1 * fixef(model9)["SameColor"] + 
    1 * fixef(model9)["SameGender"] + 
    0 * fixef(model9)["TargetNeuter"] + 
    0 * fixef(model9)["TargetBrown"] + 
    1 * 0 * fixef(model9)["SameGender:TargetNeuter"])
# (Intercept) 
#       0.248
plogis(logit)  # was 0.7
# (Intercept) 
#       0.562
qqnorm(resid(model9))
qqline(resid(model9))
eye2 <- eye[abs(scale(resid(model9))) < 2.5, ]  # remove items with which the model has trouble fitting
1 - (nrow(eye2)/nrow(eye))  # only about 0.5% removed
# [1] 0.0053
model10 <- glmer(cbind(TargetFocus, CompFocus) ~ SameColor + SameGender * TargetNeuter + TargetBrown + 
    (1 + SameColor | Subject) + (1 | Item), data = eye2, family = "binomial")
summary(model10)$coef  # all variables significant
#                         Estimate Std. Error z value  Pr(>|z|)
# (Intercept)                2.292     0.2981    7.69  1.51e-14
# SameColor                 -1.782     0.1953   -9.12  7.27e-20
# SameGender                -0.213     0.0166  -12.85  8.15e-38
# TargetNeuter              -0.419     0.0984   -4.26  2.07e-05
# TargetBrown               -0.460     0.1311   -3.51  4.48e-04
# SameGender:TargetNeuter    0.562     0.0233   24.13 1.16e-128
  R-functions are used to generate all plotsThank you for your attention!