---
title: "Electricity market price"
author: "Hele-Liis Peedosk"
date: "18 aprill 2019"
output: html_document
---

# Load libraries and functions. Run h2o.
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


```{r libraries} 
source("functions.R")
load_libraries()
```

```{r}
# Run 'h2o' interface
init_h2o(install=FALSE)
# h2o.shutdown(prompt=FALSE)
```


# Initialize results table
```{r}
results=init_results()
```


# Read in the data
```{r}    
data_dir <- "C:/Users/helts/Desktop/Masters thesis/R"
fname <- file.path(data_dir, "ELECTR_daily.csv")
data_ELD=read.csv(fname, header=TRUE, dec=",", sep=";", na.strings=c(""))
data_ELD=data_ELD[c("X", "EE")]
colnames(data_ELD)<-c("date", "value")

#data_ELD$Date
dates=as.Date(data_ELD$date, format="%d.%m.%Y")
data_ELD$date=dates
```


```{r}
# Create tibble table with original data
trend=FALSE
diff=FALSE
tbl_data=create_tbl(data_ELD, remove_trend=trend, difference = diff)
ELD=tbl_data$data
data=tbl_data$tbl
data_orig=tbl_data$tbl_orig
```


# Draw a graph of the time series
```{r}
source("functions.R")
df_orig=train_val_test_split(data_orig,val_split=val_split, test_split = 0.85)
latex_graph_df(series=df, subseries=df[df$index>'2018-07-31',], y_lab="Euros", y_axis_lab_scale=waiver())
```


# Define the variables for this time series
```{r}
# History to be used

seasonality=7
frequency=365

# Choose input size, horizon, hidden layers, seed, epochs, activation_function
seed = 1
epochs = 50
activation_function = "Rectifier"

# Information for results table
data_name = "ELD"
rolling = FALSE
comments = "-"
history_start = "orig_data"


#Choose whether to use validation set
val_split = 0.8
val_set = TRUE

order=c(3,0,0) #NULL
seasonal=list(order=c(4,0,0), period=seasonality)

```



# Determine manual grid

```{r}
# Grid search parameters
inputs=c(7, 14, 28, 35, 42)
horizons = c(1, 3, 7, 14)        
hidden_layers =list(c(2), c(4),c(6), c(7), c(9), c(3),c(5), c(8), c(10), c(10,10), c(20), c(100))
ensemble_sizes =c(10)
```


# Develop neural network models 

