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

时间:2012-06-19 10:40:17

标签: r regression lm standard-error

我们有一个lm对象,想要提取标准错误

lm_aaa<- lm(aaa~x+y+z)

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

谢谢!

5 个答案:

答案 0 :(得分: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)

m对象或列表具有许多属性。您可以使用括号或命名方法访问它们:

m$sigma
m[[6]]

要了解的一个方便的功能是,str。该函数提供了对象属性的摘要,即

str(m)

答案 1 :(得分:12)

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

summary(lm_aaa)$coefficients[, 2]

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

答案 2 :(得分: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

答案 3 :(得分:2)

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

# 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"] 

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

答案 4 :(得分:1)

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

lm_aaa<- lm(aaa~x+y+z)
se <- sqrt(diag(vcov(lm_aaa)))