2017-10-17 142 views
0

分類節點號我有ctree。投入變量

library(party) 
model.cart <- ctree(qtcf ~ ., data=training) 

改變了我的情況下,分類樹,我想在我的訓練集(訓練)和測試組(測試),顯示終端節點創建一個新的變量編號爲特定的觀察。

顯然,它可以手動進行這樣的:

training$ctreegroup[((training$sex == 'female') & (training$rs12143842.y>0) 
        & (training$rs735951>0))] <- 'node14' 
testing$ctreegroup[((testing$sex == 'female') & (testing$rs12143842.y>0) & 
         (testing$rs735951>0))] <- 'node14' 

但有無需手動編寫所有的決定自動做這一個聰明的辦法?

+0

我想'where'功能'庫(派對)'會有所幫助。 'where(model.cart)' – shuckle

+0

謝謝。我看不到黨的文檔中的功能,但培訓$ node_placement < - 其中(model.cart)工作。 –

回答

0

至於建議的shuckle

training$node_placement <- where(model.cart) 

工作了訓練集。 Unforunately它不會對測試集工作,因爲

testing$node_placement <- where(model.cart) 

產生的誤差

Error in `$<-.data.frame`(`*tmp*`, ctreegroup, value = c(22L, 22L, 23L, : 
    replacement has 4440 rows, data has 1478 

所以函數,其中不重新審視數據

+0

嘗試'預測(model.cart,newdata = testing,type =「node」)' – shuckle

+0

這已經解決了我的問題。 TX –