2

这有效:

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.

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

4

2 回答 2

4

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

emmeans:::recover_data.lm

这是一个简单的例子:

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

以下是使 emmeans() 与 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 对象的数据属性,而是假设调用将在当前环境中运行......

于 2018-02-03T00:18:42.180 回答
0

我们可以分两步完成

df1 <- warpbreaks %>%
            group_by(tension) %>%
            nest() %>%
            mutate(models = map(data,~glm(breaks~wool,data=.x)))                          

warpbreaks %>% 
      split(.$tension) %>% 
       map( ~glm(breaks ~ wool, data = .x) %>%
                emmeans(., "wool")) %>%
       mutate(df1, Means = .) 

# A tibble: 3 x 4
#   tension data              models    Means        
#  <fctr>  <list>            <list>    <list>       
#1 L       <tibble [18 x 2]> <S3: glm> <S4: emmGrid>
#2 M       <tibble [18 x 2]> <S3: glm> <S4: emmGrid>
#3 H       <tibble [18 x 2]> <S3: glm> <S4: emmGrid>
于 2018-02-02T07:06:22.487 回答