不能在地图内使用 emmeans [英] can't use emmeans inside map

查看:73
本文介绍了不能在地图内使用 emmeans的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有效:

testmodel=glm(breaks~wool,data=warpbreaks)
emmeans::emmeans(testmodel,"wool")

这有效:

warpbreaks %>%
  group_by(tension) %>%
  do(models=glm(breaks~wool,data=.)) %>% 
  ungroup() %>%
  mutate(means=map(models,~emmeans::emmeans(.x,"wool")))

这不会:

warpbreaks %>%
  group_by(tension) %>% nest() %>%
  mutate(models=map(data,~glm(breaks~wool,data=.x))) %>%
  mutate(means=map(models,~emmeans::emmeans(.x,"wool")))

Error in is.data.frame(data) : object '.x' not found
Error in mutate_impl(.data, dots) : 
  Evaluation error: Perhaps a 'data' or 'params' argument is needed.

知道是什么原因造成的吗?

Any idea what's causing this?

推荐答案

我想通了.问题在于 emmeans 尝试从 lm/glm 对象中恢复数据的方式:它尝试运行存储在对象中的调用,如果在与原始 glm() 调用不同的环境中调用 emmeans() 则会失败:

I figured it out. The issue is the way emmeans tries to recover data from lm/glm objects: it tries to run the call stored in the object, which fails if emmeans() is called in a different environment than the original glm() call:

emmeans:::recover_data.lm

这是一个简单的例子:

wb=warpbreaks
model=glm(breaks~wool,data=wb)
emmeans(model,"wool")
rm(wb)
emmeans(model,"wool")

这是使 emmeans() 与 map() 一起工作的方法:

Here's the way to make emmeans() work with map():

warpbreaks %>%
  group_by(tension) %>% nest() %>%
  mutate(models=map(data,~glm(breaks~wool,data=.x))) %>%
  mutate(means=map(models,~emmeans::emmeans(.x,"wool",data=.x$data)))

recover_data() 不只是自动使用 lm/glm 对象的数据属性,而是假设调用将在当前环境中起作用,这似乎很奇怪...

It seems strange that recover_data() doesn't just automatically use the data attribute of the lm/glm objects and instead assumes that the call will function in the current environment...

这篇关于不能在地图内使用 emmeans的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