我在嵌套数据框中有许多 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
这里有一种可能性:将模型拟合和疏浚放在一个函数中,然后嵌套/映射。
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))
dat$dredged[[1]]
Global model call: glmmTMB(formula = mpg ~ disp + drat + wt, data = x, family = gaussian(),
na.action = "na.fail", ziformula = ~0, dispformula = ~1)
---
Model selection table
cnd((Int)) dsp((Int)) cnd(dsp) cnd(drt) cnd(wt) df logLik AICc delta weight
1 19.740 + 2 -12.011 31.0 0.00 0.750
5 28.410 + -2.780 3 -9.825 33.7 2.63 0.201
3 18.490 + 0.3503 3 -11.965 37.9 6.91 0.024
2 19.080 + 0.003605 3 -11.974 37.9 6.93 0.024
6 28.190 + 0.019140 -3.835 4 -7.830 43.7 12.64 0.001
7 30.650 + -0.4436 -2.990 4 -9.702 47.4 16.38 0.000
4 8.536 + 0.022440 1.9780 4 -11.481 51.0 19.94 0.000
8 16.050 + 0.042090 2.3460 -3.989 5 -4.630 79.3 48.24 0.000
Models ranked by AICc(x)