Skip to content

Commit

Permalink
Merge pull request #52 from rapidsurveys:dev
Browse files Browse the repository at this point in the history
refactror boot_bw_estimate; fix #51
  • Loading branch information
ernestguevarra authored Jan 9, 2025
2 parents 6a83135 + 84d3c7f commit a6d89bc
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 37 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
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.5.9000
Version: 0.2.6.9000
Authors@R: c(
person("Mark", "Myatt",
email = "mark@brixtonhealth.com", role = c("aut", "cph")),
Expand Down
43 changes: 26 additions & 17 deletions R/boot_bw_estimate.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -96,4 +105,4 @@ boot_percentile <- function(boot_df) {
}

est
}
}
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.

<a name="FIG1"></a>
<a name="fig1"></a>

<p align="center">

Expand All @@ -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).
Expand Down
11 changes: 1 addition & 10 deletions tests/testthat/test-post_strat_estimation.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion vignettes/bbw-estimation.Rmd
Original file line number Diff line number Diff line change
@@ -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}
Expand All @@ -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.

0 comments on commit a6d89bc

Please sign in to comment.