r语言 - 从 lm 对象中提取标准错误



我们从中获取了一个 lm 对象,并希望提取标准错误

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

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

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

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

的输出来自 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)

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

summary(lm_aaa)$coefficients[, 2]

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

#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

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

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

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

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

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

最新更新