2014-05-08 35 views
-2

我使用party包中的ctree()構建了迴歸樹。 我的模型的結果有許多節點包含相等的因變量的概率(例如:A類= 0.33,B類= 0.33,C類= 0.33)。我想從模型中取出這些節點。包treesnip.tree()命令,我們可以指定要從模型中刪除的節點號。此命令不能識別使用ctree()構建的迴歸樹。請讓我知道,如果有一種方法可以刪除迴歸樹中的某些節點使用ctree()如何從`party`包中的`ctree()`構建的迴歸樹中刪除某些節點

建我已經使用的模型:

rv.mod1 <- ctree(ldclas ~ L2 + L3 + L4 + L5 + L6 + ele + ndvi + nd_var + nd_ps, data = rv, controls = ctree_control(minsplit = 0, minbucket = 0)) 
pr.rv.mod1 <- snip.tree(rv.mod1, nodes = nn2.rv.mod1$nodes) 

nn2.rv.mod1 $節點與節點向量從rv.mod1刪除model.But我得到一個錯誤:

Error in snip.tree(rv.mod1, nodes = nn2.rv.mod1$nodes) : 
    not legitimate tree 
+0

你能告訴我們你已經嘗試了嗎?另外:請閱讀[如何問一個好問題]的幫助頁面(http://stackoverflow.com/help/how-to-ask) – Jaap

回答

0

我不認爲有直接的方式做到這一點,但我會建議在ctree一個「黑客」使用weights說法。

讓我們先從一個重複的例子,

library(party) 
irisct <- ctree(Species ~ .,data = iris) 
plot(irisct) 

enter image description here

現在,假設你想擺脫的節點數量5.你可以做以下

NewWeigths <- rep(1, dim(iris)[1]) # Setting a weights vector which will be passed into the `weights` attribute in `ctree` 
Node <- 5 # Selecting node #5 
n <- nodes(irisct, Node)[[1]] # Retrieving the weights of that node 
NewWeigths[which(as.logical(n$weights))] <- 0 # Setting these weigths to zero, so `ctree` will disregard them 
irisct2 <- ctree(Species ~ .,data = iris, weights = NewWeigths) # creating the new tree with new weights 
plot(irisct2) 

enter image description here

請注意節點2,6和7(現在是第因爲我們有較少的分裂,所以它們被命名爲2,4和5)完全保持相同的分佈和分裂條件。

我沒有測試它的所有節點,但它似乎工作得很好

+0

謝謝!這種方法適用於我的數據集。 –

+0

@KarthikK,下次請沒問題請嘗試用可重現的例子問問題(就像我在答案中給出的),並顯示你嘗試了什麼。這很小,你因此失去了如此多的代表 –