-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatchingDates.Rmd
298 lines (247 loc) · 10.1 KB
/
MatchingDates.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
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
---
title: "Matching Date Models"
author: "AE"
date: "11/17/2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Bremen Big Data Challenge
## Matching Date Models
This file includes only those models in which I restricted the training data to the similar dates as seen in the validation model.
### Recap
The challenge given to contestants was to take wind farm data collected from January, 2016 to June 2017, and the predict the wind farm's output in July - December of 2017.
The data includes:
* Wind speed and direction at different elevations (All measurements were collected at various heights. All measurements were only taken at certain intervals, and as such certain observations have been linearly interpolated.)
* A boolean variable which reflects whether the given observation was interpolated or not
* Capacity of the farm at a given time (This capacity variable is very nearly constant, and most likely only shows variation due to maitainence or downages at the farm.)
* Output of the farm (The original "challenge" data set presented to the contestants did not have any values in the output variable. Outside of the voiding of the output column, the challenge set and validation set are identical.)
### Libraries
```{r Libs}
library(foreign)
library(MASS)
library(car)
library(corrplot)
library(rpart) #for trees
library(ipred) #for bagging
library(randomForest) #for random forest
library(readr) #read in data
library(gbm) #for gradient boosting
library(rms)
library(gam)
library(ISLR)
library(caTools)
library(circular)
library(openair)
```
### Data Import and Preprocessing
```{r Data, results = 'hide'}
#Training Data
train <- read_csv("Data/train.csv")
View(train)
summary(train)
sum(is.na(train)) #No missings
#Reviewing the date variable
class(train$Datum)
#Altering the location, so info is saved in English
Sys.setlocale("LC_ALL", "en_US.UTF-8")
#converting to date format
train$Datum <- as.Date(train$Datum, format = c("%Y-%m-%d"))
#Saving a copy of the training set without date
Train.nodate <- train[,-1]
#Validation Data
eval <- read_csv("Data/eval.csv")
View(eval)
summary(eval)
sum(is.na(eval)) #no missings
#Converting the validation set to a dataframe
class(eval)
eval <- as.data.frame(eval)
#converting date variable to type date
class(eval$Datum)
eval$Datum <- as.Date(eval$Datum, format = c("%Y-%m-%d"))
#Results Data
load("Data_out/results.Rda")
```
### Data Manipulation, Feature Creation, and Spliting the Training Data
```{r Features}
#Adding Variables for Analysis
#Adding month variable
train$month <- months(train$Datum)
train$season <- 0
train$season[train$month == "May"] = 1
train$season[train$month == "June"] = 1
train$season[train$month == "July"] = 1
train$season[train$month == "August"] = 1
train$season[train$month == "September"] = 1
train$season[train$month == "October"] = 1
train$month[train$month == "January"] = 1
train$month[train$month == "February"] = 2
train$month[train$month == "March"] = 3
train$month[train$month == "April"] = 4
train$month[train$month == "May"] = 5
train$month[train$month == "June"] = 6
train$month[train$month == "July"] = 7
train$month[train$month == "August"] = 8
train$month[train$month == "September"] = 9
train$month[train$month == "October"] = 10
train$month[train$month == "November"] = 11
train$month[train$month == "December"] = 12
train$month <- as.numeric(train$month)
eval$month <- months(eval[,1])
eval$season <- 0
eval$season[eval$month == "May"] = 1
eval$season[eval$month == "June"] = 1
eval$season[eval$month == "July"] = 1
eval$season[eval$month == "August"] = 1
eval$season[eval$month == "September"] = 1
eval$season[eval$month == "October"] = 1
eval$month[eval$month == "January"] = 1
eval$month[eval$month == "February"] = 2
eval$month[eval$month == "March"] = 3
eval$month[eval$month == "April"] = 4
eval$month[eval$month == "May"] = 5
eval$month[eval$month == "June"] = 6
eval$month[eval$month == "July"] = 7
eval$month[eval$month == "August"] = 8
eval$month[eval$month == "September"] = 9
eval$month[eval$month == "October"] = 10
eval$month[eval$month == "November"] = 11
eval$month[eval$month == "December"] = 12
eval$month <- as.numeric(eval$month)
#Descriptives for the wind direction and speed
train$winddirectionmean <- ((train$Windrichtung48M+train$Windrichtung100M+train$Windrichtung152M)/3)
train$winddirectionvar <- (((train$Windrichtung48M-train$winddirectionmean)^2+(train$Windrichtung100M-train$winddirectionmean)^2+(train$Windrichtung152M-train$winddirectionmean)^2)/3)
train$windspeedmean <- ((train$Windgeschwindigkeit48M+train$Windgeschwindigkeit100M+train$Windgeschwindigkeit152M)/3)
train$windspeedvar <- (((train$Windgeschwindigkeit48M-train$windspeedmean)^2+(train$Windgeschwindigkeit100M-train$windspeedmean)^2+(train$Windgeschwindigkeit152M-train$windspeedmean)^2)/3)
eval$winddirectionmean <- ((eval$Windrichtung48M+eval$Windrichtung100M+eval$Windrichtung152M)/3)
eval$winddirectionvar <- (((eval$Windrichtung48M-eval$winddirectionmean)^2+(eval$Windrichtung100M-eval$winddirectionmean)^2+(eval$Windrichtung152M-eval$winddirectionmean)^2)/3)
eval$windspeedmean <- ((eval$Windgeschwindigkeit48M+eval$Windgeschwindigkeit100M+eval$Windgeschwindigkeit152M)/3)
eval$windspeedvar <- (((eval$Windgeschwindigkeit48M-eval$windspeedmean)^2+(eval$Windgeschwindigkeit100M-eval$windspeedmean)^2+(eval$Windgeschwindigkeit152M-eval$windspeedmean)^2)/3)
#weighted average direction
train$winddirectionweightedmean <- (((train$Windrichtung48M*train$Windgeschwindigkeit48M)+(train$Windrichtung100M*train$Windgeschwindigkeit100M)+(train$Windrichtung152M*train$Windgeschwindigkeit152M))/(train$Windgeschwindigkeit48M+train$Windgeschwindigkeit100M+train$Windgeschwindigkeit152M))
eval$winddirectionweightedmean <- (((eval$Windrichtung48M*eval$Windgeschwindigkeit48M)+(eval$Windrichtung100M*eval$Windgeschwindigkeit100M)+(eval$Windrichtung152M*eval$Windgeschwindigkeit152M))/(eval$Windgeschwindigkeit48M+eval$Windgeschwindigkeit100M+eval$Windgeschwindigkeit152M))
eval <- as.data.frame(eval)
train <- as.data.frame(train)
```
## Matching The Dates
The data presented as the training data covers significant period of time, but the "challenge" was to predict the output for a much shorter range of time in the following year. It may be that, due to some seasonality (which would make sense as this is highly dependant on the weather) may be causing us to over or underestimate in our predictions. As such, I am going to restrict the training data set to dates which match those in the validation set.
```{r DateMatching}
summary(train$Datum) #2016 Jan - 2017 June
summary(eval$Datum) #2017 July - 2017 Dec
train.dates <- subset.data.frame(train, Datum >= "2016-07-01" & Datum <= "2016-12-31")
```
### Gradient Boosting on Matching Date Data
I'll start by using the best preforming model from the full training data set.
```{r GBM_Dates}
boost.Dates.1 <-
gbm(Output ~ Windgeschwindigkeit48M +
Windgeschwindigkeit100M +
Windgeschwindigkeit152M +
Windrichtung48M +
Windrichtung100M +
Windrichtung152M +
Windgeschwindigkeit100MP40 +
Windgeschwindigkeit100MP50 +
Windgeschwindigkeit100MP70 +
Windgeschwindigkeit100MP90 +
Verfügbare_Kapazität,
data = train.dates,
distribution = "laplace",
n.trees = 1000,
shrinkage = 0.03,
interaction.depth = 7)
boost.Dates.2 <-
gbm(Output~Windgeschwindigkeit48M +
Windgeschwindigkeit100M +
Windgeschwindigkeit152M +
Windrichtung100M +
Windrichtung152M +
Windgeschwindigkeit100MP10 +
Windgeschwindigkeit100MP30 +
Windgeschwindigkeit100MP40 +
Windgeschwindigkeit100MP50 +
Windgeschwindigkeit100MP70 +
Windgeschwindigkeit100MP90 +
Verfügbare_Kapazität +
month +
winddirectionvar +
windspeedvar +
winddirectionweightedmean,
data=train.dates,
distribution = "laplace",
n.trees = 700,
shrinkage = 0.03,
interaction.depth = 6)
```
```{r Preds_GBM_Dates}
print("No Date Model")
predict.boost.Dates.1 <- round(predict(boost.Dates.1, newdata=eval, n.trees = 1000), digits=0)
(sum((eval$Output-predict.boost.Dates.1)^2)/nrow(eval))^(1/2)
error_gbm.d.1 <- (sum(abs(predict.boost.Dates.1-eval$Output)))/sum(eval$Verfügbare_Kapazität)
error_gbm.d.1
predict.boost.Dates.2 <- round(predict(boost.Dates.2, newdata=eval, n.trees = 700), digits=0)
(sum((eval$Output-predict.boost.Dates.2)^2)/nrow(eval))^(1/2)
error_gbm.d.2 <- (sum(abs(predict.boost.Dates.2-eval$Output)))/sum(eval$Verfügbare_Kapazität)
error_gbm.d.2
results <-
rbind(results,
list("GBM No Date - Matching Date Data", error_gbm.d.1),
list("GBM New Var - Matching Dates", error_gbm.d.2))
```
### GAM Dates Models
```{r GAM_2}
#model 1
gam1.dates <- gam(formula = gam_form,
family=gaussian,
data=train.dates)
#model 2
gam2.dates <- gam(formula = gam_formsix,
family=gaussian,
data=train.dates)
```
#### Predictions
```{r Preds_GAM_2}
#Model 1
print("Model 1")
predict.gam1.dates <-
round(predict(gam1.dates, newdata=eval),
digits=0)
error_gam1.dates <- (sum(abs(predict.gam1.dates-eval$Output)))/sum(eval$Verfügbare_Kapazität)
error_gam1.dates
#Model 2
print("Model 2")
predict.gam2.dates <-
round(predict(gam2.dates, newdata=eval),
digits=0)
error_gam2.dates <- (sum(abs(predict.gam2.dates-eval$Output)))/sum(eval$Verfügbare_Kapazität)
error_gam2.dates
results <-
rbind(results,
list("GAM 1 - Matching Dates",
error_gam1.dates),
list("GAM 2 - Matching Dates",
error_gam2.dates))
```
### RF Dates Models
```{r rF_dates}
rforest.dates <- randomForest(Output~., data=train.dates)
```
#### Predictions
```{r Preds_RFDates}
print("RF Dates")
predict.rf.dates <-
round(predict(rforest.dates, newdata=eval),
digits=0)
error_rf.dates <- (sum(abs(predict.rf.dates-eval$Output)))/sum(eval$Verfügbare_Kapazität)
error_rf.dates
results <-
rbind(results,
list("RF Dates",
error_rf.dates))
```
### Saving results
```{r Save_Results}
save(results, "Data_out/results.Rda")
```