Skip to content

Instantly share code, notes, and snippets.

@jeffeaton
jeffeaton / phia-cd4-by-cascade_2024-05-02.R
Created May 3, 2024 05:31
PHIA survey analysis of CD4 <200 by care cascade stage
library(dplyr)
library(readr)
library(tidyr)
library(forcats)
library(haven)
library(survey)
phia_path <- "~/Data/household surveys/PHIA/datasets"
cmr2017phia <- rdhs::read_zipdata(file.path(phia_path, "CMR/datasets/302 CAMPHIA 2017-2018 Adult Biomarker Dataset (DTA).zip"))
@jeffeaton
jeffeaton / phia-incidence-extract_2024-04-25.R
Created May 1, 2024 13:12
PHIA HIV incidence survey extract for UNAIDS Reference Group incidence analysis
library(dplyr)
library(readr)
library(tidyr)
library(forcats)
library(haven)
library(survey)
## devtools::install_github("jeffeaton/svyincid")
library(svyincid)
@jeffeaton
jeffeaton / ahd-first90-sim_2024-04-27.R
Created April 28, 2024 20:59
Extract Shiny90 output and aggregate for cryptococcal meningitis CD4 <200 inputs
library(dplyr)
library(brio)
library(readr)
library(first90) # remotes::install_github("mrc-ide/first90release")
#' ## Define functions
get_pjnz_shiny90_filename <- function (pjnz) {
files <- utils::unzip(pjnz, list = TRUE)$Name
@jeffeaton
jeffeaton / pjnz-frr-input-update.R
Last active October 7, 2022 09:37
Function to update FRR fitting input in Spectrum PJNZ file
## Requires library eppasm
dpsub <- function(dp, tag, rows, cols, tagcol = 1) {
dp[which(dp[, tagcol] == tag) + rows, cols]
}
read_frr_fit_input <- function(pjnz) {
dp <- eppasm::read_dp(pjnz)
@jeffeaton
jeffeaton / epp-xml-anc-update.R
Last active December 16, 2022 05:04
Function to update ANC data in EPP XML file within Spectrum PJNZ
library(xml2)
library(epp)
eppd_overwrite_matrix <- function (xm, mat) {
if (!xml_attr(xm, "class") %in% c("[D", "[I"))
stop("Tried to invoke .parse_matrix() on array node not of class '[D' or '[I'.")
m_rows <- as.integer(xml_attr(xm, "length"))
stopifnot(nrow(mat) == m_rows)
@jeffeaton
jeffeaton / adjust_datapack_export2021.R
Last active March 8, 2021 09:26
Adjust PEPFAR Data Pack export for 2021 specification
#' This function saves an updated data pack export CSV in the same directory as
#' the Naomi output package file
#'
#' Example:
#'
#' file <- "~/Downloads/MWI_20210308-090536_naomi_spectrum_digest.zip" ## !! REPLACE HERE
#' adjust_datapack_export2021(file)
#'
adjust_datapack_export2021 <- function(naomi_output_package) {
@jeffeaton
jeffeaton / malawi-census-2018-district-population-projections.R
Last active December 8, 2020 09:50
Malawi Census 2018 District population projections (2018-2043)
library(dplyr)
library(pdftools)
library(readr)
library(stringr)
library(tidyr)
url <- "http://www.nsomalawi.mw/images/stories/data_on_line/demography/census_2018/Thematic_Reports/Population%20Projections%202018-2050.pdf"
file <- tempfile(fileext = ".pdf")
download.file(url, file)
#' This script is an example of the function subset_output_package() to load,
#' subset, and resave the Naomi output package.
#'
#' The arguments to the function are:
#'
#' * path: file path to the output package.
#' * output_path: file path to save the new subsetted package (end in .zip).
#' * area_id: vector of area_ids to keep or drop.
#' * area_level: vector of area_levels to keep or drop.
#' * sex: vector of sexes to keep or drop.
@jeffeaton
jeffeaton / shiny90-datapack-extract.R
Last active February 12, 2020 12:36
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
@jeffeaton
jeffeaton / shiny90-datapack-extract.R
Created February 6, 2020 06:33
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