Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions R/prep_priors.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
#' @title Prepare priors
#'The function is preparing a set of hyperparameters for a Bayesian model that characterizes antibody (or biomarker) responses.
#'It assumes that each antigen-isotype is modeled by 5 parameters, which are transformed by taking their logarithm
#'(with the fifth parameter adjusted as shape-1). These 5 parameters are named in the code as:
#'1. y0
#'2. y1,
#'3. t1,
#'4. alpha, and
#'5. shape
#'
#' @param max_antigens [integer]: how many antigen-isotypes will be modeled
#'
#' @returns a [list] with elements:
#' "n_params": how many parameters??
#' - "mu.hyp": ??
#' - "prec.hyp": ??
#' - "omega" : ??
#' - "wishdf": Wishart distribution degrees of freedom
#' - "prec.logy.hyp": array of hyper-parameters for the precision
#' (inverse variance) of the biomarkers, on log-scale
#' n_params: Number of model parameters (log-transformed y0, y1, t1, alpha, shape-1).
#' mu.hyp: Hyperparameter means for the log-transformed model parameters.
#' prec.hyp: Diagonal precision matrix for the normal priors, setting the inverse variance for each parameter.
#' omega: Diagonal scale matrix used with the Wishart prior to shape the covariance structure.
#' wishdf: Degrees of freedom for the Wishart prior (set to 20).
#' prec.logy.hyp: Hyperparameters (e.g., shape and rate) for the precision (inverse variance) of the log-scale biomarkers.

#' @export
#'
#' @examples
Expand All @@ -24,14 +32,24 @@ prep_priors <- function(max_antigens) {
prec_logy_hyp <- array(NA, dim = c(max_antigens, 2))

# Fill parameter arrays
# the parameters are log(c(y0, y1, t1, alpha, shape-1))
# the parameters are log(c(y0, y1, t1, alpha, shape-1))
for (k.test in 1:max_antigens) {
mu_hyp[k.test, ] <- c(1.0, 7.0, 1.0, -4.0, -1.0)
prec_hyp[k.test, , ] <- diag(c(1.0, 0.00001, 1.0, 0.001, 1.0))
omega[k.test, , ] <- diag(c(1.0, 50.0, 1.0, 10.0, 1.0))
wishdf[k.test] <- 20
prec_logy_hyp[k.test, ] <- c(4.0, 1.0)
}


# uninformative testing priors
# for (k.test in 1:max_antigens) {
# mu_hyp[k.test, ] <- c(1.0, 1.0, 1.0, -4.0, -1.0)
# prec_hyp[k.test, , ] <- diag(c(0.00001, 1e-8, 0.00001, 0.00001, 0.00001))
# omega[k.test, , ] <- diag(c(1.0, 50.0, 1.0, 10.0, 1.0))
# wishdf[k.test] <- 20
# prec_logy_hyp[k.test, ] <- c(4.0, 1.0)
# }

# test for change

Expand Down
49 changes: 49 additions & 0 deletions inst/examples/run-mod-ka-example.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

options(scipen = 999)
library(runjags)

dataset <- serodynamics_example(

Check warning on line 5 in inst/examples/run-mod-ka-example.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=inst/examples/run-mod-ka-example.R,line=5,col=4,[indentation_linter] Indentation should be 0 spaces but is 4 spaces.
"SEES_Case_Nepal_ForSeroKinetics_02-13-2025.csv"
) |>
readr::read_csv() |>
dplyr::mutate(
.by = person_id,
visit_num = dplyr::row_number()
) |>
as_case_data(
id_var = "person_id",
biomarker_var = "antigen_iso",
value_var = "result",
time_in_days = "dayssincefeveronset"
)

results <- run_mod(

Check warning on line 20 in inst/examples/run-mod-ka-example.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=inst/examples/run-mod-ka-example.R,line=20,col=4,[indentation_linter] Indentation should be 0 spaces but is 4 spaces.
data = dataset, # The data set input
file_mod = serodynamics_example("model.jags"),
nchain = 2, # Number of mcmc chains to run
nadapt = 100, # Number of adaptations to run
nburn = 100, # Number of unrecorded samples before sampling begins
nmc = 100,
niter = 1000, # Number of iterations
strat = NA # Variable to be stratified
) |>
suppressWarnings() |>
magrittr::use_series("curve_params")


test.1 <- results %>% group_by(Iso_type, Parameter_sub) %>% summarise(mean = mean(value), sd = sd(value))

Check warning on line 34 in inst/examples/run-mod-ka-example.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=inst/examples/run-mod-ka-example.R,line=34,col=1,[object_name_linter] Variable and function name style should match snake_case or symbols.

Check warning on line 34 in inst/examples/run-mod-ka-example.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=inst/examples/run-mod-ka-example.R,line=34,col=19,[pipe_consistency_linter] Use the |> pipe operator instead of the %>% pipe operator.

Check warning on line 34 in inst/examples/run-mod-ka-example.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=inst/examples/run-mod-ka-example.R,line=34,col=57,[pipe_consistency_linter] Use the |> pipe operator instead of the %>% pipe operator.

Check warning on line 34 in inst/examples/run-mod-ka-example.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=inst/examples/run-mod-ka-example.R,line=34,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 105 characters.

wide_predpar_df <- results %>%

Check warning on line 36 in inst/examples/run-mod-ka-example.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=inst/examples/run-mod-ka-example.R,line=36,col=28,[pipe_consistency_linter] Use the |> pipe operator instead of the %>% pipe operator.
select(-Parameter) %>%

Check warning on line 37 in inst/examples/run-mod-ka-example.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=inst/examples/run-mod-ka-example.R,line=37,col=22,[pipe_consistency_linter] Use the |> pipe operator instead of the %>% pipe operator.
rename(parameter = Parameter_sub,
antigen_iso = Iso_type) %>%

Check warning on line 39 in inst/examples/run-mod-ka-example.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=inst/examples/run-mod-ka-example.R,line=39,col=34,[pipe_consistency_linter] Use the |> pipe operator instead of the %>% pipe operator.
pivot_wider(names_from = "parameter", values_from="value") %>%

Check warning on line 40 in inst/examples/run-mod-ka-example.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=inst/examples/run-mod-ka-example.R,line=40,col=52,[infix_spaces_linter] Put spaces around all infix operators.
rowwise() %>%
#mutate(y1 = y0+y1) %>%
droplevels() %>%
ungroup() %>%
rename(r = shape)


autoplot(serocalculator::as_curve_params(wide_predpar_df))

Loading