```{r}

df=train_val_test_split(data,val_split=val_split, test_split = 0.85)
df_orig=train_val_test_split(data_orig,val_split=val_split, test_split = 0.85)

for (n_output in horizons){
    for (n_input in inputs){    
       tryCatch({      # In case the input horizon is too long.
         
          # Generate data with certain colnames, extract data, X colnames, y colnames
          io_df<-gen_input_output(df, n_input, n_output, val_set=TRUE, step_fn=NULL, key_val=NULL)
          io_df_orig<-gen_input_output(df_orig, n_input, n_output, val_set=TRUE, step_fn=NULL, key_val=NULL, standardize=FALSE)

          for (ensemble_size in ensemble_sizes){

              for (hidden in hidden_layers){
                     if (sum(hidden)>=20) model_name="h2o: big" else model_name="h2o: small"
                     tryCatch({      # In case of some error with the model
                   y=  train_eval(io_df=io_df, ensemble_size=ensemble_size, hidden=hidden,  data_name=data_name, train_predict_model = train_predict_h2o_sep
                                , package="h2o", comments="multi NN", rolling=rolling, history_start=history_start
                                , mod_fn=mod_fn_h2o_reg, model_name=model_name, print_info=TRUE, hyper_params=NULL, io_df_orig=io_df_orig
                                , write_results_path= "", diff=diff, trend=trend, loss="Absolute")
                     },error=function(e){cat("ERROR: Cannot handle the h2o model with horizon=",n_output
                                          , ", lookback=",n_input, ", and hidden ", paste0(hidden, collapse = ", "), "\n")})


                      tryCatch({      # In case of some error with the model
                   y=   train_eval(io_df=io_df, ensemble_size=ensemble_size, hidden=hidden,  data_name=data_name, train_predict_model = train_predict_h2o_rec
                                 , package="h2o", comments=paste(comments, "iterative", sep=", "), rolling=rolling, history_start=history_start
                                 , mod_fn=mod_fn_h2o_reg, model_name=model_name, print_info=TRUE, hyper_params=NULL, io_df_orig=io_df_orig, loss="Absolute"
                                 , write_results_path= "", diff=diff, trend=trend)
                      },error=function(e){cat("ERROR: Cannot handle the h2o model with horizon=",n_output
                                           , ", lookback=",n_input, ", and hidden ", paste0(hidden, collapse = ", "), "\n")})
    
                      tryCatch({      # In case of some error with the model
                    y=      train_eval(io_df=io_df, ensemble_size=ensemble_size, hidden=hidden,  data_name=data_name
                                     , train_predict_model = train_predict_neuralnet
                                     , package="neuralnet", comments="direct", rolling=rolling, history_start=history_start
                                     , diff=diff, trend=trend, loss="sse"
                                     , mod_fn=mod_fn_h2o_reg, model_name="small", print_info=TRUE, hyper_params=NULL, io_df_orig=io_df_orig
                                     , write_results_path= "")
                      },error=function(e){cat("ERROR: Cannot handle the neuralnet model with horizon=",n_output
                                               , ", lookback=",n_input, ", and hidden ", paste0(hidden, collapse = ", "), "\n")})
     
    
    
                   }}
    
              tryCatch({      # In case of some error with the model
         y=     train_eval(io_df=io_df_orig, ensemble_size, hidden,  data_name, train_predict_rwf, package="Naive", comments, rolling, history_start
                             , mod_fn=NULL, model_name="snaive", print_info=TRUE, hyper_params=NULL, seasonality=0, io_df_orig=io_df_orig
                             , write_results_path= "C:/Users/helts/Desktop/Masters thesis/R/results_", diff=diff, trend=trend)
              },error=function(e){cat("ERROR: Cannot handle naive model with horizon =",n_output
                                   , "and lookback=",n_input, "\n")})



              tryCatch({      # In case of some error with the model
         y=     train_eval(io_df=io_df_orig, ensemble_size, hidden,  data_name, train_predict_arima, package="ARIMA", comments, rolling, history_start
                             , mod_fn=NULL, model_name="ARIMA", print_info=TRUE, hyper_params=NULL, seasonality=seasonality, io_df_orig=io_df_orig
                             , order=order, seasonal=seasonal
                             , diff=diff, trend=trend, val_set=TRUE
                             , write_results_path= "")

                        },error=function(e){cat("ERROR: Cannot handle arima model with horizon =",n_output
                                             , "and lookback=",n_input, "\n")})

          },error=function(e){cat("ERROR: Cannot handle input-output split with horizon =",n_output
                           , "and lookback=",n_input, "\n")})

  }}

    results
```


#===================================================================================================================================

# Cut the high peaks
```{r}
tr_ELD=data_ELD
changes=c()
i=8
for ( i in 8:length(data_ELD$value)){
  avg=mean(tr_ELD$value[(i-7):i])
  if(tr_ELD$value[i]>1.5*avg){
      tr_ELD$value[i]=1.5*avg
      changes=c(changes, i)
  }
}
latex_graph(data_ELD, tr_ELD, y_lab="Euros", y_axis_lab_scale=scales::comma)
```


```{r}
# Create tibble table with original data
history_start = "cut_data"

data_ELD=tr_ELD
trend=FALSE
diff=FALSE
tbl_data=create_tbl(data_ELD, remove_trend=trend, difference = diff)
ELD=tbl_data$data
data=tbl_data$tbl
data_orig=tbl_data$tbl_orig
```


# Draw a graph of the time series
```{r}
source("functions.R")
df_orig=train_val_test_split(data_orig,val_split=val_split, test_split = 0.85)
latex_graph_df(series=df, subseries=df[df$index>'2018-07-31',], y_lab="Euros", y_axis_lab_scale=waiver())
```


# Develop neural network models 

