Skip to content

Commit

Permalink
Merge pull request #88 from nutriverse:dev
Browse files Browse the repository at this point in the history
create function for patchy test; fix #85
  • Loading branch information
ernestguevarra authored Feb 2, 2025
2 parents ea5b300 + 0f942a1 commit 186a60b
Show file tree
Hide file tree
Showing 16 changed files with 270 additions and 31 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ Depends:
R (>= 4.1.0)
Imports:
cli,
methods,
parallel,
parallelly,
squeacr
squeacr,
stats
Suggests:
covr,
knitr,
Expand Down
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

S3method(plot,lqasSim)
S3method(print,lqasClass)
export(check_coverage_homogeneity)
export(estimate_coverage)
export(estimate_coverage_overall)
export(get_n_cases)
Expand All @@ -20,12 +21,16 @@ export(lqas_simulate_run)
export(lqas_simulate_runs)
export(lqas_simulate_test)
importFrom(cli,cli_abort)
importFrom(cli,cli_alert_info)
importFrom(cli,cli_alert_warning)
importFrom(graphics,abline)
importFrom(graphics,legend)
importFrom(graphics,lines)
importFrom(graphics,plot)
importFrom(graphics,points)
importFrom(methods,is)
importFrom(parallel,mclapply)
importFrom(parallelly,availableCores)
importFrom(stats,lowess)
importFrom(stats,pchisq)
importFrom(stats,phyper)
4 changes: 3 additions & 1 deletion R/sleacr.R → R/00-sleacr.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
#' @importFrom stats lowess phyper
#' @importFrom parallel mclapply
#' @importFrom parallelly availableCores
#' @importFrom cli cli_abort
#' @importFrom cli cli_abort cli_alert_warning cli_alert_info
#' @importFrom methods is
#' @importFrom stats pchisq
#'

"_PACKAGE"
Expand Down
123 changes: 123 additions & 0 deletions R/06-chi-square.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#'
#' Check coverage distribution
#'
#' @inheritParams estimate_coverage_overall
#' @param p Minimum p-value to test statistic. Default is 0.05.
#'
#' @returns A named list of 2 lists: one for case-finding effectiveness (*cf*)
#' and the second for treatment coverage (*tc*). For each list, the following
#' values are provided:
#' * **statistic** - calculated chi-square statistic
#' * **df** - degrees of freedom
#' * **p** - p-value of chi-square statistic
#'
#' @examples
#' check_coverage_homogeneity(survey_data)
#'
#' @export
#'

check_coverage_homogeneity <- function(cov_df, k = 3, p = 0.05) {
## Check coverage data ----
check_coverage_data(cov_df)

## Check p ----
check_p(p)

## Calculate chi-square statistic ----
x2 <- calculate_x2_stat(
cases_in = cov_df$cases_in, cases_out = cov_df$cases_out,
rec_in = cov_df$rec_in, k = k
)

## Sum of chi-square statistics ----
x2_cf <- sum(x2[[1]])
x2_tc <- sum(x2[[2]])

deg_free <- nrow(cov_df) - 1

## Get critical value ----
#crit_value <- get_critical_value(df = deg_free, p = as.character(p))

p_cf <- stats::pchisq(x2_cf, df = deg_free, lower.tail = FALSE)
p_tc <- stats::pchisq(x2_tc, df = deg_free, lower.tail = FALSE)

## Concatenate results ----
x2_results <- list(
cf = list(
statistic = x2_cf,
df = deg_free,
p = p_cf
),
tc = list(
statistic = x2_tc,
df = deg_free,
p = p_tc
)
)

## Create messages ----
if (p_cf < p) {
cli::cli_alert_warning(
"{.strong Case-finding effectiveness} across {nrow(cov_df)} surveys is {.strong patchy}."
)
} else {
cli::cli_alert_info(
"{.strong Case-finding effectiveness} across {nrow(cov_df)} surveys is {.strong not patchy}."
)
}

if (p_tc < p) {
cli::cli_alert_warning(
"{.strong Treatment coverage} across {nrow(cov_df)} surveys is {.strong patchy}."
)
} else {
cli::cli_alert_info(
"{.strong Treatment coverage} across {nrow(cov_df)} surveys is {.strong not patchy}."
)
}

## Return x2_results ----
x2_results
}


#'
#' @keywords internal
#'

