Solved – Is SPSS giving the wrong residuals for a repeated measures design

anovarrepeated measuresresidualsspss

When running a repeated measures ANOVA in SPSS, it's possible to 'Save' the residuals as new variables in the data editor.

But the values output do not match the residuals given in R, and seem to be residuals for a between-subjects model. Unless I am missing something? Is SPSS giving the wrong residuals?

Example in R:

set.seed(1)  # hopefully this keeps things the same every time!

  # create a data frame with each line representing one subject,
  # and create first and second observations for some experiment

DF <- data.frame(participant=factor(1:5), first=rnorm(5, 10, 5), second=rnorm(5, 20, 5))

DF

  participant     first   second
1           1  6.867731 15.89766
2           2 10.918217 22.43715
3           3  5.821857 23.69162
4           4 17.976404 22.87891
5           5 11.647539 18.47306

  # reshape it for an ANOVA in R
DFlong <- reshape(DF, direction="long", varying=c("first", "second"), v.names="value", idvar="participant", times=c(1, 2), timevar="group")

DFlong

    participant group     value
1.1           1     1  6.867731
2.1           2     1 10.918217
3.1           3     1  5.821857
4.1           4     1 17.976404
5.1           5     1 11.647539
1.2           1     2 15.897658
2.2           2     2 22.437145
3.2           3     2 23.691624
4.2           4     2 22.878907
5.2           5     2 18.473058

my.aov <- aov(value ~ group + Error( participant / group ), DFlong)
summary(my.aov)

Error: participant
          Df Sum Sq Mean Sq F value Pr(>F)
Residuals  4 86.474  21.619               

Error: participant:group
          Df  Sum Sq Mean Sq F value  Pr(>F)  
group      1 251.469 251.469  19.871 0.01118 *
Residuals  4  50.619  12.655                  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

my.aov$"participant:group"$residuals

        6          7          8          9         10 
0.7066837 -1.0533061 -5.5440267  3.6252135 -2.2654355 

# import into SPSS:
write.table(DF, "C:/test.txt", row.names=FALSE)

Then load SPSS, and run:

GET DATA  /TYPE = TXT
 /FILE = 'C:\test.txt'
 /DELCASE = LINE
 /DELIMITERS = " "
 /QUALIFIER = '"'
 /ARRANGEMENT = DELIMITED
 /FIRSTCASE = 2
 /IMPORTCASE = ALL
 /VARIABLES =
 participant F1.0
 first F16.14
 second F16.13
 .
CACHE.
EXECUTE.
DATASET NAME DataSet1 WINDOW=FRONT.

Now change the variable types to scale (in the 'variables' tab – I don't know the syntax for this). Then run:

GLM
  first second
  /WSFACTOR = factor1 2 Polynomial
  /METHOD = SSTYPE(3)
  /SAVE = RESID
  /CRITERIA = ALPHA(.05)
  /WSDESIGN = factor1 .

Or, do the above SPSS commands using the GUI: File->Read text data… find C:\test.txt, import it, remember to specify that the file has variable names as the first case, and run:

  1. Analyze->General Linear Model->Repeated Measures…

  2. Set number of levels to 2

  3. Put variables into analysis, 'first' and 'second'.

  4. Open 'Save…' dialog box, check 'Residuals->Unstandardized'

  5. Run analysis, SPSS creates two variables of residuals:

    RES_1    RES_2
    -3.78    -4.78
      .27     1.76
    -4.82     3.02
     7.33     2.20
     1.00    -2.20
    

Note these values are different to R. So has SPSS got it wrong?

Best Answer

SPSS is giving the residuals from the group means without correcting for individual error.

> my.lm <- lm(value ~ group, DFlong)
> round(matrix(residuals(my.lm),ncol=2),2)
      [,1]  [,2]
[1,] -3.78 -4.78
[2,]  0.27  1.76
[3,] -4.82  3.02
[4,]  7.33  2.20
[5,]  1.00 -2.20

The residuals after correcting for individual error are as follows; they're not what you're finding in the aov fit either.

> my.lm <- lm(value ~ group + participant, DFlong)
> round(matrix(residuals(my.lm),ncol=2),2)
      [,1]  [,2]
[1,]  0.50 -0.50
[2,] -0.74  0.74
[3,] -3.92  3.92
[4,]  2.56 -2.56
[5,]  1.60 -1.60

The aov fit doesn't have a residuals method, which is a big hint that the residuals it's calculating are probably not what most end users want.