-
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 #33 from rapidsurveys:dev
refactor boot BW; fix #31
- Loading branch information
Showing
8 changed files
with
253 additions
and
17 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 |
---|---|---|
|
@@ -11,4 +11,4 @@ | |
/revdep/library.noindex | ||
|
||
README.html | ||
|
||
docs |
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.1.9000 | ||
Version: 0.2.2.9000 | ||
Authors@R: c( | ||
person("Mark", "Myatt", | ||
email = "[email protected]", role = c("aut", "cph")), | ||
|
@@ -22,6 +22,10 @@ License: GPL-3 | |
Depends: R (>= 3.0.1) | ||
Imports: | ||
car, | ||
doParallel, | ||
foreach, | ||
parallel, | ||
parallelly, | ||
withr | ||
Suggests: | ||
knitr, | ||
|
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,146 @@ | ||
#' | ||
#' Blocked weighted bootstrap - vectorised and parallel | ||
#' | ||
#' @inheritParams bootBW | ||
#' @param index Logical. Should index values be returned or a list of | ||
#' data.frames. Default to FALSE. | ||
#' @param cluster_df A [data.frame()] or a list of [data.frame()]s for selected | ||
#' clusters | ||
#' @param p A random probability of selection | ||
#' @param cores The number of computer cores to use/number of child processes | ||
#' will be run simultaneously. | ||
#' | ||
#' @returns A [data.frame()] with: | ||
#' * ncol = length(outputColumns) | ||
#' * nrow = replicates | ||
#' * names = outputColumns | ||
#' | ||
#' @examples | ||
#' boot_bw( | ||
#' x = indicatorsHH, w = villageData, statistic = bootClassic, | ||
#' params = "anc1", replicates = 49 | ||
#' ) | ||
#' | ||
#' @export | ||
#' @rdname boot_bw | ||
#' | ||
|
||
boot_bw_weight <- function(w) { | ||
## Scale weights and accumulate weights | ||
w$weight <- w$pop / sum(w$pop) | ||
w$cumWeight <- cumsum(w$weight) | ||
|
||
## Return w | ||
w | ||
} | ||
|
||
#' | ||
#' @export | ||
#' @rdname boot_bw | ||
#' | ||
|
||
boot_bw_sample_cluster <- function(p, w) { | ||
## Select cluster based on p ---- | ||
psu <- w$psu[which.max(w$cumWeight >= p)] | ||
|
||
## Return psu ---- | ||
psu | ||
} | ||
|
||
#' | ||
#' @export | ||
#' @rdname boot_bw | ||
#' | ||
|
||
boot_bw_sample_clusters <- function(x, w, index = FALSE) { | ||
## Get number of clusters ---- | ||
nClusters <- nrow(w) | ||
|
||
## Get vector of random probabilities ---- | ||
p <- runif(n = nClusters) | ||
|
||
## Select clusters based on p ---- | ||
selected_clusters <- lapply( | ||
X = p, | ||
FUN = boot_bw_sample_cluster, | ||
w = w | ||
) |> | ||
unlist() | ||
|
||
if (index) { | ||
## Return selected_clusters ---- | ||
selected_clusters | ||
} else { | ||
lapply( | ||
X = selected_clusters, | ||
FUN = function(y, z) subset(z, subset = psu == y), | ||
z = x | ||
) | ||
} | ||
} | ||
|
||
|
||
#' | ||
#' @export | ||
#' @rdname boot_bw | ||
#' | ||
|
||
boot_bw_sample_within_cluster <- function(cluster_df) { | ||
cluster_size <- seq_len(nrow(cluster_df)) | ||
|
||
index <- sample(cluster_size, replace = TRUE) | ||
|
||
cluster_df[index, ] | ||
} | ||
|
||
|
||
#' | ||
#' @export | ||
#' @rdname boot_bw | ||
#' | ||
|
||
boot_bw_sample_within_clusters <- function(cluster_df) { | ||
lapply( | ||
X = cluster_df, | ||
FUN = boot_bw_sample_within_cluster | ||
) |> | ||
do.call(rbind, args = _) | ||
} | ||
|
||
|
||
#' | ||
#' @export | ||
#' @rdname boot_bw | ||
#' | ||
|
||
boot_bw <- function(x, w, statistic, | ||
params, outputColumns = params, | ||
replicates = 399, | ||
cores = parallelly::availableCores(omit = 1)) { | ||
## Get cumulative weights for clusters ---- | ||
w <- boot_bw_weight(w) | ||
|
||
## Setup parallelism ---- | ||
cl <- parallel::makeCluster(cores) | ||
doParallel::registerDoParallel(cl) | ||
|
||
## Resample ---- | ||
boot <- foreach::foreach(seq_len(replicates), .combine = rbind) %dopar% { | ||
sampled_clusters <- boot_bw_sample_clusters(x = x, w = w) | ||
|
||
xBW <- boot_bw_sample_within_clusters(sampled_clusters) | ||
|
||
statistic(xBW, params) | ||
} | ||
|
||
## Rename output data.frame ---- | ||
boot <- as.data.frame(boot) | ||
row.names(boot) <- NULL | ||
names(boot) <- outputColumns | ||
|
||
## Stop parallelism ---- | ||
parallel::stopCluster(cl) | ||
|
||
## Return boot ---- | ||
boot | ||
} |
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,26 +1,22 @@ | ||
Accion | ||
Arimond | ||
Centers | ||
codecov | ||
CMD | ||
CodeFactor | ||
DL | ||
doi | ||
Faim | ||
FeFol | ||
Gelbach | ||
Handwashing | ||
ICFI | ||
IYCF | ||
JB | ||
LCL | ||
lifecycle | ||
Lifecycle | ||
ORCID | ||
org | ||
outputColumns | ||
PSU | ||
PSUs | ||
RapidSurveys | ||
Ruel | ||
th | ||
CMD | ||
nrow | ||
doi | ||
ncol | ||
CodeFactor | ||
nrow | ||
outputColumns | ||
th |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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