-
Notifications
You must be signed in to change notification settings - Fork 0
Add Stan support as alternative Bayesian modeling backend #175
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
Draft
Copilot
wants to merge
6
commits into
main
Choose a base branch
from
copilot/convert-jags-model-to-stan
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6b44d2f
Initial plan
Copilot 368fb4d
Add initial Stan implementation with model files and R functions
Copilot 5d88e45
Add examples, fix linting issues, and update WORDLIST for Stan functions
Copilot 530fc7f
Update README and copilot instructions to document Stan support
Copilot e973b69
Add *.knit.* to .gitignore and install cmdstanr in copilot-setup-step…
Copilot d5ad3ce
Wrap run_mod_stan examples in \dontrun{} to fix R CMD check failure
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
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 |
|---|---|---|
|
|
@@ -28,3 +28,4 @@ docs/ | |
|
|
||
| **/*.quarto_ipynb | ||
| README.html | ||
| *.knit.* | ||
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
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,76 @@ | ||
| #' Prepare data for Stan | ||
| #' | ||
| #' @param dataframe a [data.frame] containing case data | ||
| #' @param biomarker_column [character] string indicating | ||
| #' which column contains antigen-isotype names | ||
| #' @param verbose whether to produce verbose messaging | ||
| #' @param add_newperson whether to add an extra record with missing data | ||
| #' | ||
| #' @returns a `prepped_stan_data` object (a [list] with Stan-formatted data) | ||
| #' @export | ||
| #' | ||
| #' @examples | ||
| #' set.seed(1) | ||
| #' raw_data <- | ||
| #' serocalculator::typhoid_curves_nostrat_100 |> | ||
| #' sim_case_data(n = 5) | ||
| #' prepped_data <- prep_data_stan(raw_data) | ||
| prep_data_stan <- function( | ||
| dataframe, | ||
| biomarker_column = get_biomarker_names_var(dataframe), | ||
| verbose = FALSE, | ||
| add_newperson = TRUE) { | ||
|
|
||
| # First use existing prep_data function to get the base structure | ||
| jags_data <- prep_data( | ||
| dataframe = dataframe, | ||
| biomarker_column = biomarker_column, | ||
| verbose = verbose, | ||
| add_newperson = add_newperson | ||
| ) | ||
|
|
||
| # Convert to Stan format | ||
| # Stan requires explicit max dimensions | ||
| max_nsmpl <- max(jags_data$nsmpl) | ||
|
|
||
| # Create padded arrays (Stan doesn't handle ragged arrays like JAGS) | ||
| # We need to pad smpl.t and logy to max_nsmpl | ||
| nsubj <- jags_data$nsubj | ||
| n_antigen_isos <- jags_data$n_antigen_isos | ||
|
|
||
| # Initialize with zeros (will be ignored in model for obs > nsmpl[subj]) | ||
| smpl_t_padded <- array(0, dim = c(nsubj, max_nsmpl)) | ||
| logy_padded <- array(0, dim = c(nsubj, max_nsmpl, n_antigen_isos)) | ||
|
|
||
| # Fill in actual data | ||
| for (subj in 1:nsubj) { | ||
| n_obs <- jags_data$nsmpl[subj] | ||
| if (n_obs > 0) { | ||
| smpl_t_padded[subj, 1:n_obs] <- jags_data$smpl.t[subj, 1:n_obs] | ||
| for (k in 1:n_antigen_isos) { | ||
| logy_padded[subj, 1:n_obs, k] <- jags_data$logy[subj, 1:n_obs, k] | ||
| } | ||
| } | ||
| } | ||
|
|
||
| stan_data <- list( | ||
| nsubj = nsubj, | ||
| n_antigen_isos = n_antigen_isos, | ||
| n_params = 5, # y0, y1, t1, alpha, shape | ||
| nsmpl = as.integer(jags_data$nsmpl), | ||
| max_nsmpl = as.integer(max_nsmpl), | ||
| smpl_t = smpl_t_padded, | ||
| logy = logy_padded | ||
| ) | ||
|
|
||
| # Add attributes from JAGS data | ||
| stan_data <- stan_data |> | ||
| structure( | ||
| class = c("prepped_stan_data", "list"), | ||
| antigens = attributes(jags_data)$antigens, | ||
| n_antigens = attributes(jags_data)$n_antigens, | ||
| ids = attributes(jags_data)$ids | ||
| ) | ||
|
|
||
| return(stan_data) | ||
| } |
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,80 @@ | ||
| #' @title Prepare priors for Stan | ||
| #' @description | ||
| #' Takes multiple [vector] inputs to allow for modifiable priors for Stan | ||
| #' models. Converts JAGS precision-based priors to Stan covariance-based | ||
| #' priors. | ||
| #' | ||
| #' @inheritParams prep_priors | ||
| #' | ||
| #' @returns A "curve_params_priors_stan" object | ||
| #' (a subclass of [list] with the inputs to `prep_priors_stan()` attached | ||
| #' as [attributes] entry named `"used_priors"`), containing Stan-formatted | ||
| #' priors. | ||
| #' @export | ||
| #' @example inst/examples/examples-prep_priors_stan.R | ||
|
|
||
| prep_priors_stan <- function( | ||
| max_antigens, | ||
| mu_hyp_param = c(1.0, 7.0, 1.0, -4.0, -1.0), | ||
| prec_hyp_param = c(1.0, 0.00001, 1.0, 0.001, 1.0), | ||
| omega_param = c(1.0, 50.0, 1.0, 10.0, 1.0), | ||
| wishdf_param = 20, | ||
| prec_logy_hyp_param = c(4.0, 1.0)) { | ||
|
|
||
| # Input validation (same as prep_priors) | ||
| if (length(mu_hyp_param) != 5) { | ||
| cli::cli_abort("Need to specify 5 priors for {.arg mu_hyp_param}") | ||
| } | ||
| if (length(prec_hyp_param) != 5) { | ||
| cli::cli_abort("Need to specify 5 priors for {.arg prec_hyp_param}") | ||
| } | ||
| if (length(omega_param) != 5) { | ||
| cli::cli_abort("Need to specify 5 priors for {.arg omega_param}") | ||
| } | ||
| if (length(wishdf_param) != 1) { | ||
| cli::cli_abort("Need to specify 1 prior for {.arg wishdf_param}") | ||
| } | ||
| if (length(prec_logy_hyp_param) != 2) { | ||
| cli::cli_abort("Need to specify 2 priors for {.arg prec_logy_hyp_param}") | ||
| } | ||
|
|
||
| # Model parameters | ||
| n_params <- 5 | ||
| mu_hyp <- array(NA, dim = c(max_antigens, n_params)) | ||
| prec_hyp <- array(NA, dim = c(max_antigens, n_params, n_params)) | ||
| omega <- array(NA, dim = c(max_antigens, n_params, n_params)) | ||
| wishdf <- rep(NA, max_antigens) | ||
| prec_logy_hyp <- array(NA, dim = c(max_antigens, 2)) | ||
|
|
||
| # Fill parameter arrays (same structure as JAGS) | ||
| for (k in 1:max_antigens) { | ||
| mu_hyp[k, ] <- mu_hyp_param | ||
| prec_hyp[k, , ] <- diag(prec_hyp_param) | ||
| omega[k, , ] <- diag(omega_param) | ||
| wishdf[k] <- wishdf_param | ||
| prec_logy_hyp[k, ] <- prec_logy_hyp_param | ||
| } | ||
|
|
||
| # Return results as a list (Stan model will handle conversion) | ||
| prepped_priors <- list( | ||
| "n_params" = n_params, | ||
| "mu_hyp" = mu_hyp, | ||
| "prec_hyp" = prec_hyp, | ||
| "omega" = omega, | ||
| "wishdf" = wishdf, | ||
| "prec_logy_hyp" = prec_logy_hyp | ||
| ) |> | ||
| structure(class = c("curve_params_priors_stan", "list")) | ||
|
|
||
| # Add used priors as attributes | ||
| prepped_priors <- prepped_priors |> | ||
| structure("used_priors" = list( | ||
| mu_hyp_param = mu_hyp_param, | ||
| prec_hyp_param = prec_hyp_param, | ||
| omega_param = omega_param, | ||
| wishdf_param = wishdf_param, | ||
| prec_logy_hyp_param = prec_logy_hyp_param | ||
| )) | ||
|
|
||
| return(prepped_priors) | ||
| } |
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.