Reputation: 19833
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.
Upvotes: 6
Views: 1666
Reputation: 42669
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
Upvotes: 1