Skip to content

Commit

Permalink
Merge pull request #84 from rapidsurveys:dev
Browse files Browse the repository at this point in the history
refactor estimate functions; fix #75
  • Loading branch information
ernestguevarra authored Jan 17, 2025
2 parents 88c2ef6 + 883d701 commit 6f869eb
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 121 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# oldr 0.2.1



# oldr 0.1.1

* Created full suite of estimator functions
Expand Down
5 changes: 3 additions & 2 deletions R/01-opIndicators.R
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@
#' `wgP3` | Overall 3
#' `wgPM` | Any disability
#'
#' @param svy A data.frame collected using the standard RAM-OP questionnaire
#' @param svy A [data.frame()] collected using the standard RAM-OP
#' questionnaire.
#' @param indicators A character vector of indicator set names. The vector may
#' include one or more of the following: *"demo"*, *"food"*, *"hunger"*,
#' *"disability"*, *"adl"*, *"mental"*, *"dementia"*, *"health"*, *"income"*,
Expand All @@ -297,7 +298,7 @@
#' to report indicators for *males*, *females*, or *both* respectively.
#' Default is *"mf"* for both sexes.
#'
#' @return A tibble of older people indicators
#' @return A [tibble()] of older people indicators.
#'
#' @examples
#' # Create indicators dataset from RAM-OP survey data collected from
Expand Down
10 changes: 5 additions & 5 deletions R/02-classicBoot.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#' Apply bootstrap to RAM-OP indicators using a classical estimator.
#'
#' @param x Indicators dataset produced by [create_op()] with primary sampling
#' unit (PSU) in column named `psu`
#' @param w A data frame with primary sampling unit (PSU) in column named `psu`
#' and survey weight (i.e. PSU population) in column named `pop`
#' @param statistic A function operating on data in `x`. Fixed to `bootClassic()`
#' function for means
#' unit (PSU) in column named *"psu"*.
#' @param w A data frame with primary sampling unit (PSU) in column named
#' *"psu"* and survey weight (i.e. PSU population) in column named *"pop"*.
#' @param statistic A function operating on data in `x`. Fixed to
#' [bootClassic()] function for means.
#' @param indicators A character vector of indicator set names to estimate.
#' Indicator set names are *"demo"*, *"food"*, *"hunger"*, *"disability"*,
#' *"adl"*, *"mental"*, *"dementia"*, *"health"*, *"income"*, *"wash"*,
Expand Down
128 changes: 62 additions & 66 deletions R/03-probitBoot.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@
#' Apply bootstrap to RAM-OP indicators using a PROBIT estimator.
#'
#' @param x Indicators dataset produced by [create_op()] with primary sampling
#' unit (PSU) in column named `PSU`
#' unit (PSU) in column named *"psu"*.
#' @param w A data frame with primary sampling unit (PSU) in column named
#' `psu` and survey weight (i.e. PSU population) in column named `pop`.
#' *"psu"* and survey weight (i.e. PSU population) in column named *"pop"*.
#' @param gam.stat A function operating on data in `x` to estimate GAM
#' prevalence for older people. Fixed to `probit_gam`
#' prevalence for older people. Fixed to [probit_gam()].
#' @param sam.stat A function operating on data in `x` to estimate
#' SAM prevalence for older people. Fixed to `probit_sam`
#' SAM prevalence for older people. Fixed to [probit_sam()].
#' @param params Parameters (named columns in `x`) passed to the function
#' specified in `statistic`; fixed to `MUAC` as indicator amenable
#' to probit estimation
#' @param outputColumns Names of columns in output data frame; fixed to
#' `MUAC`
#' @param replicates Number of bootstrap replicate case and non-case
#' specified in `statistic`; fixed to *"MUAC"* as indicator amenable
#' to probit estimation.
#' @param outputColumns Names of columns in output data frame.
#' @param replicates Number of bootstrap replicate case and non-case.
#'
#' @return A tibble of boot estimates using bootPROBIT function
#' @return A [tibble()] of boot estimates using PROBIT.
#'
#' @examples
#' test <- estimate_probit(x = indicators.ALL, w = testPSU, replicates = 3)
Expand All @@ -31,58 +30,46 @@ estimate_probit <- function(x,
gam.stat = probit_gam,
sam.stat = probit_sam,
params = "MUAC",
outputColumns = "MUAC",
outputColumns = params,
replicates = 399) {
## Blocking weighted bootstrap (GAM) - ALL
bootGAM.ALL <- bbw::bootBW(x = x,
w = w,
statistic = gam.stat,
params = params,
outputColumns = params,
replicates = replicates)
bootGAM.ALL <- bootBW(
x = x, w = w, statistic = gam.stat, params = params,
outputColumns = params, replicates = replicates
)

## Blocking weighted bootstrap (GAM) - MALES
bootGAM.MALES <- bbw::bootBW(x = x[x$sex1 == 1, ],
w = w,
statistic = gam.stat,
params = params,
outputColumns = params,
replicates = replicates)
bootGAM.MALES <- bootBW(
x = x[x$sex1 == 1, ], w = w, statistic = gam.stat, params = params,
outputColumns = params, replicates = replicates
)

## Blocking weighted bootstrap (GAM) - FEMALES
bootGAM.FEMALES <- bbw::bootBW(x = x[x$sex2 == 1, ],
w = w,
statistic = gam.stat,
params = params,
outputColumns = params,
replicates = replicates)
bootGAM.FEMALES <- bootBW(
x = x[x$sex2 == 1, ], w = w, statistic = gam.stat, params = params,
outputColumns = params, replicates = replicates
)

## Rename results
names(bootGAM.ALL) <- names(bootGAM.MALES) <- names(bootGAM.FEMALES) <- "GAM"

## Blocking weighted bootstrap (SAM) - ALL
bootSAM.ALL <- bbw::bootBW(x = x,
w = w,
statistic = sam.stat,
params = params,
outputColumns = params,
replicates = replicates)
bootSAM.ALL <- bootBW(
x = x, w = w, statistic = sam.stat, params = params,
outputColumns = params, replicates = replicates
)

## Blocking weighted bootstrap (SAM) - MALES
bootSAM.MALES <- bbw::bootBW(x = x[x$sex1 ==1, ],
w = w,
statistic = sam.stat,
params = params,
outputColumns = params,
replicates = replicates)
bootSAM.MALES <- bootBW(
x = x[x$sex1 ==1, ], w = w, statistic = sam.stat, params = params,
outputColumns = params, replicates = replicates
)

## Blocking weighted bootstrap (SAM) - FEMALES
bootSAM.FEMALES <- bbw::bootBW(x = x[x$sex2 ==1, ],
w = w,
statistic = sam.stat,
params = params,
outputColumns = params,
replicates = replicates)
bootSAM.FEMALES <- bootBW(
x = x[x$sex2 ==1, ], w = w, statistic = sam.stat, params = params,
outputColumns = params, replicates = replicates
)

## Rename results
names(bootSAM.ALL) <- names(bootSAM.MALES) <- names(bootSAM.FEMALES) <- "SAM"
Expand Down Expand Up @@ -111,37 +98,46 @@ estimate_probit <- function(x,

## Extract estimates from 'boot.ALL' data.frames
estimates.ALL <- data.frame(
t(apply(X = boot.ALL,
MARGIN = 2,
FUN = quantile,
probs = c(0.025, 0.5, 0.975), na.rm = TRUE)))
t(
apply(
X = boot.ALL, MARGIN = 2, FUN = quantile,
probs = c(0.025, 0.5, 0.975), na.rm = TRUE
)
)
)

## Extract estimates from 'boot.MALES' data.frames
estimates.MALES <- data.frame(
t(apply(X = boot.MALES,
MARGIN = 2,
FUN = quantile,
probs = c(0.025, 0.5, 0.975), na.rm = TRUE)))
t(
apply(
X = boot.MALES, MARGIN = 2, FUN = quantile,
probs = c(0.025, 0.5, 0.975), na.rm = TRUE
)
)
)

