# Needs packages:
#library(urbanR)
library(gbm)
library(lubridate)
library(plyr) #OJO! la llibreria plyr ha de ser cridada abans que dplyr 
library(dplyr) #per treballar amb cbinds i esas cosas
library(caret)
library(zoo)
library(scatterplot3d)
library(plotly)

#borrar
#library(RNetCDF)
library(sp)
library(sf)
library(mapview)
#library(ncdf4)
#library(rgeos) 
library(ggplot2)
#library(broom)
#library(magrittr)
#library(ggpubr)
library(raster)
#library(rgdal)
#library(tmap)
library(gstat)
#library(abind)
#library(rbind)
library(openair)
#library(tidyr)




UK_gene_20190121 <- readRDS("/home/abarrant/esarchive/scratch/abarrant/results_regional_2019/results_gene_pm2p5_sensegbm/UK_gene_pm2p5_20190121.rds")

mean_persist_senia <- readRDS("/home/abarrant/esarchive/scratch/abarrant/results_regional_2019/GBM_eval/mean_persist_senia-100.rds")
mean_persist_eixample <- readRDS("/home/abarrant/esarchive/scratch/abarrant/results_regional_2019/GBM_eval/mean_persist_eixample-100.rds")
mean_linterpol_senia <- readRDS("/home/abarrant/esarchive/scratch/abarrant/results_regional_2019/GBM_eval/mean_linterpol_senia-100.rds")
mean_linterpol_eixample <- readRDS("/home/abarrant/esarchive/scratch/abarrant/results_regional_2019/GBM_eval/mean_linterpol_eixample-100.rds")
mean_gbm_senia_n3 <- readRDS("/home/abarrant/esarchive/scratch/abarrant/results_regional_2019/GBM_eval/mean_gbm_senia-100.rds")
mean_gbm_eixample_n3 <- readRDS("/home/abarrant/esarchive/scratch/abarrant/results_regional_2019/GBM_eval/mean_gbm_eixample-100.rds")


summary_mean_bench <- rbind(mean_gbm_eixample_n3[1,], 
                            mean_linterpol_eixample[1,],
                            mean_persist_eixample[1,], 
                            mean_gbm_senia_n3[1,], 
                            mean_linterpol_senia[1, ], 
                            mean_persist_senia[1,])

rownames(summary_mean_bench) <- c("gbm_eixample", "interpol_eixample", "persist_eixample", "gbm_senia","interpol_senia", "persist_senia")

save_path    <- "/home/abarrant/esarchive/scratch/abarrant/results_regional_2019/GBM_eval/"
saveRDS(object=summary_mean_bench, file=paste0(save_path,"summary_mean_bench-100.rds"))



# bench GBM --------------------------------------------------------------------------


mean_gbm_senia_n3 <- readRDS("/home/abarrant/esarchive/scratch/abarrant/results_regional_2019/GBM_eval/mean_gbm_senia-100.rds")
mean_gbm_eixample_n3 <- readRDS("/home/abarrant/esarchive/scratch/abarrant/results_regional_2019/GBM_eval/mean_gbm_eixample-100.rds")


mean_gbm_senia_1 <- readRDS("/home/abarrant/esarchive/scratch/abarrant/results_regional_2019/GBM_eval/mean_gbm_senia-10_1.rds")
mean_gbm_eixample_1 <- readRDS("/home/abarrant/esarchive/scratch/abarrant/results_regional_2019/GBM_eval/mean_gbm_eixample-10_1.rds")
mean_gbm_senia_2 <- readRDS("/home/abarrant/esarchive/scratch/abarrant/results_regional_2019/GBM_eval/mean_gbm_senia-30_2.rds")
mean_gbm_eixample_2 <- readRDS("/home/abarrant/esarchive/scratch/abarrant/results_regional_2019/GBM_eval/mean_gbm_eixample-30_2.rds")
mean_gbm_senia_3 <- readRDS("/home/abarrant/esarchive/scratch/abarrant/results_regional_2019/GBM_eval/mean_gbm_senia-70_3.rds")
mean_gbm_eixample_3 <- readRDS("/home/abarrant/esarchive/scratch/abarrant/results_regional_2019/GBM_eval/mean_gbm_eixample-70_3.rds")


mean_gbm_senia_123 <- rbind(mean_gbm_senia_1, mean_gbm_senia_2, mean_gbm_senia_3)
mean_gbm_senia_ada <- mean_gbm_senia_123  %>%
  as.data.frame() %>%
  group_by(gap_size, train_fraction) %>%
  dplyr::summarise(r_mean= mean(r_mean), r_std = sd(r_std), MB_mean= mean(MB_mean), MB_std = sd(MB_std), RMSE_mean = mean(RMSE_mean), RMSE_std = sd(RMSE_std))

save_path    <- "/home/abarrant/esarchive/scratch/abarrant/results_regional_2019/GBM_eval/"
#saveRDS(object=mean_gbm_senia_ada, file=paste0(save_path,"mean_gbm_eixample-100_pcada.rds"))

