-
Notifications
You must be signed in to change notification settings - Fork 0
Creating a default print for sr_model #114
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
base: main
Are you sure you want to change the base?
Changes from all commits
aea299a
a13db94
eef3d8e
147ae9e
ea74517
4af5d36
102cb80
7323fda
88b96af
91a887f
a7afd63
f5be2b3
ba5973f
4ed27e6
8e5eba7
f377d5c
9711df6
ff9ee85
8eed4a7
7a5ec62
eb4e282
141ba3c
f95a88a
4dcd890
ee0a73c
0dbc9a2
a22ed27
29ec557
9de64b0
b1d15c4
0dcac18
86e9965
d97e4b2
3665740
afc12a7
42b1810
5c9fe1c
1252e18
9a6931e
6dda0ca
d72bedc
26c7800
27b1c52
e6b54c1
1657ff1
571bf16
95a91c9
3915ec2
4233b52
37f451e
3f8c706
afbb88a
ff21aa3
47ab5b0
0c2a6bd
04c7200
87dd5c7
9f2fd61
70c94c4
55c97aa
948b2e0
058ed61
a24fc41
1b90556
2cad837
97f2931
f9a3f53
597d613
3fc100c
64d7ea4
2985fc6
46d53a3
f0bf068
c25630c
4a14365
89e5bf5
a2be022
c9847dc
e160b39
7fdb0d4
c6de84f
2eb9a29
91de966
dee362d
822b1e3
d411543
9546eef
6f19b4e
deb74e8
3ef9db1
a7ddcc0
61b802d
76225d2
0e52f43
215e02b
004d427
06d8d53
5d086af
8145cc9
60acbd4
129adce
ec7f1ed
0be057f
1a97278
ec88ab1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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). | ||
| #' } | ||
|
sschildhauer marked this conversation as resolved.
|
||
| #' @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) | ||
|
sschildhauer marked this conversation as resolved.
Comment on lines
+22
to
+28
|
||
| } else { | ||
|
sschildhauer marked this conversation as resolved.
|
||
| 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) | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| <int> <int> <chr> <chr> <chr> <chr> <dbl> | ||
| 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 | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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)) | ||
| } | ||
| ) | ||
|
sschildhauer marked this conversation as resolved.
|
||
|
|
||
| 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 | ||
| ) |> | ||
|
sschildhauer marked this conversation as resolved.
|
||
| suppressWarnings() | ||
|
sschildhauer marked this conversation as resolved.
|
||
|
|
||
| testthat::expect_snapshot( | ||
| print(results), | ||
| variant = darwin_variant() | ||
| ) | ||
| } | ||
|
sschildhauer marked this conversation as resolved.
|
||
| ) | ||
Uh oh!
There was an error while loading. Please reload this page.