R: standard error output from lm object


Question


We got a lm object from and want to extract the standard error

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

I know the function summary, names and coefficients. However, summary seems to be the only way to manually access the standard error. Have you any idea how I can just output se?

thanks!


Answer 1:


The output of from the summary function is just an R list. So you can use all the standard list operations. For example:

#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)

The m object or list has a number of attributes. You can access them using the bracket or named approach:

m$sigma
m[[6]]

A handy function to know about is, str. This function provides a summary of the objects attributes, i.e.

str(m)



Answer 2:


To get a list of the standard errors for all the parameters, you can use

summary(lm_aaa)$coefficients[, 2]

As others have pointed out, str(lm_aaa) will tell you pretty much all the information that can be extracted from your model.




Answer 3:


#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



Answer 4:


If you don't want to get the standard error/deviation of the model, but instead the standard error/deviation of the individual coefficients, use

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

For more information on the standard error/deviation of the model, see here. For more information on the standard error/deviation of the coefficients, see here.




Answer 5:


I think that the following lines can also provide you with a quick answer:

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


来源:https://stackoverflow.com/questions/11099272/r-standard-error-output-from-lm-object


码神部落- 版权声明 1、本主题所有言论和图片纯属会员个人意见,与码神部落立场无关。
2、本站所有主题由该帖子作者发表,该帖子作者撒浪嘿呦码神部落享有帖子相关版权。
3、码神部落管理员和版主有权不事先通知发贴者而删除本文。
4、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者撒浪嘿呦码神部落的同意。
5、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任。
6、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责。
7、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意。

最新回复 (0)
    • 码神部落
      2
        立即登录 立即注册 GitHub登录
返回
发新帖
作者最近主题: