[R] Long model formulae

James Hirschorn james.hirschorn at hotmail.com
Sun Oct 24 15:38:42 CEST 2010



>
> Here is a dodge I often use. This is a mock-up example.

Very instructive (and helpful) ...

>
> _______________
>
> bar <- data.frame(matrix(rnorm(1001), nrow = 1))
> names(bar)[1] <- "y" ## say
> head(bar[,1:5])
>
> nbar <- names(bar)
> form <- as.formula(paste(nbar[1], "~", paste(nbar[-1], collapse = "+")))
> fitModel <- substitute(tm <- rpart(FORM, data = DATA),
> list(FORM = form, DATA = quote(bar)))
> fitModel ## the screen quietly erupts...
>
> library(rpart)
> eval(fitModel) ## to do the job.
> _______________
>
> The advantage of proceeding this way is that the object you create, fm, has a meaningful (but large!) formula in it and the name of the dataframe from which the variables come. This makes it easy, e.g. to use manipulation tools on it.
>

 		 	   		  


More information about the R-help mailing list