在函数内调用glmulti时未找到错误对象 [英] error object not found when calling glmulti within a function

查看:164
本文介绍了在函数内调用glmulti时未找到错误对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在自己的函数中使用glmulti软件包。

下面的代码是一个简化的例子,它重现了错误:
错误:找不到对象'poplrun'

这是在函数内创建的 data.frame
在第二个示例中,它找不到参数l。



我认为问题与调用glmulti的环境有关。我发现这篇文章





并尝试使用 do.call with substitute(poplrun) as.name(poplrun)我错过了一些东西,因为它不起作用。我也发现这篇文章
在将模型公式传递给另一个函数时发现对象未找到错误



并尝试识别公式中的环境(如您在第一个示例中所见),但那一个也行不通。
我真的很感激任何帮助,因为现在我试图解决这个难题,现在是两天...

感谢堆!

p>

示例1



  set.seed(5)
df1< ; -data.frame(Scenario = rep(LETTERS [1:2],each = 10),
Iteration = rep(1:10,2),V1 = rnorm(n = 20,mean = 0.5,sd = $)
LookUpT< -data.frame(Scenario = rep(LETTERS [1:5]),SV1 = 1:5,SV2 = 6:10)
InteractRun < - function(
param =V1,
SVs = c(SV1,SV2),
ic =aic,
l = 1
){
poplrun< -df1
require(plyr)
poplrun< - join(poplrun,LookUpT,by ='Scenario',type =left)
xs< -paste(SVs,collapse =*)
.env <-environment()
公式<-as.formula(paste0(param,〜,xs),env = .env)
require(betareg )
require(glmulti)
cand< -glmulti(公式,数据= poplrun,method =d,level = 1,
fitfunc = betareg,na.action = na.omit)
print(cand)
}
InteractRun()



示例2



  set.seed(5)
df1< -data.frame(Scenario = rep(LETTERS [1:2],each = 10),Iteration = rep(1:10,2),
V1 = round(rnorm(n = 20,mean = 20,sd = 2)))
LookUpT <-data.frame(Scenario = rep(LETTERS [1:5]),SV1 = 1:5 ,SV2 = 6:10)
InteractRun < - 函数(
param =V1,
SVs = c(SV1,SV2),
fam = poisson,
ic =aic,
l = 1
){
poplrun <-df1
require(plyr)
poplrun < - join (pastes(param,〜)),这样, ,xs))#设置要使用的公式
glm1< -glm(data = poplrun,formula,family = fam,na.action = na.omit)
require(glmul ti)
cand< -glmulti(glm1,method =d,level = 1,na.action = na.omit)
print(cand)
}
InteractRun )


解决方案

我要回答我自己的问题。 ..过了一会儿,我想出了如何解决这个问题。
glmulti的正确调用是使用 do.call ,但不需要 substitute() as.name()。在示例1中,调用应该是:
cand < - do.call(glmulti,list(formula,data = poplrun,method =d,level = 1,fitfunc = betareg,na.action = na.omit))
和在示例2中:
cand < - do.call(glmulti, list(glm1,method =d,level = 1,na.action = na.omit))


I'm having trouble to use the package glmulti within my own function.

The code below is a simplified example that reproduces the error: Error: object 'poplrun' not found

Which is the data.frame created within the function. In the second example, it doesn't find the argument l.

I think the problem is related to the environment where glmulti is called. I found this post

Trouble passing on an argument to function within own function

and tried to use do.call with substitute(poplrun) or as.name("poplrun") but clearly I'm missing something because it didn't work. I have also found this post Object not found error when passing model formula to another function

and tried to identify the environment within the formula (as you can see in my first example) but that one too doesn't work. I would really appreciate any help with this since it is now two days that I'm trying to solve this puzzle...

Thanks heaps!

Example 1

    set.seed(5)
    df1<-data.frame(Scenario=rep(LETTERS[1:2], each=10), 
                   Iteration=rep(1:10, 2), V1=rnorm(n=20, mean=0.5, sd=0.1))
    LookUpT<-data.frame(Scenario=rep(LETTERS[1:5]), SV1=1:5, SV2=6:10 )
    InteractRun<- function (
      param="V1" , 
      SVs=c("SV1", "SV2"),
      ic="aic",
      l=1 
      ) {
         poplrun<-df1
         require(plyr)
         poplrun<- join(poplrun, LookUpT, by = 'Scenario', type="left")
         xs<-paste(SVs, collapse="*") 
         .env<-environment() 
         formula<-as.formula(paste0(param, "~", xs), env=.env)
         require(betareg)
         require(glmulti)
         cand<-glmulti(formula, data=poplrun, method="d", level=l, 
         fitfunc=betareg,  na.action=na.omit)
         print(cand)
       }
       InteractRun()

Example 2

    set.seed(5)
    df1<-data.frame(Scenario=rep(LETTERS[1:2], each=10), Iteration=rep(1:10, 2), 
            V1=round(rnorm(n=20, mean=20, sd=2))) 
    LookUpT<-data.frame(Scenario=rep(LETTERS[1:5]), SV1=1:5, SV2=6:10 ) 
    InteractRun<- function (
    param="V1" , 
    SVs=c("SV1", "SV2"), 
    fam="poisson",
    ic="aic",
    l=1 
    ) {
       poplrun<-df1
       require(plyr)
       poplrun<- join(poplrun, LookUpT, by = 'Scenario', type="left")
       xs<-paste(SVs, collapse="*") 
       formula<-as.formula(paste0(param, "~", xs)) # set up formula to be used  
       glm1<-glm(data=poplrun, formula, family=fam, na.action=na.omit)
       require(glmulti)
       cand<-glmulti(glm1, method="d", level=l,  na.action=na.omit)
       print(cand)
      }
     InteractRun()

解决方案

I'm going to answer my own question... After a while, I figured out how to tackle the problem. The correct call for glmulti is using do.call but there is no need for either substitute() or as.name(). In the example 1 the call should be: cand <- do.call("glmulti", list(formula, data=poplrun, method="d", level=l, fitfunc=betareg, na.action=na.omit)) and in example 2: cand <- do.call("glmulti", list(glm1, method="d", level=l, na.action=na.omit))

这篇关于在函数内调用glmulti时未找到错误对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