diff --git a/DESCRIPTION b/DESCRIPTION index ea5d92de..a46b1417 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: serodynamics Title: What the Package Does (One Line, Title Case) -Version: 0.0.0.9047 +Version: 0.0.0.9048 Authors@R: c( person("Peter", "Teunis", , "p.teunis@emory.edu", role = c("aut", "cph"), comment = "Author of the method and original code."), diff --git a/NAMESPACE b/NAMESPACE index 2db4d027..01b45b59 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,7 @@ # Generated by roxygen2: do not edit by hand S3method(autoplot,case_data) +S3method(print,sr_model) export(as_case_data) export(autoplot) export(expect_snapshot_data) diff --git a/NEWS.md b/NEWS.md index 89199522..709e7f8b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -15,6 +15,7 @@ ## New features +* Creating default print.sr_model function for sr_model class (#114) * Made "newperson" optional in `prep_data()` (#73) * Including fitted and residual values as data frame in run_mod output. (#101) * Added `plot_predicted_curve()` with support for faceting by multiple IDs (#68) diff --git a/R/Run_Mod.R b/R/Run_Mod.R index 25e75d60..a065875a 100644 --- a/R/Run_Mod.R +++ b/R/Run_Mod.R @@ -209,4 +209,5 @@ run_mod <- function(data, jags_out <- jags_out |> structure(class = union("sr_model", class(jags_out))) jags_out + } diff --git a/R/print.sr_model.R b/R/print.sr_model.R new file mode 100644 index 00000000..1bddcc69 --- /dev/null +++ b/R/print.sr_model.R @@ -0,0 +1,50 @@ +#' @title Default print for [serodynamics::run_mod()] output object of class +#' `sr_model` +#' @description +#' A default print method for class `sr_model` that prints posterior medians +#' for antibody kinetic curve parameters by `Iso_type` and +#' `Stratification` (if specified). +#' @param x An `sr_model` output object from [run_mod()]. +#' @param print_tbl A [logical] indicator to print `x` in the style of a +#' [tibble::tbl_df]. +#' @param ... Additional arguments passed to the print method. +#' @returns Invisibly returns either: +#' \itemize{ +#' \item when `print_tbl = TRUE`, a tibble containing the raw `sr_model` +#' draws; +#' \item otherwise, a data summary containing posterior medians for +#' antibody kinetic curve parameters by `Iso_type` and `Stratification` +#' (if specified). +#' } +#' @export +#' @examples +#' print(nepal_sees_jags_output) +print.sr_model <- function(x, + print_tbl = FALSE, + ...) { # nolint + if (print_tbl) { + x <- dplyr::as_tibble(x) + print(x) + invisible(x) + } else { + cat("An sr_model with the following median values:") + cat("\n") + cat("\n") + x <- suppressWarnings({ + x |> + dplyr::filter(.data$Subject == "newperson") |> + dplyr::summarise(.by = c("Stratification", "Iso_type", "Parameter"), + median_val = stats::median(.data$value)) |> + tidyr::pivot_wider(names_from = "Parameter", + values_from = "median_val") |> + dplyr::arrange(.data$Iso_type) + }) + # Taking out stratification column if not specified + if (!"Stratification" %in% names(x) || all(x$Stratification == "None", + na.rm = TRUE)) { + x <- dplyr::select(x, -dplyr::any_of("Stratification")) + } + print(as.data.frame(x)) + invisible(x) + } +} diff --git a/inst/WORDLIST b/inst/WORDLIST index 8ea3768d..02f97109 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -39,6 +39,9 @@ mcmc mitre newperson nmc +npl +overfit +otimes onwards params pkgdown @@ -48,6 +51,7 @@ rhat seroconversion seroresponse seroresponses +sr strat stratifications tbl diff --git a/man/nepal_sees_jags_output.Rd b/man/nepal_sees_jags_output.Rd index 2bd13915..4ea195d1 100644 --- a/man/nepal_sees_jags_output.Rd +++ b/man/nepal_sees_jags_output.Rd @@ -5,7 +5,7 @@ \alias{nepal_sees_jags_output} \title{SEES Typhoid run_mod jags output} \format{ -An S3 object of class \code{sr_model}: A \link[dplyr:tbl_df]{dplyr::tbl_df} that contains the +An S3 object of class \code{sr_model}: A \link[dplyr:defunct]{dplyr::tbl_df} that contains the posterior predictive distribution of the person-specific parameters for a "new person" with no observed data (\code{Subject = "newperson"}) and posterior distributions of the person-specific parameters for two arbitrarily-chosen diff --git a/man/plot_predicted_curve.Rd b/man/plot_predicted_curve.Rd index 7e2fabde..b5638b88 100644 --- a/man/plot_predicted_curve.Rd +++ b/man/plot_predicted_curve.Rd @@ -31,7 +31,7 @@ samples from the posterior distribution of the model parameters.} \item{antigen_iso}{The antigen isotype to plot; for example, "HlyE_IgA" or "HlyE_IgG".} -\item{dataset}{(Optional) A \link[dplyr:tbl_df]{dplyr::tbl_df} with observed antibody response +\item{dataset}{(Optional) A \link[dplyr:defunct]{dplyr::tbl_df} with observed antibody response data. Must contain: \itemize{ diff --git a/man/print.sr_model.Rd b/man/print.sr_model.Rd new file mode 100644 index 00000000..b302a5a1 --- /dev/null +++ b/man/print.sr_model.Rd @@ -0,0 +1,35 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/print.sr_model.R +\name{print.sr_model} +\alias{print.sr_model} +\title{Default print for \code{\link[=run_mod]{run_mod()}} output object of class +\code{sr_model}} +\usage{ +\method{print}{sr_model}(x, print_tbl = FALSE, ...) +} +\arguments{ +\item{x}{An \code{sr_model} output object from \code{\link[=run_mod]{run_mod()}}.} + +\item{print_tbl}{A \link{logical} indicator to print \code{x} in the style of a +\link[tibble:tbl_df-class]{tibble::tbl_df}.} + +\item{...}{Additional arguments passed to the print method.} +} +\value{ +Invisibly returns either: +\itemize{ +\item when \code{print_tbl = TRUE}, a tibble containing the raw \code{sr_model} +draws; +\item otherwise, a data summary containing posterior medians for +antibody kinetic curve parameters by \code{Iso_type} and \code{Stratification} +(if specified). +} +} +\description{ +A default print method for class \code{sr_model} that prints posterior medians +for antibody kinetic curve parameters by \code{Iso_type} and +\code{Stratification} (if specified). +} +\examples{ +print(nepal_sees_jags_output) +} diff --git a/man/run_mod.Rd b/man/run_mod.Rd index 08f91427..585210ae 100644 --- a/man/run_mod.Rd +++ b/man/run_mod.Rd @@ -93,7 +93,7 @@ If specified, must be 2 values long: }} } \value{ -An \code{sr_model} class object: a subclass of \link[dplyr:tbl_df]{dplyr::tbl_df} that +An \code{sr_model} class object: a subclass of \link[dplyr:defunct]{dplyr::tbl_df} that contains MCMC samples from the joint posterior distribution of the model parameters, conditional on the provided input \code{data}, including the following: diff --git a/man/use_att_names.Rd b/man/use_att_names.Rd index f43fcef0..9a2151e0 100644 --- a/man/use_att_names.Rd +++ b/man/use_att_names.Rd @@ -16,7 +16,7 @@ The input \link{data.frame} with columns named after attributes. \description{ \code{use_att_names} takes prepared longitudinal data for antibody kinetic modeling and names columns using attribute values to allow merging -with a modeled \code{\link[=run_mod]{run_mod()}} output \link[dplyr:tbl_df]{dplyr::tbl_df}. The column names include +with a modeled \code{\link[=run_mod]{run_mod()}} output \link[dplyr:defunct]{dplyr::tbl_df}. The column names include \code{Subject}, \code{Iso_type}, \code{t}, and \code{result}. } \keyword{internal} diff --git a/pkgdown/_pkgdown.yml b/pkgdown/_pkgdown.yml index 61c98202..cc4b60b8 100644 --- a/pkgdown/_pkgdown.yml +++ b/pkgdown/_pkgdown.yml @@ -42,6 +42,7 @@ reference: - title: Summarize seroresponse model estimates contents: - post_summ + - print.sr_model - title: Example data sets contents: - serodynamics_example diff --git a/tests/testthat/_snaps/darwin/print.sr_model.md b/tests/testthat/_snaps/darwin/print.sr_model.md new file mode 100644 index 00000000..3948ad19 --- /dev/null +++ b/tests/testthat/_snaps/darwin/print.sr_model.md @@ -0,0 +1,11 @@ +# results consistent with printed output for sr_model as tbl no strat + + Code + print(results) + Output + An sr_model with the following median values: + + Iso_type alpha shape t1 y0 y1 + 1 HlyE_IgA 0.01637070 1.352345 2.74673 2.68448 968.6465 + 2 HlyE_IgG 0.00938515 1.288250 2.97289 2.61949 1099.8900 + diff --git a/tests/testthat/_snaps/print.sr_model.md b/tests/testthat/_snaps/print.sr_model.md new file mode 100644 index 00000000..9aa9ff03 --- /dev/null +++ b/tests/testthat/_snaps/print.sr_model.md @@ -0,0 +1,44 @@ +# results are consistent with printed output for sr_model class + + Code + nepal_sees_jags_output + Output + An sr_model with the following median values: + + Stratification Iso_type alpha shape t1 y0 y1 + 1 typhi HlyE_IgA 0.001508265 1.673340 6.358405 2.340330 258.1235 + 2 paratyphi HlyE_IgA 0.001556295 1.561960 3.903690 2.852925 191.8805 + 3 typhi HlyE_IgG 0.001393980 1.385280 6.019110 1.788035 243.9110 + 4 paratyphi HlyE_IgG 0.001432405 1.386685 4.726980 2.330555 272.8455 + +# results are consistent with printed output for sr_model class as tbl + + Code + print(nepal_sees_jags_output, print_tbl = TRUE) + Output + # A tibble: 70,000 x 7 + Iteration Chain Parameter Iso_type Stratification Subject value + + 1 1 1 alpha HlyE_IgA typhi sees_npl_1 0.00757 + 2 2 1 alpha HlyE_IgA typhi sees_npl_1 0.00794 + 3 3 1 alpha HlyE_IgA typhi sees_npl_1 0.00794 + 4 4 1 alpha HlyE_IgA typhi sees_npl_1 0.0103 + 5 5 1 alpha HlyE_IgA typhi sees_npl_1 0.00925 + 6 6 1 alpha HlyE_IgA typhi sees_npl_1 0.00925 + 7 7 1 alpha HlyE_IgA typhi sees_npl_1 0.00950 + 8 8 1 alpha HlyE_IgA typhi sees_npl_1 0.00950 + 9 9 1 alpha HlyE_IgA typhi sees_npl_1 0.00852 + 10 10 1 alpha HlyE_IgA typhi sees_npl_1 0.00852 + # i 69,990 more rows + +# results consistent with printed output for sr_model as tbl no strat + + Code + print(results) + Output + An sr_model with the following median values: + + Iso_type alpha shape t1 y0 y1 + 1 HlyE_IgA 0.01718040 1.368230 2.755075 2.646905 1184.585 + 2 HlyE_IgG 0.01005685 1.293325 2.910905 2.641075 817.486 + diff --git a/tests/testthat/test-print.sr_model.R b/tests/testthat/test-print.sr_model.R new file mode 100644 index 00000000..87afa4d5 --- /dev/null +++ b/tests/testthat/test-print.sr_model.R @@ -0,0 +1,36 @@ +test_that( + desc = "results are consistent with printed output for sr_model class", + code = { + testthat::expect_snapshot(nepal_sees_jags_output) + } +) + +test_that( + desc = "results are consistent with printed output for sr_model class as tbl", + code = { + testthat::expect_snapshot(print(nepal_sees_jags_output, print_tbl = TRUE)) + } +) + +test_that( + desc = "results consistent with printed output for sr_model as tbl no strat", + code = { + withr::local_seed(1) + dataset <- serodynamics::nepal_sees + results <- run_mod( + data = dataset, # The data set input + file_mod = serodynamics_example("model.jags"), + nchain = 2, # Number of mcmc chains to run + nadapt = 10, # Number of adaptations to run + nburn = 10, # Number of unrecorded samples before sampling begins + nmc = 100, + niter = 100 # Number of iterations + ) |> + suppressWarnings() + + testthat::expect_snapshot( + print(results), + variant = darwin_variant() + ) + } +)