Q:

Creating an X number of nnet models programatically in R

I was hoping I could get some help on the following problem. Basically, I want to create a number of nnet models programatically based on the length of a particular vector, PredVector. Each value in PredVector indirectly refers to a column in my data set, PSTrain.
The code is as follows:
PredVector <- c(1, 3, 5)
for (i in 1:length(PredVector)) {
   modelName <- paste("nnModel", PredVector[i], sep="")
   modelForm <- paste("TPlus", PredVector[i], "~.", sep="")
   as.formula(paste(modelName, "<- nnet(", modelForm, ", PSTrain, size=5, maxit=2000, linout=F)"))
}

I was hoping for three models to be created: nnModel1, nnModel3 and nnModel5. However, while the code successfully runs the nnet model three times at the desired settings, the models are not saved to my workspace.
Any ideas on how to solve this problem?
Thanks in advance!

A:

Make a list of models:
models <- list()
for(i in seq(length(ProdVector))) {
   models[[i]] <- # run model here
}

If you want to save other things in the loop, make more lists and store more results.

