Generalized Linear Mixed Models Bodo Winter
Regression with categorical predictors
Regression with categorical predictors
Regression with categorical predictors
Regression with categorical predictors
Regression with categorical predictors
Regression with categorical predictors
Regression with categorical predictors
Regression with categorical predictors
Regression with categorical predictors
Output for categorical predictors Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 109.287 2.954 37.00 <2e-16 *** genderFemale 112.727 4.178 26.98 <2e-16 *** The males are in the intercept (=109) The slope for “female” is the change with respect to the male group
Default: Treatment coding
Default: Treatment coding pitch gender 1 123.71 male 2 104.35 male 3 113.63 male 4 116.33 male 5 224.04 female 6 218.94 female 7 235.12 female 8 219.05 female
Default: Treatment coding pitch gender gender01 1 123.71 male 0 2 104.35 male 0 3 113.63 male 0 4 116.33 male 0 5 224.04 female 1 6 218.94 female 1 7 235.12 female 1 8 219.05 female 1 these are called dummy codes by default, R does “treatment coding” and assumes that the alphanumerically first element is the reference level
Changing the reference level xdata$myfac = relevel(xdata$myfac,ref=“male”) Before releveling: Estimate Std. Error t value Pr(>|t|) (Intercept) 109.287 2.954 37.00 <2e-16 *** genderFemale 112.727 4.178 26.98 <2e-16 *** After releveling: (Intercept) 222.014 2.954 75.16 <2e-16 *** myfactor.revmale -112.727 4.178 -26.98 <2e-16 ***
Other coding schemes
Other coding schemes
the intercept is now the mean of all pitch values (ignoring gender) Other coding schemes the intercept is now the mean of all pitch values (ignoring gender)
this is now the difference from the mean (=55) Other coding schemes This is called sum coding this is now the difference from the mean (=55)
Other coding schemes This is called deviation coding
Female = 0, Male = 1 (Treatment coding) Estimate Std Female = 0, Male = 1 (Treatment coding) Estimate Std. Error t value Pr(>|t|) (Intercept) 224.287 3.902 57.48 1.86e-09 *** gender01 -109.783 5.518 -19.89 1.05e-06 *** Female = -1, Male = 1 (Sum coding) (Intercept) 169.396 2.759 61.40 1.25e-09 *** gender01 -54.891 2.759 -19.89 1.05e-06 *** Female = -0.5, Male = 0.5 (Deviation coding) gender01.2 -109.783 5.518 -19.89 1.05e-06 ***
An extended linear Model Y ~ b0 + b1*X1 + b2*X2 + error coefficients predictors can be continuous or categorical and there can be (in principle) infinitely many
Interactions
Continuous * categorical interaction
Continuous * categorical interaction
Continuous * categorical interaction RT ~ Noise + Gender
Continuous * categorical interaction RT ~ Noise + Gender + Noise:Gender RT ~ Noise * Gender
Continuous * categorical interaction
Continuous * categorical interaction
Continuous * categorical interaction
Schielzeth (2010) example Schielzeth, H. (2010). Simple means to improve the interpretability of regression coefficients. Methods in Ecology and Evolution, 1(2), 103-113.
Interpreting cont * cat interactions Example: Intercept 500 Noise 2 GenderF -150 Noise:GenderF 5 Y ~ b0 + b1*X1 + b2*X2 + b3*(X1*X2) interaction term
Interpreting cont * cat interactions Example: Intercept 500 Noise 2 GenderF -150 Noise:GenderF 5 say you wanted the prediction for noise = 10 for females Y ~ b0 + b1*X1 + b2*X2 + b3*(X1*X2)
Interpreting cont * cat interactions Example: Intercept 500 Noise 2 GenderF -150 Noise:GenderF 5 say you wanted the prediction for noise = 10 for females Y ~ 500 + b1*X1 + b2*X2 + b3*(X1*X2)
Interpreting cont * cat interactions Example: Intercept 500 Noise 2 GenderF -150 Noise:GenderF 5 say you wanted the prediction for noise = 10 for females Y ~ 500 + 2*10 + b2*X2 + b3*(X1*X2)
Interpreting cont * cat interactions Example: Intercept 500 Noise 2 GenderF -150 Noise:GenderF 5 say you wanted the prediction for noise = 10 for females Y ~ 500 + 2*10 + (-150)*1 + b3*(X1*X2)
Interpreting cont * cat interactions Example: Intercept 500 Noise 2 GenderF -150 Noise:GenderF 5 say you wanted the prediction for noise = 10 for females Y ~ 500 + 2*10 + (-150)*1 + 5*(10*1) “1” for female and “10” for the noise value we wanted
Interpreting cont * cat interactions Example: Intercept 500 Noise 2 GenderF -150 Noise:GenderF 5 What about the men? Y ~ 500 + 2*10 + (-150)*1 + 5*(1*10)
Interpreting cont * cat interactions Example: Intercept 500 Noise 2 GenderF -150 Noise:GenderF 5 What about the men? Y ~ 500 + 2*10 + (-150)*0 + 5*(0*10)
Interactions between continuous variables No interaction Interaction
Interactions between continuous variables No interaction Interaction
Interactions between continuous variables No interaction Interaction
Interactions between continuous variables No interaction Interaction
Interpreting continuous interactions Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 4.75621 1.50160 3.167 0.0037 ** word_frequency -1.63550 0.87642 -1.866 0.0725 . CD 2.45615 0.03699 66.404 <2e-16 *** word_frequency:CD 1.50333 0.02119 70.943 <2e-16 *** Y ~ b0 + b1*X1 + b2*X2 + b3*(X1*X2)
Interpreting continuous interactions Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 4.75621 1.50160 3.167 0.0037 ** word_frequency -1.63550 0.87642 -1.866 0.0725 . CD 2.45615 0.03699 66.404 <2e-16 *** word_frequency:CD 1.50333 0.02119 70.943 <2e-16 *** Y ~ 4.8 + (-1.6)*X1 + 2.5*X2 + 1.5*(X1*X2) Predictions for word frequency 3 and CD 50 Y = 4.8 + (-1.6)*3 + 2.5*50 + 1.5*(3*50)
Interpreting continuous interactions Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 4.75621 1.50160 3.167 0.0037 ** word_frequency -1.63550 0.87642 -1.866 0.0725 . CD 2.45615 0.03699 66.404 <2e-16 *** word_frequency:CD 1.50333 0.02119 70.943 <2e-16 *** Y ~ 4.8 + (-1.6)*X1 + 2.5*X2 + 1.5*(X1*X2) Predictions for word frequency 3 and CD 50 Y = 801
Sign of the interaction Coefficients: Estimate (Intercept) 10 A +1 B +1 A:B +1 A and B have a positive effect on the response, and A and B together increase the response more than either one of them alone
Sign of the interaction Coefficients: Estimate (Intercept) 10 A +1 B +1 A:B -1 A and B have a positive effect on the response, but both together decrease it
Sign of the interaction Coefficients: Estimate (Intercept) 10 A -1 B -1 A:B +1 A and B have a negative effect on the response, but both together increase it
Categorical predictors: Simple vs. Main effects Simple effect = the effect of A at a specific level of B Main effect = the effect of A averaging over all levels of B
2 X 2 Example output with treatment coding attitude = “inf” or “pol” gender = “F” or “M” Estimate (Intercept) 260 attitudepol -30 genderM -120 attitudepol:genderM 15 informal polite female male
2 X 2 Example output with treatment coding attitude = “inf” or “pol” gender = “F” or “M” Estimate (Intercept) 260 attitudepol -30 genderM -120 attitudepol:genderM 15 informal polite female male
2 X 2 Example output with treatment coding attitude = “inf” or “pol” gender = “F” or “M” Estimate (Intercept) 260 attitudepol -30 genderM -120 attitudepol:genderM 15 informal polite female 260 male
2 X 2 Example output with treatment coding attitude = “inf” or “pol” gender = “F” or “M” Estimate (Intercept) 260 attitudepol -30 genderM -120 attitudepol:genderM 15 informal polite female 260 male
2 X 2 Example output with treatment coding attitude = “inf” or “pol” gender = “F” or “M” Estimate (Intercept) 260 attitudepol -30 genderM -120 attitudepol:genderM 15 informal polite female 260 260-30 male
2 X 2 Example output with treatment coding attitude = “inf” or “pol” gender = “F” or “M” Estimate (Intercept) 260 attitudepol -30 genderM -120 attitudepol:genderM 15 informal polite female 260 260-30 male
2 X 2 Example output with treatment coding attitude = “inf” or “pol” gender = “F” or “M” Estimate (Intercept) 260 attitudepol -30 genderM -120 attitudepol:genderM 15 informal polite female 260 260-30 male 260-120 260-30-120
2 X 2 Example output with treatment coding attitude = “inf” or “pol” gender = “F” or “M” Estimate (Intercept) 260 attitudepol -30 genderM -120 attitudepol:genderM 15 informal polite female 260 260-30 male 260-120 260-30-120
2 X 2 Example output with treatment coding attitude = “inf” or “pol” gender = “F” or “M” Estimate (Intercept) 260 attitudepol -30 genderM -120 attitudepol:genderM 15 informal polite female 260 260-30 male 260-120 260-30-120+15
2 X 2 Example output with treatment coding attitude = “inf” or “pol” gender = “F” or “M” Opposite sign means that the effect of politeness is smaller for males Estimate (Intercept) 260 attitudepol -30 genderM -120 attitudepol:genderM 15 informal polite female 260 230 male 140 125
Increasing interpretability with sum or deviation coding these are not main effects, they are simple effects!! Treatment coding: Estimate (Intercept) 260 attitudepol -30 genderM -120 attitudepol:genderM 15 that is, they are only the differences between specific levels (not the mean differences of attitude/gender)
How to change the coding in R For a two-level factor: contrasts(df$A) = contr.sum(2) For a three-level factor: contrasts(df$B) = contr.sum(3) etc. …
How classical tests map to their LM framework counterparts One-sample t-test against 0 lm(y ~ 1) # t.test(y, mu = 0) Two-sample t-test (unpaired / independent) lm(y ~ group) # t.test(x, y, paired = F) Paired t-test lm(differences ~ 1) # t.test(x, y, paired = T)
How classical tests map to their LM framework counterparts One-way ANOVA etc. lm(y ~ factor) # aov(y ~ x) ANCOVA lm(y ~ factor * covariate) # ?
Exercise: working memory data