-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathml_glm_regularization.Rmd
261 lines (190 loc) · 6.48 KB
/
ml_glm_regularization.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
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
---
title: "Programming R Workgroup Project: Machine Learning Model"
author: "Group E"
date: "3/20/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Preparation
## Load libraries
```{r load_libraries, message=FALSE}
# General porpuse
library(tidyverse)
library(data.table)
library(lubridate)
library(dplyr)
# Descriptive
library(skimr)
# Visualization
library(ggplot2)
library(ggpubr)
# Clustering
library(factoextra)
library(NbClust)
# Machine learning
library(e1071)
library(caret)
library(randomForest)
# Calculations
library(mice)
# Paralel computing
library(foreach)
library(doParallel)
```
# Definitions
```{r}
num_clusters <- 8
num_lag <- 30
num_times <- 5
num_col_importance <- 200
```
# Load data
```{r load_data}
data_solar <- readRDS(file = file.path('data', 'solar_dataset.RData'))
data_station <- fread(file = file.path('data', 'station_info.csv'))
data_add <- readRDS(file = file.path('data', 'additional_variables.RData'))
```
## Transform data
```{r}
# Source dataset
data_solar <- data_solar[j = Date2 := as.Date(x = Date, format = "%Y%m%d")]
# Add date conversions
data_solar <- data_solar %>%
mutate(Year = year(Date2),
Month = lubridate::month(Date2, label = TRUE),
Day = lubridate::day(Date2),
Day_Of_Year = lubridate::yday(Date2),
Day_Of_Week = lubridate::wday(Date2, label = TRUE, week_start = 1),
Days_Since_Origin = time_length(interval(origin, Date2), unit = 'day')) %>%
as.data.table(.)
# Columns defined from the enunciate
data_solar_col_produ <- colnames(data_solar)[2:99]
data_solar_col_predi <- colnames(data_solar)[100:456]
data_solar_col_dates <- setdiff(colnames(data_solar), c(data_solar_col_produ, data_solar_col_predi))
data_solar_col_predi <- data_solar[,100:463]
# Columns defined from the enunciate
data_add_col <- colnames(data_add)[2:101]
data_add_col_dates <- setdiff(colnames(data_add), data_add_col)
```
## Complete data_add
```{r}
data <- select(data_add, all_of(data_add_col))
m_ <- 5
maxit_ <- 5
# data_mice_ <- mice(data, m=m_, maxit=maxit_, meth='pmm', seed=500)
# saveRDS(data_mice_, file.path('storage', 'data_add_mice.rds'))
data_mice_ <- readRDS(file.path('storage', 'data_add_mice.rds'))
# summary(data_mice_)
# Average of all the Multivariate Imputation
data_mice <- 0
for (i in 1:m_) data_mice <- data_mice + complete(data_mice_, i)
data_mice <- data_mice/m_
data_add_mice <- bind_cols(select(data_add, all_of(data_add_col_dates)), data_mice)
# Cleanup
rm(list = c('data', 'data_mice_', 'm_', 'maxit_', 'i', 'data_mice', 'data_add'))
```
# Join datasets
```{r}
data_solar_add <- data_solar %>%
left_join(data_add_mice, by = 'Date', suffix = c(".solar", ".add"))
rm(list = c('data_solar', 'data_add_mice'))
# skim(data_solar_add)
```
# Train, validation, test and predict split
```{r}
data_solar_add_train_ <- data_solar_add[1:5113,2:99]
# row indices for training data (70%)
nrow_train <- round(nrow(data_solar_add_train_)*.7, 0)
# row indices for validation data (15%)
nrow_val <- round(nrow(data_solar_add_train_)*.15, 0)
# row indices for test data (15%), the reminder rows
nrow_test <- nrow(data_solar_add_train_)-nrow_train-nrow_val
# Target columns
data_solar_add_train <- data_solar_add_train_[1:nrow_train, ]
data_solar_add_val <- data_solar_add_train_[(nrow_train+1):(nrow_train+nrow_val), ]
data_solar_add_test <- data_solar_add_train_[(nrow_train+nrow_val+1):nrow(data_solar_add_train_), ]
# Predictor columns
data_solar_predi_train <- data_solar_col_predi[1:nrow_train,]
data_solar_pred_val <- data_solar_col_predi[(nrow_train+1):(nrow_train+nrow_val), ]
#data_solar_dates <- setdiff(colnames(data_solar), c(data_solar_col_produ, data_solar_col_predi))
rm(list=c('nrow_train', 'nrow_val', 'nrow_test', 'data_solar_add_train_', 'data_add_col_dates'))
```
```{r}
numeric_vars <- colnames(data_solar_predi_train)[sapply(data_solar_predi_train, class) %in% c("integer","numeric")]
data_solar_predi_train <- data_solar_predi_train[,..numeric_vars]
```
# Ridge regularization glmnet
### Finding the best lambda value
```{r}
# library(glmnet)
#target_stations <- colnames(data_solar_add_train)
ridge_df <- data.frame()
dim(data_solar_add_train)
dim(data_solar_predi_train)
# for each target column
for (i in 1:ncol(data_solar_add_train)){
train_x <- model.matrix(unlist(data_solar_add_train[i],use.names = FALSE) ~ ., data_solar_predi_train)
train_y <- unlist(data_solar_add_train[i], use.names = FALSE)
# finding the mae of each target
ridge_solar <- cv.glmnet(
x=train_x,
y=train_y,
alpha = 0,
standardize = TRUE,type.measure = "mae"
)
# build table with each target and lambda.min
ridge_df <- rbind(ridge_df,
data.frame(station=i, lambda_min = ridge_solar$lambda.min))
}
```
### average lambda_min
```{r}
opt_lambda <- mean(ridge_df$lambda_min)
```
```{r}
### Train model with lambda
res_df <- data.frame()
for (i in 1:ncol(data_solar_add_train)){
x_train <- model.matrix(unlist(data_solar_add_train[i],use.names = FALSE) ~ ., data_solar_predi_train)
y_train <- unlist(data_solar_add_train[i], use.names = FALSE)
model_ridge <- train(x=x_pred,y=y_pred,
method="glmnet",
metric="MAE",
maximize=FALSE,
tuneGrid=expand.grid(alpha=0, # Ridge regression
lambda=opt_lambda),preProcess = c('scale','center'))
### resultant
res_df <- rbind(res_df,
data.frame(station = i,mae =model_ridge$results["MAE"]))
}
```
```{r}
res_df
```
### build the model with optimal lambda for each station
```{r}
cols <- colnames(data_solar_predi_train)
final_predictors <- final_predictors[,cols]
final_res <- data.frame(data_solar[5114:nrow(data_solar),1])
library(glmnet)
for (i in 1:ncol(data_solar_add_train)){
x_train <- data.matrix(data_solar_predi_train)
y_train <- unlist(data_solar_add_train[i], use.names = FALSE)
x_test <- data.matrix(final_predictors)
# y_test <- unlist(final_targets[1], use.names = FALSE)
ridge_best <- glmnet(x_train, y_train, alpha = 0, lambda = opt_lambda, standardize = TRUE)
prediction <- predict(object = ridge_best,newx = x_test)
final_res <- cbind(final_res,
prediction
)
}
```
```{r}
names <- c("Date",colnames(data_solar_add_train))
colnames(final_res) <- names
```
```{r}
write.table(x = final_res, file = '~/Downloads/ridge.csv', sep = ',', dec = '.', row.names = FALSE, quote = FALSE)
```