将ctree的终端节点拆分规则存储到R中的数据帧中

时间:2015-02-20 21:42:04

标签: r decision-tree party

我有一个包含6个品牌的数据集(Node1),在应用ctree()时,它被分成2个终端节点。 Node2包含4个品牌 Node3包含2个品牌。 我想提取每个终端节点的这些品牌,并将它们存储在两个不同的数据帧中。请建议如何做同样的事。

这就是我编码的内容:

    library("party")
    library(gridExtra)
    fileName <- "C\\"
    data <- read.csv(paste(fileName, ".csv", sep=""), header=TRUE)
    pdd <- subset(data, select=c(col1,col2))

    pdd_ctree <- ctree(col1~col2, data=pdd, controls =  ctree_control(minsplit=30))

    print(pdd_ctree@tree$psplit$splitpoint[1:6])
    print(pdd_ctree@tree$psplit$splitpoint)

我得到的结果是:

    print(pdd_ctree@tree$psplit$splitpoint[1:6])
    [1] 1 0 1 1 0 1

    print(pdd_ctree@tree$psplit$splitpoint)
    [1] 1 0 1 1 0 1
    attr(,"levels")
    [1] "Brand01"            "Brand02"                "Brand03"          "Brand04"             
    [5] "Brand05" "Brand06"  

我的要求:

有2个数据帧left.df和right.df

left.df将包含[Brand01,Brand03,Brand04,Brand06]

right.df将包含[Brand02,Brand05]

1 个答案:

答案 0 :(得分:0)

我想你只是想:

lvls <- levels(pdd_ctree@tree$psplit$splitpoint)
left.df = lvls[pdd_ctree@tree$psplit$splitpoint == 1]
right.df = lvls[pdd_ctree@tree$psplit$splitpoint == 0]