From fe57ce21141bf1669a5bb4673fabb57e83dcdb71 Mon Sep 17 00:00:00 2001 From: Ernest Guevarra Date: Thu, 9 Jan 2025 11:13:27 +0000 Subject: [PATCH 1/2] refactror boot_bw_estimate; fix #51 --- DESCRIPTION | 2 +- R/boot_bw_estimate.R | 43 +++++++++++++-------- README.md | 21 ++++++---- man/structure_estimates.Rd | 12 ++++++ tests/testthat/test-post_strat_estimation.R | 11 +----- vignettes/bbw-estimation.Rmd | 4 +- 6 files changed, 56 insertions(+), 37 deletions(-) create mode 100644 man/structure_estimates.Rd diff --git a/DESCRIPTION b/DESCRIPTION index bce8a9a..97c76f5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: bbw Type: Package Title: Blocked Weighted Bootstrap -Version: 0.2.5.9000 +Version: 0.2.6.9000 Authors@R: c( person("Mark", "Myatt", email = "mark@brixtonhealth.com", role = c("aut", "cph")), diff --git a/R/boot_bw_estimate.R b/R/boot_bw_estimate.R index e4970cc..a251a50 100644 --- a/R/boot_bw_estimate.R +++ b/R/boot_bw_estimate.R @@ -28,24 +28,33 @@ boot_bw_estimate <- function(boot_df) { ) ## Structure list names ---- - if (nrow(est[[1]]) == 1) { - names(est) <- paste( - names(est), - lapply(X = est, FUN = row.names) |> unlist(), - sep = "." + if (!is.data.frame(boot_df)) { + if (nrow(est[[1]]) == 1) { + names(est) <- paste( + names(est), + lapply(X = est, FUN = row.names) |> unlist(), + sep = "." + ) + } + + ## Flatten list ---- + est <- est |> + do.call(rbind, args = _) + + ## Re-structure results ---- + est <- data.frame( + strata = gsub("\\.[^\\.]{1,}", "", row.names(est)), + indicator = gsub("[^\\.]{1,}\\.", "", row.names(est)), + est ) - } - - ## Flatten list ---- - est <- est |> - do.call(rbind, args = _) + } else { + ## Flatten list ---- + est <- est |> + do.call(rbind, args = _) - ## Re-structure results ---- - est <- data.frame( - strata = gsub("\\.[^\\.]{1,}", "", row.names(est)), - indicator = gsub("[^\\.]{1,}\\.", "", row.names(est)), - est - ) + ## Re-structure results ---- + est <- data.frame(indicator = row.names(est), est) + } ## Tidy up row names ---- row.names(est) <- NULL @@ -96,4 +105,4 @@ boot_percentile <- function(boot_df) { } est -} \ No newline at end of file +} diff --git a/README.md b/README.md index fa01ee2..afd551b 100644 --- a/README.md +++ b/README.md @@ -26,10 +26,13 @@ stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https:// The **blocked weighted bootstrap** is an estimation technique for use with data from two-stage cluster sampled surveys in which either prior weighting (e.g. *population-proportional sampling* or *PPS* as used in -**Standardized Monitoring and Assessment of Relief and Transitions** or -**SMART** surveys) or *posterior weighting* (e.g. as used in **Rapid -Assessment Method** or **RAM** and **Simple Spatial Sampling Method** or -**S3M** surveys) is implemented. +[Standardized Monitoring and Assessment of Relief and Transitions +(SMART)](https://smartmethodology.org/) surveys) or *posterior +weighting* (e.g. as used in [Rapid Assessment Method +(RAM)](https://rapidsurveys.io/ramOPmanual/) and [Simple Spatial +Sampling Method +(S3M)](https://researchonline.lshtm.ac.uk/id/eprint/2572543) surveys) is +implemented. ## Installation @@ -73,11 +76,11 @@ for the sample design. A blocked weighted bootstrap can be used: sampling (PPS) to weight the sample prior to data collection (e.g. as is done with SMART surveys). This means that a posterior weighting procedure is required. `{bbw}` uses a *“roulette wheel”* - algorithm (see [illustration below](#FIG1)) to weight (i.e. by + algorithm (see [illustration below](#fig1)) to weight (i.e. by population) the selection probability of PSUs in bootstrap replicates. - +

