I am using mtcars data to show my problem. The following code works fine with glm. It generates new models by adding each variable in the vlist to the model of glm(vs ~ mpg, family = binomial(), data = mtcars.
check_glm <- function(crude, vlist, data, ...){
a <- glm(crude, data = data, family = binomial())
lapply(vlist, function(x) update(a, as.formula(paste0(". ~ . +", x))))
}
check_glm(crude = "vs ~ mpg", vlist = c("am", "hp"), data = mtcars)
However, when I replaced glm with speedglm,
library(speedglm)
check_speedglm <- function(crude, vlist, data, ...){
a <- speedglm(crude, data = data, family = binomial())
lapply(vlist, function(x) update(a, as.formula(paste0(". ~ . +", x))))
}
check_speedglm(crude = "vs ~ mpg", vlist = c("am", "hp"), data = mtcars)
I got:
Error in model.frame.default(formula = vs ~ mpg + am, data = data, drop.unused.levels = TRUE) : argument "data" is missing, with no default.
I think the problem is in the lapply line but I could not work out a solution. Any suggestions to fix this would be appreciated.
Essentially, you are mixing up package methods that may not be compatible with each other. Though they share same name, both of these methods are from different packages so different authors for different purposes and output different objects (
glmclass vs.speedglmclass which may be S3 vs S4 objects).Specifically, the
glmmethod is part of R's standard library instatspackage, which works with its relatedstatsmethod,update.Per
updatedocs,Main argument:
Therefore, if
speedglmstores the call to capture formula, data, and others args and resembles the return object structure asglm(which inherits fromlmclass), thenupdatewould work.To resolve, consider doing what
updatedoes by dynamically buildingformulawith iterative model calls usinglapply. This would work in both methods, since each uses theformulaobject.