```{r}

df=train_val_test_split(data,val_split=val_split, test_split = 0.85)
df_orig=train_val_test_split(data_orig,val_split=val_split, test_split = 0.85)

for (n_output in horizons){
    for (n_input in inputs){    
       tryCatch({      # In case the input horizon is too long.
         
          # Generate data with certain colnames, extract data, X colnames, y colnames
          io_df<-gen_input_output(df, n_input, n_output, val_set=TRUE, step_fn=NULL, key_val=NULL)
          io_df_orig<-gen_input_output(df_orig, n_input, n_output, val_set=TRUE, step_fn=NULL, key_val=NULL, standardize=FALSE)

          for (ensemble_size in ensemble_sizes){

              for (hidden in hidden_layers){
                     if (sum(hidden)>=20) model_name="h2o: big" else model_name="h2o: small"
                     tryCatch({      # In case of some error with the model
                   y=  train_eval(io_df=io_df, ensemble_size=ensemble_size, hidden=hidden,  data_name=data_name, train_predict_model = train_predict_h2o_sep
                                , package="h2o", comments="multi NN", rolling=rolling, history_start=history_start
                                , mod_fn=mod_fn_h2o_reg, model_name=model_name, print_info=TRUE, hyper_params=NULL, io_df_orig=io_df_orig
                                , write_results_path= "", diff=diff, trend=trend, loss="Absolute")
                     },error=function(e){cat("ERROR: Cannot handle the h2o model with horizon=",n_output
                                          , ", lookback=",n_input, ", and hidden ", paste0(hidden, collapse = ", "), "\n")})


                      tryCatch({      # In case of some error with the model
                   y=   train_eval(io_df=io_df, ensemble_size=ensemble_size, hidden=hidden,  data_name=data_name, train_predict_model = train_predict_h2o_rec
                                 , package="h2o", comments=paste(comments, "iterative", sep=", "), rolling=rolling, history_start=history_start
                                 , mod_fn=mod_fn_h2o_reg, model_name=model_name, print_info=TRUE, hyper_params=NULL, io_df_orig=io_df_orig, loss="Absolute"
                                 , write_results_path= "", diff=diff, trend=trend)
                      },error=function(e){cat("ERROR: Cannot handle the h2o model with horizon=",n_output
                                           , ", lookback=",n_input, ", and hidden ", paste0(hidden, collapse = ", "), "\n")})
    
                      tryCatch({      # In case of some error with the model
                    y=      train_eval(io_df=io_df, ensemble_size=ensemble_size, hidden=hidden,  data_name=data_name
                                     , train_predict_model = train_predict_neuralnet
                                     , package="neuralnet", comments="direct", rolling=rolling, history_start=history_start
                                     , diff=diff, trend=trend, loss="sse"
                                     , mod_fn=mod_fn_h2o_reg, model_name="small", print_info=TRUE, hyper_params=NULL, io_df_orig=io_df_orig
                                     , write_results_path= "")
                      },error=function(e){cat("ERROR: Cannot handle the neuralnet model with horizon=",n_output
                                               , ", lookback=",n_input, ", and hidden ", paste0(hidden, collapse = ", "), "\n")})
     
    
    
                   }}
    
              tryCatch({      # In case of some error with the model
         y=     train_eval(io_df=io_df_orig, ensemble_size, hidden,  data_name, train_predict_rwf, package="Naive", comments, rolling, history_start
                             , mod_fn=NULL, model_name="snaive", print_info=TRUE, hyper_params=NULL, seasonality=0, io_df_orig=io_df_orig
                             , write_results_path= "C:/Users/helts/Desktop/Masters thesis/R/results_", diff=diff, trend=trend)
              },error=function(e){cat("ERROR: Cannot handle naive model with horizon =",n_output
                                   , "and lookback=",n_input, "\n")})



              tryCatch({      # In case of some error with the model
         y=     train_eval(io_df=io_df_orig, ensemble_size, hidden,  data_name, train_predict_arima, package="ARIMA", comments, rolling, history_start
                             , mod_fn=NULL, model_name="ARIMA", print_info=TRUE, hyper_params=NULL, seasonality=seasonality, io_df_orig=io_df_orig
                             , order=order, seasonal=seasonal
                             , diff=diff, trend=trend, val_set=TRUE
                             , write_results_path= "")

                        },error=function(e){cat("ERROR: Cannot handle arima model with horizon =",n_output
                                             , "and lookback=",n_input, "\n")})

          },error=function(e){cat("ERROR: Cannot handle input-output split with horizon =",n_output
                           , "and lookback=",n_input, "\n")})

  }}

    results
```

