# using as.formula() in glmer and dredging it with MuMIn

I am trying to use the dredge function from the MuMIn package, as follows. Since I want to run it for different datasets, I am using a function, that takes in the vector of covariates and the formula is created from this vector within the function.

require(lme4);require(MuMIn) x1 <- rnorm(100) x2 <- rnorm(100) zrand <- sample(letters, 100, replace =T) yind <- sample(1:1000, size=100, replace=T) mydata <- data.frame(yind, x1, x2, zrand) vars <- c('x1', 'x2')

When I run it this way, I get the error that symbol is not subsettable. I think it has to do with using vars in the formula, though the lmer runs fine

myformula <- as.formula(paste('yind~',paste(vars,collapse='+'),'+(1|zrand)')) mylmer1 <- lmer(myformula, mydata, gaussian) dredge(mylmer1)

Error in terms(as.formula(formula(x))) : error in evaluating the argument 'x' in selecting a method for function 'terms': Error: object of type 'symbol' is not subsettable

But this works fine, for a reason I do not understand fully, but has to do with the scope of the dredge function. (I may be totally off the mark here)

mylmer2 <- lmer(as.formula(paste('yind~',paste(vars,collapse='+'),'+(1|zrand)')), mydata, gaussian) dredge(mylmer2)

What I doing in my analysis is more akin to this, where lmer and dredge are run inside a function

myfn <- function(fnd,fnvar){ myformula <- as.formula(paste('yind~',paste(fnvar,collapse='+'),'+(1|zrand)')) print(myformula) mylmer1 <- lmer(myformula, fnd, gaussian) print(mylmer1@call) print(dredge(mylmer1)) mylmer2 <- lmer(as.formula(paste('yind~',paste(fnvar,collapse='+'),'+(1|zrand)')), fnd, gaussian) print(mylmer2@call) print(dredge(mylmer2)) } myfn(fnd=mydata, fnvar=vars)

Dredgin only lmer1 gives this error:

Error in print(dredge(mylmer1)) : error in evaluating the argument 'x' in selecting a method for function 'print': Error in terms(as.formula(formula(x))) : error in evaluating the argument 'x' in selecting a method for function 'terms': Error: object of type 'symbol' is not subsettable

Dredging only lmer2, gives the following error

Error in print(dredge(mylmer2)) : error in evaluating the argument 'x' in selecting a method for function 'print': Error in terms(as.formula(formula(x))) : error in evaluating the argument 'x' in selecting a method for function 'terms': Error in paste(fnvar, collapse = "+") : object 'fnvar' not found.

Is there a way to get around this? Is this a necessary limitation of the dredge function?

## Answers

I found the solution here. Apparently it is called a "deparse-substitute trick".

Do

mylmer1@call$formula <- tmp.formula

before dredging the mer object

Missed it as I thought it was something to do with MuMIn.