Skip to content
Prev 163441 / 398506 Next

Coercing a list of variables in a function call

try the following:

model.it <- function (form, data, factor.id) {
     if (!missing(factor.id) && all(factor.id %in% names(data)))
         data[factor.id] <- lapply(data[factor.id], factor)
     glm(form, data = data)
}

y <- c(1,2.1,3.3,4.3,5,6.5)
x1 <- c(1,1,1,2,2,2)
x2 <- c(1,2,3,1,2,3)
x3 <- c(1,3,2,4,2,4)
d <- data.frame(y, x1, x2, x3)

model.it(y ~ x1 + x2 + x3, d, c("x1", "x2"))
model.it(y ~ x1 + x2 + x3, d)


If you only plan to fit linear regression models, then it is advisable 
that you lm() instead of glm(). I hope it helps.

Best,
Dimitris
Philip Whittall wrote: