Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor estimate functions; fix #75 #84

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
refactor estimate functions; fix #75
ernestguevarra committed Jan 17, 2025
commit 883d70198fef533604ef36fa2ec40104be32d296
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
5 changes: 3 additions & 2 deletions R/01-opIndicators.R
Original file line number Diff line number Diff line change
@@ -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"*,
@@ -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
10 changes: 5 additions & 5 deletions R/02-classicBoot.R
Original file line number Diff line number Diff line change
@@ -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"*,
128 changes: 62 additions & 66 deletions R/03-probitBoot.R
Original file line number Diff line number Diff line change
@@ -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)
@@ -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"
@@ -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(
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)`
7 changes: 4 additions & 3 deletions R/06-estimate_op.R
Original file line number Diff line number Diff line change
@@ -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)
20 changes: 2 additions & 18 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
@@ -11,13 +11,11 @@ Codecov
Colpe
DDS
DK
DS
Deitchler
Dop
EpiData
FANTA
FAO
FG
FHI
Grotz
HH
@@ -31,7 +29,6 @@ Jaffe
LJ
Lifecycle
MAM
MF
MUAC
Moskowitz
Mroczek
@@ -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.

21 changes: 10 additions & 11 deletions man/estimate_probit.Rd
7 changes: 4 additions & 3 deletions man/op_probit.Rd