Skip to content

Messaging conflicts in estimate_means() given offset and estimate = 'population' #628

Description

@ejvalencia

Summary

For models with an offset term, modelbased::estimate_means() will automatically detect the presence of an offset term and provide an informative message that the user can fix the offset term to a single value via the function's offset argument.

  • For modelbased::estimate_means(..., estimate = 'population', offset = NULL) the message is printed (as expected).
  • For modelbased::estimate_means(..., estimate = 'population', offset = 1) a new message is printed indicating that the offset term is always ignored when estimate = 'population'.

If the offset argument is always ignored under estimate = 'population' then it may be useful to suppress the "hint" that users can specify the offset argument when modelbased::estimate_means(..., estimate = 'population', offset = NULL).

Worked Example

# Installing required packages
if (!require("modelbased")) install.packages("modelbased", quiet = TRUE)

# Generating data
df <-
  data.frame(
    agecat = factor(c(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)),
    smokes = factor(c(1, 1, 1, 1, 1, 0, 0, 0, 0, 0)),
    deaths = c(32, 104, 206, 186, 102, 2, 12, 28, 28, 31),
    pyears = c(52407, 43248, 28612, 12663, 5317, 18790, 10673, 5710, 2585, 1462)
  )

# Estimating age-sex adjusted expected deaths via regression w/ g-computation
# NOTE: The function prints a message suggesting we specify the `offset` arg.
glm(deaths ~ smokes * agecat + offset(log(pyears)), family = poisson, data = df) |>
  modelbased::estimate_means(by = 'smokes', 
                             predict = 'response', 
                             estimate = 'population',
                             offset = NULL)

#> Model contains an offset-term and you average predictions over the
#>   distribution of that offset. If you want to fix the offset to a specific
#>   value, for instance `1`, use `offset = 1` and set `estimate =
#>   "typical"`.
#>   We also found that the model has a log-transformed offset term. If you
#>   use the `offset` argument, the log-transformation will automatically be
#>   applied to the provided offset-value. I.e., consider using, for
#>   instance, `offset = 10` and not `offset = log(10)`.
#> Average Counterfactual Predictions
#> 
#> smokes |  Mean |   SE |         95% CI |     z
#> ----------------------------------------------
#> 0      | 54.54 | 5.47 | [43.83, 65.25] |  9.98
#> 1      | 77.43 | 3.09 | [71.38, 83.48] | 25.09
#> 
#> Variable predicted: deaths
#> Predictors modulated: smokes
#> Predictors averaged: agecat, pyears (1.8e+04)
#> Predictions are on the response-scale.

# The same, but specifying the `offset` arg.
# NOTE: The function prints a message saying the `offset` arg is ignored.
glm(deaths ~ smokes * agecat + offset(log(pyears)), family = poisson, data = df) |>
  modelbased::estimate_means(by = 'smokes', 
                             predict = 'response', 
                             estimate = 'population',
                             offset = 1000)

#> For `estimate = "population"`, predictions are averaged over the
#>   distribution of the offset and the `offset` argument is ignored. If you
#>   want to fix the offset to a specific value, for instance `1`, use
#>   `offset = 1` and set `estimate = "typical"`.
#> Average Counterfactual Predictions
#> 
#> smokes |  Mean |   SE |         95% CI |     z
#> ----------------------------------------------
#> 0      | 54.54 | 5.47 | [43.83, 65.25] |  9.98
#> 1      | 77.43 | 3.09 | [71.38, 83.48] | 25.09
#> 
#> Variable predicted: deaths
#> Predictors modulated: smokes
#> Predictors averaged: agecat, pyears (1e+03)
#> Predictions are on the response-scale.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions