-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeuralNetwork.Rmd
131 lines (101 loc) · 3.24 KB
/
NeuralNetwork.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
---
title: "R Notebook"
output:
pdf_document: default
html_notebook: default
---
Import Data:
```{r}
#Why Load_history(1)? : removed the "," at excel
Load_history <- read.csv("E:/ETUDE/EP/MAP573/Projet/global-energy-forecasting-competition-2012-load-forecasting/Load_history(1).csv")
temperature <- read.csv("E:/ETUDE/EP/MAP573/Projet/global-energy-forecasting-competition-2012-load-forecasting/temperature_history.csv")
```
Pre-traitement of Data
Seperate the datas and the temperatures by the zones and stations
```{r}
library(dplyr)
varnames <- paste("data_",1:20,sep="")
for (i in 1:20){
assign(varnames[i],value=filter(Load_history,zone_id == i)[2:28])
}
varnames <- paste("T_",1:11,sep="")
for (i in 1:11){
assign(varnames[i],value=filter(temperature,station_id == i)[2:28])
}
```
to 1D Vector
```{r}
varnames <- paste("T_",1:11,sep="")
for (i in 1:11){
name <- varnames[i]
assign(paste(name,"l",sep=""),value=as.vector(t(get(name)[,4:27])))
name <- paste(name,"l",sep="")
assign(paste(name,"n",sep=""),value=get(name)/(max(get(name),na.rm = TRUE)))
}
varnames <- paste("data_",1:20,sep="")
for (i in 1:20){
name <- varnames[i]
assign(paste(name,"l",sep=""),value=as.vector(t(get(name)[,4:27])))
name <- paste(name,"l",sep="")
assign(paste(name,"n",sep=""),value=get(name)/(max(get(name),na.rm = TRUE)))
}
```
Calculate the mean of temperature
```{r}
varnames <- paste("T_",1:11,sep="")
T <- unlist(T_1ln, use.names=FALSE)
for (i in 2:11){
T <- rbind(T,unlist(get(paste(varnames[i],"ln",sep="")), use.names=FALSE))
}
Tm <-colMeans(T)
```
External regretor:
According to the reference and observation,
We choose the mean temperature of 48h before t0
and the temperature 12h before t0
as external regressors
```{r}
library(pracma)
end<-which(is.na(Tm))[1]-1
Tmu<-Tm[1:end]
Tm_Avr<-movavg(Tmu, 48, "s")[49:end]
Tm_Bf<-Tmu[(49-12):(end-12)]
L <- list (Tm_Avr,Tm_Bf)
M <- do.call( rbind,L)
M <- t(M)
#We take the first Zone
data_1lu<-data_1ln[49:end]
```
Neural Network Time Series Forecasts without external regressors
```{r}
library(forecast)
library(fpp2)
Day<-300
data_1luts<-ts(data_1lu,frequency = 24)
fit.nn <- nnetar(window(data_1luts,end=c(Day,24)),lambda="auto")
autoplot(forecast(fit.nn,h=7*24))+
ylab("nnetar without xreg")+
xlab("Time/Day")
```
```{r}
autoplot(window(data_1luts,start=c((Day-5),24),end=c((Day+7),24)))+
autolayer(forecast(fit.nn,h=7*24),series = "nntar")+
guides(colour=guide_legend(title="prediction"))+
ylab("Neural Network Time Series Forecasts")+
xlab("Time/Day")
```
Neural Network Time Series Forecasts with external regressors
```{r}
fit.nnx <- nnetar(window(data_1luts,end=c(Day,24)), lambda="auto",xreg = M[1:(24*Day),])
autoplot(forecast(fit.nnx,h=7*24,xreg=M[(24*Day):(24*(Day+7)),]))+
ylab("nnetar xreg")+
xlab("Time/Day")
```
```{r}
autoplot(window(data_1luts,start=c((Day-5),24),end=c((Day+7),24)))+
autolayer(forecast(fit.nnx,h=7*24,xreg=M[(24*Day):(24*(Day+7)),]),series = "nntar_xreg")+
# autolayer(forecast(fit.nn,h=7*24),series = "nntar")+
guides(colour=guide_legend(title="prediction"))+
ylab("Neural Network Time Series Forecasts")+
xlab("Time/Day")
```