1

I would like to execute the following function:

library(glmulti)
jmra <- function(fname){
    mydata <- read.csv(fname)
    myvars <- c('X1', 'X2', 'X3', 'X4', 'X5', 'X6')
    formula <- paste('Y ~', paste(myvars, collapse='+'))
    glmulti.lm.out <- glmulti(formula, data=mydata, level=1, method="h", crit="aicc", confsetsize=50, plotty=F, report=F, fitfunction='lm')
    return(glmulti.lm.out)
}
test <- jmra('test.csv')

However, what I get is this error message:

Error in glmulti(y = Y ~ X1 + X2 + X3 + X4 + X5 + X6 : object 'mydata' not found

Interestingly, everything works fine if the same code is outside of this function. So what is happening here?

HyperCube
  • 3,870
  • 9
  • 41
  • 53

1 Answers1

0

The answer was already provided here: error object not found when calling glmulti within a function

if "glmulti" is called like this, everything works as supposed to:

glmulti.lm.out<- do.call("glmulti", list(formula, data=mydata, level=1, method="h", crit="aicc", confsetsize=50, plotty=F, report=F, fitfunction='lm'))
Community
  • 1
  • 1
HyperCube
  • 3,870
  • 9
  • 41
  • 53