mean_gbm_eixample_123 <- rbind(mean_gbm_eixample_1, mean_gbm_eixample_2, mean_gbm_eixample_3)
mean_gbm_eixample_ada <- mean_gbm_eixample_123  %>%
  as.data.frame() %>%
  group_by(gap_size, train_fraction) %>%
  dplyr::summarise(r_mean= mean(r_mean), r_std = sd(r_std), MB_mean= mean(MB_mean), MB_std = sd(MB_std), RMSE_mean = mean(RMSE_mean), RMSE_std = sd(RMSE_std))

save_path    <- "/home/abarrant/esarchive/scratch/abarrant/results_regional_2019/GBM_eval/"
#saveRDS(object=mean_gbm_senia_ada, file=paste0(save_path,"mean_gbm_eixample-100_pcada.rds"))



# plotting bench ---------------------------------------------------------------------------
ggplot(mean_gbm_senia_n3, aes(x = gap_size, y = r_mean, color = as.factor(train_fraction))) +
  geom_line() +
  labs(title = "Stats gbm La Sènia",
       x = "gap_size",
       y = "r_mean", 
       color = "Train fraction") +
  scale_color_viridis_d() +  # paleta de colores magma?
  theme_minimal() +
  ylim(0, 1) 

ggplot(mean_gbm_eixample_n3, aes(x = gap_size, y = r_mean, color = as.factor(train_fraction))) +
  geom_line() +
  labs(title = "Stats gbm Eixample",
       x = "gap_size",
       y = "r_mean", 
       color = "Train fraction") +
  scale_color_viridis_d() + 
  theme_minimal() +
  ylim(0, 1) 



## MB mean value ----------------------------------------------------------------
ggplot(mean_gbm_senia_n3, aes(x = gap_size, y = MB_mean, color = as.factor(train_fraction))) +
  geom_line() +
  labs(title = "Stats gbm La Sènia",
       x = "gap_size",
       y = "MB_mean", 
       color = "Train fraction") +
  scale_color_viridis_d() +
  theme_minimal() +
  ylim(-1, 1) #ajustar limits 

ggplot(mean_gbm_eixample_n3, aes(x = gap_size, y = MB_mean, color = as.factor(train_fraction))) +
  geom_line() +
  labs(title =  "Stats gbm Eixample",
       x = "gap_size",
       y = "MB_mean", 
       color = "Train fraction") +
  scale_color_viridis_d() +  
  theme_minimal() +
  ylim(-1, 1) 


ggplot(mean_linterpol_senia, aes(x = gap_size, y = MB_mean, color = as.factor(train_fraction))) +
  geom_line() +
  labs(title = "Stats linear interpol La Sènia",
       x = "gap_size",
       y = "MB_mean", 
       color = "Train fraction") +
  scale_color_viridis_d() +
  theme_minimal()  +
  ylim(-1, 1) #ajustar limits 

ggplot(mean_persist_senia, aes(x = gap_size, y = MB_mean, color = as.factor(train_fraction))) +
  geom_line() +
  labs(title = "Stats persistence La Sènia",
       x = "gap_size",
       y = "MB_mean", 
       color = "Train fraction") +
  scale_color_viridis_d() +  
  theme_minimal() +
  ylim(-1, 1) 

#eixample
ggplot(mean_linterpol_eixample, aes(x = gap_size, y = MB_mean, color = as.factor(train_fraction))) +
  geom_line() +
  labs(title = "Stats linear interpol Eixample",
       x = "gap_size",
       y = "MB_mean", 
       color = "Train fraction") +
  scale_color_viridis_d() +
  theme_minimal()  +
  ylim(-1, 1) #ajustar limits 

ggplot(mean_persist_eixample, aes(x = gap_size, y = MB_mean, color = as.factor(train_fraction))) +
  geom_line() +
  labs(title = "Stats persistence Eixample",
       x = "gap_size",
       y = "MB_mean", 
       color = "Train fraction") +
  scale_color_viridis_d() +  
  theme_minimal() +
  ylim(-1, 1) 

## RMSE mean value ---------------------------------------------------------------

ggplot(mean_gbm_senia_n3, aes(x = gap_size, y = RMSE_mean, color = as.factor(train_fraction))) +
  geom_line() +
  labs(title = "Stats gbm La Sènia",
       x = "gap_size",
       y = "RMSE_mean", 
       color = "Train fraction") +
  scale_color_viridis_d() +
  theme_minimal()  +
  ylim(2, 9) #ajustar limits 

ggplot(mean_gbm_eixample_n3, aes(x = gap_size, y = RMSE_mean, color = as.factor(train_fraction))) +
  geom_line() +
  labs(title =  "Stats gbm Eixample",
       x = "gap_size",
       y = "RMSE_mean", 
       color = "Train fraction") +
  scale_color_viridis_d() +  
  theme_minimal() +
  ylim(2, 9) 

