2017-10-17 141 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 –