-
Notifications
You must be signed in to change notification settings - Fork 0
/
RF_Importance.R
282 lines (243 loc) · 12.1 KB
/
RF_Importance.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
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
################################ One random forest at national level #####################################
rm(list=objects())
library(lubridate)
library(dplyr)
library(glmnet)
library(ranger)
library(ggpubr)
setwd("~/Desktop/Simulations Covid Article/data")
set.seed(42)
### - Load Data
Data <- readRDS("Data_GAM.RDS")
Data <- filter(Data, Date >= ymd("2019-09-01"), Date < ymd("2020-09-17"), weights > 0, region == 'France')
# Define usefull variables
lockdown_start <- ymd("2020-03-16")
### We select 4, 9, 14 variables using Lasso for each region, and use themm in the formula with the variable "WeekDays"
GAM_terms <- paste0("GAM_terms", 1:9)[-c(4,5)] # we remove terms 4 et 5, corresponding to toy and date
google_terms <- names(Data)[23:28]
form <- as.formula(paste0("residuals_norm ~ WeekDays + residuals_norm.48 + residuals_norm.336 + GovernmentResponseTracker + ",
paste0(GAM_terms, collapse = ' + '), " + ",
paste0(google_terms, collapse = ' + ')))
nvar = length(all.vars(form))-1
variables <- all.vars(form)[2:(nvar+1)]
# Initialise the results
forecast_date <- lockdown_start
dates <- unique(filter(Data, Date > lockdown_start + duration(1, units = "days"))$DateD)
N <- length(dates)
importance <- matrix(0, ncol = nvar, nrow = N)
colnames(importance) <- all.vars(form)[2:(nvar+1)]
## %%%%%%%%%%%%% Compute the predictions for the pandemix period %%%%%%%%%%%%%%% ##
for (j in 1:N){
forecast_date <- dates[j]
# Train the RF using the avalaible pandemic data, produce forecasts
for (i in 0:47){
sel_test <- which(Data$tod == i & Data$DateD == forecast_date)
if (length(sel_test) != 0){
sel_train <- which(Data$tod == i & Data$Date >= lockdown_start & Data$Date < forecast_date)
rf <- ranger(form, data = Data[sel_train,], quantreg = T, importance = 'impurity')
importance[j, names(rf$variable.importance)] <- importance[j, names(rf$variable.importance)] + rf$variable.importance/48
}
}
print(forecast_date)
}
# Save results
saveRDS(importance, 'importance.RDS')
################# Importance plot
importance <- readRDS('importance.RDS')
for (r in 1:N){
importance[r,] <- 100*importance[r,]/sum(importance[r,])
}
groupes <- list(WeekDays = 1, laggued_residuals = 2:3, GovernmentResponseTracker = 4,
GAM_effects = 5:11, Google_mobility = 12:17)
groupe_importance <- data.frame(Date = Date(0), region = character(0), type = character(0), importance = numeric(0))
for (g in 1:length(groupes)){
if (g %in% c(1,3)){
groupe_importance <- rbind(groupe_importance,
data.frame(Date = dates,
type = names(groupes)[g],
importance = importance[,groupes[[g]]]))
}else{
groupe_importance <- rbind(groupe_importance,
data.frame(Date = dates,
type = names(groupes)[g],
importance = rowSums(importance[,groupes[[g]]], na.rm = T)))
}
}
library(ggplot2)
ggplot(data = filter(groupe_importance, Date < ymd("2020-08-01"))) +
geom_line( aes(x = Date, y = importance, color = type, ymax = 60)) +
theme(legend.position = "None")
#ggsave('/Users/solenne/Desktop/Simulations Covid Article/images/Goupes_importance', width = 10, height = 8)
groupe_importance$type <- as.factor(groupe_importance$type)
levels(groupe_importance$type) <- c("GAM effect", "Google mobility data", "Government Response Tracker",
"Laggued residuals", "Day of week")
################# Google mobility importance plot
google_importance <- data.frame(Date = Date(0), region = character(0), groupe = character(0), importance = numeric(0))
for (g in groupes$Google_mobility){
google_importance <- rbind(google_importance,
data.frame(Date = dates,
variable = variables[g],
importance = importance[,g]))
}
ggplot(data = filter(google_importance, Date < ymd("2020-08-01")), aes(x = Date, y = importance, color = variable, ymax = 13)) +
geom_line() + theme(legend.position = "None")
################################ One random forest for all levels #####################################
rm(list=objects())
library(lubridate)
library(dplyr)
library(glmnet)
library(quantregForest)
setwd("~/Desktop/Simulations Covid Article/data")
set.seed(42)
### - Load Data
Data <- readRDS("Data_GAM.RDS")
Data <- filter(Data, Date >= ymd("2019-09-01"), Date < ymd("2020-09-17"), weights > 0)
# Define usefull variables
lockdown_start <- ymd("2020-03-16")
### We select 4, 9, 14 variables using Lasso for each region, and use themm in the formula with the variable "WeekDays"
GAM_terms <- paste0("GAM_terms", 1:9)[-c(4,5)] # we remove terms 4 et 5, corresponding to toy and date
google_terms <- names(Data)[23:28]
form <- as.formula(paste0("residuals_norm ~ WeekDays + residuals_norm.48 + residuals_norm.336 + GovernmentResponseTracker + ",
paste0(GAM_terms, collapse = ' + '), " + ",
paste0(google_terms, collapse = ' + ')))
nvar = length(all.vars(form))-1
variables <- all.vars(form)[2:(nvar+1)]
# Initialise the results
forecast_date <- lockdown_start
dates <- unique(filter(Data, Date > lockdown_start + duration(1, units = "days"))$DateD)
N <- length(dates)
importance <- matrix(0, ncol = nvar, nrow = N)
colnames(importance) <- all.vars(form)[2:(nvar+1)]
## %%%%%%%%%%%%% Compute the predictions for the pandemix period %%%%%%%%%%%%%%% ##
for (j in 1:N){
forecast_date <- dates[j]
# Train the RF using the avalaible pandemic data, produce forecasts
for (i in 0:47){
sel_test <- which(Data$tod == i & Data$DateD == forecast_date)
if (length(sel_test) != 0){
sel_train <- which(Data$tod == i & Data$Date >= lockdown_start & Data$Date < forecast_date)
rf <- ranger(form, data = Data[sel_train,], quantreg = T, importance = 'impurity')
importance[j, names(rf$variable.importance)] <- importance[j, names(rf$variable.importance)] + rf$variable.importance/48
}
}
print(forecast_date)
}
# Save results
saveRDS(importance, 'importance_common.RDS')
################# Importance plot
importance <- readRDS('importance_common.RDS')
for (r in 1:N){
importance[r,] <- 100*importance[r,]/sum(importance[r,])
}
groupes <- list(WeekDays = 1, laggued_residuals = 2:3, GovernmentResponseTracker = 4,
GAM_effects = 5:11, Google_mobility = 12:17)
variables[12:17] <- c('Retail and recreation', 'Grocery and pharmacy', 'Parks', 'Transit stations',
'Worplaces', 'Residential')
groupe_importance <- data.frame(Date = Date(0), region = character(0), groupe = character(0), importance = numeric(0))
for (g in 1:length(groupes)){
if (g %in% c(1,3)){
groupe_importance <- rbind(groupe_importance,
data.frame(Date = dates,
groupe = names(groupes)[g],
importance = importance[,groupes[[g]]]))
}else{
groupe_importance <- rbind(groupe_importance,
data.frame(Date = dates,
groupe = names(groupes)[g],
importance = rowSums(importance[,groupes[[g]]], na.rm = T)))
}
}
library(ggplot2)
#ggsave('/Users/solenne/Desktop/Simulations Covid Article/images/Goupes_importance', width = 10, height = 8)
groupe_importance[,"type"] <- as.factor(groupe_importance$groupe)
levels(groupe_importance[,"type"]) <- c("GAM effect", "Google mobility data", "Government Response Tracker",
"Laggued residuals", "Day of week")
ggplot(data = filter(groupe_importance, Date < ymd("2020-08-01")), aes(x = Date, y = importance, color = type, ymax = 60)) +
geom_line()
################# Google mobility importance plot
google_importance <- data.frame(Date = Date(0), region = character(0), groupe = character(0), importance = numeric(0))
for (g in groupes$Google_mobility){
google_importance <- rbind(google_importance,
data.frame(Date = dates,
variable = variables[g],
importance = importance[,g]))
}
library(ggplot2)
ggplot(data = filter(google_importance, Date < ymd("2020-08-01")), aes(x = Date, y = importance, color = variable, ymax = 14)) +
geom_line()
#############################################################################################################
############## Importance on the last day for the quantile RF at level 0.5 and 0.05 at national level #######
rm(list=objects())
set.seed(42)
### functions
pinball_loss <- function(pred, target, tau){
l <- (target > pred) * tau * (target - pred) +
(pred >= target) * (1 - tau) * (pred - target)
return(l)
}
importance_quantile <- function(form, data, qt, rep = 10){
variables <- all.vars(form)[-1]
target <- all.vars(form)[1]
nvar <- length(variables)
results <- matrix(0, ncol = nvar, nrow = length(qt))
colnames(results) <- variables
rownames(results) <- qt
rf.base <- ranger(form, data = data, quantreg = T, keep.inbag = TRUE)
pinball.base <- rep(NA, length(qt))
for (q in 1:length(qt)){
pinball.base[q] <- mean(pinball_loss(target = data[,target],
pred = predict(rf.base, type = "quantiles", quantiles = qt[q])$predictions,
tau = qt[q]))
}
for (v in variables){
for (r in rep){
data_rep <- data
data_rep[, v] <- data[sample(nrow(data)), v]
rf.rep <- ranger(form, data = data_rep, quantreg = TRUE, keep.inbag = TRUE)
for (q in 1:length(qt)){
pinball.rep <- mean(pinball_loss(target = data_rep[,target],
pred = predict(rf.rep, type = "quantiles", quantiles = qt[q])$predictions,
tau = qt[q]))
results[q, v] <- results[q, v] + (pinball.rep - pinball.base[q])
}
}
}
return(results)
}
### - Load Data
Data <- readRDS("Data_GAM.RDS")
Data <- filter(Data, Date >= ymd("2019-09-01"), Date < ymd("2020-09-17"), weights > 0, region == 'France')
# Define usefull variables
lockdown_start <- ymd("2020-03-16")
### We select 4, 9, 14 variables using Lasso for each region, and use themm in the formula with the variable "WeekDays"
GAM_terms <- paste0("GAM_terms", 1:9)[-c(4,5)] # we remove terms 4 et 5, corresponding to toy and date
google_terms <- names(Data)[23:28]
form <- as.formula(paste0("residuals_norm ~ WeekDays + residuals_norm.48 + residuals_norm.336 + GovernmentResponseTracker + ",
paste0(GAM_terms, collapse = ' + '), " + ",
paste0(google_terms, collapse = ' + ')))
nvar = length(all.vars(form))-1
variables <- all.vars(form)[2:(nvar+1)]
importance <- matrix(0, ncol = nvar, nrow = 3)
names(importance) <- variables
qt <- c(0.05, 0.5, 0.95)
## %%%%%%%%%%%%% Compute the predictions for the pandemic period %%%%%%%%%%%%%%% ##
for (i in 0:47){
sel_train <- which(Data$tod == i & Data$Date >= lockdown_start)
importance <- importance + importance_quantile(form, Data[sel_train,], qt, rep = 100)
print(i)
}
importance <- importance * (importance >0)
colnames(importance)[5:11] <- paste("GAM_", c("Load_norm.336", "WeekDays:DLS", "WeekDays:Load_norm.48", "Date:Temp",
"Temp_s95", "Temp_s99", "Temp_s99_min:Temp_s99_max"))
variables <- colnames(importance)
importance_df <- data_frame(importance = numeric(0), quantile = character(0), variable = character(0))
for (i in 1:3){
importance_df <- rbind(importance_df,
data_frame(importance = 100*importance[i,]/sum(importance[i,]),
quantile = c("0.05", "0.5", "0.95")[i],
variable = variables))
}
importance_df$variable <- factor(importance_df$variable, levels = variables[order(importance[2,], decreasing = T)])
ggplot(data = importance_df, aes(x = variable, y = importance, color = quantile)) +
geom_point() +
theme(axis.text.x = element_text(angle=45, hjust = 1))