@@ -87,8 +90,10 @@ for the sample design. A blocked weighted bootstrap can be used: In the case of prior weighting by PPS all clusters are given the same weight. With posterior weighting (as in RAM or S3M) the weight is the -population of each PSU. This procedure is very similar to the *fitness -proportional selection* technique used in *evolutionary computing*. +population of each PSU. This procedure is very similar to the [*fitness +proportional +selection*](https://en.wikipedia.org/wiki/Fitness_proportionate_selection) +technique used in *evolutionary computing*. A total of `m` PSUs are sampled with replacement for each bootstrap replicate (where `m` is the number of PSUs in the survey sample). diff --git a/man/structure_estimates.Rd b/man/structure_estimates.Rd new file mode 100644 index 0000000..fa70c60 --- /dev/null +++ b/man/structure_estimates.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/boot_bw_estimate.R +\name{structure_estimates} +\alias{structure_estimates} +\title{Structure boot estimate outputs} +\usage{ +structure_estimates(boot_df, est) +} +\description{ +Structure boot estimate outputs +} +\keyword{internal} diff --git a/tests/testthat/test-post_strat_estimation.R b/tests/testthat/test-post_strat_estimation.R index 2a9387c..17efc5b 100644 --- a/tests/testthat/test-post_strat_estimation.R +++ b/tests/testthat/test-post_strat_estimation.R @@ -3,7 +3,7 @@ test_that("estimate_total works as expected", { boot_df <- boot_bw( indicatorsHH, villageData, statistic = bootClassic, params = "anc1", - replicates = 9 + replicates = 9, strata = "region" ) est_df <- boot_bw_estimate(boot_df) @@ -15,15 +15,6 @@ test_that("estimate_total works as expected", { expect_s3_class(estimate_total(est_df, pop_df), "data.frame") - boot_df <- boot_bw( - indicatorsHH, villageData, statistic = bootClassic, params = "anc1", - replicates = 9, strata = "region" - ) - - est_df <- boot_bw_estimate(boot_df) - - expect_s3_class(estimate_total(est_df, pop_df), "data.frame") - boot_df <- boot_bw( indicatorsHH, villageData, statistic = bootClassic, params = c("anc1", "anc2"), replicates = 9, strata = "region" diff --git a/vignettes/bbw-estimation.Rmd b/vignettes/bbw-estimation.Rmd index 6a21476..e5457a1 100644 --- a/vignettes/bbw-estimation.Rmd +++ b/vignettes/bbw-estimation.Rmd @@ -1,5 +1,7 @@ --- title: "Blocked weighted bootstrap estimation" +author: "Ernest Guevarra" +date: "8 January 2025" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Blocked weighted bootstrap estimation} @@ -18,4 +20,4 @@ knitr::opts_chunk$set( library(bbw) ``` -The `{bbw}` package was developed primarily as a tool for analysing complex survey data. +The `{bbw}` package was developed primarily as a tool for analysing complex sample survey data. From 84d3c7f3d7b6edcdfbc84ee483373facc7586802 Mon Sep 17 00:00:00 2001 From: Ernest Guevarra Date: Thu, 9 Jan 2025 11:21:19 +0000 Subject: [PATCH 2/2] update NAMESPACE --- man/structure_estimates.Rd | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 man/structure_estimates.Rd diff --git a/man/structure_estimates.Rd b/man/structure_estimates.Rd deleted file mode 100644 index fa70c60..0000000 --- a/man/structure_estimates.Rd +++ /dev/null @@ -1,12 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/boot_bw_estimate.R -\name{structure_estimates} -\alias{structure_estimates} -\title{Structure boot estimate outputs} -\usage{ -structure_estimates(boot_df, est) -} -\description{ -Structure boot estimate outputs -} -\keyword{internal}