Skip to content

Can't use offsets with offset = argument, only with offset() term #667

Description

@capnrefsmmat

Two examples of using offsets:

> fit <- lm(mpg ~ disp + factor(cyl) + offset(wt), data=mtcars)
> estimate_means(fit, by="cyl")
Model contains an offset-term, which is set to its mean value. If you
  want to average predictions over the distribution of the offset (if
  appropriate), use `estimate = "average"` or `estimate = "population"`.
  If you want to fix the offset to a specific value, for instance `1`, use
  `offset = 1`.
Estimated Marginal Means

cyl |  Mean |   SE |         95% CI | t(28)
-------------------------------------------
4   | 23.06 | 1.74 | [19.51, 26.62] | 13.29
6   | 18.13 | 1.32 | [15.42, 20.85] | 13.69
8   | 18.73 | 1.65 | [15.36, 22.10] | 11.39

Variable predicted: mpg
Predictors modulated: cyl
Predictors averaged: disp (2.3e+02), wt (3.2)
> fit <- lm(mpg ~ disp + factor(cyl), data=mtcars, offset=wt)
> estimate_means(fit, by="cyl")
Model contains an offset-term, which is set to its mean value. If you
  want to average predictions over the distribution of the offset (if
  appropriate), use `estimate = "average"` or `estimate = "population"`.
  If you want to fix the offset to a specific value, for instance `1`, use
  `offset = 1`.
Error: Sorry, calculating marginal means failed with following error:
  Unable to compute predicted values with this model. This error can
  arise when `get_modeldata()` is unable to extract the dataset from the
  model object, or when the original data frame (usually in the global
  environment) was modified since fitting the model. Make sure the
  original data object is unchanged and/or try to supply a different
  dataset to the `newdata` argument.In addition, this error message was
  raised:object 'wt' not foundBug Tracker:
  https://github.com/vincentarelbundock/marginaleffects/issues

I'd assume estimate_means() is evaluating the offset argument in the wrong environment, perhaps. It should use the data argument and then the calling environment. I can dig in more detail if it'd be helpful.

I recall ggeffects had something similar: strengejacke/ggeffects#123

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions