我试图使用以下代码来计算标准化系数:
rm(list=ls())
#install.packages("AER")
library(AER)
data("CASchools",package="AER")
CASchools$score=with(CASchools,(math+read)/2)
attach(CASchools)
#regular regressopm
lm1<-lm(score~income)
#standardized regression
stdscore<-(score-mean(score)/sd(score))
stdincome<-(income-mean(income))/sd(income)
lm1e<-lm(stdscore~stdincome)
#standardized regression provided by different package
#install.packages("QuantPsyc")
library(QuantPsyc)
lm.beta(lm1)
#but lm.beta is different from the coefficients I calculated with standardized regression
lm1e$coef
然而,似乎QuantPsyc包的输出与我自己标准化回归量和因变量所获得的结果不同。
两个输出是:
> lm.beta(lm1)
income
0.7124308
> lm1e$coef
(Intercept) stdincome
619.82365 13.57419
正如你所看到的,结果之一是0.7124308,另一个是13.57419。它们应该与我的理解相同。
知道为什么吗?
答案 0 :(得分:1)
您在缩放变量方面犯了错误。 lm.beta
对非标准化系数进行事后转换为标准化系数,因此公式不同。但是,正确的变量前回归标准化会产生相同的结果。
您的错误:(score-mean(score)/sd(score))
应为(score-mean(score))/sd(score)
。操作顺序很重要!
检查:
> (score[1]-mean(score))/sd(score)
[1] 1.923202 #Clearly standardized
> (score[1]-mean(score)/sd(score))
[1] 656.4671 #Clearly NOT standardized!
所以:
stdscore<-(score-mean(score))/sd(score)
stdincome<-(income-mean(income))/sd(income)
lm1e<-lm(stdscore~stdincome)
lm.beta(lm1)
income
0.7124308
lm1e$coef[2]
stdincome
0.7124308
round(lm.beta(lm1),5) == round(lm1e$coef[2],5)
income
TRUE