Mario M.
Mario M.

Reputation: 872

How to train several models within a loop for

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]).

Upvotes: 0

Views: 2132

Answers (2)

timfaber
timfaber

Reputation: 2070

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="".....)
}

Upvotes: 1

Gilles San Martin
Gilles San Martin

Reputation: 4370

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")
}

Upvotes: 2

Related Questions