Search code examples
rlistoopobjectstore

Create list of predefined S3 objects


I am busy with comparing different machine learning techniques in R. This is the case: I made several functions that, in an automated way are able to create each a different prediction model (e.g: logistic regression, random forest, neural network, hybrid ensemble , etc.) , predictions, confusion matrices, several statistics (e.g AUC and Fscore) ,and different plots.

I managed to create an S3 object that is able to store the required data. However, when I try to create a list of my defined object, this fails and all data is stored sequentially in 1 big list.

This is my S3 object (as this is the first time that I create S3, I am not really sure that the code is a 100% correct):

modelObject <- function(modelName , modelObject, modelPredictions , rocCurve , aUC , confusionMatrix )
{

  modelObject <- list(
    model.name = modelName,
    model.object = modelObject,
    model.predictions = modelPredictions,
    roc.curve = rocCurve,
    roc.auc = aUC,
    confusion.matrix = confusionMatrix

  )

  ## Set the name for the class
  class(modelObject) <- "modelObject"
  return(modelObject)
}

at the end of each machine learning function, I define and return the object: shortened example:

NeuralNetworkAnalysis<- function() {
  #I removed the unnecessary code, as only the end of the code is relevant
  nn.model <- modelObject(modelName = "Neural.Network" , modelObject = NN , modelPredictions = predNN , rocCurve = roc , aUC = auc , confusionMatrix = confu  )
 return(nn.model)
  }

At last, in my 'script' function, I create an empty list and try to append the different objects

 #function header and arguments before this part are irrelevant

# Build predictive model(s)
  modelList = list("model" = modelObject)
  modelList <- append(modelList ,   NeuralNetworkAnalysis())
  modelList <- append(modelList,  RandomForestAnalysis())
  mod <<- RandomForestAnalysis() #this is to test what the outcome is when I do not put it in a list
  return(modelList) } #end of the function ModelBuilding
models <- ModelBuilding( '01/01/2013'  , '01/01/2014'  ,  '02/01/2014' ,  '02/01/2015' )

Now, when I take a look at the models list, I don't have a list of objects, I just have a list with all the data of each algorithm.

class(models) [1] "list"

class(mod) [1] "modelObject"

How can i fix this problem, so that i can have a list that contains for example:

list$random.forest$variable.I.want.to.access (most favorable)

or

list[i]$variable.of.random.forest.that.I.want.to.access

thx in advance!

Olivier


Solution

  • Not sure if I understand correctly, but maybe the issue is only how your model list is built. If you try

     modelList[["neural.network"]] <- NeuralNetworkAnalysis()
     modelList[["random.forest"]] <- RandomForestAnalysis()
    

    etc., does that give you the access methods you are looking for?