Skip to content

Instantly share code, notes, and snippets.

@jeffeaton
Created February 6, 2020 06:33
Show Gist options
  • Save jeffeaton/c0d66467ae50f4b2d19222c93b75653b to your computer and use it in GitHub Desktop.
Save jeffeaton/c0d66467ae50f4b2d19222c93b75653b to your computer and use it in GitHub Desktop.
Function to export proportion aware of status by age and sex from .shiny90 output file
#' Export proportion aware by five year age group from Shiny90
#'
#' Export estimates for proportion aware of status from a Shiny90
#' output file to five-year age groups 15-19 to 50+.
#'
#' @param shiny90_path file path to .shiny90 digest file.
#' @param out_path output path to save CSV of proportion aware.
#' @param year year(s) to generate estimates; an integer or a vector of integers.
#'
#' @return
#' Path name to saved CSV file (`out_path`). As a side effect the function
#' saves a CSV file to this location.
#'
#' @details
#' This function requires the 'first90' package from
#' https://github.com/mrc-ide/first90release. Install this via:
#'
#' `devtools::install_github("mrc-ide/first90release")`
#'
#' @examples
#'
#' library(first90)
#'
#' path <- "~/Downloads/Mozambique.zip.shiny90"
#' out_path <- "~/Downloads/moz_shiny90_datapack.csv"
#'
#' shiny90_datapack_age_sex(path, out_path, year = 2019)
#'
shiny90_datapack_age_sex <- function(shiny90_path, out_path, year = 2019) {
exdir <- tempfile()
unzip(path, exdir = exdir)
name <- readLines(file.path(exdir, "country.txt"))[1]
spec <- lapply(list.files(file.path(exdir, "spectrum_data"), "rds$", full.names = TRUE), readRDS)
spec <- lapply(spec, "[[", "data")
fp <- first90::prepare_inputs_from_extracts(spec)
fp$popadjust <- FALSE
par <- readRDS(file.path(exdir, "model_outputs/par.rds"))
fpsim <- first90::create_hts_param(par, fp)
mod <- first90::simmod(fpsim)
age_groups <- c("15-19", "20-24", "25-29", "30-34", "35-39", "40-44", "45-49", "50-99")
diagno <- expand.grid(area = name,
year = year,
sex = c("female", "male"),
agegr = age_groups,
hivstatus = 'positive',
stringsAsFactors = FALSE)
diagno_ss <- first90::add_ss_indices(diagno, fp$ss)
diagno$proportion_aware <- first90::diagnosed(mod, fp, diagno_ss)
diagno$agegr[diagno$agegr == "50-99"] <- "50+"
write.csv(diagno, out_path, row.names = FALSE, na = "")
out_path
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment