#Uncomment to check the function line by line
#	REG_BIRTH_MODEL=MOD_BIRTHS;START_BASIC_DATA=FIRST_PREDICT_YEAR_POPULATION_DATA;START_DETAILED_DATA=START_DEM_DATA;Mortality_Rate_Sim=MORTALITY_SIMULATION;SIM_NUMBER=1;START_OF_SIM_YEAR=2023;Migration_ARIMA_Sim=MIGRATION_ARIMA_SIMULATION;Migration_Age_Distribution=MIG_AGE_DIST

RUN_SINGLE_SIM <- function(REG_BIRTH_MODEL,START_BASIC_DATA,START_DETAILED_DATA,Mortality_Rate_Sim,SIM_NUMBER,START_OF_SIM_YEAR=2023,Migration_ARIMA_Sim,Migration_Age_Distribution ){
	#REG_BIRTH_MODEL: Feols regression object of population model.
	#START_BASIC_DATA: A single row of data, with information for the birth regression (Male_Birth_Group,PREV_BIRTH etc.)
	#START_DETAILED_DATA: A data set, with the number of men and women at each individual age (zero to 85+)
	#Mortality_Rate_Sim: A list object with a set of project future mortality rates by age. See ./Scripts/Mortality_Rate_Over_Time_Simulation_Function.r. By passing this in the simulation speed is increased significantly.
	#SIM_NUMBER: The current Monte Carlo simulation being applied. This extracts the correct index of Mortality_Rate_Sim Object for the present simulation.
	#START_OF_SIM_YEAR: This is the first year of data which requires a simulation. This allows for the time trend to be properly estimated as this depends on the number of years since the forecast began
	#The simulates paths of of net migration using an ARIMA model, from the start year until the end year.
	#Migration_Age_Distribution: A vector which has entries from age 1 to 90 of the assessed probability of migrating in a single year, for each net migrant in the county.
		NEXT_BASIC_DATA <- START_BASIC_DATA #Create a data set for the data to feed into the next run.
		C_BIRTHS <- BIRTH_SIM(REG_BIRTH_MODEL,START_BASIC_DATA)
		NEXT_BASIC_DATA[,"PREV_TWO_BIRTH"] <- START_BASIC_DATA[,"PREV_BIRTH"]
		NEXT_BASIC_DATA[,"PREV_BIRTH"] <- sum(C_BIRTHS[,3:4])
		NEXT_BASIC_DATA[,"Year"] <- NEXT_BASIC_DATA[,"Year"]+1
		#Update the initial data to include the projected births
		START_BASIC_DATA[,"Births"] <- sum(C_BIRTHS[,3:4])

	#Increment the ages of the provided demographic data so that all ages increase by one. Add in the new births as Age zero. This is done so the distribution for deaths makes sense, having new births in the zero age and implying all other ages increase by one year.
	START_DETAILED_DATA[,"Age"] <- START_DETAILED_DATA[,"Age"]+1
	START_DETAILED_DATA[START_DETAILED_DATA$Age==85,c("Num_Male","Num_Female")] <- START_DETAILED_DATA[START_DETAILED_DATA$Age==86,c("Num_Male","Num_Female")]+START_DETAILED_DATA[START_DETAILED_DATA$Age==85,c("Num_Male","Num_Female")] #Sum the 85 and 86 ages into one row for age 85
	START_DETAILED_DATA <- START_DETAILED_DATA[START_DETAILED_DATA$Age!=86,] #Anyone older than 85 is lumped into one group remove the 86 group
	START_DETAILED_DATA <- rbind(C_BIRTHS,START_DETAILED_DATA) #Add the new simulated births
	#Run a preliminary Monte Carlo which estimates the future mortality rate, for each simulation and year of of Monte Carlo Simulation

		YEARS_AHEAD <- max(START_BASIC_DATA[,'Year']-START_OF_SIM_YEAR,1) #Define the number of years forward from the simulation start based on the current year of analysis, and the user provided first year.
	TOTAL_MIGRATION <- Migration_ARIMA_Sim[YEARS_AHEAD,SIM_NUMBER]
	START_BASIC_DATA[,"Migration"] <- TOTAL_MIGRATION
	MIG <- MIGRATION_SIMULATION(Migration_Age_Distribution,START_DETAILED_DATA,TOTAL_MIGRATION)
	MORTALITY_INPUT_DETAILED_DATA <- START_DETAILED_DATA
	MORTALITY_INPUT_DETAILED_DATA[-1,c("Num_Male","Num_Female")] <- MORTALITY_INPUT_DETAILED_DATA[-1,c("Num_Male","Num_Female")]+MIG[,c("Num_Male","Num_Female")]
	MORTALITY_C_RES <- MORTALITY_SIM(YEARS_AHEAD,SIM_NUMBER,MORTALITY_INPUT_DETAILED_DATA,FALSE,Mortality_Rate_Sim )
	#Update number of deaths in the current run (which should be blank when supplied to the function)
        START_BASIC_DATA[,"Deaths"] <- sum(MORTALITY_C_RES[,c("Male_Deaths","Female_Deaths")] )
	#
	NEXT_DETAILED_DATA  <- MORTALITY_C_RES[1:4]
	NEXT_DETAILED_DATA[,"Num_Male"] <- MORTALITY_C_RES[,"Num_Male"]-MORTALITY_C_RES[,"Male_Deaths"]
	NEXT_DETAILED_DATA[,"Num_Female"] <- MORTALITY_C_RES[,"Num_Female"]-MORTALITY_C_RES[,"Female_Deaths"]
	NEXT_DETAILED_DATA[,"Year"] <- NEXT_DETAILED_DATA[,"Year"] +1
	####
	NEXT_BASIC_DATA[,"Population"] <- sum(NEXT_DETAILED_DATA[,3:4])
	NEXT_BASIC_DATA[,"Male_Birth_Group"] <-  sum(NEXT_DETAILED_DATA[NEXT_DETAILED_DATA$Age>=18 & NEXT_DETAILED_DATA$Age<=30,"Num_Male"])
	NEXT_BASIC_DATA[,"Female_Birth_Group"] <-  sum(NEXT_DETAILED_DATA[NEXT_DETAILED_DATA$Age>=18 & NEXT_DETAILED_DATA$Age<=28,"Num_Female"])
	NEXT_BASIC_DATA[,"Min_Birth_Group"] <- min(NEXT_BASIC_DATA[,c("Female_Birth_Group","Male_Birth_Group")])
	NEXT_BASIC_DATA <-	NEXT_BASIC_DATA[,-10:-11]
	return(list(START_BASIC_DATA,NEXT_DETAILED_DATA,NEXT_BASIC_DATA))
}
