I am trying to pass the effect
function from the effects package together with a (gl)merMod
object from the lme4 package through a lapply
loop and encounter an error I do not expect. It seems that the effect
function fails to look for objects inside the loop. What do I do wrong and how to get the loop working without manually placing the data frame into the workspace?
library(lme4)
library(reshape2)
library(effects)
dat <- data.frame(var = rep(c("A", "B", "C"), 100), treat = rep(c("T1", "T2"),
each = 150), rand = rep(c("B", "C", "A"), 100), value = rep(c(1,0), 150))
lapply(levels(dat$treat), function(k) {
y <- subset(dat, treat == k)
mod <- glmer(value ~ var + (1|rand), data = y, family = binomial)
})
## Works
lapply(levels(dat$treat), function(k) {
y <- subset(dat, treat == k)
mod <- glmer(value ~ var + (1|rand), data = y, family = binomial)
effects::effect("var", mod)
})
## Error in is.data.frame(data) : object 'y' not found
y <- subset(dat, treat == "T1")
mod <- glmer(value ~ var + (1|rand), data = y, family = binomial)
effects::effect("var", mod)
## Works
lapply(levels(dat$treat), function(k) {
y <- subset(dat, treat == k)
mod <- glmer(value ~ var + (1|rand), data = y, family = binomial)
effects::effect("var", mod)
})
## Works, because object y is in the workspace