R:来自lm对象的标准错误输出

Fab*_*olz 23 regression r standard-error lm

我们得到了一个lm对象,并希望提取标准错误

lm_aaa<- lm(aaa~x+y+z)
Run Code Online (Sandbox Code Playgroud)

我知道函数摘要,名称和系数.但是,摘要似乎是手动访问标准错误的唯一方法.你知道我怎么能输出se吗?

谢谢!

csg*_*pie 20

summary函数的输出只是一个R 列表.因此,您可以使用所有标准列表操作.例如:

#some data (taken from Roland's example)
x = c(1,2,3,4)
y = c(2.1,3.9,6.3,7.8)

#fitting a linear model
fit = lm(y~x)
m = summary(fit)
Run Code Online (Sandbox Code Playgroud)

m对象或列表包含若干属性.您可以使用括号或命名方法访问它们:

m$sigma
m[[6]]
Run Code Online (Sandbox Code Playgroud)

一个方便的功能是,str.此函数提供对象属性的摘要,即

str(m)
Run Code Online (Sandbox Code Playgroud)

  • 但是,@csgillespie 所指的是模型的**残差**标准差,而不是单个系数的标准差。函数`m$sigma` 对应于`sigma(fit)`,参见[此处](https://stat.ethz.ch/R-manual/R-devel/library/stats/html/sigma.html)。我相信这个问题实际上是关于 **个体** 系数的标准偏差。 (3认同)

smi*_*lig 12

要获取所有参数的标准错误列表,您可以使用

summary(lm_aaa)$coefficients[, 2]
Run Code Online (Sandbox Code Playgroud)

正如其他人所指出的那样,str(lm_aaa)几乎可以告诉您从模型中提取的所有信息.


Rol*_*and 8

#some data
x<-c(1,2,3,4)
y<-c(2.1,3.9,6.3,7.8)

#fitting a linear model
fit<-lm(y~x)

#look at the statistics summary
summary(fit)

#get the standard error of the slope
se_slope<-summary(fit)$coef[[4]] 
#the index depends on the model and which se you want to extract

#get the residual standard error
rse<-summary(fit)$sigma
Run Code Online (Sandbox Code Playgroud)


Ann*_*e N 8

如果您不想获得模型的标准误差/偏差而是单个系数的标准误差/偏差,请使用

# some data (taken from Roland's example)
x = c(1, 2, 3, 4)
y = c(2.1, 3.9, 6.3, 7.8)

# fitting a linear model
fit = lm(y ~ x)

# get vector of all standard errors of the coefficients
coef(summary(fit))[, "Std. Error"] 
Run Code Online (Sandbox Code Playgroud)

有关模型标准误差/偏差的更多信息,请参阅此处。有关系数标准误差/偏差的更多信息,请参阅此处


Whi*_*d13 5

我认为以下几行也可以为您提供快速答案:

lm_aaa<- lm(aaa~x+y+z)
se <- sqrt(diag(vcov(lm_aaa)))
Run Code Online (Sandbox Code Playgroud)