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
/METHOD = SSTYPE(3)
/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)
summary(m)
anova(m)
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?
lm
. How do I account for repeated measurements in this framework? $\endgroup$ez
package and theafex
package. They were both created to recreate SPSS analyses. You could also trycar::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 withlmer(dv ~ iv1*iv2 + I(iv1^2)*iv2 + I(iv1^3)*iv2 + (1|id), data = df)
$\endgroup$anova()
and pass yourlmer
intocar::Anova()
instead. $\endgroup$