calculate_x2_stat <- function(cases_in, cases_out, rec_in, k = 3) {
## Get observed ----
observed_cf <- cases_in
observed_tc <- cases_in + rec_in

## Calculate rec_out ----
rec_out <- squeacr::calculate_rout(
cin = cases_in, cout = cases_out, rin = rec_in, k = k
)

## Calculate variables needed ----
cases_cf <- cases_in + cases_out
cases_tc <- cases_in + cases_out + rec_in + rec_out

total_cases_cf <- sum(cases_cf)
total_cases_tc <- sum(cases_tc)

total_cases_in_cf <- sum(cases_in)
total_cases_in_tc <- sum(cases_in + rec_in)

## Calculate expected ----
expected_cf <- cases_cf * (total_cases_in_cf / total_cases_cf)
expected_tc <- cases_tc * (total_cases_in_tc / total_cases_tc)

## Calculate chi-square ----
x2_cf <- ((observed_cf - expected_cf) ^ 2) / expected_cf
x2_tc <- ((observed_tc - expected_tc) ^ 2) / expected_tc

## Concatenate ----
x2 <- list(cf = x2_cf, tc = x2_tc)

## Return chi-square ----
x2
}

14 changes: 14 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,19 @@ check_pop_data <- function(pop_df) {
}


#'
#' Check p value
#'
#' @keywords internal
#'

check_p <- function(p) {
if (!is(p, "numeric"))
cli::cli_abort("{.arg p} should be numeric.")

if (p >= 1 | p <= 0)
cli::cli_abort("{.arg p} should be greater than 0 and less than 1")
}



4 changes: 2 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Using this dataset, per district coverage classifications can be calculated as f
```{r classify-coverage, eval = FALSE}
with(
survey_data,
lqas_classify_coverage(
lqas_classify(
cases_in = cases_in, cases_out = cases_out, rec_in = rec_in
)
)
Expand All @@ -136,7 +136,7 @@ which outputs the following results:
```{r classify-coverage-show, echo = FALSE}
with(
survey_data,
lqas_classify_coverage(
lqas_classify(
cases_in = cases_in, cases_out = cases_out, rec_in = rec_in
)
)
Expand Down
40 changes: 20 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,29 +249,29 @@ calculated as follows:
``` r
with(
survey_data,
lqas_classify_coverage(
lqas_classify(
cases_in = cases_in, cases_out = cases_out, rec_in = rec_in
)
)
```

which outputs the following results:

#> cf tc
#> 1 Low Moderate
#> 2 Low Low
#> 3 Low Low
#> 4 Low Low
#> 5 Low Low
#> 6 Low Moderate
#> 7 Low Low
#> 8 Low Moderate
#> 9 Low Moderate
#> 10 Low Moderate
#> 11 Low Low
#> 12 Low Low
#> 13 Low Low
#> 14 Low Low
#> cf tc
#> 1 0 1
#> 2 0 0
#> 3 0 0
#> 4 0 0
#> 5 0 0
#> 6 0 1
#> 7 0 0
#> 8 1 1
#> 9 1 1
#> 10 1 1
#> 11 0 0
#> 12 0 0
#> 13 0 0
#> 14 0 0

The function provides estimates for *case-finding effectiveness* and for
*treatment coverage* as a `data.frame` object.
Expand All @@ -294,10 +294,10 @@ lqas_sim_pop <- lqas_simulate_test(

## Get classification probabilities ----
lqas_get_class_prob(lqas_sim_pop)
#> Low : 0.9549
#> Moderate : 0.8308
#> High : 0.8317
#> Overall : 0.9054
#> Low : 0.9552
#> Moderate : 0.8305
#> High : 0.8395
#> Overall : 0.9062
#> Gross misclassification : 0
```

Expand Down
12 changes: 12 additions & 0 deletions data-raw/chi_square.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Retrieve chi-square statistic table ------------------------------------------

session <- rvest::session(
"https://people.richland.edu/james/lecture/m170/tbl-chi.html"
)

chi_square_table <- rvest::html_table(session) |>
(\(x) x[[1]])()

usethis::use_data(
chi_square_table, internal = TRUE, overwrite = TRUE, compress = "xz"
)
1 change: 1 addition & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ Yalenga
Yarlenga
Yengema
cf
df
frac
lceil
lfloor
Expand Down
41 changes: 41 additions & 0 deletions man/check_coverage_homogeneity.Rd

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

12 changes: 12 additions & 0 deletions man/check_p.Rd

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

Binary file modified man/figures/README-classifier-test-plot-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion man/sleacr.Rd

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

8 changes: 6 additions & 2 deletions pkgdown/_pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ title: sleacr

url: https://nutriverse.io/sleacr/

development:
mode: auto
# development:
# mode: auto

template:
bootstrap: 5
Expand Down Expand Up @@ -73,6 +73,10 @@ reference:
contents:
- starts_with("estimate_coverage")

- title: Test for coverage homogeneity
contents:
- check_coverage_homogeneity

- title: Datasets
contents:
- village_list
Expand Down
Loading

0 comments on commit 186a60b

Please sign in to comment.