我试图使用以下代码来计算标准化系数:
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,根据我的理解,它们应该是等价的。
知道为什么吗?
发布于 2017-11-05 17:01:01
您在缩放变量的方式上犯了错误。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 https://stackoverflow.com/questions/47123863
复制相似问题