ggplot(mean_linterpol_senia, aes(x = gap_size, y = RMSE_mean, color = as.factor(train_fraction))) +
  geom_line() +
  labs(title = "Stats linear interpol La Sènia",
       x = "gap_size",
       y = "RMSE_mean", 
       color = "Train fraction") +
  scale_color_viridis_d() +
  theme_minimal()  +
  ylim(2, 9) #ajustar limits 

ggplot(mean_persist_senia, aes(x = gap_size, y = RMSE_mean, color = as.factor(train_fraction))) +
  geom_line() +
  labs(title = "Stats persistence La Sènia",
       x = "gap_size",
       y = "RMSE_mean", 
       color = "Train fraction") +
  scale_color_viridis_d() +  
  theme_minimal() +
  ylim(2, 9) 

#exiample
ggplot(mean_linterpol_eixample, aes(x = gap_size, y = RMSE_mean, color = as.factor(train_fraction))) +
  geom_line() +
  labs(title = "Stats linear interpol Eixample",
       x = "gap_size",
       y = "RMSE_mean", 
       color = "Train fraction") +
  scale_color_viridis_d() +
  theme_minimal()  +
  ylim(2, 9) #ajustar limits 

ggplot(mean_persist_eixample, aes(x = gap_size, y = RMSE_mean, color = as.factor(train_fraction))) +
  geom_line() +
  labs(title = "Stats persistence Eixample",
       x = "gap_size",
       y = "RMSE_mean", 
       color = "Train fraction") +
  scale_color_viridis_d() +  
  theme_minimal() +
  ylim(2, 9) 

# mitjanes per qualsevol train fraction -------------------------------------------

mean_trainfraction <- function(df, meto) {
  mean_train_df <- df  %>%
    as.data.frame() %>%
    group_by(gap_size) %>%
    dplyr::summarise(r_mean = mean(r_mean), r_std_mean = mean(r_std), MB_mean= mean(MB_mean), MB_std_mean = mean(MB_std), RMSE_mean = mean(RMSE_mean), RMSE_std = mean(RMSE_std))
  
  mean_train_df$method <- meto
  return(mean_train_df)
}

mean_train_gbm_eixample <- mean_trainfraction(mean_gbm_eixample_n3, "GBM")
mean_train_linterpol_eixample <- mean_trainfraction(mean_linterpol_eixample, "Linear interpolation")
mean_train_persist_eixample <- mean_trainfraction(mean_persist_eixample, "Persistence")

mean_train_gbm_senia <- mean_trainfraction(mean_gbm_senia_n3, "GBM")
mean_train_linterpol_senia <- mean_trainfraction(mean_linterpol_senia, "Linear interpolation")
mean_train_persist_senia <- mean_trainfraction(mean_persist_senia, "Persistence")

summary_eixample <- rbind(mean_train_gbm_eixample, mean_train_linterpol_eixample, mean_train_persist_eixample)
summary_senia <- rbind(mean_train_gbm_senia, mean_train_linterpol_senia, mean_train_persist_senia)


ggplot(summary_eixample, aes(x = gap_size, y = r_mean, color = as.factor(method))) +
  geom_line() +
  labs(title = "Summary Stats Eixample",
       x = "Gap size",
       y = "Mean r ", 
       color = "Methodolody") +
  theme_bw() +
  ylim(0, 1) #ajustar limits 

ggplot(summary_senia, aes(x = gap_size, y = r_mean, color = as.factor(method))) +
  geom_line() +
  labs(title = "Summary Stats La Sènia",
       x = "Gap size",
       y = "Mean r", 
       color = "Methodolody") +
  theme_bw() +
  ylim(0, 1) #ajustar limits 



ggplot(summary_eixample, aes(x = gap_size, y = MB_mean, color = as.factor(method))) +
  geom_line() +
  labs(title = "Summary Stats Eixample",
       x = "Gap size",
       y = "Mean MB ", 
       color = "Methodolody") +
  theme_bw() +
  ylim(-0.25, 0.25) #ajustar limits 

ggplot(summary_senia, aes(x = gap_size, y = MB_mean, color = as.factor(method))) +
  geom_line() +
  labs(title = "Summary Stats La Sènia",
       x = "Gap size",
       y = "Mean MB", 
       color = "Methodolody") +
  theme_bw() +
  ylim(-0.25, 0.25) #ajustar limits 



ggplot(summary_eixample, aes(x = gap_size, y = RMSE_mean, color = as.factor(method))) +
  geom_line() +
  labs(title = "Summary Stats Eixample",
       x = "Gap size",
       y = "Mean RMSE ", 
       color = "Methodolody") +
  theme_bw() +
  ylim(2, 9) #ajustar limits 

ggplot(summary_senia, aes(x = gap_size, y = RMSE_mean, color = as.factor(method))) +
  geom_line() +
  labs(title = "Summary Stats La Sènia",
       x = "Gap size",
       y = "Mean RMSE", 
       color = "Methodolody") +
  theme_bw() +
  ylim(2, 9) #ajustar limits 