如何在嵌套数据框中的模型上使用MuMIn::dredge

cl25kdpy  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(114)

我在一个嵌套的数据框架中有许多glmmTMB模型,我想迭代地挖掘,但即使尝试挖掘单个模型,我也会得到这样的结果:
eval(call(“is.data.frame”,gmCall$data),gmEnv)中出错:未找到对象““is.data.frame”“
只要数据保存为环境中的对象,在嵌套数据框之外拟合相同的模型就不会有问题。是否有使用嵌套数据框的修复方法?或者如果需要,可以使用列表?我宁愿不要有几十个独立的数据框和附带的模型。
使用mtcars数据集的示例:

# Example model to fit
mod_fx <- function(x) {
  glmmTMB(mpg ~ disp + drat + wt, 
          data = x,
          family = gaussian(),
          na.action = "na.fail")
}

# Grouping and nesting data
dat <- mtcars %>% 
  group_by(cyl) %>%
  nest() %>% 
  mutate(mod = map(data, mod_fx))

# Model summary works fine, but note: Data = x
# Related to problem?
summary(dat$mod[[1]])

# Dredge fails
dred_dat <- dredge(dat$mod[[1]],
                   rank = "AICc",
                   evaluate = T)

# Dredge does work when same model is built with separate data frame object stored in environment, not a nested data frame.
dat2 <- filter(mtcars, cyl == 6)

m1 <- glmmTMB(mpg ~ disp + drat + wt, 
        data = dat2,
        family = gaussian(),
        na.action = "na.fail")

summary(m1) # Same model results, (data = dat2), but dredge works:

dred_dat <- dredge(m1,
                   rank = "AICc",
                   evaluate = T)

# This also fails without data stored as separate object
m3 <- glmmTMB(mpg ~ disp + drat + wt, 
              data = filter(mtcars, cyl == 6),
              family = gaussian(),
              na.action = "na.fail")

summary(m3) # Data: filter(mtcars, cyl == 6)

dred_dat <- dredge(m3,
                   rank = "AICc",
                   evaluate = T)
# Error in dredge(m3, rank = "AICc", evaluate = T) : 'global.model' uses "data" that is a function value: use a variable instead

字符串

mbjcgjjk

mbjcgjjk1#

这里有一种可能性:将模型拟合和挖掘放在一个函数中,然后nest/map。

library(tidyverse)
library(MuMIn)

dfun <- function(x) {
   g1 <- glmmTMB(mpg ~ disp + drat + wt, 
          data = x,
          family = gaussian(),
          na.action = "na.fail")
   dredge(g1, rank = "AICc",  evaluate = TRUE)
}
dat <- mtcars %>% 
  group_by(cyl) %>%
  nest() %>% 
  mutate(dredged = map(data, dfun))

个字符

相关问题