Skip to contents

sample_posterior() draws samples from the posterior distribution of the specified model given a data set with visit data.

sample_prior() draws samples from the prior distribution of the specified model object.

Usage

sample_posterior(
  model,
  data,
  now = NULL,
  nsim = 2000L,
  seed = NULL,
  warmup = 500L,
  nuts_control = list(),
  acceptable_divergent_transition_fraction = 0.1,
  ...
)

sample_prior(
  model,
  nsim = 2000L,
  seed = NULL,
  warmup = 500L,
  nuts_control = list(),
  ...
)

Arguments

model

an object of class srpmodel containing prior information

data

a data frame with variables subject_id<chr> (subject identifier), group_id<chr> (group identifier), t<dbl> (time of visit, relative to first visit in study), state<chr> (state recorded at visit). Allowed states are "stable", "response", "progression" (or death), and "EOF" (end of follow-up). The EOF state marks the end of an individual's follow-up before the absorbing state "progression".

now

numeric, time from first visit in data if different form last recorded visit

nsim

integer, number of samples to draw

seed

integer, fixed random seed; NULL for no fixed seed

warmup

integer, number of warm-up samples for the MCMC sampler before retaining samples; see warmup parameter in rstan::stan().

nuts_control

list, parameters for NUTS algorithm see control argument inrstan::stan()

acceptable_divergent_transition_fraction,

numeric between 0 and 1 giving the acceptable fraction of divergent transitions before throwing an error

...

further arguments passed to method implementations

Value

A rstan::stanfit object with posterior samples.

Examples

mdl <- create_srpmodel(A = define_srp_prior())
tbl <- tibble::tibble(
  subject_id = c("A1", "A1"),
  group_id = c("A", "A"),
  t = c(0, 1.5),
  state = c("stable", "response")
)
sample_posterior(mdl, tbl, seed = 42L)
#> Inference for Stan model: srp_model_simple.
#> 1 chains, each with iter=2500; warmup=500; thin=1; 
#> post-warmup draws per chain=2000, total post-warmup draws=2000.
#> 
#>                mean se_mean    sd   2.5%   25%   50%   75%  97.5% n_eff Rhat
#> p[1]           0.62    0.00  0.22   0.16  0.47  0.64  0.81   0.97  2190    1
#> median_t[1,1]  3.72    0.23  7.51   0.42  1.10  1.95  3.80  17.83  1101    1
#> median_t[1,2] 16.23    0.72 26.85   0.71  3.32  7.80 17.96  81.91  1404    1
#> median_t[1,3] 17.67    0.79 28.91   1.02  3.55  8.00 18.84  90.19  1325    1
#> shape[1,1]     1.54    0.01  0.47   0.83  1.21  1.47  1.81   2.66  2493    1
#> shape[1,2]     1.57    0.01  0.49   0.83  1.22  1.50  1.82   2.68  3233    1
#> shape[1,3]     1.60    0.01  0.51   0.85  1.22  1.52  1.91   2.73  3183    1
#> scale[1,1]     5.01    0.32 10.51   0.53  1.40  2.48  5.02  25.33  1066    1
#> scale[1,2]    21.09    0.95 35.37   0.90  4.25 10.13 23.37 103.49  1385    1
#> scale[1,3]    22.94    1.02 38.15   1.26  4.57 10.34 24.44 119.57  1409    1
#> lp__          -8.45    0.08  2.00 -13.16 -9.47 -8.11 -6.99  -5.59   660    1
#> 
#> Samples were drawn using NUTS(diag_e) at Mon Apr 17 14:49:45 2023.
#> For each parameter, n_eff is a crude measure of effective sample size,
#> and Rhat is the potential scale reduction factor on split chains (at 
#> convergence, Rhat=1).

sample_prior(mdl, seed = 42L)
#> Inference for Stan model: srp_model_simple.
#> 1 chains, each with iter=2500; warmup=500; thin=1; 
#> post-warmup draws per chain=2000, total post-warmup draws=2000.
#> 
#>                mean se_mean    sd   2.5%   25%   50%   75%  97.5% n_eff Rhat
#> p[1]           0.50    0.00  0.24   0.06  0.31  0.50  0.68   0.93  4210    1
#> median_t[1,1] 18.44    1.66 60.24   0.66  3.35  7.62 17.93  93.39  1313    1
#> median_t[1,2] 15.94    0.70 27.92   0.71  3.45  7.75 17.83  77.51  1570    1
#> median_t[1,3] 16.69    0.76 31.94   0.68  3.35  7.63 17.93  93.79  1761    1
#> shape[1,1]     1.57    0.01  0.49   0.83  1.21  1.51  1.86   2.70  2917    1
#> shape[1,2]     1.56    0.01  0.49   0.84  1.20  1.48  1.85   2.68  4107    1
#> shape[1,3]     1.58    0.01  0.52   0.83  1.19  1.51  1.86   2.80  3429    1
#> scale[1,1]    23.99    2.22 80.73   0.86  4.36  9.84 23.33 123.84  1321    1
#> scale[1,2]    20.70    0.90 35.96   0.94  4.45  9.97 22.84 100.43  1609    1
#> scale[1,3]    21.89    1.05 43.80   0.84  4.33  9.75 22.90 120.76  1735    1
#> lp__          -6.02    0.07  1.97 -10.54 -7.08 -5.65 -4.56  -3.28   768    1
#> 
#> Samples were drawn using NUTS(diag_e) at Mon Apr 17 14:49:45 2023.
#> For each parameter, n_eff is a crude measure of effective sample size,
#> and Rhat is the potential scale reduction factor on split chains (at 
#> convergence, Rhat=1).