Have a look at the question and answers here for building a dynamic formula: Formula with dynamic number of variables
If I were you, I would go with the reformulate
suggestion:
statRemoveOne <- function(df, response, predictors, intercept){
formula <- reformulate(predictors, response, intercept)
as.data.frame(laply(1:nrow(df),
function(x) coef(lm(formula, df[-x,]))))
}
statRemoveOne(mtcars, response = "mpg",
predictors = c("hp", "wt"),
intercept = TRUE)