Reputation: 21292
library(ISLR)
Choosing a model based on any feature:
amodel <- lm(crim ~ poly(dat$zn, 3), data = Boston)
Works, returns a model. When I try to do this with every variable in a loop I get:
dat <- dplyr::select(Boston, -crim)
sapply(dat, function(x) {
mod <- lm(crim ~ poly(x, 3), data = Boston)
summary(mod)
})
Error in poly(x, 3) : 'degree' must be less than number of unique points
How can I get this to run within a loop? I would like to create a model for each poly(feature, 3)
in Boston (minus crim feature).
Upvotes: 0
Views: 1451
Reputation: 393
Numerical overflow would be my guess. If you dig into the code for poly
, you'll see that it generates the individual polynomial terms as an intermediate step:
X <- outer(x, seq_len(n) - 1, "^")
and when n
(the degree of the polynomial you want) is 3, combined with the code used to generate the linear model lm()
, the resulting terms go up to exponentially.
Upvotes: 1