Mikko
Mikko

Reputation: 7755

How to use substitute() to loop lme functions from nlme package?

I am trying to use lme function from nlme package inside a lapply loop. This works for lmer function from lme4 package, but produces an error message for lme. How can I loop lme functions similarly to the lmer function in the example below?

library("nlme")
library("lme4")

set.seed(1)
dt <- data.frame(Resp1 = rnorm(100, 50, 23), Resp2 = rnorm(100, 80, 15), Pred = rnorm(100,10,2), group = factor(rep(LETTERS[1:10], each = 10)))

## Syntax:
lmer(Resp1 ~ Pred + (1 |group), data = dt)
lme(Resp1 ~ Pred, random = ~1 | group, data = dt)

## Works for lme4
lapply(c("Resp1", "Resp2"), function(k) {
  lmer(substitute(j ~ Pred + (1 | group), list(j = as.name(k))), data = dt)})

## Does not work for nlme
lapply(c("Resp1", "Resp2"), function(k) {
lme(substitute(j ~ Pred, list(j = as.name(k))), random = ~1 | group, data = dt)})

# Error in UseMethod("lme") : 
# no applicable method for 'lme' applied to an object of class "call"

PS. I am aware that this solution exists, but I would like to use a method substituting response variable directly in the model function instead of subsetting data using an additional function.

Upvotes: 2

Views: 1562

Answers (2)

Mikko
Mikko

Reputation: 7755

As @CarlWitthoft suggested, adding eval into the function will solve the issue:

lapply(c("Resp1", "Resp2"), function(k) {
lme(eval(substitute(j ~ Pred, list(j = as.name(k)))), random = ~1 | group, data = dt)})

Also see @thothal's alternative.

Upvotes: 1

thothal
thothal

Reputation: 20329

Instead of fiddling around with substitute and eval you also could do the following:

lapply(c("Resp1", "Resp2"), function(r) {
   f <- formula(paste(r, "Pred", sep = "~"))
   m <- lme(fixed = f, random = ~ 1 | group, data = dt)
   m$call$fixed <- f
   m})

You could use the same trick if you want to provide different data sets to a modelling function:

makeModel <- function(dat) {
   l <- lme(Resp1 ~ Pred, random = ~ 1 | group, data = dat)
   l$call$data <- as.symbol(deparse(substitute(dat)))
   l
}

I use this snippet quite a bit, when I want to generate a model from within a function and want to update it afterwards.

Upvotes: 3

Related Questions