Reputation: 161
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!
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()
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()
Upvotes: 2
Views: 1263
Reputation: 161
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))
Upvotes: 5