Extract standard errors from lm object
Solution 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)
Solution 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.
Solution 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
Solution 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.
Solution 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)))
Fabian Stolz
Updated on October 26, 2021Comments
-
Fabian Stolz over 2 years
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?
-
Annerose N over 7 yearsHowever, what @csgillespie refers to is the residual standard deviation of the model, not the standard deviation of the individual coefficients. The function
m$sigma
corresponds tosigma(fit)
, see here. I believe the question was really about the standard deviation of the individual coefficients.