Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract standard errors from lm object

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?

like image 318
Fabian Stolz Avatar asked Jun 19 '12 10:06

Fabian Stolz


People also ask

What is standard error in lm?

In R, the lm summary produces the standard deviation of the error with a slight twist. Standard deviation is the square root of variance. Standard Error is very similar. The only difference is that instead of dividing by n-1, you subtract n minus 1 + # of variables involved.

How do you find the standard error of an R model?

The formula for standard error of mean is the standard deviation divided by the square root of the length of the data. It is relatively simple in R to calculate the standard error of the mean. We can either use the std. error() function provided by the plotrix package, or we can easily create a function for the same.

What is standard error in linear regression?

The standard error of the regression (S), also known as the standard error of the estimate, represents the average distance that the observed values fall from the regression line. Conveniently, it tells you how wrong the regression model is on average using the units of the response variable.

What does lm Linearregression () do?

The lm() function is used to fit linear models to data frames in the R Language. It can be used to carry out regression, single stratum analysis of variance, and analysis of covariance to predict the value corresponding to data that is not in the data frame.


2 Answers

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) 
like image 84
csgillespie Avatar answered Sep 29 '22 14:09

csgillespie


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.

like image 44
smillig Avatar answered Sep 29 '22 15:09

smillig