/
tar_stan_example_data.R
42 lines (42 loc) · 1.68 KB
/
tar_stan_example_data.R
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
#' @title Simulate example data for [tar_stan_example_file()].
#' @export
#' @family examples
#' @description An example dataset compatible with the model file
#' from [tar_stan_example_file()].
#' @details The `tar_stan_example_data()` function draws a Stan
#' dataset from the prior predictive distribution of the
#' model from [tar_stan_example_file()]. First, the
#' regression coefficient `beta` is drawn from its standard
#' normal prior, and the covariate `x` is computed.
#' Then, conditional on the `beta` draws and the covariate,
#' the response vector `y` is drawn from its
#' Normal(`x * beta`, 1) likelihood.
#' @return List, dataset compatible with the model file from
#' [tar_stan_example_file()].
#' @format A list with the following elements:
#' * `n`: integer, number of data points.
#' * `x`: numeric, covariate vector.
#' * `y`: numeric, response variable.
#' * `true_beta`: numeric of length 1, value of the regression
#' coefficient `beta` used in simulation.
#' * `.join_data`: a list of simulated values to be appended
#' to as a `.join_data` column in the output of
#' targets generated by functions such as
#' [tar_stan_mcmc_rep_summary()]. Contains the
#' regression coefficient `beta` (numeric of length 1)
#' and prior predictive data `y` (numeric vector).
#' @param n Integer of length 1, number of data points.
#' @examples
#' tar_stan_example_data()
tar_stan_example_data <- function(n = 10L) {
beta <- stats::rnorm(n = 1, mean = 0, sd = 1)
x <- seq(from = -1, to = 1, length.out = n)
y <- stats::rnorm(n, x * beta, 1)
list(
n = n,
x = x,
y = y,
true_beta = beta,
.join_data = list(beta = beta, y_rep = y)
)
}