-
Notifications
You must be signed in to change notification settings - Fork 0
Reorganize repo into package-ready structure with pkgdown + mock data + tests #11
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
Merged
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
ee4c8e9
Delete R/create_incidence_table.R
Kwan-Jenny 4207ef5
Delete R/create_noise_df.R
Kwan-Jenny a05bd51
Delete R/plot_summary_metrics.R
Kwan-Jenny 1b39d19
Delete R/simulate_seroincidence.R
Kwan-Jenny 992fdb0
Delete R/prepare_df_for_serocalculator.R
Kwan-Jenny 94f3bd9
Delete R/postprocess_jags_output.R
Kwan-Jenny 19eb4d2
Delete R/generate_final_table.R
Kwan-Jenny fb6f507
Delete R/create_xs_data.R
Kwan-Jenny 40ddcd8
Delete R/process_shigella_data.R
Kwan-Jenny 9d1b8ac
Add process_shigella_data function for data reshaping
Kwan-Jenny 58db1c7
Add function to summarize antibody trajectories for newperson
Kwan-Jenny 137a598
Add function to predict posterior at specified times
Kwan-Jenny f054420
Add compute_residual_metrics function for antibody data
Kwan-Jenny 9efd9b1
Add model comparison function with residual metrics
Kwan-Jenny 4655d41
Add fmt_mci function to format median and intervals
Kwan-Jenny ae38681
Add function to extract and summarize newperson parameters
Kwan-Jenny 87c45a2
Update R/fig2_overall_newperson.R
Kwan-Jenny e7648cd
Update R/prep_newperson_params.R
Kwan-Jenny 3702aa1
Update R/model_comparison_table.R
Kwan-Jenny be6012c
Update R/compute_residual_metrics.R
Kwan-Jenny cca5392
Update R/process_shigella_data.R
Kwan-Jenny 9b608fe
Fix package dependencies, namespace exports, and package infrastructu…
Copilot 756544d
Fix 5 CI failures: version bump, LICENSE, non-ASCII chars, VignetteBu…
Copilot e4e6c42
Update man/fmt_mci.Rd to match roxygen docs (devtools::document() sync)
Copilot 190c42f
Fix review feedback: rlang::sym rename, t1=0 edge case, shigella::: i…
Copilot c101941
Restore ab() and bt() to exact upstream implementations; update t1=0 …
Copilot 913eb3b
Update data-raw/mock_data.R
Kwan-Jenny 1fa288b
Fix roxygen docs: give bt() its own params, move trajectory docs to ab()
Copilot 6b11bbf
Fix Github -> GitHub capitalization in inst/WORDLIST
Copilot 9059965
Fix Github -> GitHub capitalization in NEWS.md
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,48 @@ | ||
| on: | ||
| push: | ||
| branches: [main, master] | ||
| pull_request: | ||
| branches: [main, master] | ||
|
|
||
| name: R-CMD-check | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| R-CMD-check: | ||
| runs-on: ${{ matrix.config.os }} | ||
|
|
||
| name: ${{ matrix.config.os }} (${{ matrix.config.r }}) | ||
|
|
||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| config: | ||
| - {os: ubuntu-latest, r: 'release'} | ||
| - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'} | ||
|
|
||
| env: | ||
| GITHUB_PAT: ${{ github.token }} | ||
| R_KEEP_PKG_SOURCE: yes | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - uses: r-lib/actions/setup-pandoc@v2 | ||
|
|
||
| - uses: r-lib/actions/setup-r@v2 | ||
| with: | ||
| r-version: ${{ matrix.config.r }} | ||
| http-user-agent: ${{ matrix.config.http-user-agent }} | ||
| use-public-rspm: true | ||
|
|
||
| - uses: r-lib/actions/setup-r-dependencies@v2 | ||
| with: | ||
| extra-packages: any::rcmdcheck | ||
| needs: check | ||
|
|
||
| - uses: r-lib/actions/check-r-package@v2 | ||
| with: | ||
| upload-snapshots: true | ||
| build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")' |
This file contains hidden or 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,46 @@ | ||
| on: | ||
| push: | ||
| branches: [main, master] | ||
| release: | ||
| types: [published] | ||
| workflow_dispatch: | ||
|
|
||
| name: pkgdown | ||
|
|
||
| permissions: | ||
| contents: write | ||
| pages: write | ||
|
|
||
| jobs: | ||
| pkgdown: | ||
| runs-on: ubuntu-latest | ||
| # Only restrict concurrency for non-PR jobs | ||
| concurrency: | ||
| group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }} | ||
| env: | ||
| GITHUB_PAT: ${{ github.token }} | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - uses: r-lib/actions/setup-pandoc@v2 | ||
|
|
||
| - uses: r-lib/actions/setup-r@v2 | ||
| with: | ||
| use-public-rspm: true | ||
|
|
||
| - uses: r-lib/actions/setup-r-dependencies@v2 | ||
| with: | ||
| extra-packages: any::pkgdown, local::. | ||
| needs: website | ||
|
|
||
| - name: Build site | ||
| run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE) | ||
| shell: Rscript {0} | ||
|
|
||
| - name: Deploy to GitHub pages 🚀 | ||
| if: github.event_name != 'pull_request' | ||
| uses: JamesIves/github-pages-deploy-action@v4 | ||
| with: | ||
| clean: false | ||
| branch: gh-pages | ||
| folder: docs |
This file contains hidden or 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 hidden or 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 hidden or 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,2 @@ | ||
| YEAR: 2024 | ||
| COPYRIGHT HOLDER: Kwan Ho Lee, Douglas Ezra Morrison |
This file contains hidden or 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 hidden or 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 hidden or 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,183 @@ | ||
| #' Residual-based posterior predictive metrics for longitudinal antibody curves | ||
| #' | ||
| #' Computes residuals between observed antibody measurements and posterior | ||
| #' median predictions evaluated at the observed time points. Returns pointwise | ||
| #' residuals or aggregated error metrics (MAE, RMSE, SSE) at multiple | ||
| #' summary levels. | ||
| #' | ||
| #' @param model A data frame of posterior draws in long format with columns: | ||
| #' \code{Subject}, \code{Iso_type}, \code{Chain}, \code{Iteration}, | ||
| #' \code{Parameter}, \code{value}. | ||
| #' @param dataset A \code{serodynamics} case dataset produced by | ||
| #' \code{serodynamics::as_case_data()} (must contain \code{id} and | ||
| #' \code{antigen_iso} columns, and time/value attributes). | ||
| #' @param ids Character vector of subject IDs to include (matched against | ||
| #' \code{dataset$id} and \code{model$Subject}). | ||
| #' @param antigen_iso Character scalar specifying the antigen/isotype to analyze | ||
| #' (matched against \code{dataset$antigen_iso} and \code{model$Iso_type}). | ||
| #' @param scale Scale on which to compute residuals. One of \code{"original"} | ||
| #' or \code{"log"}. If \code{"log"}, residuals are computed on the natural | ||
| #' log scale and observations/predictions \eqn{\le 0} are removed. | ||
| #' @param summary_level Level at which to summarize metrics. One of: | ||
| #' \describe{ | ||
| #' \item{\code{"pointwise"}}{ | ||
| #' Return pointwise residuals at each observed time point. | ||
| #' } | ||
| #' \item{\code{"id_antigen"}}{ | ||
| #' Summarize by \code{id} and \code{antigen_iso}. | ||
| #' } | ||
| #' \item{\code{"antigen"}}{Summarize by \code{antigen_iso} only.} | ||
| #' \item{\code{"overall"}}{Single summary across all included observations.} | ||
| #' } | ||
| #' | ||
| #' @return A tibble. For \code{summary_level = "pointwise"}, returns | ||
| #' per-observation residuals. Otherwise returns MAE, RMSE, SSE, and | ||
| #' \code{n_obs} at the requested summary level. | ||
| #' | ||
| #' @details | ||
| #' The posterior predictive summary uses the posterior median at each observed | ||
| #' time point, with a 95\% credible interval computed from draw-level | ||
| #' predictions. Predictions are generated by | ||
| #' \code{\link{predict_posterior_at_times}}. | ||
| #' | ||
| #' @examples | ||
| #' \dontrun{ | ||
| #' # Per-ID error metrics (original scale) | ||
| #' m_id <- compute_residual_metrics( | ||
| #' model = overall_sf2a, | ||
| #' dataset = dL_clean_sf2a, | ||
| #' ids = unique(dL_clean_sf2a$id), | ||
| #' antigen_iso = "IgG", | ||
| #' scale = "original", | ||
| #' summary_level = "id_antigen" | ||
| #' ) | ||
| #' | ||
| #' # Pointwise residuals on log scale | ||
| #' r_pw <- compute_residual_metrics( | ||
| #' model = overall_sf2a, | ||
| #' dataset = dL_clean_sf2a, | ||
| #' ids = "SOSAR-22008", | ||
| #' antigen_iso = "IgA", | ||
| #' scale = "log", | ||
| #' summary_level = "pointwise" | ||
| #' ) | ||
| #' } | ||
| #' | ||
| #' @export | ||
| compute_residual_metrics <- function(model, | ||
| dataset, | ||
| ids, | ||
| antigen_iso, | ||
| scale = c("original", "log"), | ||
| summary_level = c( | ||
| "id_antigen", | ||
| "pointwise", | ||
| "antigen", | ||
| "overall" | ||
| )) { | ||
|
|
||
| scale <- match.arg(scale) | ||
| summary_level <- match.arg(summary_level) | ||
|
|
||
| time_var <- get_timeindays_var(dataset) | ||
| value_var <- serocalculator::get_values_var(dataset) | ||
|
Kwan-Jenny marked this conversation as resolved.
|
||
|
|
||
| observed_data <- dataset |> | ||
| dplyr::rename( | ||
| t = !!rlang::sym(time_var), | ||
| obs = !!rlang::sym(value_var) | ||
| ) |> | ||
|
Kwan-Jenny marked this conversation as resolved.
|
||
| dplyr::select(dplyr::all_of(c("id", "t", "obs", "antigen_iso"))) |> | ||
| dplyr::mutate(id = as.character(.data$id)) |> | ||
| dplyr::filter(.data$id %in% ids, .data$antigen_iso == antigen_iso) | ||
|
|
||
| if (nrow(observed_data) == 0) { | ||
| cli::cli_abort( | ||
| "No observed data found for the specified IDs and antigen_iso." | ||
| ) | ||
| } | ||
|
|
||
| obs_times <- sort(unique(observed_data$t)) | ||
|
|
||
| predictions_all <- predict_posterior_at_times( | ||
| model = model, | ||
| ids = ids, | ||
| antigen_iso = antigen_iso, | ||
| times = obs_times | ||
| ) | ||
|
|
||
| pred_summary <- predictions_all |> | ||
| dplyr::summarise( | ||
| .by = c("id", "t"), | ||
| pred_med = stats::median(.data$res, na.rm = TRUE), | ||
| pred_lower = stats::quantile(.data$res, probs = 0.025, na.rm = TRUE), | ||
| pred_upper = stats::quantile(.data$res, probs = 0.975, na.rm = TRUE) | ||
| ) |> | ||
| dplyr::mutate(id = as.character(.data$id)) | ||
|
|
||
| residual_data <- observed_data |> | ||
| dplyr::inner_join(pred_summary, by = c("id", "t")) | ||
|
|
||
|
Kwan-Jenny marked this conversation as resolved.
|
||
| if (scale == "log") { | ||
| residual_data <- residual_data |> | ||
| dplyr::filter(.data$obs > 0, .data$pred_med > 0) |> | ||
| dplyr::mutate( | ||
| residual = log(.data$obs) - log(.data$pred_med), | ||
| abs_residual = abs(.data$residual), | ||
| sq_residual = .data$residual^2 | ||
| ) | ||
| } else { | ||
| residual_data <- residual_data |> | ||
| dplyr::mutate( | ||
| residual = .data$obs - .data$pred_med, | ||
| abs_residual = abs(.data$residual), | ||
| sq_residual = .data$residual^2 | ||
| ) | ||
| } | ||
|
|
||
| if (summary_level == "pointwise") { | ||
| return(residual_data |> | ||
| dplyr::select( | ||
| "id", | ||
| "antigen_iso", | ||
| "t", | ||
| "obs", | ||
| "pred_med", | ||
| "pred_lower", | ||
| "pred_upper", | ||
| "residual", | ||
| "abs_residual", | ||
| "sq_residual" | ||
| )) | ||
| } | ||
|
|
||
| if (summary_level == "id_antigen") { | ||
| return(residual_data |> | ||
| dplyr::summarise( | ||
| .by = c("id", "antigen_iso"), | ||
| MAE = mean(.data$abs_residual, na.rm = TRUE), | ||
| RMSE = sqrt(mean(.data$sq_residual, na.rm = TRUE)), | ||
| SSE = sum(.data$sq_residual, na.rm = TRUE), | ||
| n_obs = dplyr::n() | ||
| )) | ||
| } | ||
|
|
||
| if (summary_level == "antigen") { | ||
| return(residual_data |> | ||
| dplyr::summarise( | ||
| .by = "antigen_iso", | ||
| MAE = mean(.data$abs_residual, na.rm = TRUE), | ||
| RMSE = sqrt(mean(.data$sq_residual, na.rm = TRUE)), | ||
| SSE = sum(.data$sq_residual, na.rm = TRUE), | ||
| n_obs = dplyr::n() | ||
| )) | ||
| } | ||
|
|
||
| residual_data |> | ||
| dplyr::summarise( | ||
| MAE = mean(.data$abs_residual, na.rm = TRUE), | ||
| RMSE = sqrt(mean(.data$sq_residual, na.rm = TRUE)), | ||
| SSE = sum(.data$sq_residual, na.rm = TRUE), | ||
| n_obs = dplyr::n() | ||
| ) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.