# R vs SPSS - simple effects analysis in mixed 2x2 ANOVA scheme - same data, different results

I prepared a mixed 2x2 ANOVA design analysis both in SPSS and in R. The SPSS script is correct, but in R script there is a mistake somewhere. To test that I generated artificial data from a normal distribution to simulate the interaction between two independent variables. There were no difference between the results in main effects, but results of simple effects analysis do not match when comparing between levels of variable which introduced repeated measures (GROUP A: PRE vs POST ; GROUP B: PRE vs POST).

I would be very thankful if you can help me. The code below will do everything for you.

Here is the code in R which: - generates the data - calculates mixed ANOVA - prepares data to csv format to import to SPSS - performs simple effect analysis (there is probably a mistake)

N <- 100
absMean <- 1
sdCustom <- 5

grA_pre <- data.frame(ID = seq(N), lvl=rnorm(N, mean=absMean, sd=sdCustom), group=factor('A'), stage = factor('pre'))
grA_post <- data.frame(ID = seq(N), lvl=rnorm(N, mean=-absMean, sd=sdCustom), group=factor('A'), stage = factor('post'))
grB_pre <- data.frame(ID = seq(N+1,2*N), lvl=rnorm(N, mean=-absMean, sd=sdCustom), group=factor('B'), stage = factor('pre'))
grB_post <- data.frame(ID = seq(N+1,2*N), lvl=rnorm(N, mean=absMean, sd=sdCustom), group=factor('B'), stage = factor('post'))

gr <- rbind(grA_pre, grA_post, grB_pre, grB_post)
names(gr)

# save set to .csv to import to SPSS
grSPSS <- reshape(data = gr, timevar = "stage", idvar = c("ID", "group"), direction = "wide")

write.csv2(grSPSS, file = "sample2.csv")

library(ggplot2)
library(plyr)
library(ez)

print("Omnibus mixed ANOVA - main effects and interactions")
ezPlot(data = gr, wid = ID, dv = lvl, between = group, within = stage, type = "III", x = group, split = stage, x_lab = "Group", y_lab = "Level of experience")
ezANOVA(data = gr, wid = ID, dv = lvl, between = group, within = stage, detailed = TRUE, type = "III")
#ezStats(data = gr, wid = ID, dv = lvl, between = group, within = stage, type = "III")

print("Simple main effects analysis")
dataA <- subset(gr, group == "A" )
dataB <- subset(gr, group == "B" )
dataPRE <- subset(gr, stage == "pre" )
dataPOST <- subset(gr, stage == "post" )

print("GROUP = A: PRE vs POST")
simpleEffControlANOVA <- ezANOVA(data = dataA, dv = lvl, wid = ID, within = stage, detailed = TRUE, type = "III" )
print(simpleEffControlANOVA)

print("GROUP = B: PRE vs POST")
simpleEffControlANOVA <- ezANOVA(data = dataB, dv = lvl, wid = ID, within = stage, detailed = TRUE, type = "III" )
print(simpleEffControlANOVA)

print("STAGE = PRE: A vs B")
simpleEffControlANOVA <- ezANOVA(data = dataPRE, dv = lvl, wid = ID, between = group, detailed = TRUE, type = "III" )
print(simpleEffControlANOVA)

print("STAGE = POST: A vs B")
simpleEffControlANOVA <- ezANOVA(data = dataPOST, dv = lvl, wid = ID, between = group, detailed = TRUE, type = "III" )
print(simpleEffControlANOVA)


Here is the code for SPSS Syntax which: - calculates everything on imported data, generated by R

DATASET ACTIVATE DataSet1.
GLM lvl.pre lvl.post BY group
/WSFACTOR=stage 2 Polynomial
/METHOD=SSTYPE(3)
/POSTHOC=group(TUKEY T3)
/EMMEANS=TABLES(group*stage) COMPARE(group)
/EMMEANS=TABLES(group*stage) COMPARE(stage)
/PLOT=PROFILE(group*stage)
/PRINT=DESCRIPTIVE ETASQ OPOWER HOMOGENEITY
/CRITERIA=ALPHA(.05)
/WSDESIGN=stage
/DESIGN=group.

• Just immediate guess w/o exploring your Q: could some differences be due to the default SS type? In SPSS, the default is type III. In R anova packages, it is usually type I (if I'm correct). – ttnphns May 20 '16 at 9:46
• Good point, but as you can see I forced R to use type "III" of sum (look at ezAnova calls). SPSS uses type "III" sum also. – micholeodon May 20 '16 at 10:01
• I cannot follow R commands easily (sorry, not R user) - but could it be that in R you are comparing observed means - as if in post hoc comparisons? The comparison of estimated means requested by you in SPSS is not a comparison of observed ones. In computing estimated marginal means, levels of a factor get equal weights (plus adjustment for covariates, if any) - that will sure give different values with an unbalanced design. – ttnphns May 20 '16 at 10:45
• This design is balanced - every group has the same number of observations. Im not sure what do you mean by saying "The comparison of estimated means requested by you in SPSS is not a comparison of observed ones" . Did I took wrong subset of data to simple effect analysis in R ? – micholeodon May 20 '16 at 11:58
• What do you mean by "observed mean" and "estimated mean"? What is the difference? – micholeodon May 23 '16 at 16:09