knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(fig.width=10, fig.height=7) 
  library(fpemdata)
  library(fpemmodeling)
  library(fpemreporting)
  library(ggplot2)
  library(grid)
  library(gridExtra)
  library(rjags)
  library(R2jags)

Table of Contents

  1. Introduction
  2. Settings
  3. Run models
  4. Combine posterior samples arrays
  5. Aggregation
  6. Post-process
  7. Plot

Introduction

In this vignette we will run the country specific model for women in union in the countries in Central Asia and aggregate the results. We will use the package default data fpemdata::contraceptive_use for this example. Alternatively, you can supply a survey file path to fpemmodeling::do_1country_run and run your own data.

options(warn=-1)

Choose countries to run

## Example codes for countries in Central Asia: Kazakhstan, Kyrgyzstan, Tajikistan, Turkmenistan, Uzbekistan respectively   
codes <- c(398,417,762,795,860)

Run one-country, in-union model, for each country in Central Asia

for(code in codes)
{
  post_samps <- fpemmodeling::do_1country_run(
    is_in_union = "Y",
    surveydata_filepath = NULL,
    division_numeric_code = code,
    first_year = 1989,
    last_year = 2030
  )$posterior_samples
  save(post_samps, file = paste0("post_samps_", code, ".rda"))
}

Combine posterior samples arrays into a single array

## COMBINE RUNS
post_samps_combine <- fpemreporting::combine_runs(codes = codes)

Aggregation

## Get population data
population_data <- fpemdata::population_counts %>%
 dplyr::filter(is_in_union == "Y") %>%
   dplyr::filter(mid_year <= 2030) %>%
   dplyr::filter(mid_year >= 1989) %>% 
   dplyr::filter(division_numeric_code %in% codes) %>% 
   dplyr::group_by(division_numeric_code,mid_year) %>%
   dplyr::top_n(1) %>% # years are doubled for some reason so this choses the correct ones
   dplyr::ungroup()

## Get divisions data
division_level_data <- fpemdata::divisions %>%
   dplyr::mutate(division_level = region_numeric_code)%>%
   dplyr::select(division_numeric_code, division_level) %>% 
   dplyr::filter(division_numeric_code %in% codes)

## Create the array with the weigthed posterior samples i.e., aggregate
# undebug(weight_samples)
# # debug(fpemreporting:::weight_division_match)
# debug(weight_generator)
posterior_samples_list <- fpemreporting::weight_samples(division_level_data, 
                                         population_data, 
                                         posterior_samples =  post_samps_combine)
## Pull out the aggregate samples, in this case we aggregated for a sinlge region
samps_central_asia <- posterior_samples_list$`935`

Post-process, get point estimates from weighted samples

## create results data 
results_central_asia <- fpemreporting::fpem_calculate_results(
  posterior_samples = samps_central_asia,
  first_year = 1989,
  country_population_counts = population_data)

Plot results for aggregate

indicators <- fpemreporting:::indicator_names()
plots <- fpemreporting::fpem_plot_country_results(
  country_results = results_central_asia,
  first_year = 1989,
  last_year = 2030,
  is_in_union = "Y",
  indicators = indicators
  )
gridExtra::grid.arrange(grobs=plots[1:length(indicators)],
                 ncol=2,
                 top=textGrob("In-union women"))  
#remove sample files created for this vignettes
file.remove(paste0("post_samps_", codes, ".rda"))


FPRgroup/FPEM documentation built on March 3, 2020, 8:19 a.m.