Search code examples
rlapplyscoping

update() does not work for models created via lapply()


I would like to use lapply() to compute several models in R, but it seems that the update() function can't handle models generated through lapply().

A minimal example:

d1 <- data.frame(y = log(1:9), x = 1:9, trt = rep(1:3, each = 3))

f <- list(y ~ 1, y ~ x, y ~ trt)

modsa <- lapply(f, function(formula) glm(formula, data = d1))
modsb <- lapply(f, glm, data = d1)

update(modsa[[1]], data = d1[1:7, ])
#> Error: object of type 'closure' is not subsettable
update(modsb[[1]], data = d1[1:7, ])
#> Error in FUN(formula = X[[i]], data = d1[1:7, ]): could not find function "FUN"

Is there a way that allows update() to deal with models generated through lapply()?


Solution

  • Esther is correct, the problem is with the call element of glm. From ?update:

    ‘update’ will update and (by default) re-fit a model. It does this by extracting the call stored in the object, updating the call and (by default) evaluating that call.

    As already mentioned, one can update including the formula as well:

    update(modsa[[1]], data = d1[1:7, ], formula=f[[1]])
    

    If for some reason this is not convenient, here is how to run your lapply() and have it assign directly the correct formula to the call element:

    modsa <- lapply(f, function(formula) eval(substitute(glm(F, data = d1), list(F=formula))))
    

    This substutites the respective formula to the glm call, and then evaluates it. With this long one-liner you can run update(modsa[[1]], data = d1[1:7, ]) with no problem.