Consider the following code:
library(ISLR)
row_list <- structure(list(`1` = 1:40, `2` = 41:79, `3` = 80:118, `4` = 119:157,
`5` = 158:196, `6` = 197:235, `7` = 236:274, `8` = 275:313,
`9` = 314:352, `10` = 353:392),
.Names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"))
test <- row_list[[1]]
train <- setdiff(unlist(row_list), row_list[[1]])
> glm(mpg ~ poly(horsepower, 1), data = Auto, subset = train)
Call: glm(formula = mpg ~ poly(horsepower, 1), data = Auto, subset = train)
Coefficients:
(Intercept) poly(horsepower, 1)
23.37 -133.05
Degrees of Freedom: 351 Total (i.e. Null); 350 Residual
Null Deviance: 21460
Residual Deviance: 8421 AIC: 2122
> glm(mpg ~ poly(horsepower, 1), data = Auto[train,])
Call: glm(formula = mpg ~ poly(horsepower, 1), data = Auto[train, ])
Coefficients:
(Intercept) poly(horsepower, 1)
24.05 -114.19
Degrees of Freedom: 351 Total (i.e. Null); 350 Residual
Null Deviance: 21460
Residual Deviance: 8421 AIC: 2122
As it can be seen above, the (Intercept)
and poly(horsepower, 1)
values differ between the two outputs. Why is this?
At least for lm()
, Introduction to Statistical Learning suggests (see p. 191) that row indices can be used in the subset
argument. Is this not the case for glm()
, or is subset
just not being used correctly?
This is to do with how the orthogonal polynomials are constructed by poly
.
In the first example, they are constructed before subsetting, and in the second the subsetting takes place first (as you pass the subsetted data to glm
).
Using raw polynomials gives identical results:
coef(glm(mpg~poly(hp,1),data=mtcars,subset=10:32))
(Intercept) poly(hp, 1)
20.63307 -28.66876
coef(glm(mpg~poly(hp,1),data=mtcars[10:32,]))
(Intercept) poly(hp, 1)
19.93043 -25.43935
coef(glm(mpg~poly(hp,1,raw=TRUE),data=mtcars,subset=10:32))
(Intercept) poly(hp, 1, raw = TRUE)
31.64927851 -0.07509986
coef(glm(mpg~poly(hp,1,raw=TRUE),data=mtcars[10:32,]))
(Intercept) poly(hp, 1, raw = TRUE)
31.64927851 -0.07509986
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