简体   繁体   English

找不到变量; 范围问题

[英]Variable not Found; Scoping Issue

I'm running into some bizarre scoping (maybe?) problem... The MWE below calls RunSamples, prints the variable pr , then throws an error on the next line saying that pr does not exist. 我遇到了一些奇怪的作用域(也许是?)问题...下面的MWE调用RunSamples,打印变量pr ,然后在下一行抛出错误,说pr不存在。 I can't seem to understand why the print function can find and print the variable pr , but lmer cannot. 我似乎无法理解为什么print函数可以找到并打印变量pr ,但是lmer无法。 Any help would be appreciated. 任何帮助,将不胜感激。 Also, If I change the pr argument to prot , the code runs fine. 另外,如果我将pr参数更改为prot ,则代码可以正常运行。

require(lme4)
dat <-data.frame(value=1:10,Item=1:10,Protocol=rep(c("FFT","Data"),5))
RunSamples <- function(dat,form,pr) {
  rets <- list()
  print(pr)
  rets$Full <- lmer(update.formula(form,.~.),data=dat, subset= Protocol==pr )
  return( rets )
}
RunFullMain <- function(prot="CLASS") {
  ret <- list()

  form <-  value~0+Item
  ret$Item <- RunSamples(dat=dat,form=form    ,prot)
  return(ret)
}

Results <- list()
for (pp in c("FFT","CLASS","PLATO")) {
  Results[[pp]] <- RunFullMain(pp)
}

I'm not sure I can fix this, in the sense of having it Just Work, but I think I can explain what's happening and give you a reasonable workaround. 我不确定我是否可以解决此问题,但我认为我可以解释发生了什么并为您提供合理的解决方法。

lmer does a variety of (probably overly) complicated things to try to evaluate the data and make sure that all the variables referred to in the formula are present. lmer做各种(可能过于复杂)的事情来尝试评估数据并确保公式中涉及的所有变量都存在。 It tries to carry along all the variables in the data argument, and those present in the environment of the formula, but beyond that it has trouble. 它试图携带data参数中的所有变量以及公式环境中存在的变量,但除此之外,它还会带来麻烦。 The proximal problem here is that your subset call refers to a variable that doesn't exist in either of those places. 这里的最接近的问题是您的subset调用引用了在这两个位置中都不存在的变量。 The easy workaround is to use the subset function instead of the subset argument: 一个简单的解决方法是使用subset函数而不是subset参数:

RunSamples <- function(dat,form,pr) {
  rets <- list()
  rets$Full <- lmer(update.formula(form,.~.),
               data=subset(dat,Protocol==pr))
  return( rets )
}

so that the subsetting gets done right away, rather than waiting until the function is called from an environment where pr can't be found. 这样子集就可以立即完成,而不必等到从找不到pr的环境中调用该函数。

When I do this I then encounter a series of different problems, which are probably due to your setting up an unrealistically simple MWE ... 当我这样做时,我会遇到一系列不同的问题,这可能是由于您设置了一个不切实际的简单MWE ...

  • Error: no random effects terms specified in the formula . Error: no random effects terms specified in the formula (Changed 0+Item to 0+(1|Item) in the original formula) (在原始公式中将0+Item更改为0+(1|Item)
  • Error: number of levels of each grouping factor must be < number of observations (could probably deal with this one too ...) Error: number of levels of each grouping factor must be < number of observations (也可能也可以处理此数...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM