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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With