Standarized residuals in SPSS not maching R rstandard(lm())
Asked Answered
D

3

2

While looking for a R related solution I found some inconsistency between R and SPSS (ver. 24) in computing standardized residuals in a simple linear model.

It appears that what SPSS calls standarized residuals matches R studentized residuals

enter image description here

I'm far for assuming there is a software bug somewhere, but clearly things differ between those two programs.

Have a look at this example

#generate data in R
set.seed(111)
y = rnorm(20, 0, 1) 
x = rnorm(20, 1, 1)

#calculate and standarized residuals
zresid<- rstandard(lm(y ~ x))
sresid<- rstudent(lm( y ~ x))

#make data frame
sampleData <- data.frame(y, x, zresid, sresid)

#save data for SPSS
library(foreign)
write.foreign(sampleData, "~/sampleData.sav",   package="SPSS") 

Then, in SPSS click your way through all the windows to import data and set up a linear regression ZRE and SRE residuals saved.

#load data to spss via syntax 
GET DATA  /TYPE=TXT
  /FILE="~\sampleData.sav"
  /DELCASE=LINE
  /DELIMITERS=","
  /ARRANGEMENT=DELIMITED
  /FIRSTCASE=1
  /DATATYPEMIN PERCENTAGE=95.0
  /VARIABLES=
  y F8.0
  x F8.0
  zresid F8.0
  sresid F8.0
  /MAP.
RESTORE.

#run a simple regression with standarized residuals (ZRESID) and studentized residuals (SRESID)

REGRESSION
  /MISSING LISTWISE
  /CRITERIA=PIN(.05) POUT(.10)
  /NOORIGIN 
  /DEPENDENT y
  /METHOD=ENTER x
  /SAVE ZRESID SRESID.

Am I mad (or dumb) or indeed something is wrong here?

enter image description here

Derian answered 15/10, 2016 at 18:6 Comment(0)
K
1

I did a bit more: Here are the conclusions:

r stats::rstandard = MASS::stdres = SPSS studentized residual
r z score of resid or residuals = SPSS z score of unstandardized residual

enter image description here

Here are my codes:

#generate data in R
set.seed(111)
y = rnorm(20, 0, 1) 
x = rnorm(20, 1, 1)

#calculate and standarized residuals
stats_rstudent = stats::rstudent(lm( y ~ x))
stats_rstandard = stats::rstandard(lm(y ~ x))
MASS_stdres = MASS::stdres(lm( y ~ x))
scale_resid = as.vector(scale(resid(lm(y ~ x)),center=T,scale=T))
scale_residuals = as.vector(scale(residuals(lm(y ~ x)),center=T,scale=T))

#make data frame
sampleData <- data.frame(y, x, stats_rstudent, stats_rstandard, MASS_stdres, scale_resid, scale_residuals)

#save data for SPSS
library(foreign)
write.foreign(sampleData, "sampleData.sav",   package="SPSS")

SPSS syntax:

REGRESSION
  /MISSING LISTWISE
  /CRITERIA=PIN(.05) POUT(.10)
  /NOORIGIN 
  /DEPENDENT y
  /METHOD=ENTER x
  /SAVE RESID ZRESID SRESID.

* calc z score of resid.
descriptives RES_1_Unstandardized_Residual/save.

formats stats_rstudent(f11.6).
formats stats_rstandard(f11.6).
formats MASS_stdres(f11.6).
formats scale_resid(f11.6).
formats scale_residuals(f11.6).
formats ZRE_1_Standardized_Residual(f11.6).
formats SRE_1Studentized_Residual(f11.6).
formats RES_1_Unstandardized_Residual(f11.6).
formats Zscore_RES_1_Unstandardized_Residual(f11.6).
Kokoruda answered 10/1, 2019 at 20:1 Comment(0)
D
0

Not very familiar with SPSS, but I ran the model R and Stata, getting the same residuals. So the problem is on the SPSS end. My guess is that it looks like you have called a stepwise regression command in SPSS. Could you try simply:

REGRESSION
  /DEPENDENT y  
  /METHOD=ENTER x
  /SAVE ZRESID SRESID.

And see if that works?

Danettedaney answered 15/10, 2016 at 21:47 Comment(4)
with a single predictor regression there is no difference between methods (i.e stepwise or otherwise). Your syntax gives same results as mine. /method=enter basically defines itDerian
My guess is they are using "standardized" and "studentized" differently. There are "internally studentized" and "externally studentized" residuals. The latter finds the ith residual by leaving the ith case out of the regression ("deleted" residuals). Using methods("rstandard.lm")' and methods("rstudent.lm"), it looks like R calculates standardized residuals using internally studentized residuals and studentized residuals using externally studentized residuals. The studentized residuals in SPSS match the standardized residuals in the R output.Danettedaney
What SPSS is doing to find its standardized residuals, I'm not sure!Danettedaney
Ok, I looked into it a bit more and I see now. SPSS is calculating the standardized residual by dividing the residuals by the root mean square error. You can do this in R by residuals(lm)/sqrt(sum(residuals(lm)^2)/df) where df is your degrees of freedom (here = 18). To get what SPSS calls "studentized residuals" use the rstandard call in R.Danettedaney
D
0

Following @JKP suggestion I went though SPSS Algorithm manual and on page 853 (Regression Algorithm chapter) we can find, that Standardized Residuals saved via simple regression analysis are computed as follows:

Standardized Residuals

Derian answered 16/10, 2016 at 17:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.