4.1. Sricp 1: Setting up Output Data Objects¶
4.1.1. Version: “Imaginary Country” Start Year 2000¶
This setup file generates all country-specific objects used by the set of R simulation output analysis and visualization scripts. The output of this script is an image file “globals_for_analysis.RData” which is read by each of the thematic analysis scripts. This is the only R script which has to be adapted when porting the model to another country context.
Files in ../../Model/OutputScenarios/
- Output_ABC_2000_base.csv
- Output_ABC_2000_alt.csv
- Tables_ABC_2000_base.xlsx
- Tables_ABC_2000_alt.xlsx
####################################################################################################
#
# DYNAMIS-POP Output Analysis File 1 - Generate all country-specific R objects
# This file has to be run before performing all other output analysis steps
# Last Update: Martin Spielauer 2019-04-14
#
####################################################################################################
####################################################################################################
# Clear work space and load required packages
####################################################################################################
rm(list=ls())
library("dplyr")
library("tidyr")
library("ggplot2")
library("ggthemes")
library("cowplot")
library("sf")
library("gdalUtils")
library("tmap")
library("readxl")
####################################################################################################
# Maps
####################################################################################################
f_shape <- st_read('../Data/ABC_2000/Shapes/shape_abc.shp')
dynxls_base <- '../../Model/OutputScenarios/Tables_ABC_2000_base.xlsx'
dynxls_alt <- '../../Model/OutputScenarios/Tables_ABC_2000_alt.xlsx'
f_4years <- c("2000", "2010", "2020", "2030")
dyn_base <- read_xlsx(dynxls_base)
sheet_names_base <- excel_sheets(dynxls_base)
df_sheets_base <- lapply(sheet_names_base, function(.sheet){read_xlsx(dynxls_base, .sheet)})
# We read all sheets except the first three, which only contain meta information
for (n in 4:length(df_sheets_base)) {
s <- data.frame(df_sheets_base[n])
s[1] <- NULL # The first column contains no data; we drop it
assign(paste0(sheet_names_base[n],'_base'), s)
}
dyn_alt <- read_xlsx(dynxls_alt)
sheet_names_alt <- excel_sheets(dynxls_alt)
df_sheets_alt <- lapply(sheet_names_alt, function(.sheet){read_xlsx(dynxls_alt, .sheet)})
# We read all sheets except the first three, which only contain meta information
for (n in 4:length(df_sheets_alt)) {
s <- data.frame(df_sheets_alt[n])
s[1] <- NULL # The first column contains no data; we drop it
assign(paste0(sheet_names_alt[n],'_alt'), s)
}
# District labels
district_labels <- variable.names(TabPrimSchoolOutOfSchool9to11_base)
district_labels <- district_labels[3:length(district_labels)-1]
f_shape$DISTRICT <- district_labels
tmap_options_reset()
bgc <- "white" # Background color for plot maps
# Suggestions: lightblue, grey85, white
credit <- "World Bank, DYNAMIS-POP v2.0" # Text for credit
p_cred <- "left" # Position of credit
titl_sz <- 0.8 # Title size
savemap <- "Y" # Maps to be saved as images? Y/N
sav_loc <- "Figures/" # Location of saved maps (leave blank if = default folder)
sav_fmt <- "jpg" # File format for saved maps. Options: pdf, eps, svg,
# wmf (Windows only), png, jpg, bmp, or tiff
####################################################################################################
# Read Simulation Output Files and generate data objects
####################################################################################################
g_data_base <- read.csv("../../Model/OutputScenarios/Output_ABC_2000_base.csv")
g_data_alt <- read.csv("../../Model/OutputScenarios/Output_ABC_2000_alt.csv")
g_data_base$g_scenario <- 'base'
g_data_alt$g_scenario <- 'alt'
g_data <- rbind(g_data_base,g_data_alt)
g_data$m_agegr5 <- as.integer((g_data$TIME - g_data$BIRTH)/5) * 5
####################################################################################################
# Generate a 5y age pyramid by education data frame
####################################################################################################
g_pyramid <- as.data.frame(xtabs(g_data$WEIGHT ~ g_data$MALE +
g_data$m_agegr5 + g_data$EDUCATION + g_data$TIME +
g_data$DISTRICT + g_data$g_scenario))
g_pyramid <- g_pyramid[!(g_pyramid$g_data.m_agegr5==100),]
g_pyramid$gender[g_pyramid$g_data.MALE==0] <- 'female'
g_pyramid$gender[g_pyramid$g_data.MALE==1] <- 'male'
g_pyramid$educ[g_pyramid$g_data.EDUCATION==0] <- '1-non '
g_pyramid$educ[g_pyramid$g_data.EDUCATION==1] <- '2-incomplete'
g_pyramid$educ[g_pyramid$g_data.EDUCATION==2] <- '3-completed'
g_pyramid$age[g_pyramid$g_data.m_agegr5==0] <- ' 0-4'
g_pyramid$age[g_pyramid$g_data.m_agegr5==5] <- ' 5-9'
g_pyramid$age[g_pyramid$g_data.m_agegr5==10] <- '10-14'
g_pyramid$age[g_pyramid$g_data.m_agegr5==15] <- '15-19'
g_pyramid$age[g_pyramid$g_data.m_agegr5==20] <- '20-24'
g_pyramid$age[g_pyramid$g_data.m_agegr5==25] <- '25-29'
g_pyramid$age[g_pyramid$g_data.m_agegr5==30] <- '30-34'
g_pyramid$age[g_pyramid$g_data.m_agegr5==35] <- '35-39'
g_pyramid$age[g_pyramid$g_data.m_agegr5==40] <- '40-44'
g_pyramid$age[g_pyramid$g_data.m_agegr5==45] <- '45-49'
g_pyramid$age[g_pyramid$g_data.m_agegr5==50] <- '50-54'
g_pyramid$age[g_pyramid$g_data.m_agegr5==55] <- '55-59'
g_pyramid$age[g_pyramid$g_data.m_agegr5==60] <- '60-64'
g_pyramid$age[g_pyramid$g_data.m_agegr5==65] <- '65-69'
g_pyramid$age[g_pyramid$g_data.m_agegr5==70] <- '70-74'
g_pyramid$age[g_pyramid$g_data.m_agegr5==75] <- '75-79'
g_pyramid$age[g_pyramid$g_data.m_agegr5==80] <- '80-84'
g_pyramid$age[g_pyramid$g_data.m_agegr5==85] <- '85-89'
g_pyramid$age[g_pyramid$g_data.m_agegr5==90] <- '90-94'
g_pyramid$age[g_pyramid$g_data.m_agegr5==95] <- '95+'
for (nY in 1:length(district_labels))
{
g_pyramid$sDistrict[g_pyramid$g_data.DISTRICT==nY-1] <- district_labels[nY]
}
####################################################################################################
# Save global objects
####################################################################################################
save.image(file="globals_for_output_analysis.RData")