Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

order of coefficients in lm, R

When running a regression in R, what is the order for the returned coefficients? For example:

 coef(lm(y ~ x + z, data=data.frame(x=1:10, y=10:1, z=1:5)))

Is it guaranteed that the coefficient associated with x will always be returned before the coefficient associated with z? By order I mean the order in the vector of returned coefficients. The reason this matters for me is I would like to test a linear hypothesis about the coefficients in my model and therefore the order of the coefficients in the variance covariance matrix returned by vcov and the actual estimates returned by coef matters.

like image 492
Alex Avatar asked May 19 '13 03:05

Alex


People also ask

Does the order of variables matter in lm R?

The order is not important for the summary of the linear model (which is based on t-tests that don't change). You can see this in your output which is the same. Note the different p-values for the factors b and c.

What is the general format of the lm () function?

The lm() function Note that the formula argument follows a specific format. For simple linear regression, this is “YVAR ~ XVAR” where YVAR is the dependent, or predicted, variable and XVAR is the independent, or predictor, variable.

What is Pr (>| t |) in R?

The Pr(>|t|) column represents the p-value associated with the value in the t value column. If the p-value is less than a certain significance level (e.g. α = . 05) then the predictor variable is said to have a statistically significant relationship with the response variable in the model.

How does lm in R work?

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.


1 Answers

Index by name, not by position. Then you will always get the right answer.

coef(lm(y ~ x+z, data=data.frame(x=1:10, y=10:1, z=1:5)))['x']
##  x 
## -1 
coef(lm(y ~ x+z, data=data.frame(x=1:10, y=10:1, z=1:5)))['z']
##             z 
## -1.855301e-16 

And both of them, in the desired order:

coef(lm(y ~ x+z, data=data.frame(x=1:10, y=10:1, z=1:5)))[c('x', 'z')]
##             x             z 
## -1.000000e+00 -1.855301e-16 
like image 82
Matthew Lundberg Avatar answered Nov 14 '22 23:11

Matthew Lundberg