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 (
glm
class vs.speedglm
class which may be S3 vs S4 objects).Specifically, the
glm
method is part of R's standard library instats
package, which works with its relatedstats
method,update
.Per
update
docs,Main argument:
Therefore, if
speedglm
stores the call to capture formula, data, and others args and resembles the return object structure asglm
(which inherits fromlm
class), thenupdate
would work.To resolve, consider doing what
update
does by dynamically buildingformula
with iterative model calls usinglapply
. This would work in both methods, since each uses theformula
object.