Repository navigation
Expand file tree
/
Copy pathR_simple_ml_example.Rmd
More file actions
316 lines (251 loc) · 10 KB
/
Copy pathR_simple_ml_example.Rmd
File metadata and controls
316 lines (251 loc) · 10 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
---
title: "Untitled"
output: html_document
date: '2022-07-16'
---
```{r setup, include=FALSE}
library(readxl)
library(dplyr)
library(tidyverse)
library(tidymodels) # for the recipes package, along with the rest of tidymodels
library(vip)
library(skimr) # for variable summaries
library(rpart.plot)
```
```{r}
# file location to read
file <- "C:\\Users\\kelvi\\Desktop\\HMRC_Senior_Officials_Travel_July_to_September_2022.csv"
# load file
file <- read_csv(file)
# drop not needed columns
file <- dplyr::select(file,-`Purpose of trip`,-`Total Cost of Use of Official Secure Car`,-`Other (including hospitality given)`)
# remove any "Nil Return" from dates
file <- file %>%
filter(`Start date of trip` !="Nil return")
# for Class of travel replace Nil return with Standard
file <- file %>%
mutate(`Class of travel`=ifelse(`Class of travel`=="Nil return","Standard",`Class of travel`))
# for accommodation/meals() replace Nil return with 0 and make numeric
file <- file %>%
mutate(`Accommodation/Meals ()`=ifelse(`Accommodation/Meals ()`=="Nil return","0",`Accommodation/Meals ()`))
file$`Accommodation/Meals ()` <- as.numeric(file$`Accommodation/Meals ()`)
file$`Duration of Visit (Days)` <- as.numeric(file$`Duration of Visit (Days)`)
# make total cost numeric and rename column
file <- rename(file,c('total cost'='Total cost, including all visas, accommodation, travel, meals etc. (£)'))
file$`total cost` <- as.numeric(file$`total cost`)
# make new column that has 'transport cost'
file$`transport cost` <- file$`total cost` - file$`Accommodation/Meals ()`
# change any NAs to 0
file$`transport cost` <- replace_na(file$`transport cost`,0)
file$`transport cost` <- as.numeric(file$`transport cost`)
# filter to cost > 0
file <- file %>%
filter(`transport cost` > 0) %>%
#filter(`Mode of transport` != "Nil Return") %>%
filter (`Mode of transport` == "Rail" | `Mode of transport` == "Car") #%>%
#filter(`Class of travel` == 'Standard')
# make start date a date
file$`Start date of trip` <- as.Date(file$`Start date of trip`,format = "%d/%m/%Y") # UK date format
# to make things easier for machine learning, let's remove Mode of transport categories with multiple entries
# first we need to find instances with commas
file$`find_text` <- grepl(",",file$`Mode of transport`) # returns TRUE or FALSE
# filter to only FALSE and drop the columns not needed for machine learning
file <- file %>%
filter(`find_text`==FALSE)
file <- select(file,-`find_text`, -`Accommodation/Meals ()`, -`total cost`, -`Senior Officials Name`)
# now set the data up for machine learning
# Fix the random numbers by setting the seed
# This enables the analysis to be reproducible when random numbers are used
set.seed(222)
# Put 3/4 of the data into the training set
data_split <- initial_split(file, prop = 0.7)
# Create data frames for the two sets:
train_data <- training(data_split)
train_data <- na.omit(train_data) # remove any NAs
test_data <- testing(data_split)
# use a tidymodels recipe to predict the transport cost
file_rec <-
recipe(`transport cost` ~ ., data = train_data) %>%
step_date(`Start date of trip`) %>%
step_rm(`Start date of trip`) %>%
step_other(Destination, threshold = 0.01) %>% # remove destination values that occur <1% of the time
step_dummy(all_nominal_predictors()) %>% # change categorical values to numeric
#step_dummy(all_nominal(), -all_outcomes()) %>%
#step_impute_median(all_predictors()) %>%
step_zv(all_predictors())%>% # remove columns with a single value
#step_nzv(all_numeric_predictors()) %>% # remove columns with a single value
step_normalize(all_predictors()) # centre and scale
###### decision tree model #####
tree_spec <- decision_tree() %>%
set_engine("rpart") %>%
set_mode("regression")
tree_wflow <-
workflow() %>%
add_model(tree_spec) %>%
add_recipe(file_rec)
#show hyperparameters
extract_parameter_set_dials(file_rec)
#val set - for candidate models
val_set <- validation_split(file, prop = 0.50, strata = `transport cost`)
#train_folds = vfold_cv(train_data, strata = `transport cost`)
#create 25 candidate models
set.seed(22)
tree_res <-
tune_grid(tree_wflow,
val_set,
#resamples = train_folds,
grid = 25,
#control = control_resamples(save_pred = TRUE),
control = control_grid(save_pred = TRUE),
metrics = metric_set(rmse))
# show top 5 of 25 models
tree_res %>%
show_best(metric = "rmse")
#select the best model
tree_best <-
tree_res %>%
select_best(metric = "rmse")
tree_best
tree_res %>%
collect_predictions()
tree_auc1 <-
tree_res %>%
collect_predictions(parameters = tree_best)
# finalise the model for fitting
final_mlp_wflow <- tree_wflow %>%
finalize_workflow(tree_best)
final_mlp_fit <- final_mlp_wflow %>%
fit(train_data) # fit on train_data
feeder_vip <-
extract_fit_parsnip(final_mlp_fit) %>%
vip()
feeder_vip
### or
#feeder_vip <-
# extract_fit_engine(final_mlp_fit) %>%
# vi()
# view the predicted data
predict(final_mlp_fit, train_data) # predict on new, test or train data
flights_aug <-
augment(final_mlp_fit, train_data)
# graph of test predictions vs actual data:
#create line graph
ggplot(flights_aug,aes(x=`.pred`,y=`transport cost`))+
geom_point(color="#CD2456")+
#scale_x_date(date_labels="%d/%m/%Y",date_breaks="1 week")+ #can be 1 day, 1 week, 1 month
#geom_text(data=filter(df,`yvalue`==last(`yvalue`)), aes(label=round(`yvalue`,0)),vjust=-0.5)+
#scale_color_manual(values=alpha(c("#CD2456","14022E")),name="xyz")+
#geom_point(aes(shape=`column`))+
#geom_vline(aes(xintercept=as.numeric(Date[c(90)])))+
#geom_vline(aes(xintercept=as.integer(as.POSIXct("2020-05-05"))),linetype=4)+
theme_classic()+
#ylim(0,max((`yaxis`)+5))+
#facet_grid(~col)+
#coord_flip()+
xlim(0,max(flights_aug$.pred,flights_aug$`transport cost`))+
ylim(0,max(flights_aug$.pred,flights_aug$`transport cost`))+
geom_abline(slope = 1, intercept = 0, color="#CD2456", linetype="dashed")+
theme(legend.position="none")+
theme(axis.text.x=element_text(angle=0,size=10))+
labs(x="Train",y="Test",title="Real vs predicted data on Train dataset")
#### repeat for random forest model ############
rfr_model <-
rand_forest(mtry = tune(), min_n = tune(), trees = 10000) %>%
set_engine("ranger", importance = "impurity") %>% # for importance plot this line is needed
#set_engine("ranger",num.threads=8) %>%
#set_engine("randomForest") %>%
#set_mode("unknown")
#set_mode("classification")
set_mode("regression")
# use a tidymodels recipe to predict the transport cost
# random forest models do not need dummy values or normalised values
file_rec1 <-
recipe(`transport cost` ~ ., data = train_data) %>%
step_date(`Start date of trip`) %>%
step_rm(`Start date of trip`) %>%
step_other(Destination, threshold = 0.01) %>% # remove destination values that occur <1% of the time
step_dummy(all_nominal_predictors()) %>% # change categorical values to numeric
#step_dummy(all_nominal(), -all_outcomes()) %>%
step_impute_median(all_predictors()) %>% # impute missing values with median values
step_zv(all_predictors())%>% # remove columns with a single value
#step_nzv(all_numeric_predictors()) %>% # remove columns with a single value
step_normalize(all_predictors()) # centre and scale
rfr_wflow <-
workflow() %>%
add_model(rfr_model) %>%
add_recipe(file_rec1)
#show hyperparameters
extract_parameter_set_dials(file_rec1)
#val set - for candidate models
#val_set <- validation_split(file, prop = 0.80, strata = `transport cost`)
extract_parameter_set_dials(rfr_model)
#create 25 candidate models
set.seed(22)
rfr_res <-
rfr_wflow %>%
tune_grid(val_set,
grid = 25,
control = control_grid(save_pred = TRUE),
metrics = metric_set(rmse))
# show top 5 of 25 models
rfr_res %>%
show_best(metric = "rmse")
#select the best model
rfr_best <-
rfr_res %>%
select_best(metric = "rmse")
rfr_best
rfr_res %>%
collect_predictions()
rfr_auc1 <-
rfr_res %>%
collect_predictions(parameters = rfr_best)
# get metrics
#rfr_met <- rfr_best %>%
# collect_metrics()
# finalise the model for fitting
final_mlp_wflow <- rfr_wflow %>%
finalize_workflow(rfr_best)
final_mlp_fit <- final_mlp_wflow %>%
fit(train_data) # fit on train_data
feeder_vip <-
extract_fit_parsnip(final_mlp_fit) %>%
vip()
feeder_vip
### or as a table
feeder_vip <-
extract_fit_engine(final_mlp_fit) %>%
vi()
# graph this
feeder_vip[1:10,] %>%
ggplot(aes(Importance, fct_reorder(Variable, Importance))) +
geom_col(fill="#CD2456") +
labs(y = NULL, title = "Top 10 feature importances") +
theme(legend.position = "none")+
theme_classic()
feeder_vip
# view the predicted data
predict(final_mlp_fit, train_data) # predict on new, test or train data
flights_aug <-
augment(final_mlp_fit, train_data)
# graph of test predictions vs actual data:
#create line graph
ggplot(flights_aug,aes(x=`.pred`,y=`transport cost`))+
geom_point(color="#CD2456")+
#scale_x_date(date_labels="%d/%m/%Y",date_breaks="1 week")+ #can be 1 day, 1 week, 1 month
#geom_text(data=filter(df,`yvalue`==last(`yvalue`)), aes(label=round(`yvalue`,0)),vjust=-0.5)+
#scale_color_manual(values=alpha(c("#CD2456","14022E")),name="xyz")+
#geom_point(aes(shape=`column`))+
#geom_vline(aes(xintercept=as.numeric(Date[c(90)])))+
#geom_vline(aes(xintercept=as.integer(as.POSIXct("2020-05-05"))),linetype=4)+
theme_classic()+
#ylim(0,max((`yaxis`)+5))+
#facet_grid(~col)+
#coord_flip()+
xlim(0,max(flights_aug$.pred,flights_aug$`transport cost`))+
ylim(0,max(flights_aug$.pred,flights_aug$`transport cost`))+
geom_abline(slope = 1, intercept = 0, color="#CD2456", linetype="dashed")+
theme(legend.position="none")+
theme(axis.text.x=element_text(angle=0,size=10))+
labs(x="Train",y="Test",title="Real vs predicted data on Train dataset")
```