SPSS 是否为重复测量设计提供了错误的残差?

机器算法验证 r 方差分析 重复测量 spss 残差
2022-04-01 07:36:32

在 SPSS 中运行重复测量方差分析时,可以在数据编辑器中将残差“保存”为新变量。

但是输出的值与 R 中给出的残差不匹配,并且似乎是主体间模型的残差。除非我错过了什么?SPSS是否给出了错误的残差?

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)

然后加载 SPSS,并运行:

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.

现在将变量类型更改为缩放(在“变量”选项卡中 - 我不知道此语法)。然后运行:

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

或者,使用 GUI 执行上述 SPSS 命令:File->Read text data... 找到 C:\test.txt,导入它,记得指定文件具有变量名称作为第一个 case,然后运行:

  1. 分析->一般线性模型->重复测量...

  2. 将级别数设置为 2

  3. 将变量放入分析中,“第一”和“第二”。

  4. 打开“保存...”对话框,选中“残差->未标准化”

  5. 运行分析,SPSS 创建两个残差变量:

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

请注意,这些值与 R 不同。那么 SPSS 是否弄错了?

2个回答

SPSS 给出了组均值的残差,而不纠正个别错误。

> 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

修正个别误差后的残差如下:它们也不是您所找到的aov合适的。

> 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

aov拟合没有方法,residuals是一个很大的暗示,它计算的残差可能不是大多数最终用户想要的。

我现在已经阅读了更多关于重复测量一般线性模型的内容,并且可以解释 R 报告的残差是如何计算的my.aov

重复测量方差分析应用于对象内变量的转换,该模型的残差是从aov拟合中返回的残差。

变换将原始变量乘以变换矩阵,可以通过添加/PRINT = TEST(MMATRIX)到 GLM 命令在 SPSS 中输出。SPSS 似乎不允许保存实际转换后的变量,但我们可以自己计算它们,在 R 中,见下文。

转换矩阵的生成方式遵循一定的规则,如下所述: http ://www.uccs.edu/~faculty/lbecker/SPSS/glm_1withn.htm

即系数之和应为零,系数平方和应为1。但是现在我不处理生成变换矩阵,我只是将SPSS的矩阵输出应用于我们的原始变量以创建变换变量,然后aov通过将残差与我们自己的模型上my.aovlm调用进行比较来检查它是使用的模型。

# As before, continuing from the code in the original question:
# my.aov <- aov(value ~ group + Error( participant / group ), DFlong)
summary(my.aov)

# create our transformed variable
# (there will always be one less new variable
# than the number of within subject variables)
# We are applying here the transformation matrix that is output from SPSS,
DF$DV <- sqrt(0.5) * DF$first + -sqrt(0.5) * DF$second
residuals(lm(DV ~ 1, DF))

# Our self-calculated residuals should be the same as the ones returned by the aov fit.
# They are the same except for the last one,
# which is the negative of the residual calculated
my.aov$"participant:group"$residuals

# Again fitted values are the same except for the *-1 at the end
fitted.values(lm(DV ~ 1, DF))
my.aov$"participant:group"$fitted.values

我不确定如何aov计算参与者残差,但大概这就是他们有 的*-1原因,因为我没有在上面的模型中包含个体差异。

总之,SPSS 保存的残差不是来自重复测量 GLM。