# Script 10: Ethnicity > Parameters are calculated for the transmission of the ethnicity variable from mother to child and the ethnicity distribution of immigrants by age and sex. ## File output The code below generates 2 model parameters stored in a Modgen .dat file - Ethnic transmission from mother to child by sex of child and mothers ethnicity - Ethnicity distribution of immigrants by sex and desdination district ## Code ```{r, message=FALSE, warning=FALSE} #################################################################################################### # # DYNAMIS-POP Parameter Generation File 10 - Ethnicity # 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") #################################################################################################### # PARAMETER 1 : TRANSMISSION #################################################################################################### dat <- as.data.table(g_residents_dat) # Set Parameter Output File parafile <- file(g_para_ethnicity, "w") # remove irrelevant records dat$i_age <- as.integer(dat$M_AGE) dat <- dat[!(dat$M_MALE==1 & dat$i_age>0),] dat <- dat[!(dat$M_BIR12==0 & dat$i_age>0),] dat$M_HHID <- as.character(dat$M_HHID) # only two with same hh id dat[,hhsize:=.N,by=M_HHID] dat <- dat[dat$hhsize==2] dat$is_mother <- FALSE dat$is_mother[dat$i_age > 0] <- TRUE dat[,ethnomo:=M_ETHNO[which(is_mother==TRUE)],by=M_HHID] dat <- dat[dat$i_age==0,] dat <- dat[!is.na(ethnomo)] #exclude hh of only to babies # Keep only what's needed dat <- dat[,c("M_WEIGHT", "M_MALE", "M_ETHNO", "ethnomo" )] #################################################################################################### # Calculate the parameter cumrate EthnicTransmission[ETHNICITY][ETHNICITY] #################################################################################################### popmig <- dat popmig$M_WEIGHT <- as.double(popmig$M_WEIGHT) popmig$M_MALE <- as.integer(popmig$M_MALE) popmig$M_ETHNO <- as.integer(popmig$M_ETHNO) popmig$ethnomo <- as.integer(popmig$ethnomo) popmig <- xtabs(popmig$M_WEIGHT ~ popmig$M_MALE + popmig$ethnomo + popmig$M_ETHNO) popmig <- as.data.frame(popmig) popmig$groupsum <- ave(popmig$Freq, popmig$popmig.M_MALE, popmig$popmig.ethnomo,FUN=sum) popmig$paravalue <- popmig$Freq / popmig$groupsum popmig <- popmig[order(popmig$popmig.M_MALE, popmig$popmig.ethnomo),] #################################################################################################### # Write the parameter EthnicTransmission[ETHNICITY][ETHNICITY] #################################################################################################### cat("parameters { \n //EN Transmission of ethnicity\ncumrate EthnicTransmission[SEX][ETHNICITY][ETHNICITY] = {\n", file=parafile) cat(format(round(popmig$paravalue,10),scientific=FALSE), file=parafile, sep=", ", append=TRUE) cat("\n}; \n\n", file=parafile, append=TRUE) #################################################################################################### # PARAMETER 2 : IMMIGRANTS #################################################################################################### dat <- as.data.table(g_residents_dat) n_abroad = max(dat$M_DOB) # Add an integer variable for age a year ago dat$m_ageago <- as.integer(dat$M_AGE)-1 dat$M_ETHNO <- as.integer(dat$M_ETHNO) dat$M_DOR <- as.integer(dat$M_DOR) # remove those not born a year ago dat <- dat[!(dat$m_ageago<0),] # Remove those in the country 12 months ago (previous district was <75) dat <- dat[dat$M_PDIST==n_abroad,] #################################################################################################### # Create and append a dataset of all possible immigrations for each age # This is to avoid empty cells in matrices # The records have very low weights which do not affect overall immigration #################################################################################################### umale <- unique(dat$M_MALE) udist <- 0:(n_abroad-1) uethno <- unique(dat$M_ETHNO) # Keep only what's needed dat <- dat[,c("M_WEIGHT", "M_MALE", "M_DOR", "M_ETHNO" )] allmigs <- expand.grid(M_MALE=umale, M_DOR=udist, M_ETHNO=uethno) allmigs$M_WEIGHT <- 0.0000001 dat <- rbind(dat, allmigs) #################################################################################################### # Calculate the parameter EthnicityImmigrants[SEX][AGE5_PART][DISTRICT_NAT][ETHNICITY] #################################################################################################### dat$M_WEIGHT <- as.numeric(dat$M_WEIGHT) immidest <- as.data.frame(xtabs(dat$M_WEIGHT ~ dat$M_MALE + dat$M_DOR + dat$M_ETHNO)) immidest$groupsum <- ave(immidest$Freq, immidest$dat.M_MALE, immidest$dat.M_DOR, FUN=sum) immidest$paravalue <- immidest$Freq / immidest$groupsum immidest <- immidest[order(immidest$dat.M_MALE, immidest$dat.M_DOR, immidest$dat.M_ETHNO),] #################################################################################################### # Write the parameter EthnicTransmission[ETHNICITY][ETHNICITY] #################################################################################################### cat("\n\n//EN Ethnicity of immigrants\ncumrate EthnicityImmigrants[SEX][DISTRICT_NAT][ETHNICITY] = {\n", file=parafile, append=TRUE) cat(format(round(immidest$paravalue,10),scientific=FALSE), file=parafile, sep=", ", append=TRUE) cat("\n }; \n};\n", file=parafile, append=TRUE) close(parafile) ```