-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from rapidsurveys:dev
- Loading branch information
Showing
26 changed files
with
558 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ | |
|
||
README.html | ||
docs | ||
inst/doc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
Package: bbw | ||
Type: Package | ||
Title: Blocked Weighted Bootstrap | ||
Version: 0.2.4.9000 | ||
Version: 0.2.5.9000 | ||
Authors@R: c( | ||
person("Mark", "Myatt", | ||
email = "[email protected]", role = c("aut", "cph")), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
#' | ||
#' Post-stratification analysis | ||
#' | ||
#' @param est_df A [data.frame()] of stratified indicator estimates to get | ||
#' overall estimates of. `est_df` should have a variable named `est` for | ||
#' the values of the indicator estimate, a variable named `strata` for | ||
#' information on the stratification or grouping of the estimates, and a | ||
#' variable named `se` for the standard errors for the values of the | ||
#' indicator estimate. This is usually produced via a call to | ||
#' [boot_bw_estimate()]. | ||
#' @param pop_df A [data.frame()] with at least two variables: `strata` for the | ||
#' stratification/grouping information that matches `strata` in `est_df` and | ||
#' `pop` for information on population for the given `strata`. | ||
#' | ||
#' @returns A vector of values for the overall estimate, overall 95% lower | ||
#' confidence limit, and overall 95% upper confidence limit for each of the | ||
#' `strata` in `est_df`. | ||
#' | ||
#' @examples | ||
#' est_df <- boot_bw( | ||
#' x = indicatorsHH, w = villageData, statistic = bootClassic, | ||
#' params = "anc1", strata = "region", replicates = 9 | ||
#' ) |> | ||
#' boot_bw_estimate() | ||
#' | ||
#' ## Add population ---- | ||
#' pop_df <- somalia_population |> | ||
#' subset(select = c(region, total)) | ||
#' | ||
#' names(pop_df) <- c("strata", "pop") | ||
#' | ||
#' estimate_total(est_df, pop_df) | ||
#' | ||
#' @export | ||
#' | ||
|
||
estimate_total <- function(est_df, pop_df) { | ||
## Check the data ---- | ||
check_est_df(est_df) | ||
check_pop_df(pop_df) | ||
|
||
## Merge estimates with population data ---- | ||
est_pop_df <- merge(pop_df, est_df, by = "strata", all.y = TRUE) | ||
|
||
## Get total estimates ---- | ||
if (length(unique(est_pop_df$indicator)) > 1) { | ||
est_pop_df <- split(x = est_pop_df, f = est_pop_df$indicator) | ||
|
||
total_est_df <- lapply( | ||
X = est_pop_df, | ||
FUN = estimate_total_ | ||
) |> | ||
do.call(rbind, args = _) |> | ||
as.data.frame() | ||
} else { | ||
total_est_df <- estimate_total_(est_pop_df) | ||
} | ||
|
||
## Return estimates ---- | ||
total_est_df | ||
} | ||
|
||
|
||
#' | ||
#' Estimate post-stratification weighted totals | ||
#' | ||
#' @keywords internal | ||
#' | ||
|
||
estimate_total_ <- function(est_pop_df) { | ||
with(est_pop_df, { | ||
data.frame( | ||
strata = "Overall", | ||
indicator = unique(indicator), | ||
est = calc_total_estimate(est, pop), | ||
lcl = calc_total_ci(est, pop, se, "lcl"), | ||
ucl = calc_total_ci(est, pop, se, "ucl"), | ||
se = calc_total_sd(se, pop) | ||
) | ||
}) | ||
} | ||
|
||
|
||
#' | ||
#' Calculate total estimate | ||
#' | ||
#' @keywords internal | ||
#' | ||
|
||
calc_total_estimate <- function(est, pop) { | ||
sum(est * pop, na.rm = TRUE) / sum(pop, na.rm = TRUE) | ||
} | ||
|
||
|
||
#' | ||
#' Calculate total sd | ||
#' | ||
#' @keywords internal | ||
#' | ||
|
||
calc_total_sd <- function(se, pop) { | ||
sum(se ^ 2 * pop / sum(pop, na.rm = TRUE), na.rm = TRUE) | ||
} | ||
|
||
|
||
#' | ||
#' Calculate confidence limits | ||
#' | ||
#' @keywords internal | ||
#' | ||
|
||
calc_total_ci <- function(est, pop, se, ci = c("lcl", "ucl")) { | ||
ci <- match.arg(ci) | ||
|
||
operator <- ifelse(ci == "lcl", "-", "+") | ||
|
||
str2expression( | ||
paste0( | ||
"sum(est * pop, na.rm = TRUE) / sum(pop, na.rm = TRUE) ", | ||
operator, | ||
" 1.96 * sqrt(sum(se ^ 2 * pop / sum(pop, na.rm = TRUE), na.rm = TRUE))" | ||
) | ||
) |> | ||
eval() | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Somalia regional population estimates 2022 | ||
|
||
.url <- "https://fsnau.org/downloads/2022-Gu-IPC-Population-Tables-Current.pdf" | ||
|
||
somalia_population <- pdftools::pdf_text(.url) |> | ||
stringr::str_split(pattern = "\n") |> | ||
(\(x) x[[1]])() |> | ||
(\(x) x[c(8:15, 18:19, 22:29, 31)])() |> | ||
stringr::str_remove_all(pattern = ",") |> | ||
stringr::str_split(pattern = "[ ]{2,}") |> | ||
do.call(rbind, args = _) |> | ||
data.frame() | ||
|
||
names(somalia_population) <- c( | ||
"region", "total", "urban", "rural", "idp", | ||
"urban_stressed", "rural_stressed", "idp_stressed", | ||
"urban_crisis", "rural_crisis", "idp_crisis", | ||
"urban_emergency", "rural_emergency", "idp_emergency", | ||
"urban_catastrophe", "rural_catastrophe", "idp_catastrophe", | ||
"percent_at_least_crisis" | ||
) | ||
|
||
somalia_population[ , c("total", "urban", "rural", "idp", | ||
"urban_stressed", "rural_stressed", "idp_stressed", | ||
"urban_crisis", "rural_crisis", "idp_crisis", | ||
"urban_emergency", "rural_emergency", "idp_emergency", | ||
"urban_catastrophe", "rural_catastrophe", "idp_catastrophe", | ||
"percent_at_least_crisis")] <- lapply( | ||
X = subset(somalia_population, select = -region), | ||
FUN = as.numeric | ||
) |> | ||
do.call(cbind, args = _) | ||
|
||
usethis::use_data(somalia_population, overwrite = TRUE, compress = "xz") |
Binary file not shown.
Oops, something went wrong.