我忙於比較R.中的不同機器學習技術。 這是這種情況:我創建了幾個函數,以自動方式 能夠創建每個不同的預測模型(例如:logistic迴歸,隨機森林,神經網絡,混合集成等),預測,混淆矩陣,若干統計量(例如AUC和Fscore)以及不同的圖。在R中創建S4對象
現在我想在R中創建一個S4(或S3?)對象的列表,其中每個對象包含模型,預測,繪圖,混淆矩陣,auc和fscore。
這個想法是,每個函數創建這樣的對象,然後將它附加到return語句中的對象列表。
我應該如何編程這樣的類?我怎樣才能定義每個模型可以是一些不同的類型(我認爲我創建的所有模型都是S3對象,那麼我怎麼能在我的S4類中定義這個?
最終結果應該能夠做這樣的事情:modelList [i] @plot應該舉例說明召喚請求的圖,name(modelList [i])應該給出所使用模型的名字(如果這是不可能的,modelList [i] @name會做)。另外,應該可以根據參數(如AUC)從列表中選擇最佳模型。 我沒有創建這樣的對象的經驗,所以這是我現在的代碼/想法:
modelObject <- setClass(
# Set the name for the class
"modelObject",
# Define the slots
slots = c(
modelName = "character"
model = #should contain a glm, neural network, random forest , etc model
predictions = #should contain a matrix or dataframe of custid and prediction
rocCurve = #when summoned, the ROC curve should be plotted
plotX = #when summoned, plot X should be plotted
AUC = "numeric" #contains the value of the AUC
confusionMatrix = "matrix" #prints the confusion matrix in the console
statX = "numeric"#contains statistic X about the confusion matrix e.g. Fscore
),
# Set the default values for the slots. (optional)
prototype=list(
# I guess i can assign NULL to each variable of the S4 object
),
# Make a function that can test to see if the data is consistent.
# This is not called if you have an initialize function defined!
validity=function(object)
{
#not really an idea how to handle this
}
return(TRUE)
}
)
相關:[1](http://adv-r.had.co.nz/OO-essentials.html),[2](http://www.cyclismo.org/tutorial/R/s4Classes。 html),[3](http://adv-r.had.co.nz/S4.html) – Laterow