[R] Error in lm() with very small (close to zero) regressor
Ben Bolker
bbolker at gmail.com
Sun Mar 29 20:34:50 CEST 2015
RiGui <raluca.gui <at> business.uzh.ch> writes:
>
[snip]
> I am terribly sorry for the code not being reproducible, is the
> first time I am posting here, I run the code several times before I
> posted, but...I forgot about the library used.
Thanks for updating.
> To answer to your questions:
>
>> How do you know this answer is "correct"?
> What I am doing is actually a "fixed effect" estimation. I apply a
> projection matrix to the data, both dependent and independent
> variables, projection which renders the regressors that do not vary,
> equal to basically zero - the x1 from the post.
> Once I apply the projection, I need to run OLS to get the estimates,
> so x1 should be zero.
Yes, but not *exactly* zero.
> Therefore, the results with the scaled regressor is not correct.
> Besides, I do not see why the bOLS is wrong, since is the formula of
> the OLS estimator from any Econometrics book.
Because it's numerically unstable.
Unfortunately, you can't always translate formulas directly from
books into code and expect them to be reliable.
Based on Peter's comments, I believe that as expected lm()
is actually getting closer to the 'correct' answer.
> Here again the corrected code:
> library(corpcor)
>
> n_obs <- 1000
> y <- rnorm(n_obs, 10,2.89)
> x1 <- rnorm(n_obs, 0.00000000000001235657,0.000000000000000045)
> x2 <- rnorm(n_obs, 10,3.21)
> X <- cbind(x1,x2)
As Peter points out, one example uses an intercept and the
other doesn't: you should either use X <- cbind(1,x1,x2) or
lm(y~x1+x2-1) for compatibility.
> bFE <- lm(y ~ x1 + x2)
> bFE
>
> bOLS <- pseudoinverse(t(X) %*% X) %*% t(X) %*% y
> bOLS
[snip: Gmane doesn't like it if I quote "too much"]
More information about the R-help
mailing list