[英]How do I convert an “RWeka” decision tree into a “party” tree in R?
我正在R中使用RWeka軟件包,以使用“ M5P”將M5'樹適合數據集。 然后,我想將生成的樹轉換為“派對”樹,以便可以訪問變量的重要性。 我遇到的問題是,在沒有出現以下錯誤的情況下,我似乎無法使as.party
函數as.party
工作:
"Error: all(sapply(split, head, 1) %in% c("<=", ">")) is not TRUE"
僅當我在for循環中應用函數時才會出現此錯誤,但是當我運行5倍交叉驗證時,for循環是必需的。
下面是我一直在運行的代碼:
n <- nrow(data)
k <- 5
indCV <- sample( rep(1:k,each=ceiling(n/k)), n)
for(i in 1:k){
#Training data is for all the observations where indCV is not equal to i
training_data <- data.frame(x[-which(indCV==i),])
training_response <- y[-which(indCV==i)]
#Test the data on the fifth of the data where the observation indices are equal to i
test_data <- x[which(indCV==i),]
test_response <- y[which(indCV==i)]
#Fit a pruned model to the training data
fit <- M5P(training_response~., data=training_data, control=Weka_control(N=TRUE))
#Convert to party
p <- as.party(fit)
}
RWeka
軟件包提供了一個將M5P
樹轉換為party
對象的示例。 如果您運行example("M5P", package = "RWeka")
那么example("M5P", package = "RWeka")
可視化實際上是由partykit
繪制的。 運行示例之后,請參見plot(m3)
和as.party(m3)
。
但是,盡管對於J48
您可以獲得完整的constparty
對象,但對於M5P
並非如此。 在后一種情況下,樹結構本身可以轉換為party
但是節點內的線性模型轉換為lm
對象並非完全簡單。 因此,如果要使用參與party
表示來計算僅取決於樹結構的度量(例如,用於拆分的變量,拆分數,拆分點等),則可以這樣做。 但是,如果您要計算依賴於模型或預測的度量(例如均方誤差等),那么party
類將無濟於事。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.