knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(fpemdata)
library(fpemdata)
library(fpemmodeling)
library(ggplot2)
library(grid)
library(gridExtra)

Table of Contents

  1. Introduction
  2. Run models
  3. Plot model results

Introduction

In this vignette we will run two models for each country available, one for women in union and another for women not in union. We will use the package default data fpemdata::contraceptive_use for this example.

Make directories for model files

wd <- getwd()
wholedf <- fpemdata::contraceptive_use
wholedf <- wholedf %>% dplyr::filter(division_numeric_code %in% fpemdata::fp2020$division_numeric_code)
if(!dir.exists("output")) dir.create("output")
for(div in wholedf$division_numeric_code) {
  first_year = 1989,
  last_year = 2030
  run_y <- fpemmodeling::do_1country_run(
    is_in_union = "Y",
    surveydata_filepath = NULL,
    service_stats = FALSE,
    division_numeric_code = div,
    first_year = first_year,
    last_year = last_year
  )
  run_n <- fpemmodeling::do_1country_run(
    is_in_union = "N",
    surveydata_filepath = NULL,
    service_stats = FALSE,
    division_numeric_code = div,
    first_year = first_year,
    last_year = last_year
  )
  core_data <- run_y$core_data
  core_data$observations <-
    rbind(run_y$core_data$observations, run_n$core_data$observations)
  samples_all <-
    fpemmodeling::posterior_samples_all_women(
      in_union_posterior_samples = run_y$posterior_samples,
      not_in_union_posterior_samples = run_n$posterior_samples,
      core_data = core_data
    )
  population_counts <- fpemdata::population_counts %>%
    dplyr::filter(division_numeric_code == div)
  results_y <- fpemreporting::fpem_calculate_results(
    posterior_samples = run_y$posterior_samples,
    country_population_counts = population_counts %>%
      dplyr::filter(is_in_union == "Y"),
    first_year = first_year)
  )
  results_n <- fpemreporting::fpem_calculate_results(
    posterior_samples = run_n$posterior_samples,
    country_population_counts = population_counts %>%
      dplyr::filter(is_in_union == "N"),
    first_year = first_year)
  )
  results_all <- fpemreporting::fpem_calculate_results(
    posterior_samples = samples_all,
    country_population_counts = population_counts,
    first_year = first_year)
  )
  reslist <- list(results_y, results_n, results_all, core_data)
  saveRDS(reslist, file.path("output", paste0("reslist", div, ".rds")))
}

Plot

#loop through all countries for one type of union, get the item from the list
#here we loop through in union results
indicators <- c(
    "unmet_need_any",
    "contraceptive_use_modern",
    "contraceptive_use_traditional",
    "contraceptive_use_any"
    )

for(div in wholedf$division_numeric_code){
  reslist <- readRDS(file.path("output", paste0("reslist", div, ".rds")))
  for(k in 1:length(indicators)) {
    templs[[k]] <- fpem_plot_country_results(
      country_results = reslist$results_y,
      observations = core_data$observations,
      first_year = first_year,
      last_year = last_year,
      is_in_union = "Y",
      indicators = indicators
  }
  .pl[[i+j]] <- grid.arrange(grobs=templs[1:length(indicators)], top = textGrob("In-union women") ,ncol=2)
}


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