How to build a large regular formula for a model in R?

I am trying to create a model for predicting "y" from data "D" containing the predictor x1-x100 and another 200 variables. since all Xs are not saved, so I cannot name them by column.

I can not use ctree( y ~ , data = D) , because there are other variables, is there a way I can refer to them x1: 100 ?? in the model?

instead of writing very long code

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

Some recommendations will be appreciated.

+4
source share
3 answers

Build the formula as a text string and transform 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, ...) 
+4
source

Two more. The simplest thing in my head is a subset of data:

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

Or a more functional approach to the construction of dynamic formulas:

 ctree(reformulate(paste0("x", 1:100), "y"), data = D) 
+8
source

You can use this:

 fml = as.formula(paste("y", paste0("x", 1:100, collapse=" + "), sep=" ~ ")) ctree(fmla) 
+2
source

Source: https://habr.com/ru/post/1488563/


All Articles