You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
8c27696 adds a new transform argument to tar_jags_rep_draws() to support simulation-based calibration (SBC). I do not have a section in the vignettes because the SBC R package is not yet on CRAN, but here is an example to demonstrate the use case. I use the model from the vignettes:
model {
for (i in 1:n) {
y[i] ~ dnorm(beta[1] + x[i] * beta[2], 1)
}
for (i in 1:2) {
beta[i] ~ dnorm(0, 1)
}
}
and the following _targets.R file:
# _targets.R file:
library(targets)
library(jagstargets)
options(crayon.enabled=FALSE)
tar_option_set(
packages= c("dplyr", "posterior", "purrr", "tibble"),
memory="transient",
garbage_collection=TRUE
)
generate_data<-function(n=10L) {
beta<-stats::rnorm(n=2, mean=0, sd=1)
x<- seq(from=-1, to=1, length.out=n)
y<-stats::rnorm(n, beta[1] +x*beta[2], 1)
.join_data<-list(beta=beta) # Used by tar_jags_rep_summary() and get_ranks().list(n=n, x=x, y=y, .join_data=.join_data)
}
#' @title Get simulation-based calibration (SBC) ranks for a simulation rep.#' @return A one-row tibble of SBC rank statistics#' with one column for each model parameter.#' @param data JAGS data list.#' @param draws Data frame with one row per MCMC sample and one column#' per model parameter#' @examples#' library(dplyr)#' library(posterior)#' library(purrr)#' library(tibble)#' data <- generate_data()#' draws <- tibble(`beta[1]` = rnorm(100), `beta[2]` = rnorm(100))#' get_ranks(data, draws)get_ranks<-function(data, draws) {
draws<- select(draws, starts_with(names(data$.join_data)))
truth<- map_dbl(names(draws), ~eval(parse(text=.x), envir=data$.join_data))
out<-SBC::calculate_ranks_draws_matrix(truth, as_draws_matrix(draws))
as_tibble(as.list(out))
}
list(
tar_jags_rep_draws(
name=model,
jags_files="model.jags",
data= generate_data(),
parameters.to.save="beta",
batches=5,
reps=4,
transform=get_ranks
)
)
After I run tar_make(), I get the following output for the rank statistics:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
8c27696 adds a new
transform
argument totar_jags_rep_draws()
to support simulation-based calibration (SBC). I do not have a section in the vignettes because theSBC
R package is not yet on CRAN, but here is an example to demonstrate the use case. I use the model from the vignettes:and the following
_targets.R
file:After I run
tar_make()
, I get the following output for the rank statistics:Beta Was this translation helpful? Give feedback.
All reactions