r - Create list of predefined S3 objects -
i busy comparing different machine learning techniques in r. case: made several functions that, in automated way able create each different prediction model (e.g: logistic regression, random forest, neural network, hybrid ensemble , etc.) , predictions, confusion matrices, several statistics (e.g auc , fscore) ,and different plots.
i managed create s3 object able store required data. however, when try create list of defined object, fails , data stored sequentially in 1 big list.
this s3 object (as first time create s3, not sure code 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 name class class(modelobject) <- "modelobject" return(modelobject) }
at end of each machine learning function, define , return object: shortened example:
neuralnetworkanalysis<- function() { #i removed unnecessary code, end of code relevant nn.model <- modelobject(modelname = "neural.network" , modelobject = nn , modelpredictions = prednn , roccurve = roc , auc = auc , confusionmatrix = confu ) return(nn.model) }
at last, in 'script' function, create empty list , try append different objects
#function header , arguments before part irrelevant # build predictive model(s) modellist = list("model" = modelobject) modellist <- append(modellist , neuralnetworkanalysis()) modellist <- append(modellist, randomforestanalysis()) mod <<- randomforestanalysis() #this test outcome when not put in list return(modellist) } #end of function modelbuilding models <- modelbuilding( '01/01/2013' , '01/01/2014' , '02/01/2014' , '02/01/2015' )
now, when take @ models list, don't have list of objects, have list data of each algorithm.
class(models) [1] "list"
class(mod) [1] "modelobject"
how can fix problem, can have list contains 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
not sure if understand correctly, maybe issue how model list built. if try
modellist[["neural.network"]] <- neuralnetworkanalysis() modellist[["random.forest"]] <- randomforestanalysis()
etc., give access methods looking for?
Comments
Post a Comment