2016-02-27 55 views
0

我忙於R.這比較不同的機器學習技術是這樣的:我做了幾個功能,以自動的方式能夠創建每個不同的預測模型(例如:邏輯迴歸,隨機森林,神經網絡,混合整體等),預測,混淆矩陣,若干統計數據(例如AUC和Fscore)以及不同的圖。預定義的S3的創建對象列表

我設法創建S3對象,它是能夠存儲所需的數據。 然而,當我嘗試創建我的定義對象的名單,這將失敗,所有數據都存儲在順序1名大名單。

這是我的S3對象(因爲這是我創建S3的第一次,我真的不知道該代碼是100%正確的):

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

在每個機器學習結束函數,我定義和返回對象: 縮短例如:

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

最後,在我的「腳本」功能,我創建一個空的列表,並嘗試添加不同的對象

#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') 

現在,當我看看模特名單,我沒有對象的列表,我只是有每個算法的所有數據的列表。

類(型號)[1] 「清單」

類(MOD)[1] 「modelObject」

我怎樣才能解決這個問題,讓我可以有一個列表,其中包含例如:

list $ random.forest $ variable.I.want.to.access(最有利)

列表[我] $ variable.of.random.forest.that.I.want.to.access

THX提前!

奧利維爾

回答

1

不知道如果我理解正確的,但也許這個問題是你的模型列表只如何構建的。如果你嘗試

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

等,這是否給你你正在尋找的訪問方法?

+0

沒錯,這就是我一直在尋找,小白的錯誤。謝謝! –