# Script 6: Emigration > Emigration is modeled calculating probabilities by age, sex, and location from the census and a corresponding file of household members who left the country in the past year. ## File output The code below generates 2 model parameters stored in a Modgen .dat file - Emigration rates by age, sex and district - Switching module on/off ![*Figure: Emigration Parameters*](Figures/ParaEmigration.png) ## Code ### Emigration by Region ```{r, message=FALSE, warning=FALSE} ################################################################################################### # # DYNAMIS-POP Parameter Generation File 6 - Chunk A - Emigration by region # 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) library(rgdal) load(file="globals_for_analysis.RData") dat_res <- g_residents_dat dat_res <- dat_res[,c("M_WEIGHT", "M_AGE", "M_MALE", "M_DOR", "M_PDIST", "M_PREG", "M_ROR")] dat_emi <- g_emigrants_dat dat_emi <- dat_emi[,c("M_WEIGHT", "M_AGE", "M_MALE", "M_DOR", "M_PDIST", "M_PREG", "M_ROR")] dat <- rbind(dat_emi,dat_res) # Remove objects not needed anymore rm(dat_emi) rm(dat_res) # Set Parameter Output File parafile <- file(g_para_emigration, "w") # constant n_abroad = max(dat$M_DOR) # Add an integer variable for age a year ago dat$m_ageago <- as.integer(dat$M_AGE)-1 # remove those not born a year ago dat <- dat[!(dat$m_ageago<0),] # Age groups 5 years ago, up to 60+ dat$m_agegr5 <- as.integer(dat$m_ageago/5) * 5 dat$m_agegr5[dat$m_agegr5>60] <- 60 # Remove those not in the country 12 months ago dat <- dat[dat$M_PDIST=20 & dat$m_agegr5<=35, ] popall <- xtabs(dat_male2035$M_WEIGHT ~ dat_male2035$M_PDIST) migs <- dat_male2035[dat_male2035$is_migrant==TRUE,] migs <- migs[,c("M_WEIGHT", "M_MALE","m_agegr5" ,"M_PDIST")] ################################################################################################### # Create and append a dataset of all possible migrations for each age group. This is to avoid empty # cells in origin-destination matrices. The records have very low weights which do not affect # overall migration ################################################################################################### umale <- unique(dat$M_MALE) uage <- sort(unique(dat$m_agegr5)) updist <- sort(unique(dat$M_PDIST)) allmigs <- expand.grid(M_MALE=umale, m_agegr5=uage, M_PDIST=updist) allmigs$M_WEIGHT <- 0.0000001 migs <- rbind(migs, allmigs) popmig <- xtabs(migs$M_WEIGHT ~ migs$M_PDIST) propmig <- as.data.frame(popmig/popall) propmig <- propmig[order(propmig$migs.M_PDIST),] country_shape <- rgdal::readOGR(g_shapes) ## Census districts as coded in Census in the order of districts in the Nepal shape object (ID_3) country_shape$censusdist <- g_shape_display_order ################################################################################################### # Function taking a SpatialPolygonsDataFrame and adding a vector of values to be displayed on map # The values come in the order of provinces as coded in the census and are re-ordered to fit the # Nepal shape object ################################################################################################### AddVectorForDisplay <- function(country_shape,VectorForDisplay) { testb <- data.frame(cbind(new_order=country_shape$censusdist,orig_order=c(0:max(g_shape_display_order)))) testb <- testb[order(testb$new_order),] testb$newcol <- VectorForDisplay testb <- testb[order(testb$orig_order),] country_shape$graphval <- testb$newcol return(country_shape) } ################################################################################################### # Plot data ################################################################################################### spplot(AddVectorForDisplay(country_shape,propmig$Freq),c("graphval"), main = "Emigration Probabilities\nMale, age 20-34" ) ``` ![](Figures/04_03.png)