---
title: "Excluded province Plots"
author: "Laura Albrecht"
date: "8/27/2020"
output:
  html_document: default
  pdf_document: default
---
Using that GAM to predict the excluded province's outcome:
```{r echo = FALSE, message = FALSE}

source("ReadAllData1.R")
library(mgcv)
library(ggplot2)
#library(tidyverse)

```

```{r echo = FALSE, warning = FALSE, message = FALSE}
province_names <- unique(italy_25$province_name)
for (province in province_names) {
  new_data = subset(italy_25, province_name!=province)
  assign( paste(province,"_exclude_gam", sep = ""), gam(log(rt)~ 
                    s(home, k = 5)+s(pm25, k = 5)+s(med_temp, k = 4)+s(med_hum, k = 4) , data = new_data, family = gaussian))

 plot(get(paste(province, "_exclude_gam", sep = "")),scale=0,se=2, shade=TRUE,pages=1, scheme = 2, main = province)

}
```

```{r}
province_data = subset(italy_25, province_name=="Roma")
i = 0
pred_mat <- matrix(NA, length(province_data$rt), length(province_names))
true_mat <- matrix(NA, length(province_data$rt), length(province_names))

pred_cases <- rep(NA, length(province_data$rt))
mse <- rep(NA, length(province_data$rt))
for (province in province_names) {
  i = i + 1
  province_data = subset(italy_25, province_name==province)
  province_gam <- get(paste(province, "_exclude_gam", sep = ""))
  pred_cases <- predict(province_gam,province_data, se.fit = TRUE)
  mse[i] <- sum((exp(pred_cases$fit) - province_data$rt)^2, na.rm = T)/length(province_data$rt)
  #mse2[i] <- pred_cases$se.fit
 #plot(y=exp(pred_cases$fit), x=province_data$rt, pch = 16, xlab = "True", ylab = "Predicted",main = province)
  df <- data.frame("Predicted" = exp(pred_cases$fit), "True" = province_data$rt, "Date" =province_data$date)
  #df_long <- pivot_longer(df, 1:2, names_to = "Method", values_to = "R_e")
  

plot_province <- suppressWarnings( ggplot(df) + geom_point(aes(x = True, y = Predicted))+ geom_abline(slope =1, intercept = 0, color = "red") + theme_classic()+ggtitle(label = province))


print(plot_province)
}
```
