我正在尝试将effect
包中的(gl)merMod
函数与effects包中的lapply
对象一起传递到effect
循环并遇到我不指望的错误。似乎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
函数无法在循环内查找对象。 如果不将数据框手动放入工作区,如何让循环工作?
import { BackHandler } from 'react-native';
constructor(props) {
super(props)
this.handleBackButtonClick = this.handleBackButtonClick.bind(this);
}
componentWillMount() {
BackHandler.addEventListener('hardwareBackPress', this.handleBackButtonClick);
}
componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress', this.handleBackButtonClick);
}
handleBackButtonClick() {
this.props.navigation.goBack(null);
return true;
}
答案 0 :(得分:2)
我有一个类似的问题(将lm与model.matrix一起使用),效果本身可以正常工作,但后来不适用于我的lapply循环。
我尝试使用for循环重写代码,这解决了我的问题。由于for循环每次都会将对象分配给全局环境,因此您不必手动将其放置在全局环境中。
在您的示例中,for循环不会引发错误,尽管现在您需要将输出分配给以下内容:
for(k in levels(dat$treat)) {
y <- subset(dat, treat == k)
mod <- glmer(value ~ var + (1|rand), data = y, family = binomial)
effects::effect("var", mod)
}