簡體   English   中英

R:來自lm對象的標准錯誤輸出

[英]R: standard error output from lm object

我們得到了一個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)))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM