【问题标题】:Can't implement Decision tree in R using 'party' package. How to do it?无法使用“party”包在 R 中实现决策树。怎么做?
【发布时间】:2014-06-07 10:08:50
【问题描述】:

我正在尝试使用“party”包在 R 中构建决策树,

我正在遵循http://www.rdatamining.com/examples/decision-tree中提到的方法

他们使用“party”包展示了决策树。

我的数据集类似于示例中显示的 iris 数据集。这是我的数据集副本的链接。 https://drive.google.com/file/d/0B6cqWmwsEk20TXQyMnVlbGppcTQ/edit?usp=sharing

这是我尝试过的代码。我使用 read.csv 命令加载数据并将其提供给 dat3 变量。

library(party)
> str(dat3)
'data.frame':   1000 obs. of  4 variables:
 $ Road_Type              : num  2 3 3 1 1 1 3 3 1 3 ...
 $ Light_Conditions       : num  2 3 3 3 3 3 3 3 3 3 ...
 $ Road_Surface_Conditions: num  1 2 2 2 2 2 2 2 2 2 ...
 $ Accident_Severity      : chr  "three" "three" "three" "three" ...
> dat3$Accident_Severity<-as.factor(dat3$Accident_Severity)
> str(dat3)
'data.frame':   1000 obs. of  4 variables:
 $ Road_Type              : num  2 3 3 1 1 1 3 3 1 3 ...
 $ Light_Conditions       : num  2 3 3 3 3 3 3 3 3 3 ...
 $ Road_Surface_Conditions: num  1 2 2 2 2 2 2 2 2 2 ...
 $ Accident_Severity      : Factor w/ 3 levels "one","three",..: 2 2 2 2 3 2 2 2 3 2 ...
> mytree<- ctree(Accident_Severity ~ Road_Type + Light_Conditions + Road_Surface_Conditions, data=dat3)
> print(mytree)

     Conditional inference tree with 1 terminal nodes

Response:  Accident_Severity 
Inputs:  Road_Type, Light_Conditions, Road_Surface_Conditions 
Number of observations:  1000 

1)*  weights = 1000 
> 

正如您所见,构造的树没有节点,当我以图形方式绘制这棵树时,结果也不如预期的那样,因为没有构造树。我不确定我在这里做错了什么。

【问题讨论】:

  • 我猜你已经构建了一个线性回归模型。也许您需要“告诉” ctree 您正在对离散变量进行建模,并且您的意思是将这些数值预测变量建模为因子。
  • 是的,我确实想这样做,但我该怎么做呢?
  • @BondedDust 你能帮我吗?

标签: r tree data-mining decision-tree


【解决方案1】:

我认为数据中没有足够的信息来做 0.95 显着性水平的任何事情。查看表格拆分:

> with( dat3, table(Accident_Severity, Light_Conditions, Road_Type))
, , Road_Type = 1

                 Light_Conditions
Accident_Severity   1   2   3
            one     0   2   4
            three   2 157 158
            two     0  14  35

, , Road_Type = 2

                 Light_Conditions
Accident_Severity   1   2   3
            one     0   0   0
            three   1  17  11
            two     0   0   0

, , Road_Type = 3

                 Light_Conditions
Accident_Severity   1   2   3
            one     0   2   2
            three   3 269 251
            two     0  38  34

所以我想没有不明显的分裂。该函数认为它已经被充分分割。如果你降低最小标准,你会得到分裂:

 mytree<- ctree(Accident_Severity ~ Road_Type + Light_Conditions + Road_Surface_Conditions, 
                  data=dat3, control=ctree_control(  mincriterion =0.50) )
 print(mytree)
#----------------------
     Conditional inference tree with 4 terminal nodes

Response:  Accident_Severity 
Inputs:  Road_Type, Light_Conditions, Road_Surface_Conditions 
Number of observations:  1000 

1) Light_Conditions <= 2; criterion = 0.653, statistic = 4.043
  2) Road_Surface_Conditions <= 1; criterion = 0.9, statistic = 6.742
    3)*  weights = 193 
  2) Road_Surface_Conditions > 1
    4)*  weights = 312 
1) Light_Conditions > 2
  5) Road_Type <= 1; criterion = 0.792, statistic = 5.187
    6)*  weights = 197 
  5) Road_Type > 1
    7)*  weights = 298 

plot(mytree)

如果你在变量名周围使用 factor(),它们是非序数句柄:

 mytree2 <- ctree(Accident_Severity ~ factor(Road_Type) + factor(Light_Conditions) + factor(Road_Surface_Conditions), 
                   data=dat3, control=ctree_control(  mincriterion =0.50) )
  print(mytree2)
#------------------------
     Conditional inference tree with 2 terminal nodes

Response:  Accident_Severity 
Inputs:  factor(Road_Type), factor(Light_Conditions), factor(Road_Surface_Conditions) 
Number of observations:  1000 

1) factor(Road_Type) == {1, 3}; criterion = 0.635, statistic = 6.913
  2)*  weights = 971 
1) factor(Road_Type) == {2}
  3)*  weights = 29 

【讨论】:

  • 非常感谢,但问题是所有变量都是分类的,我的意思是光照条件 = 1 与 2 或 0 完全不同,我认为上面这个东西考虑的是连续类型多变的。此外,我注意到所有拆分都有 2 个孩子,但我不需要它是强制二进制的。
  • 您应该尝试将factor() 包裹在变量周围,这确实会给您带来不同的答案。上面的模型将用于序数测量。
猜你喜欢
  • 2013-08-26
  • 2018-07-04
  • 2014-12-11
  • 2018-11-17
  • 1970-01-01
  • 1970-01-01
  • 2015-10-18
  • 2023-04-05
  • 1970-01-01
相关资源
最近更新 更多