Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to construct a big regular formula for a model in R?

I am trying create model to predict "y" from data "D" that contain predictor x1 to x100 and other 200 variables . since all Xs are not stored consequently I can't call them by column.

I can't use ctree( y ~ , data = D) because other variables , Is there a way that I can refer them x1:100 ?? in the model ?

instead of writing a very long code

ctree( y = x1 + x2 + x..... x100) 

Some recommendation would be appreciated.

like image 316
JPC Avatar asked Dec 08 '22 14:12

JPC


2 Answers

Two more. The simplest in my mind is to subset the data:

ctree(y ~ ., data = D[, c("y", paste0("x", 1:100))]

Or a more functional approach to building dynamic formulas:

ctree(reformulate(paste0("x", 1:100), "y"), data = D)
like image 77
flodel Avatar answered Dec 11 '22 08:12

flodel


Construct your formula as a text string, and convert it with as.formula.

vars <- names(D)[1:100] # or wherever your desired predictors are
fm <- paste("y ~", paste(vars, collapse="+"))
fm <- as.formula(fm)

ctree(fm, data=D, ...)
like image 32
Hong Ooi Avatar answered Dec 11 '22 09:12

Hong Ooi