I am having problem running a classification (binary outcome) gam model using the additive
package in combination with tidymodels
. I have tried to follow the creator of additive
's GetStarted.Rmd workflow on his github page (https://github.com/hsbadr/additive/blob/main/vignettes/GetStarted.Rmd), but I am unable to get the model to run for a classification gam model. The error I keep getting is:
Error in gam.fit3(x = X, y = y, sp = L %*% lsp + lsp0, Eb = Eb, UrS = UrS, :
NA/NaN/Inf in foreign function call (arg 1)
In addition: Warning messages:
1: In Ops.factor(y, mu) : ‘-’ not meaningful for factors
2: In Ops.factor(eta, offset) : ‘-’ not meaningful for factors
3: In Ops.factor(yg, mug) : ‘-’ not meaningful for factors
I have reproducible code below that will show the problem.
R.Version = 4.4.2
additive version: additive_1.0.1
library(additive)
library(tidymodels)
train_dat <- data.frame(collision = sample(rep(0:1, 100)),
AADT = sample(rep(0:1000,ceiling(nrow(1000) / 2), 100))) %>%
mutate(collision = factor(collision))
test_recipe <- train_dat |>
recipe() |>
update_role(collision, new_role = "outcome") |>
update_role(AADT, new_role = "predictor") |>
step_normalize(all_numeric_predictors())
test_model <- additive(
method = "REML"
) |>
set_engine("mgcv") |>
set_mode("classification")
test_workflow <- workflow() |>
add_recipe(test_recipe) |>
add_model(
spec = test_model,
formula = collision ~ s(AADT)
)
test_workflow_fit <- test_workflow |>
fit(data = train_dat)
The error you encounter comes from the fact that additive()
fails to set the family correctly for classification models. The workaround is to set the family manually. The following code runs the classification model with additive()
:
library(additive)
library(tidymodels)
train_dat <- data.frame(collision = sample(rep(0:1, 100)),
AADT = sample(rep(0:1000,ceiling(1000 / 2), 100))) %>%
mutate(collision = factor(collision))
test_recipe <- train_dat |>
recipe() |>
update_role(collision, new_role = "outcome") |>
update_role(AADT, new_role = "predictor") |>
step_normalize(all_numeric_predictors())
test_model <- additive(
method = "REML", family=binomial()
) |>
set_engine("mgcv") |>
set_mode("classification")
test_workflow <- workflow() |>
add_recipe(test_recipe) |>
add_model(
spec = test_model,
formula = collision ~ s(AADT)
)
test_workflow_fit <- test_workflow |>
fit(data = train_dat)
Note the family=binomial()
in the additive()
call.