###R code to run a Monte Carlo of possible future deaths within population groups (age-sex). 

#####Packages
library(tidyverse)
source("Scripts/Mortality_Rate_Over_Time_Simulation_Function.r") #Load the mortality rate simulation functions. Note that this will clean the mortality rate data if already missing, by sourcing the data clean script
source("Scripts/Death_Simulation_Functions.r") #Load the death across population groups simulation functions. 
MORTALITY_RATE_SIMULATION(1000000,50,RERUN=TRUE) #Run a simulation of future mortality rates, this informs the Monte Carlo of actual deaths, given a future year, and set of demographics.

Mortality_Rate_Sim <- readRDS("./Data/Simulated_Data_Sets/MORTALITY_MONTE_CARLO.Rds") #Load the Mortality simulation to speed up simulation
LIN_CURRENT_DEM <- readRDS("Data/Cleaned_Data/Lincoln_Demographic_Data.Rds") %>% group_by(County) %>% filter(Year==max(Year)) %>% ungroup %>% select(-County)
#Run the full simulation in the current year (1), across all simulations x, passing in the demographic, and mortality data.
TEST <- mclapply(1:10^4,function(x){MORTALITY_SIM(1,x,LIN_CURRENT_DEM,FALSE,Mortality_Rate_Sim )},mc.cores = detectCores()-1)
