-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecision Tree.Rmd
152 lines (106 loc) · 2.61 KB
/
Decision Tree.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
---
title: "Decision Tree Regression Models"
author: "Yihao Zhao"
date: "12/6/2022"
output: html_document
---
## Import the libraries
```{r}
library(readxl)
library(rpart)
library(rpart.plot)
library(caret)
library(dplyr)
```
## Import the database
```{r warning = FALSE}
df <- read_excel("globalterrorismdb_0522dist.xlsx")
head(df)
```
## Data cleaning
```{r}
# Selected features
features = c('country','latitude','longitude','success','suicide','attacktype1','targtype1','gname','claimed','weaptype1','nkill','nwound','property','ransom')
df = df[features]
str(df)
```
```{r}
selected_gname = df %>%
filter(gname != 'Unknown') %>%
group_by(gname) %>%
count() %>%
arrange(desc(n)) %>%
head(10)
selected_gname # focus on these most active 10 terrorist groups
```
```{r}
df2 = df %>%
filter(gname %in% selected_gname$gname)
head(df2)
```
```{r}
df2[is.na(df2)] <- 0 # fill all NAs to 0
str(df2)
```
## Create train/test set
```{r}
# create a function to split the train and test data set
create_train_test = function(data, size = 0.8, train = TRUE) {
n_row = nrow(data)
total_row = size * n_row
train_sample = 1: total_row
if (train == TRUE) {
return (data[train_sample, ])
} else {
return (data[-train_sample, ])
}
}
```
```{r}
df_train <- create_train_test(df2, 0.8, train = TRUE)
df_test <- create_train_test(df2, 0.8, train = FALSE)
dim(df_train)
```
```{r}
dim(df_test)
```
**Interpretation:** The train dataset has 37386 rows while the test dataset has 9347 rows.
```{r}
# verify if the randomization process is correct
prop.table(table(df_train$gname))
```
```{r}
# verify if the randomization process is correct
prop.table(table(df_test$gname))
```
## Build Decision Tree Regressioin Models
```{r}
tree <- rpart(success ~ ., data = df_train, method = 'class')
rpart.plot(tree, extra = 106)
```
## Measure performance
```{r}
# Function that measures the performance
accuracy <- function(tree) {
pred_test <- predict(tree, df_test, type = "class")
conf <- table(df_test$success, pred_test)
accuracy <- sum(diag(conf))/sum(conf)
accuracy
}
```
```{r}
accuracy(tree)
```
**Interpretation:** The accuracy is 89.97%. This indicates that these data are sufficient enough and appropriate for decision tree modeling.
```{r}
df$gname = as.factor(df$gname)
fit <- rpart(gname ~ ., data = df_train, method = 'class')
rpart.plot(fit, box.palette=0)
```
```{r}
accuracy(fit)
```
**Interpretation:** The accuracy is 8.39%. This indicates that these data are not sufficient enough and appropriate for decision tree modeling.
```{r}
head(df2)
```