30

我们得到了一个lm对象并想要提取标准错误

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

我知道函数摘要、名称和系数。

但是,摘要似乎是手动访问标准错误的唯一方法。

你知道我怎么能输出se吗?

4

5 回答 5

26

函数的输出summary只是一个 R list。所以你可以使用所有标准的列表操作。例如:

#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)
于 2012-06-19T12:37:30.930 回答
12

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

summary(lm_aaa)$coefficients[, 2]

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

于 2012-06-19T12:40:42.227 回答
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
于 2012-06-19T10:55:45.757 回答
5

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

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

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

于 2016-12-21T09:47:02.960 回答
5

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

lm_aaa<- lm(aaa~x+y+z)
se <- sqrt(diag(vcov(lm_aaa)))
于 2018-05-08T15:25:09.677 回答