[R] generating an expression for a formula automatically

Maria Montez montez at bu.edu
Thu Aug 24 23:01:42 CEST 2006


Hi!

I would like to be able to create formulas automatically. For example, I 
want to be able to create a function that takes on two values: resp and 
x, and then creates the proper formula to regress resp on x.

My code:

fit.main <- function(resp,x) {
 form <- expression(paste(resp," ~ ",paste(x,sep="",collapse=" + "),sep=""))
  z <- lm(eval(form))
 z
}
main <- fit.main("y",c("x1","x2","x3","x4"))

and I get this error:
Error in terms.default(formula, data = data) :
        no terms component

Any suggestions?

Thanks, Maria



More information about the R-help mailing list