Reputation: 33
I'm new to the R tool and am having some trouble with the glm()
function.
I have some data that I have showed below. When the linear predictor is just x, the glm()
function works fine but as soon as I change the linear predictor to x + x^2, it starts giving me the same results that I got for the first model.
The code is as follows:
model1 <- glm(y ~ x, data=data1, family=poisson (link="log"))
coef(model1) (Intercept) x 0.3396339 0.2565236
model2 <- glm(y ~ x + x^2, data=data1, family=poisson (link="log"))
coef(model2) (Intercept) x 0.3396339 0.2565236
As you can see there's no coefficient for x^2 as if it's not even in the model.
Upvotes: 1
Views: 276
Reputation: 2115
The lm
and glm
functions have a special interpretation of the formula (see ?formula
) which can be confusing if you are not expecting it. The intended usage of the interface is (w + x)^2
means a*w + b*x + c*w*x + d
! If you wish to suppress this you need to use the literal function, I
.
model2 <- glm(gear ~ disp + I(disp^2),
data = mtcars, family = poisson (link = "log"))
coef(model2)
# (Intercept) disp I(disp^2)
# 1.542059e+00 -1.248689e-03 6.578518e-07
Put another way, I
allows you to perform transformations in the call to glm
. The following is equivalent.
mtcars1 <- mtcars
mtcars1$disp_sq <- mtcars1$disp^2
model2a <- glm(gear ~ disp + disp_sq,
data = mtcars1, family = poisson (link = "log"))
coef(model2a)
# (Intercept) disp disp_sq
# 1.542059e+00 -1.248689e-03 6.578518e-07
Upvotes: 3