-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvip.R
160 lines (121 loc) · 6.41 KB
/
vip.R
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
#Linear
2 0
library(vip)
vip(lm_fit)
#Poisson
summary(m1 <- glm(SeonsorID_ ~ bikeshop_countsNUMPOINTS + edupois_reprojected_joinNUMPOINTS +
shoppoints_count_NUMPOINTS + trafficpoints_count_NUMPOINTS + trafficsignals_count_NUMPOINTS +
NDVImean20 + LSTmean + slope + UC_bikeshops_HubDist + UC_edu_HubDist + UC_shops_HubDist +
UC_streetlights_HubDist + UC_trafficpoints_HubDist, family="poisson", data=train.set.sample))
poi_fit <- poi_plan %>%
fit(SeonsorID_ ~ bikeshop_countsNUMPOINTS + edupois_reprojected_joinNUMPOINTS +
shoppoints_count_NUMPOINTS + trafficpoints_count_NUMPOINTS + trafficsignals_count_NUMPOINTS +
NDVImean20 + LSTmean + slope + UC_bikeshops_HubDist + UC_edu_HubDist + UC_shops_HubDist +
UC_streetlights_HubDist + UC_trafficpoints_HubDist, data = train.set.sample)
# View lm_fit properties
poi_fit
summary(poi_fit$fit)
vip(poi_fit)
###RF
set.seed(234)
rf_fit <- rf_plan %>%
fit(SeonsorID_ ~ bikeshop_countsNUMPOINTS + edupois_reprojected_joinNUMPOINTS +
shoppoints_count_NUMPOINTS + trafficpoints_count_NUMPOINTS + trafficsignals_count_NUMPOINTS +
NDVImean20 + LSTmean + slope + UC_bikeshops_HubDist + UC_edu_HubDist + UC_shops_HubDist +
UC_streetlights_HubDist + UC_trafficpoints_HubDist, data = citybound)
rf_fit
summary(rf_fit$fit)
vip(rf_fit)
#load libraries
#install.packages("easypackages")
easypackages::packages ("sf", "sp", "tmap", "mapview", "car", "RColorBrewer",
"tidyverse", "osmdata", "nngeo", "FNN", "rpart", "rpart.plot",
"randomForest", "sessioninfo", "caret", "rattle", "ipred", "tidymodels",
"ranger", "recipes", "workflows", "themis","xgboost", "modelStudio", "DALEX",
"DALEXtra", "vip", "pdp")
#Fit the decision tree
#Note: We are using, method = "poisson". It can also be "a"nova", "poisson", "class" or "exp". Depending on the data type. In this case pedal is a count data so we selected poisson.
DT0 <- rpart (SeonsorID_ ~ bikeshop_countsNUMPOINTS + edupois_reprojected_joinNUMPOINTS +
shoppoints_count_NUMPOINTS + trafficpoints_count_NUMPOINTS + trafficsignals_count_NUMPOINTS +
NDVImean20 + LSTmean + slope + UC_bikeshops_HubDist + UC_edu_HubDist + UC_shops_HubDist +
UC_streetlights_HubDist + UC_trafficpoints_HubDist, data = train.set.sample, method = "anova")
summary (DT0)
rpart.plot(DT0)
#plot the complexity parameter
plotcp(DT0)
DT1 <- rpart (SeonsorID_ ~ bikeshop_countsNUMPOINTS + edupois_reprojected_joinNUMPOINTS +
shoppoints_count_NUMPOINTS + trafficpoints_count_NUMPOINTS + trafficsignals_count_NUMPOINTS +
NDVImean20 + LSTmean + slope + UC_bikeshops_HubDist + UC_edu_HubDist + UC_shops_HubDist +
UC_streetlights_HubDist + UC_trafficpoints_HubDist, data = train.set.sample, method = "poisson",
control = list(cp = 0))
#plot the cp values
plotcp(DT1)
DT1_pruned <- rpart::prune (DT1, cp = 0.01) # here I am selecting a value of 0.1, you can play with other values and see how the model changes with different values of cp.
#plot the model
rpart.plot(DT1_pruned)
#plot the cp values
plotcp(DT1_pruned)
#plot the model
rpart.plot(DT1)
#explain the model
#let us plot the feature importance for the top ten features from the model
vip (DT1_pruned, num_features = 19, aesthetics = list(fill = "green3"), include_type = T)
#create model explainer
explainer_DT <- DALEX::explain(
model = DT1_pruned,
data = train.set,
y = as.integer (train.set$pedal),
label = "Decision Tree Purned",
verbose = FALSE
)
#now make an interactive dashboard
modelStudio::modelStudio(explainer_DT)
##XGB
set.seed(234)
xgb_fit <- xgb_plan %>%
fit(SeonsorID_ ~ bikeshop_countsNUMPOINTS + edupois_reprojected_joinNUMPOINTS +
shoppoints_count_NUMPOINTS + trafficpoints_count_NUMPOINTS + trafficsignals_count_NUMPOINTS +
NDVImean20 + LSTmean + slope + UC_bikeshops_HubDist + UC_edu_HubDist + UC_shops_HubDist +
UC_streetlights_HubDist + UC_trafficpoints_HubDist, data = citybound)
xgb_fit
summary(xgb_fit$fit)
vip(xgb_fit)
set.seed(123)
#fit a random forest model, here I am selecting some hyper parameter such as mtry = 6 you can test different numbers but we shall explore these in details in the following session.
#here the rule of thumb is to use p/3 variables for regression trees, here p is the number of predictors in the model, and we had 19 predictors
xgb_final <-xgboost(SeonsorID_ ~ bikeshop_countsNUMPOINTS + edupois_reprojected_joinNUMPOINTS +
shoppoints_count_NUMPOINTS + trafficpoints_count_NUMPOINTS + trafficsignals_count_NUMPOINTS +
NDVImean20 + LSTmean + slope + UC_bikeshops_HubDist + UC_edu_HubDist + UC_shops_HubDist +
UC_streetlights_HubDist + UC_trafficpoints_HubDist, data= train.set, max.depth = 2, eta = 1, nthread = 2, nrounds = 2, objective = "binary:logistic", verbose = 1)
print(xgb_final)
#define final model
library(xgboost)
library(caret) # for general data preparation and model fitting
library(rpart.plot)
library(tidyverse)
# createDataPartition() function from the caret package to split the original dataset into a training and testing set and split data into training (80%) and testing set (20%)
parts = createDataPartition(citybound$SeonsorID_, p = .8, list = F)
train = data[parts, ]
test = data[-parts, ]
#define predictor and response variables in training set
train_x = data.matrix(train[, -1])
train_y = train[,1]
#define predictor and response variables in testing set
test_x = data.matrix(test[, -1])
test_y = test[, 1]
#define final training and testing sets
xgb_train = xgb.DMatrix(data = train_x, label = train_y)
xgb_test = xgb.DMatrix(data = test_x, label = test_y)
library(caret)
library(xgboost)
library(tidyverse)
xgb_fit <- train(SeonsorID_ ~ bikeshop_countsNUMPOINTS + edupois_reprojected_joinNUMPOINTS +
shoppoints_count_NUMPOINTS + trafficpoints_count_NUMPOINTS + trafficsignals_count_NUMPOINTS +
NDVImean20 + LSTmean + slope + UC_bikeshops_HubDist + UC_edu_HubDist + UC_shops_HubDist +
UC_streetlights_HubDist + UC_trafficpoints_HubDist,
data = train.set.sample,
method = "xgbLinear")
caret_imp <- varImp(xgb_fit)
caret_imp
plot(caret_imp)
xgb.ggplot.importance(xgb_imp)