I was given some SPSS syntax and need to recreate the analyses in R. In SPSS, the analysis is a repeated measures GLM where the outcome variable is measured four times per subject and each subject comes from one of three groups. The goal is to see if there are main effects of time and group and an interaction between time and group. I thought to recreate the analyses in R using mixed-effects modeling and the lme4 package.

This is the original SPSS syntax:

GLM y1 y2 y3 y4 BY group
/WSFACTOR = time 4 Polynomial
/WSDESIGN = time
/DESIGN = group.

This is my attempt in R:

long <- reshape(wide,varying=c("y1","y2","y3","y4"),timevar="time",dir="long",sep="")
long$timesq = long$time^2
long$timecubed = long$time^3
m <- lmer(y ~ 1 + time*group + timesq*group + timecubed*group + (1 | subject), data=long)

The results are pretty similar in terms of which effects are significant. However, the type III sums of squares vary considerably between the SPSS and R results.

SPSS Sums of Squares:

time          linear        267.244
time          quadratic      45.658
time          cubic          18.226
time*group    linear       3027.543
time*group    quadratic    3462.319
time*group    cubic        2148.856
group                     13298.301

R Sums of Squares:

time                63.7
timesq              67.2
timecubed           78.0
time*group        3403.7
timesq*group      2690.9
timecubed*group   2246.1
group             3710.3

Any help you can provide to help me understand these differences would be appreciated. Is it possible to recreate the SPSS results exactly? If not, which results would be considered more trustworthy?

  • $\begingroup$ GLM command in SPSS is general, not generalized linear models. Aka just "linear models" of other software. So update yor tags and select R package doing just linear modeling. $\endgroup$
    – ttnphns
    Aug 20, 2017 at 20:17
  • $\begingroup$ The R package for linear modeling is lm. How do I account for repeated measurements in this framework? $\endgroup$ Aug 20, 2017 at 20:31
  • 2
    $\begingroup$ You should investigate the ez package and the afex package. They were both created to recreate SPSS analyses. You could also try car::Anova(m, type = 3, test = "F"). In general however trying to recreate the exact results of SPSS analyses in R can be a huge headache. One more thing, you can specify power functions within calls with lmer(dv ~ iv1*iv2 + I(iv1^2)*iv2 + I(iv1^3)*iv2 + (1|id), data = df) $\endgroup$
    – llewmills
    Jan 22, 2019 at 10:59
  • 2
    $\begingroup$ One more thing to note is that R produces Type 1 Sums of Squares by default. To get type 3 Sums of Squares like SPSS you need to use the packages I mentioned. Some will tell you that setting 'options(contrasts = c("contr.sum", "contr.poly")` is enough but that has not been my experience. You can reproduce SPSS analyses with different factor contrast coding structures, but even if you do open that can of worms steer well clear of anova() and pass your lmer into car::Anova() instead. $\endgroup$
    – llewmills
    Jan 22, 2019 at 11:11


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.