最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - Using a mids object with the marginaleffects package - newdata argument not working - Stack Overflow

programmeradmin1浏览0评论

I am trying to use marginaleffects::avg_comparisons with an imputed dataset (imputed via the mice package) but get an error if I try to use the newdata argument.

This works:

with(data_imputed, avg_comparisons(my_model, variables = list(Allocation = c("Control", "Intervention")), type = "link"))

But the newdata argument doesn't.

with(data_imputed, avg_comparisons(my_model, variables = list(Allocation = c("Control", "Intervention")), type = "link", newdata=datagrid(age=50)))

The error message is:

Error in evalup(calltmp) : 
  Unable to compute predicted values with this model. This error can arise when
  `insight::get_data()` is unable to extract the dataset from the model object, or when the
  data frame was modified since fitting the model. You can try to supply a different dataset
  to the `newdata` argument.
  
  Error in model.frame.default(delete.response(terms(object, fixed.only = TRUE, : variable
  lengths differ (found for 'scale(age)')
  • Even if I don't use 'with', I get the same error whenever the newdata argument is used
  • The model looked similar to:
   with(data_imputed, glmer(reponse_variable ~ scale(age) + Allocation +
   scale(age)*Allocation + (1|RandomEffect), family="binomial",
   control=glmerControl(optimizer="bobyqa",optCtrl = list(maxfun =
   2e5))))
  • I am using marginaleffects version 0.25.0
  • I will try to come up with a minimal reproducible example!

I am trying to use marginaleffects::avg_comparisons with an imputed dataset (imputed via the mice package) but get an error if I try to use the newdata argument.

This works:

with(data_imputed, avg_comparisons(my_model, variables = list(Allocation = c("Control", "Intervention")), type = "link"))

But the newdata argument doesn't.

with(data_imputed, avg_comparisons(my_model, variables = list(Allocation = c("Control", "Intervention")), type = "link", newdata=datagrid(age=50)))

The error message is:

Error in evalup(calltmp) : 
  Unable to compute predicted values with this model. This error can arise when
  `insight::get_data()` is unable to extract the dataset from the model object, or when the
  data frame was modified since fitting the model. You can try to supply a different dataset
  to the `newdata` argument.
  
  Error in model.frame.default(delete.response(terms(object, fixed.only = TRUE, : variable
  lengths differ (found for 'scale(age)')
  • Even if I don't use 'with', I get the same error whenever the newdata argument is used
  • The model looked similar to:
   with(data_imputed, glmer(reponse_variable ~ scale(age) + Allocation +
   scale(age)*Allocation + (1|RandomEffect), family="binomial",
   control=glmerControl(optimizer="bobyqa",optCtrl = list(maxfun =
   2e5))))
  • I am using marginaleffects version 0.25.0
  • I will try to come up with a minimal reproducible example!
Share Improve this question edited Mar 17 at 15:21 medahank asked Mar 17 at 14:53 medahankmedahank 251 silver badge4 bronze badges 6
  • What version of marginaleffects are you using? I don't get this error on 0.25.0. Also, you should not be using with() with avg_comparisons(); avg_comparisons(my_model, variables = list(Allocation = c("Control", "Intervention")), type = "link", newdata=datagrid(age=50)) should be enough. What model did you run? Please provide additional context and ideally a minimal reproducible example so we can better help you. – Noah Commented Mar 17 at 15:04
  • Have updated the post with more context - thanks – medahank Commented Mar 17 at 15:22
  • Thanks for that. It could be thay you are using scale(age) in the model. Does it work if you remove scale()? – Noah Commented Mar 17 at 15:28
  • The issue might also be the datagrid() function, which does not explicitly support this kind of object. – Vincent Commented Mar 17 at 18:49
  • It doesn't look like it's scale(). I'm not sure how to run it without using datagrid()! – medahank Commented Mar 18 at 8:27
 |  Show 1 more comment

1 Answer 1

Reset to default 1

You could try the modelbased-package, which internally uses the marginaleffects package as backend (thus, supporting the same range of models - it just has a slightly different user interface) and which has two function to deal with multiple imputation: pool_predictions() and pool_contrasts().

The basic idea is that you run estimate_means(), which calculates predictions/marginal means, on each imputed data set and then pool those results.

# example for multiple imputed datasets
data("nhanes2", package = "mice")
imp <- mice::mice(nhanes2, printFlag = FALSE)
predictions <- lapply(1:5, function(i) {
  m <- lm(bmi ~ age + hyp + chl, data = mice::complete(imp, action = i))
  estimate_means(m, "age")
})
pool_predictions(predictions)
#> Estimated Marginal Means
#> 
#> age   |  Mean |   SE |        95% CI |  t(1)
#> --------------------------------------------
#> 20-39 | 30.54 | 1.67 | [9.38, 51.71] | 29.23
#> 40-59 | 24.83 | 1.55 | [5.16, 44.50] | 22.84
#> 60-99 | 23.15 | 1.71 | [1.48, 44.82] | 17.44
#> 
#> Variable predicted: bmi
#> Predictors modulated: age
#> Predictors averaged: hyp, chl (1.9e+02)
#> 

In your particular case, you can either use the newdata argument as well (which is passed to the marginaleffects functions), or you define your "data grid" directly in those arguments, where you specify your focal terms. There are a lot of examples shown here.

All (focal) variables specified in by (or contrast for estimate_contrasts()) and their "representative value" definitions are passed to insight::get_datagrid(). This is where you find one difference in the user interface between modelbased and marginaleffects. If you write by = "age=50", this internally creates a corresponding data grid, thus being a "convenient" shortcut.

发布评论

评论列表(0)

  1. 暂无评论