substitute in r together with anova -


i tried run anova on different sets of data , didn't quite know how it. goolged , found useful: http://www.ats.ucla.edu/stat/r/pages/looping_strings.htm

hsb2 <- read.csv("http://www.ats.ucla.edu/stat/data/hsb2.csv") names(hsb2) varlist <- names(hsb2)[8:11] models <- lapply(varlist, function(x) { lm(substitute(read ~ i, list(i = as.name(x))), data = hsb2) }) 

my understanding of above codes creates function lm() , apply each variable in varlist , linear regression on each of them.

so thought use aov instead of lm work me this:

aov(substitute(read ~ i, list(i = as.name(x))), data = hsb2) 

however, got error:

error in terms.default(formula, "error", data = data) :  no terms component nor attribute 

i have no idea of error comes from. please help!

the problem substitute() returns expression, not formula. think @thelatemail's suggestion of

lm(as.formula(paste("read ~",x)), data = hsb2) 

is work around. alternatively evaluate expression formula with

models <- lapply(varlist, function(x) {     aov(eval(substitute(read ~ i, list(i = as.name(x)))), data = hsb2) }) 

i guess depends on want list of models afterward. doing

models <- lapply(varlist, function(x) {     eval(bquote(aov(read ~ .(as.name(x)), data = hsb2))) }) 

gives "cleaner" call property each of result.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -