使用marginaleffects包处理mids对象时newdata参数失效问题
使用marginaleffects处理mice插补数据集时,newdata参数报错问题
尝试用marginaleffects::avg_comparisons函数处理通过mice包插补得到的mids对象数据集,不加newdata参数时代码可正常运行,但添加该参数后出现报错。
可正常运行的代码
with(data_imputed, avg_comparisons(my_model, variables = list(Allocation = c("Control", "Intervention")), type = "link"))
添加newdata参数后报错的代码
with(data_imputed, avg_comparisons(my_model, variables = list(Allocation = c("Control", "Intervention")), type = "link", newdata=datagrid(age=50)))
报错信息
Error in evalup(calltmp) :
Unable to compute predicted values with this model. This error can arise wheninsight::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 thenewdataargument.Error in model.frame.default(delete.response(terms(object, fixed.only = TRUE, : variable
lengths differ (found for 'scale(age)')
补充说明
- 无论是否使用
with()函数,只要添加newdata参数就会出现相同报错 - 模型拟合代码如下:
with(data_imputed, glmer(reponse_variable ~ scale(age) + Allocation + scale(age)*Allocation + (1|RandomEffect), family="binomial", control=glmerControl(optimizer="bobyqa",optCtrl = list(maxfun = 2e5))))
- 使用的marginaleffects版本为0.25.0
- 后续将提供最小可复现示例
内容的提问来源于stack exchange,提问作者medahank
相关产品推荐
相关产品推荐

