0

I want to train several models with caret package (one for each of the 7 response variables) within a loop for.

My data.frame data has 46 predictors (all of them are used to train all models) and 7 responses.

Some Rcode I tried but it failed:

models.list = list()
Ynames = names(data)[47:ncol(data)]
for(y in Ynames)
{
models.list[[y]] = train(as.name(y)~., subset(data,select=-Ynames[-y]),method="".....)
}

My variable Ynames contains all the responses. Each model must be trained with a single response variable. So for iteration 1, we would train the model for Ynames[1] response and all 46 predictors, but it's necessary to exclude from the dataset data all non-first response variables (Ynames[-1]).

jmuhlenkamp
  • 2,102
  • 1
  • 14
  • 37
Mario M.
  • 802
  • 11
  • 26

2 Answers2

1

It will be easier if you avoid the formula class and use one matrix or data.frame for your responses and another for your predictors :

Y <- matrix(runif(700, 0, 100), ncol = 7)
X <- matrix(runif(4600, 0, 100), ncol = 46)
colnames(Y) <- paste("Y", 1:ncol(Y))
colnames(X) <- paste("X", 1:ncol(X))

library(caret)

models.list = as.list(vector(length = ncol(Y)))
for(i in 1:ncol(Y)) {
    models.list[[i]] <- train(x = X, y = Y[,i], method = "lm")
}
Gilles San Martin
  • 4,224
  • 1
  • 18
  • 31
1

This might be an alternative which matches your example (using iris). The subsetting was based on this post: removing a list of columns from a data.frame using subset

models.list = list()
Ynames = names(iris)[3:ncol(iris)]

for(y in Ynames)
{
  to.remove <- Ynames[!Ynames==y]
  `%ni%` <- Negate(`%in%`)
  models.list[[y]] = train(as.name(y)~., subset(iris,select = names(iris) %ni% to.remove),method="".....)
}
Community
  • 1
  • 1
timfaber
  • 2,060
  • 1
  • 15
  • 17
  • thanks for the answer. It doesn't work for me, I get the following error: "Error in model.frame.default(form = as.name(y) ~ ., data = subset(iris, : object is not a matrix" – Mario M. May 03 '17 at 07:19
  • Fixed if I modify the input formula: "train(as.formula(paste(y,"~.",sep="")), subset(iris,s...." – Mario M. May 03 '17 at 07:31
  • ah, did not check the actual train function but great! – timfaber May 03 '17 at 09:06