diff --git a/.github/workflows/pkgdown.yaml b/.github/workflows/pkgdown.yaml index 12811eec..f1e8c137 100644 --- a/.github/workflows/pkgdown.yaml +++ b/.github/workflows/pkgdown.yaml @@ -1,28 +1,54 @@ -# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples -# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help +# Deploys pkgdown for Pull Requests, tags, and pushes to main branch +# PRs are deployed to /preview/pr/ +# Tags are deployed to // +# copied from https://github.com/rstudio/education-workflows/blob/main/examples/pkgdown.yaml +# referred from https://github.com/r-lib/actions/issues/865 on: - push: - branches: [main, master] pull_request: - branches: [main, master] - release: - types: [published] + branches: + - main + types: + - opened + - reopened + - synchronize + - closed + paths: + - 'man/**' + - 'pkgdown/**' + - 'vignettes/**' + push: + tags: + - 'v[0-9]+.[0-9]+.[0-9]+' # build on version tags + - '!v[0-9]+.[0-9]+.[0-9]+.[0-9]+' # but not if version involves a dev component + branches: + - main workflow_dispatch: + inputs: + tag: + description: Tag to deploy + required: true + default: '' -name: pkgdown.yaml +name: pkgdown jobs: - pkgdown: + pkgdown-build: runs-on: ubuntu-latest - # Only restrict concurrency for non-PR jobs - concurrency: - group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }} + if: ${{ !(github.event_name == 'pull_request' && github.event.action == 'closed') }} env: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} - permissions: - contents: write steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v2 + + - name: Configure git + run: | + git config --local user.name "$GITHUB_ACTOR" + git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com" + + - uses: r-lib/actions/pr-fetch@v2 + if: ${{ github.event_name == 'pull_request' }} + with: + repo-token: ${{ github.token }} - uses: r-lib/actions/setup-pandoc@v2 @@ -32,17 +58,91 @@ jobs: - uses: r-lib/actions/setup-r-dependencies@v2 with: - extra-packages: any::pkgdown, local::. - needs: website + needs: | + connect + website + extra-packages: | + local::. + any::pkgdown + + # If events is a PR, set subdir to 'preview/pr' + - name: "[PR] Set documentation subdirectory" + if: github.event_name == 'pull_request' + run: | + echo "PKGDOWN_DEV_MODE=unreleased" >> $GITHUB_ENV + echo "subdir=preview/pr${{ github.event.number }}" >> $GITHUB_ENV - - name: Build site - run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE) + # If event is a tag, set subdir to '' + - name: "[tag] Set documentation subdirectory" + if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') + run: | + echo "PKGDOWN_DEV_MODE=release" >> $GITHUB_ENV + echo "subdir=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV + + # If event is workflow_dispatch, set subdir to 'inputs.tag' + - name: '[dispatch] Set documentation subdirectory' + if: github.event_name == 'workflow_dispatch' + run: | + echo "subdir=${{ github.event.inputs.tag }}" >> $GITHUB_ENV + + - name: Deploy pkgdown site + id: deploy shell: Rscript {0} + run: | + subdir <- "${{ env.subdir }}" + pkg <- pkgdown::as_pkgdown(".") + + # Deploy pkgdown site to branch + pkgdown::deploy_to_branch(subdir = if (nzchar(subdir)) subdir, clean = nzchar(subdir)) + + # Report deployed site URL + deployed_url <- file.path(pkg$meta$url, subdir) + cat(sprintf('url=%s', deployed_url), file = Sys.getenv("GITHUB_OUTPUT"), append = TRUE) + + - name: Notify pkgdown deployment + if: github.event_name == 'pull_request' + uses: hasura/comment-progress@v2.2.0 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + repository: ${{ github.repository }} + number: ${{ github.event.number }} + id: pkgdown-deploy + append: false + message: > + :book: ${{ steps.deploy.outputs.url }} + + Preview documentation for this PR (at commit ${{ github.event.pull_request.head.sha }}) + + pkgdown-clean: + if: ${{ github.event_name == 'pull_request' && github.event.action == 'closed' }} + runs-on: ubuntu-latest + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + steps: + - uses: actions/checkout@v2 + with: + ref: "gh-pages" + + - name: Clean up PR Preview + run: | + git config --local user.name "$GITHUB_ACTOR" + git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com" + + preview_dir="preview/pr${{ github.event.pull_request.number }}" + if [ -d "$preview_dir" ]; then + git rm -r $preview_dir + git commit -m "Remove $preview_dir (GitHub Actions)" || echo 'No preview to remove' + git push origin || echo 'No preview to remove' + else + echo 'No preview to remove' + fi - - name: Deploy to GitHub pages 🚀 - if: github.event_name != 'pull_request' - uses: JamesIves/github-pages-deploy-action@v4.5.0 + - name: Notify pkgdown cleanup + uses: hasura/comment-progress@v2.2.0 with: - clean: false - branch: gh-pages - folder: docs + github-token: ${{ secrets.GITHUB_TOKEN }} + repository: ${{ github.repository }} + number: ${{ github.event.number }} + id: pkgdown-deploy + message: | + _:closed_book: Preview documentation for this PR has been cleaned up._ diff --git a/DESCRIPTION b/DESCRIPTION index 21eeb85e..a059428a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: serocalculator Title: Estimating Infection Rates from Serological Data -Version: 1.2.0.9017 +Version: 1.2.0.9018 Authors@R: c( person("Peter", "Teunis", , "p.teunis@emory.edu", role = c("aut", "cph"), comment = "Author of the method and original code."), @@ -38,7 +38,8 @@ Imports: tibble, tidyr, tidyselect, - utils + utils, + purrr Suggests: bookdown, devtag (>= 0.0.0.9000), @@ -55,7 +56,9 @@ Suggests: ssdtools (>= 1.0.6.9016), testthat (>= 3.0.0), tidyverse, - qrcode + qrcode, + svglite, + vdiffr LinkingTo: Rcpp Config/testthat/edition: 3 diff --git a/NEWS.md b/NEWS.md index 4abcd819..87839e24 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,9 @@ # serocalculator (development version) ## New features +* Improved error messaging for `autoplot.pop_data()` (#234). + +* Clarified package installation instructions in scrub typhus vignette (#234). * Add `as_noise_params` (#228) @@ -19,7 +22,11 @@ * Added template for pull requests (from ) (#265) -## Developer-facing changes +## Internal changes + +* Added online preview builds for PRs that change the `pkgdown` website (#309) + +* Added `test-autoplot.pop_data` test (#234) * initialized [`lintr`](https://lintr.r-lib.org/) with `lintr::use_lint()` (#278) @@ -28,6 +35,7 @@ * fixed `dplyr::select()` deprecation warning in `df_to_array()` (#276) * Added `devtag` to package (using `devtag::use_devtag()`) (#292) + * Added `@dev` tag to `?df_to_array()` (#292) * Generalized `get_()` and `set_()` methods to be general-purpose @@ -47,6 +55,7 @@ including: - lint changed files (#256) # serocalculator 1.2.0 + * Added `test-summary.pop_data` test * Modified `test-est.incidence` test diff --git a/R/autoplot.pop_data.R b/R/autoplot.pop_data.R index efbd0a09..786207de 100644 --- a/R/autoplot.pop_data.R +++ b/R/autoplot.pop_data.R @@ -4,9 +4,11 @@ #' #' @param object A `pop_data` object (from [load_pop_data()]) #' @param log whether to show antibody responses on logarithmic scale -#' @param strata the name of a variable in `pop_data` to stratify by (or `NULL` for no stratification) +#' @param strata the name of a variable in `pop_data` +#' to stratify by (or `NULL` for no stratification) #' @param ... unused -#' @param type an option to choose type of chart: the current options are `"density"` or `"age-scatter"` +#' @param type an option to choose type of chart: +#' the current options are `"density"` or `"age-scatter"` #' #' @return a [ggplot2::ggplot] object #' @@ -31,40 +33,58 @@ autoplot.pop_data <- function( type = "density", strata = NULL, ...) { + + if (!is.null(strata) && !is.element(strata, names(object))) { + cli::cli_abort( + class = "unavailable_strata", + message = c( + x = "The variable {.var {strata}} specified by argument {.arg strata} + does not exist in {.arg object}.", + i = "Please choose a column that exists in {.arg object}." + ) + ) + } + if (type == "age-scatter") { age_scatter(object, strata) } else if (type == "density") { density_plot(object, strata, log) } else { cli::cli_abort( - '`type = "{type}"` is not a valid input; - the currently available options for `type` are "density" or "age-scatter"' + class = "unavailable_type", + message = c( + x = "{.fn autoplot.pop_data} does not currently have an option for + {.arg type} = {.str {type}}.", + i = "The {.arg type} argument accepts options + {.str density} or {.str age-scatter}." + ) ) } } age_scatter <- function( object, - strata = NULL) { + strata = NULL, + age_var = object %>% get_age_var(), + value_var = object %>% get_value_var()) { # create default plotting if (is.null(strata)) { plot1 <- object %>% - ggplot2::ggplot(ggplot2::aes(x = .data[[object %>% get_age_var()]], - y = .data[[object %>% get_value_var()]], - col = get(strata) - ) - ) + ggplot2::ggplot() + + ggplot2::aes( + x = .data[[age_var]], + y = .data[[value_var]] + ) } else { plot1 <- object %>% - ggplot2::ggplot( - ggplot2::aes( - x = .data[[object %>% get_age_var()]], - y = .data[[object %>% get_value_var()]] - ), - col = get(strata) + ggplot2::ggplot() + + ggplot2::aes( + col = .data[[strata]], + x = .data[[age_var]], + y = .data[[value_var]] ) + ggplot2::labs(colour = strata) } @@ -73,7 +93,7 @@ age_scatter <- function( ggplot2::theme_linedraw() + # ggplot2::scale_y_log10() + - # avoid log 0 (https://forum.posit.co/t/using-log-transformation-but-need-to-preserve-0/129197/4) + # avoid log 0 (https://bit.ly/4eqDkT4) ggplot2::scale_y_continuous( trans = scales::pseudo_log_trans(sigma = 0.01), breaks = c(-1, -0.1, 0, 0.1, 1, 10), @@ -99,10 +119,12 @@ age_scatter <- function( density_plot <- function( object, strata = NULL, - log = FALSE) { + log = FALSE, + value_var = object %>% get_value_var()) { plot1 <- object %>% - ggplot2::ggplot(ggplot2::aes(x = .data[[object %>% get_value_var()]])) + + ggplot2::ggplot() + + ggplot2::aes(x = .data[[value_var]]) + ggplot2::theme_linedraw() + ggplot2::facet_wrap(~antigen_iso, nrow = 3) @@ -122,10 +144,11 @@ density_plot <- function( ggplot2::labs(fill = strata) } if (log) { + min_nonzero_val <- object %>% - filter(object %>% get_value() > 0) %>% get_value() %>% + purrr::keep(~ . > 0) %>% min() max_val <- diff --git a/_pkgdown.yml b/_pkgdown.yml index e837b2fc..f41dd17c 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -4,3 +4,6 @@ template: light-switch: true bibliography: vignettes/references.bib + +search: + exclude: ['preview/'] diff --git a/inst/WORDLIST b/inst/WORDLIST index f03a07b0..ef8029da 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -53,6 +53,7 @@ Nadu OSF Orientia PLoS +PRs Pebody Polina RStudio @@ -84,9 +85,6 @@ Valk Vectorized Vellore Versteegh -VignetteEncoding -VignetteEngine -VignetteIndexEntry Volterra Wetering Wiens @@ -97,7 +95,6 @@ behaviour bioassays biomarker boldsymbol -bookdown callout campylobacteriosis cdot @@ -125,7 +122,6 @@ isotypes jinf jitter kDa -knitr le leq llik @@ -155,7 +151,6 @@ qquad recombinant renewcommand rescale -rmarkdown savePath sectionally sera @@ -178,11 +173,9 @@ subfigures th tibble titers -toc tsutsugamushi undercount unstratified varepsilon vec vee -xsectionalData diff --git a/man/autoplot.pop_data.Rd b/man/autoplot.pop_data.Rd index 177e283b..c6e0908b 100644 --- a/man/autoplot.pop_data.Rd +++ b/man/autoplot.pop_data.Rd @@ -11,9 +11,11 @@ \item{log}{whether to show antibody responses on logarithmic scale} -\item{type}{an option to choose type of chart: the current options are \code{"density"} or \code{"age-scatter"}} +\item{type}{an option to choose type of chart: +the current options are \code{"density"} or \code{"age-scatter"}} -\item{strata}{the name of a variable in \code{pop_data} to stratify by (or \code{NULL} for no stratification)} +\item{strata}{the name of a variable in \code{pop_data} +to stratify by (or \code{NULL} for no stratification)} \item{...}{unused} } diff --git a/tests/testthat/_snaps/autoplot.pop_data/age-scatter-no-strat.svg b/tests/testthat/_snaps/autoplot.pop_data/age-scatter-no-strat.svg new file mode 100644 index 00000000..11409cfc --- /dev/null +++ b/tests/testthat/_snaps/autoplot.pop_data/age-scatter-no-strat.svg @@ -0,0 +1,3409 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0.0 +0.1 +1.0 +10.0 + + + + + + + + + + +0 +5 +10 +15 +20 +25 +Age +Antibody Response Value +Quantitative Antibody Responses by Age + + diff --git a/tests/testthat/_snaps/autoplot.pop_data/age-scatter-strat-country.svg b/tests/testthat/_snaps/autoplot.pop_data/age-scatter-strat-country.svg new file mode 100644 index 00000000..f9c0b8fc --- /dev/null +++ b/tests/testthat/_snaps/autoplot.pop_data/age-scatter-strat-country.svg @@ -0,0 +1,3425 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +0.0 +0.1 +1.0 +10.0 + + + + + + + + + + +0 +5 +10 +15 +20 +25 +Age +Antibody Response Value + +Country + + + + + + + + + +Bangladesh +Nepal +Pakistan +Quantitative Antibody Responses by Age + + diff --git a/tests/testthat/_snaps/autoplot.pop_data/density.svg b/tests/testthat/_snaps/autoplot.pop_data/density.svg new file mode 100644 index 00000000..ac954af4 --- /dev/null +++ b/tests/testthat/_snaps/autoplot.pop_data/density.svg @@ -0,0 +1,163 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +HlyE_IgG + + + + + + + + + + +HlyE_IgA + + + + + + + +0 +50 +100 +150 +200 +0.0 +0.1 +0.2 +0.3 +0.4 + + + + + +0.0 +0.1 +0.2 +0.3 +0.4 + + + + + +Antibody Response Value +Frequency + +Country + + + + + + +Bangladesh +Nepal +Pakistan +Distribution of Cross-sectional Antibody Responses + + diff --git a/tests/testthat/test-autoplot.pop_data.R b/tests/testthat/test-autoplot.pop_data.R new file mode 100644 index 00000000..1c3c649a --- /dev/null +++ b/tests/testthat/test-autoplot.pop_data.R @@ -0,0 +1,71 @@ +test_that("`autoplot.pop_data()` raise + an error when unavailable type is provided", + { + xs_data <- load_pop_data( + file_path = "https://osf.io/download//n6cp3/", + age = "Age", + id = "index_id", + value = "result", + standardize = TRUE + ) + expect_error(object = xs_data %>% + autoplot(strata = "Country", type = "den")) + }) + +test_that("`autoplot.pop_data()` raise + an error when unavailable `strata` is provided", + { + xs_data <- load_pop_data( + file_path = "https://osf.io/download//n6cp3/", + age = "Age", + id = "index_id", + value = "result", + standardize = TRUE + ) + expect_error(object = xs_data %>% + autoplot(strata = "strat1", type = "density")) + }) + +test_that("`autoplot.pop_data()` produces + stable results for `type = 'density'`", + { + skip_if(getRversion() < "4.4.1") # 4.3.3 had issues + xs_data <- load_pop_data( + file_path = "https://osf.io/download//n6cp3/", + age = "Age", + id = "index_id", + value = "result", + standardize = TRUE + ) %>% + autoplot(strata = "Country", type = "density") %>% + vdiffr::expect_doppelganger(title = "density") + }) + +test_that("`autoplot.pop_data()` produces stable results for + `type = 'age-scatter'`", + { + + xs_data <- load_pop_data( + file_path = "https://osf.io/download//n6cp3/", + age = "Age", + id = "index_id", + value = "result", + standardize = TRUE + ) %>% + autoplot(strata = "Country", type = "age-scatter") %>% + vdiffr::expect_doppelganger(title = "age_scatter_strat_country") + }) + +test_that("`autoplot.pop_data()` produces stable results + for `type = 'age-scatter', strata = NULL`", + { + xs_data <- load_pop_data( + file_path = "https://osf.io/download//n6cp3/", + age = "Age", + id = "index_id", + value = "result", + standardize = TRUE + ) %>% + autoplot(strata = NULL, type = "age-scatter") %>% + vdiffr::expect_doppelganger(title = "age_scatter_no_strat") + }) diff --git a/vignettes/articles/enteric_fever_example.Rmd b/vignettes/articles/enteric_fever_example.Rmd index 87fb2552..812307d7 100644 --- a/vignettes/articles/enteric_fever_example.Rmd +++ b/vignettes/articles/enteric_fever_example.Rmd @@ -13,7 +13,12 @@ bibliography: ../references.bib --- ## Introduction -This vignette provides users with an example analysis using the [**serocalculator**](https://github.com/UCD-SERG/serocalculator) package by reproducing the analysis for: [**Estimating typhoid incidence from community-based serosurveys: a multicohort study**](https://www.thelancet.com/journals/lanmic/article/PIIS2666-5247(22)00114-8/fulltext) (@Aiemjoy_2022_Lancet). We review the methods underlying the analysis and then walk through an example of enteric fever incidence in Pakistan. Note that because this is a simplified version of the analysis, the results here will differ slightly from those presented in the publication. +This vignette provides users with an example analysis using the [**serocalculator**](https://github.com/UCD-SERG/serocalculator) package +by reproducing the analysis for: +[**Estimating typhoid incidence from community-based serosurveys: a multicohort study**](https://www.thelancet.com/journals/lanmic/article/PIIS2666-5247(22)00114-8/fulltext) (@Aiemjoy_2022_Lancet). +We review the methods underlying the analysis +and then walk through an example of enteric fever incidence in Pakistan. +Note that because this is a simplified version of the analysis, the results here will differ slightly from those presented in the publication. In this example, users will determine the seroincidence of enteric fever in cross-sectional serosurveys conducted as part of the SeroEpidemiology and Environmental Surveillance (SEES) for enteric fever study in Bangladesh, Nepal, and Pakistan. Longitudinal antibody responses were modeled from 1420 blood culture-confirmed enteric fever cases enrolled from the same countries. diff --git a/vignettes/articles/scrubTyphus_example.Rmd b/vignettes/articles/scrubTyphus_example.Rmd index 35ddb68d..9199e4a3 100644 --- a/vignettes/articles/scrubTyphus_example.Rmd +++ b/vignettes/articles/scrubTyphus_example.Rmd @@ -40,9 +40,7 @@ knitr::opts_chunk$set( ) ``` - ### Load packages - The first step in conducting this analysis is to load our necessary packages. If you haven't installed already, you will need to do so before loading. We will also need to have the `tidyverse` and `mixtools` packages installed @@ -128,7 +126,7 @@ Let's also take a look at how antibody responses change by age. ```{r plot-age} # Plot antibody responses by age -autoplot(object = xs_data, type = "age-scatter", strata = "Country") +autoplot(object = xs_data, type = "age-scatter", strata = "country") ``` @@ -166,7 +164,11 @@ b_noise <- xs_data %>% lower_mu <- mixmod$mu[1] lower_sigma <- sqrt(mixmod$sigma[1]) # Calculate the 90th percentile of the lower distribution - percentile75 <- qnorm(0.75, lower_mu, lower_sigma) + percentile75 <- qnorm( + 0.75, + lower_mu, + lower_sigma + ) # Return the results data.frame(antigen_iso = .$antigen_iso[1], percentile75 = percentile75) @@ -247,10 +249,11 @@ est2df <- summary(est2) est_comb <- rbind(estdf, est2df) # Create barplot (rescale incidence rate and CIs) -ggplot(est_comb) + - aes(y = ageQ, x = incidence.rate * 1000, fill = country) + - geom_bar(stat = "identity", - position = position_dodge2(width = 0.8, preserve = "single")) + +ggplot(est_comb, aes(y = ageQ, x = incidence.rate * 1000, fill = country)) + + geom_bar( + stat = "identity", + position = position_dodge2(width = 0.8, preserve = "single") + ) + geom_linerange(aes(xmin = CI.lwr * 1000, xmax = CI.upr * 1000), position = position_dodge2(width = 0.8, preserve = "single")) + labs(title = "Enteric Fever Seroincidence by Catchment Area",