I wish to write a function using dlply to fit a linear regression stratified by
"cat1 by arg1"
So my function looks like this
fun1 <- function(arg1) {
m1 <- data.frame(...)
mod.var <- ...
mod.form <- formula(paste("y ~", paste(mod.var, collapse = " + ")))
list_of_models <- dlply(m1, .(cat1,arg1), function(X) lm(mod.form, data = X,
na.action=na.omit), .parallel=FALSE)
}
How to write a function so that when I call the function fun1("cat2") the function will execute
list_of_models <- dlply(m1, .(cat1,cat2), function(X) lm(mod.form, data = X,
na.action = na.omit), .parallel=FALSE)
and call the function fun1("cat3"), the function will execute
list_of_models <- dlply(m1,.(cat1,cat3), function(X) lm(mod.form, data = X,
na.action=na.omit), .parallel=FALSE)
where cat1, cat2 and cat3 are the names of categorical variables.
Thank you for your help.
Edit: As the function stands at the moment, it does not work properly because ".(cat1,arg1)" is not “recognizable” in dlply. Some modifications are needed, but how?
Try
c("cat1", arg1)instead of.(cat1, arg1).Citing
?dlply: