[R] change lm log(x) to glm poisson
Joshua Wiley
jwiley.psych at gmail.com
Mon Oct 29 04:25:37 CET 2012
Hi Elaine,
If you want identical models, you need to use the same family and then
the formula is the same. Here is an example with a built in dataset:
## these two are identical
> coef(lm(mpg ~ hp + log(wt), data = mtcars))
(Intercept) hp log(wt)
38.86095585 -0.02808968 -13.06001270
> coef(glm(mpg ~ hp + log(wt), data = mtcars, family = gaussian))
(Intercept) hp log(wt)
38.86095585 -0.02808968 -13.06001270
## not identical
> coef(glm(mpg ~ hp + wt, data = mtcars, family = gaussian(link = "log")))
(Intercept) hp wt
3.88335638 -0.00173717 -0.20851238
I show the log link because the poisson family default to a log link,
but that is equivalent to:
log(E(y)) = Xb
where X is your design matrix (intercept, A, B, log(C), log(D) for
you). In short the link function operates on the outcome, not the
predictors so even though the poisson family includes a log link, it
will not yield the same results as a log transformation of two of your
predictors.
I do not have any online references off the top of my head, but it
seems like you may be well served by reading some about generalized
linear models and the concept of link functions.
Cheers,
Josh
On Sun, Oct 28, 2012 at 8:01 PM, Elaine Kuo <elaine.kuo.tw at gmail.com> wrote:
>
> Hello list,
>
> I am running a regression using
>
> lm(Y~A+B+log(C)+log(D))
>
>
> Now, I would like to test if glm can produce similar results.
> So the code was revised as
>
> glm(Y~A+B+C+D, family=poisson) (code 1)
>
>
> However, I found some example using glm for lm.
> It suggests that the code should be revised like
> glm(Y~A+B+log(C)+log(D), family=poisson) (code 2)
>
> Please kindly advise which code is correct.
> Thank you.
>
> Elaine
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/
More information about the R-help
mailing list