[英]Can't implement Decision tree in R using 'party' package. How to do it?
我正在嘗試使用“ party”包在R中構造決策樹,
我正在遵循http://www.rdatamining.com/examples/decision-tree上提到的方法
他們使用“ party”包顯示了決策樹。
我的數據集類似於示例中顯示的虹膜數據集。 這是指向我的數據集副本的鏈接。 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
>
如您所見,構建的樹沒有節點,當我以圖形方式繪制該樹時,結果也不會像沒有構建樹那樣不理想。 我不確定我在做什么錯。
我認為數據中沒有足夠的信息來執行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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.