An unexpected error occurred while cyclizing an effect function from an effects package

I am trying to pass a function effectfrom the effects package along with an object (gl)merModfrom lme4 through a loop lapplyand encounter an error that I do not expect. It seems that the function is effectnot looking for objects inside the loop. What am I doing wrong and how to make the loop work without manually placing a data frame in 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
+4
source share

Source: https://habr.com/ru/post/1681240/


All Articles