【发布时间】: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