## Extract estimates from 'boot.FEMALES' data.frames
estimates.FEMALES <- data.frame(
t(apply(X = boot.FEMALES,
MARGIN = 2,
FUN = quantile,
probs = c(0.025, 0.5, 0.975), na.rm = TRUE)))
t(
apply(
X = boot.FEMALES, MARGIN = 2, FUN = quantile,
probs = c(0.025, 0.5, 0.975), na.rm = TRUE
)
)
)

## Join 'estimates.*' data.frames side-by-side
probitEstimates <- data.frame(estimates.ALL,
estimates.MALES,
estimates.FEMALES)
probitEstimates <- data.frame(
estimates.ALL, estimates.MALES, estimates.FEMALES
)

## Clean-up row and column names
probitEstimates$indicator <- row.names(probitEstimates)
row.names(probitEstimates) <- NULL
names(probitEstimates) <- c("LCL.ALL", "EST.ALL", "UCL.ALL",
"LCL.MALES", "EST.MALES", "UCL.MALES",
"LCL.FEMALES", "EST.FEMALES", "UCL.FEMALES",
"INDICATOR")
names(probitEstimates) <- c(
"LCL.ALL", "EST.ALL", "UCL.ALL", "LCL.MALES", "EST.MALES", "UCL.MALES",
"LCL.FEMALES", "EST.FEMALES", "UCL.FEMALES", "INDICATOR"
)

