-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue 442: Generalise to all brms distributions (#459)
* update gitignore to ignore local build paths for testing * breaking rename * start work on primarycensored port in and generalised predict and epred * check new implementatons against ebola vignette * refine posterior prediction and make vignettes faster * rename and move around * refactor tests * check tests * refine tess * add note about direct usage failing but tidybayes working Former-commit-id: 95b48ca Former-commit-id: cf73454e616950a2f6debdc2d039d443fe5537ab Former-commit-id: efd8befdd0b9b9e8f3f7abaefe9f81590bc3ad35 [formerly f61ea80] Former-commit-id: aff38d5ec25ac9be287314158163bfafd6d8fa3a
- Loading branch information
Showing
32 changed files
with
509 additions
and
437 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -25,3 +25,7 @@ data/models/*reparam | |
docs | ||
/doc/ | ||
/Meta/ | ||
vignettes/**_cache/ | ||
.vscode/ | ||
vignettes/figures/ |
This file contains 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 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 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,73 @@ | ||
#' Create a function to draw from the posterior predictive distribution for a | ||
#' latent model | ||
#' | ||
#' This function creates a function that draws from the posterior predictive | ||
#' distribution for a latent model using [primarycensored::rpcens()] to handle | ||
#' censoring and truncation. The returned function takes a `prep` argument from | ||
#' `brms` and returns posterior predictions. This is used internally by | ||
#' [brms::posterior_predict()] to generate predictions for latent models. | ||
#' | ||
#' @inheritParams epidist_family | ||
#' | ||
#' @return A function that takes a `prep` argument from brms and returns a | ||
#' matrix of posterior predictions, with one row per posterior draw and one | ||
#' column per observation. The `prep` object must have the following variables: | ||
#' * `vreal1`: relative observation time | ||
#' * `vreal2`: primary event window | ||
#' * `vreal3`: secondary event window | ||
#' | ||
#' @seealso [brms::posterior_predict()] for details on how this is used within | ||
#' `brms`, [primarycensored::rpcens()] for details on the censoring approach | ||
#' @autoglobal | ||
#' @family gen | ||
#' @export | ||
epidist_gen_posterior_predict <- function(family) { | ||
dist_fn <- .get_brms_fn("posterior_predict", family) | ||
|
||
rdist <- function(n, i, prep, ...) { | ||
prep$ndraws <- n | ||
do.call(dist_fn, list(i = i, prep = prep)) | ||
} | ||
|
||
.predict <- function(i, prep, ...) { | ||
relative_obs_time <- prep$data$vreal1[i] | ||
pwindow <- prep$data$vreal2[i] | ||
swindow <- prep$data$vreal3[i] | ||
|
||
as.matrix(primarycensored::rpcens( | ||
n = prep$ndraws, | ||
rdist = rdist, | ||
rprimary = stats::runif, | ||
pwindow = prep$data$vreal2[i], | ||
swindow = prep$data$vreal3[i], | ||
D = prep$data$vreal1[i], | ||
i = i, | ||
prep = prep | ||
)) | ||
} | ||
return(.predict) | ||
} | ||
|
||
#' Create a function to draw from the expected value of the posterior predictive | ||
#' distribution for a latent model | ||
#' | ||
#' This function creates a function that calculates the expected value of the | ||
#' posterior predictive distribution for a latent model. The returned function | ||
#' takes a `prep` argument (from brms) and returns posterior expected values. | ||
#' This is used internally by [brms::posterior_epred()] to calculate expected | ||
#' values for latent models. | ||
#' | ||
#' @inheritParams epidist_family | ||
#' | ||
#' @return A function that takes a prep argument from brms and returns a matrix | ||
#' of posterior expected values, with one row per posterior draw and one column | ||
#' per observation. | ||
#' | ||
#' @seealso [brms::posterior_epred()] for details on how this is used within | ||
#' `brms`. | ||
#' @autoglobal | ||
#' @family gen | ||
#' @export | ||
epidist_gen_posterior_epred <- function(family) { | ||
.get_brms_fn("posterior_epred", family) | ||
} |
This file contains 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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 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
Oops, something went wrong.