【问题标题】:error object not found when calling glmulti within a function在函数中调用 glmulti 时找不到错误对象
【发布时间】:2014-07-11 14:05:31
【问题描述】:

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

下面的代码是重现错误的简化示例: 错误:找不到对象“poplrun”

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

我认为问题与调用 glmulti 的环境有关。我找到了这篇文章

Trouble passing on an argument to function within own function

并尝试将do.callsubstitute(poplrun)as.name("poplrun") 一起使用,但显然我遗漏了一些东西,因为它不起作用。我也找到了这个帖子 Object not found error when passing model formula to another function

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

谢谢大家!

示例 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()

示例 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()

【问题讨论】:

    标签: r function


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-18
      • 2013-06-22
      • 2011-03-15
      • 2015-04-20
      • 1970-01-01
      • 2018-02-21
      • 1970-01-01
      • 2015-02-02
      相关资源
      最近更新 更多