## Re-order columns
col_order <- c(
Expand Down
7 changes: 4 additions & 3 deletions R/04-probit_muac.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#'
#' PROBIT statistics function for bootstrap estimation of older people GAM
#'
#' @param x A data frame with `primary sampling unit (PSU)` in column named
#' `psu` and with data column/s containing the continuous variable/s of
#' @param x A data frame with primary sampling unit (PSU) in column named
#' *"psu"* and with data column/s containing the continuous variable/s of
#' interest with column names corresponding to `params` values
#' @param params A vector of column names corresponding to the continuous
#' variables of interest contained in `x`
#' @param threshold cut-off value for continuous variable to differentiate
#' case and non-case. Default is set at 210.
#' case and non-case. Default is set at 210 for [probit_gam()] and 185 for
#' [probit_sam()].
#'
#' @return A numeric vector of the PROBIT estimate of each continuous variable
#' of interest with length equal to `length(params)`
Expand Down
7 changes: 4 additions & 3 deletions R/06-estimate_op.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
#' Estimate all standard RAM-OP indicators
#'
#' @param x Indicators dataset produced by [create_op()] with primary sampling
#' unit (PSU) in column named `PSU`
#' unit (PSU) in column named *"psu"*`
#' @param w A data frame with primary sampling unit (PSU) in column named
#' `psu` and survey weight (i.e. PSU population) in column named `pop`
#' *"psu"* and survey weight (i.e. PSU population) in column named *"pop"*.
#' @param indicators A character vector of indicator set names to estimate.
#' Indicator set names are *"demo"*, *"anthro"*, *"food"*, *"hunger"*,
#' *"disability"*, *"adl"*, *"mental"*, *"dementia"*, *"health"*, *"income"*,
#' *"wash"*, *"visual"*, and *"misc"*. Default is all indicator sets.
#' @param replicates Number of bootstrap replicates. Default is 399.
#'
#' @return Tibble of boot estimates for all specified standard RAM-OP indicators
#' @return A [tibble()] of boot estimates for all specified standard RAM-OP
#' indicators
#'
#' @examples
#' estimate_op(x = create_op(testSVY), w = testPSU, replicates = 9)
Expand Down
20 changes: 2 additions & 18 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ Codecov
Colpe
DDS
DK
DS
Deitchler
Dop
EpiData
FANTA
FAO
FG
FHI
Grotz
HH
Expand All @@ -31,7 +29,6 @@ Jaffe
LJ
Lifecycle
MAM
MF
MUAC
Moskowitz
Mroczek
Expand All @@ -49,34 +46,21 @@ RW
RapidSurveys
SLT
TD
Tibble
WG
adl
anthro
bootClassic
bootPROBIT
cd
ci
classADL
codecov
etc
frac
hasHelp
mf
moringa
poorVA
psu
resp
scoreADL
ramOP
ramOPreport
th
tibble
unmetNeed
wgCommunicatingD
wgHearingD
wgMobilityD
wgP
wgPM
wgRememberingD
wgSelfCareD
wgVisionD
widehat
5 changes: 3 additions & 2 deletions man/create_op.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions man/estimate_classic.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions man/estimate_op.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6f869eb

Please sign in to comment.