-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_jags_densitydx.R
More file actions
92 lines (79 loc) · 3.58 KB
/
Copy pathplot_jags_densitydx.R
File metadata and controls
92 lines (79 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#' @title Density Plot Diagnostics
#' @author Sam Schildhauer
#' @description
#' plot_jags_dens() takes a [list] output from [serodynamics::run_mod()]
#' to create density plots for each chain run in the mcmc estimation.
#' Defaults will produce every combination of antigen/antibody, parameters,
#' and stratifications, unless otherwise specified.
#' Antigen/antibody combinations and stratifications will vary by analysis.
#' The antibody dynamic curve includes the following parameters:
#' - y0 = baseline antibody concentration
#' - y1 = peak antibody concentration
#' - t1 = time to peak
#' - r = shape parameter
#' - alpha = decay rate
#' @param data A [list] outputted from run_mod().
#' @param iso Specify [character] string to produce plots of only a
#' specific antigen/antibody combination, entered with quotes. Default outputs
#' all antigen/antibody combinations.
#' @param param Specify [character] string to produce plots of only a
#' specific parameter, entered with quotes. Options include:
#' - `alpha` = posterior estimate of decay rate
#' - `r` = posterior estimate of shape parameter
#' - `t1` = posterior estimate of time to peak
#' - `y0` = posterior estimate of baseline antibody concentration
#' - `y1` = posterior estimate of peak antibody concentration
#' @param strat Specify [character] string to produce plots of specific
#' stratification entered in quotes.
#' @param id Specify [character] id in a [vector] format to produce plots for
#' specific individuals. Default is the `newperson` referring to the predictive
#' distribution.
#' @return A [base::list()] of [ggplot2::ggplot()] objects producing density
#' plots for all the specified input.
#' @export
#' @example inst/examples/examples-plot_jags_densitydx.R
plot_jags_dens <- function(data,
iso = unique(data$Iso_type),
param = unique(data$Parameter),
strat = unique(data$Stratification),
id = c("newperson")) {
attributes_jags <- data[["attributes"]]
dens_id_list <- list()
for (h in id) {
visualize_jags_sub <- data |>
dplyr::filter(.data$Subject == h)
stratify <- dplyr::intersect(unique(visualize_jags_sub$Stratification),
strat)
dens_strat_list <- list()
for (i in stratify) {
visualize_jags_strat <- visualize_jags_sub |>
dplyr::filter(.data$Stratification == i)
# Creating open list to store ggplots
density_out <- list()
# Looping through the isos
for (j in iso) {
visualize_jags_plot <- visualize_jags_strat |>
dplyr::filter(.data$Iso_type == j)
# Will not loop through parameters, as we may want each to show on the
# same plot by default.
visualize_jags_plot <- visualize_jags_plot |>
dplyr::filter(.data$Parameter %in% param)
visualize_jags_plot <- visualize_jags_plot |>
dplyr::mutate(Parameter = paste0("iso = ", j, ", parameter = ",
.data$Parameter, ", strat = ",
i),
value = log(.data$value))
visualize_jags_plot <- add_jags_attrs(visualize_jags_plot,
attributes_jags)
# Creating density plot
densplot <- ggmcmc::ggs_density(visualize_jags_plot) +
ggplot2::theme_bw() +
ggplot2::labs(x = "log(value)")
density_out[[j]] <- densplot
}
dens_strat_list[[i]] <- density_out
}
dens_id_list[[h]] <- dens_strat_list
}
dens_id_list
}