Extract standard errors from lm object
Asked Answered
M

5

35

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?

Macle answered 19/6, 2012 at 10:40 Comment(0)
M
29

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)
Markland answered 19/6, 2012 at 12:37 Comment(1)
However, what @Markland refers to is the residual standard deviation of the model, not the standard deviation of the individual coefficients. The function m$sigma corresponds to sigma(fit), see here. I believe the question was really about the standard deviation of the individual coefficients.Grantgranta
W
13

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.

Walker answered 19/6, 2012 at 12:40 Comment(0)
S
11
#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
Sneer answered 19/6, 2012 at 10:55 Comment(0)
G
8

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.

Grantgranta answered 21/12, 2016 at 9:47 Comment(0)
T
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)))
Tympanist answered 8/5, 2018 at 15:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.