knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
This vignette describes the motivation for offsetreg and when its usage becomes necessary.
For certain use cases, offsets are supported in tidymodels. Generally speaking, for models that allow for offsets to be specified in a model formula, tidymodels works fine out of the box and offsetreg is not needed. The glm()
function from the stats package is a good example of this.
glm()
Below, a Poisson model is fit using the us_deaths
data set with an offset equal to the log of population.
library(parsnip) library(offsetreg) library(broom) library(recipes) library(workflows) library(rsample) library(tune) us_deaths$log_pop <- log(us_deaths$population) poisson_reg() |> set_engine("glm") |> fit(deaths ~ gender + age_group + year + offset(log_pop), data = us_deaths)
The code above works for a few reasons:
fit()
captures the formula expression passed to it, and that formula is allowed to contain calls to other functions, like offset()
.glm()
function as-is, as shown in the call printed above.Let's assume we want to use a recipe to pre-process our data. In the example below, a bare bones recipe is used to verify that we can reproduce the same coefficients as the original example. Unfortunately, this creates a problem because recipe()
doesn't allow in-line functions like offset()
.
mod <- poisson_reg() |> set_engine("glm") rec <- recipe(deaths ~ gender + age_group + year + offset(log_pop), data = us_deaths)
As the hint above explains, this error can be avoided by removing the call to offset()
in the recipe and passing a second formula to add_model()
as part of a workflow. Note that the variable passed to offset()
must still be included in the recipe.
rec <- recipe(deaths ~ gender + age_group + year + log_pop, data = us_deaths) workflow() |> add_model(mod, formula = deaths ~ gender + age_group + year + offset(log_pop)) |> add_recipe(rec) |> fit(us_deaths)
These coefficients match the first example without a recipe, so we know this model was set up correctly.
glmnet()
Not all modeling engines allow for offsets to be passed via the formula interface. For example, the glmnet()
function does not not accept formulas; it requires model matrices. Instead, offsets are passed as a numeric vector using an optional engine-specific offset
argument.
poisson_reg(penalty = 1E-5) |> set_engine("glmnet", offset = us_deaths$log_pop) |> fit(deaths ~ year + gender + age_group, data = us_deaths) |> tidy()
This code works because the argument offset = us_deaths$log_pop
is captured and passed directly into glmnet()
.
If we try to use a recipe with an offset passed to the formula
argument of add_model()
, a difficult-to-spot problem emerges. The model runs without errors, but a completely different set of coefficients is returned.
mod_glmnet <- poisson_reg(penalty = 1E-5) |> set_engine("glmnet") rec <- recipe(deaths ~ year + gender + age_group + log_pop, data = us_deaths) workflow() |> add_model(mod_glmnet, formula = deaths ~ year + gender + age_group + offset(log_pop)) |> add_recipe(rec) |> fit(us_deaths) |> tidy()
What happened here? Since glmnet()
doesn't natively support the formula interface, it doesn't know what to do with the offset()
term passed to the formula. Under the hood, the offset()
term is quietly dropped in a call to model.matrix()
that is used to convert the formula to a matrix format acceptable to glmnet()
.
model.matrix(deaths ~ year + gender + age_group + offset(log_pop), us_deaths) |> head()
As a result, the model is exactly what we would see if there were no offset terms to begin with. This is a situation when offsetreg is required.
offsetreg becomes necessary when the underlying modeling engine does not support offsets in formulas and either of these tasks are performed:
recipe()
when offsets cannot be specified in a formulaLet's continue with the last example. The problem can be addressed using offsetreg as follows:
poisson_reg()
with poisson_reg_offset()
formula
argument in add_model()
step_dummy()
. This step was previously not necessary when formula
was passed to add_model()
.mod_offset <- poisson_reg_offset(penalty = 1E-5) |> set_engine("glmnet_offset", offset_col = "log_pop") rec <- recipe(deaths ~ year + gender + age_group + log_pop, data = us_deaths) |> step_dummy(all_nominal_predictors()) workflow() |> add_model(mod_offset) |> add_recipe(rec) |> fit(us_deaths) |> tidy()
For models like glmnet()
where offsets can only be specified as a numeric vector in engine-specific arguments, resampling presents a few challenges:
glmnet()
, if the predict()
function requires offset terms, there is no mechanism to pass those along, which will result in an error.Below is what happens if we attempt to fit 5 bootstrap resamples of the us_deaths
data set without offsetreg.
resamples <- bootstraps(us_deaths, times = 5) mod_glmnet <- poisson_reg(penalty = 1E-5) |> set_engine("glmnet", offset = us_deaths$log_pop) workflow() |> add_recipe(rec) |> add_model(mod_glmnet) |> fit_resamples(resamples) |> collect_metrics()
All models failed to fit, and we receive a specific error message about no offsets being available for predictions.
show_notes(.Last.tune.result)
With offsetreg, this code performs as expected. offsetreg works because behind the scenes it ensures that offset terms are attached to the data at all times, which enables resampling and predictions to function without error.
workflow() |> add_recipe(rec) |> add_model(mod_offset) |> fit_resamples(resamples) |> collect_metrics()
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.