####################################################################################################
#
# DYNAMIS-POP Parameter Generation File 2 - Starting Population
# This file is generic and works for all country contexts.
# Input file: globals_for_analysis.RData (To generate such a file run the setup script)
# Last Update: Martin Spielauer 2018-06-29
#
####################################################################################################
####################################################################################################
# Clear work space, load required packages and the input object file
####################################################################################################
rm(list=ls())
library(haven)
library(dplyr)
library(data.table)
library(sp)
library(maptools)
library(survival)
library(fmsb)
library(eha)
load(file="globals_for_analysis.RData")
####################################################################################################
# Generate variables for the starting population file
####################################################################################################
pmc_weight <- g_residents_dat$M_WEIGHT
pmc_birth <- g_census_year - g_residents_dat$M_AGE
pmc_sex <- g_residents_dat$M_MALE
pmc_province <- g_residents_dat$M_DOR
pmc_educ <- g_residents_dat$M_EDUC
pmc_pob <- g_residents_dat$M_DOB
pmc_union <- rep(9999,length(pmc_weight))
pmc_union[!is.na(g_residents_dat$M_AGEMAR)] <- pmc_birth[!is.na(g_residents_dat$M_AGEMAR)] +
g_residents_dat$M_AGEMAR[!is.na(g_residents_dat$M_AGEMAR)]
pmc_parity <- rep(0,length(pmc_weight))
pmc_parity[!is.na(g_residents_dat$M_PARITY)] <- g_residents_dat$M_PARITY[!is.na(g_residents_dat$M_PARITY)]
pmc_lastbir <- rep(9999,length(pmc_weight))
pmc_lastbir[!is.na(g_residents_dat$M_AGEBIR)] <- g_residents_dat$M_AGEBIR[!is.na(g_residents_dat$M_AGEBIR)] +
pmc_birth[!is.na(g_residents_dat$M_AGEBIR)]
pmc_group <- g_residents_dat$M_ETHNO
####################################################################################################
# Generate and write the starting population file
####################################################################################################
cat("WEIGHT, BIRTH, SEX, DISTRICT, EDUC, DISTBIRTH, UNION, PARITY, LASTBIR, ETHNO\n", file=g_startpop_file)
startpop_file <- cbind(pmc_weight, pmc_birth, pmc_sex, pmc_province, pmc_educ, pmc_pob, pmc_union, pmc_parity, pmc_lastbir, pmc_group)
write.table(startpop_file, file = g_startpop_file,sep=", ",col.names=F, row.names=F , append=TRUE)
####################################################################################################
# Calculate model parameters
####################################################################################################
n_popsize <- sum(pmc_weight)
n_filesize <- length(pmc_weight)
####################################################################################################
# Write parameter file for starting population parameters
####################################################################################################
parafile <- file(g_para_startpop, "w")
cat("parameters\n{\n\n", file=parafile)
cat(" long MicroDataInputFileSize = { ",n_filesize,"}; //EN File size of starting population\n", file=parafile, append=TRUE)
# removed cat(" logical UseWeightsInFile = TRUE; //EN Use weights in starting population file\n", file=parafile, append=TRUE)
cat(" double StartPopSampleSize = 100000; //EN Sample size of starting population\n", file=parafile, append=TRUE)
# removed cat(" logical ScalePopulation = TRUE; //EN Scale population\n", file=parafile, append=TRUE)
cat(" logical WriteMicrodata = FALSE; //EN Write micro-data output file Y/N\n", file=parafile, append=TRUE)
cat(" file MicroDataInputFile = \"",g_startpop_name,"\"; //EN File name of starting population\n", sep = "", file=parafile, append=TRUE)
cat(" double StartPopSize = {",n_popsize,"}; //EN Total Population Size\n",file=parafile, append=TRUE)
cat(" double TimeMicroOutput[OUTPUT_TIMES] = { 9999, 9999, 0, }; //EN Time(s) of micro-data output\n",file=parafile, append=TRUE)
cat(" file MicroRecordFileName = \"MicroDataOutput.csv\"; //EN File name micro-data output file\n",file=parafile, append=TRUE)
# removed cat(" logical SelectedOutput[OUTPUT_VARIABLES] = { FALSE, (10) TRUE, }; //EN Output variable selection \n",file=parafile, append=TRUE)
cat("\n};", file=parafile, append=TRUE)
close(parafile)