I'm trying to fit a logistic regression model using all predictors as a polynomial model. I've tried doing this but didn't work:
poly_model = glm(type~ poly(., 2), data=train_data, family=binomial)
I'm using the built in dataset:
train_data = MASS::Pima.tr
What's the correct way to do this?
There's not really a way to do that with the .
syntax. You'll need to explictly build the formula yourself. You can do this with a helper function
get_formula <- function(resp) {
reformulate(
sapply(setdiff(names(train_data), resp), function(x) paste0("poly(", x, ", 2)")),
response = resp
)
}
model <- get_formula("type")
model
# type ~ poly(npreg, 2) + poly(glu, 2) + poly(bp, 2) + poly(skin,
# 2) + poly(bmi, 2) + poly(ped, 2) + poly(age, 2)
glm(model, data=train_data, family=binomial)