Skip to contents

Convert a fitted ratio model to a format compatible with the posterior and tidybayes packages. Returns draws in a tidy format suitable for further analysis and visualization.

Usage

# S3 method for class 'ratiod_fit'
as_draws(x, ...)

Arguments

x

A ratiod_fit object

...

Additional arguments passed to posterior::as_draws_df

Value

A draws_df object from the posterior package.

Details

The returned draws object is compatible with:

  • posterior package functions (summarise_draws, rvar, etc.)

  • tidybayes package functions (spread_draws, gather_draws, etc.)

  • bayesplot package visualization functions

To work in tidybayes, extract the draws with this function and pass them on to its verbs.

See also

posterior::as_draws_df(), ratio() for ratio-specific extraction

Examples

# \donttest{
# Generate synthetic data
set.seed(111)
n <- 48
site <- rep(letters[1:12], each = 4)
u <- rnorm(12, sd = 0.5)[match(site, letters[1:12])]
depth <- rnorm(n)
df <- data.frame(
  count = rpois(n, exp(3 + 0.3 * depth + u)),
  effort = rgamma(n, shape = 11, rate = exp(-u)),
  depth = depth,
  site = site
)

# Fit model
fit <- tratio(
  count | effort ~ depth + (1 | site),
  data = df,
  family = ratiod_poisson_gamma(),
  mode = "hmc",
  control = list(iter = 400, chains = 1, seed = 1)
)

# Convert to draws format
draws <- as_draws(fit)

# Use with posterior package
if (requireNamespace("posterior", quietly = TRUE)) {
  posterior::summarise_draws(draws)
}
#> # A tibble: 18 × 10
#>    variable         mean   median     sd    mad       q5      q95  rhat ess_bulk
#>    <chr>           <dbl>    <dbl>  <dbl>  <dbl>    <dbl>    <dbl> <dbl>    <dbl>
#>  1 beta_num[1]   2.67e+0  2.67    0.133  0.101   2.49     2.87    1.06      29.0
#>  2 beta_num[2]   2.53e-1  0.254   0.0363 0.0369  0.191    0.309   0.996    124. 
#>  3 beta_denom[…  2.14e+0  2.14    0.137  0.123   1.96     2.35    1.02      30.5
#>  4 beta_denom[…  4.73e-4  0.00446 0.0399 0.0395 -0.0702   0.0597  1.01      48.7
#>  5 sigma_re      4.40e-1  0.419   0.105  0.102   0.303    0.615   1.04      46.1
#>  6 re[1]         5.10e-1  0.506   0.157  0.118   0.287    0.776   1.01      38.4
#>  7 re[2]         5.86e-2  0.0704  0.154  0.152  -0.203    0.294   1.02      36.3
#>  8 re[3]         1.26e-1  0.125   0.158  0.159  -0.116    0.379   1.02      36.5
#>  9 re[4]        -9.49e-1 -0.928   0.199  0.186  -1.34    -0.652   1.02      34.3
#> 10 re[5]         2.10e-1  0.209   0.138  0.122   0.00776  0.430   1.00      50.4
#> 11 re[6]         2.62e-1  0.263   0.161  0.130   0.0800   0.515   1.00      34.5
#> 12 re[7]        -4.85e-1 -0.498   0.160  0.156  -0.754   -0.233   1.03      33.1
#> 13 re[8]        -2.12e-1 -0.214   0.155  0.109  -0.436    0.00943 1.02      38.8
#> 14 re[9]        -6.87e-2 -0.0671  0.148  0.127  -0.298    0.171   1.01      32.5
#> 15 re[10]        1.05e-1  0.105   0.172  0.150  -0.176    0.343   0.995     43.4
#> 16 re[11]        2.40e-1  0.242   0.152  0.150  -0.00974  0.489   1.04      41.8
#> 17 re[12]        2.02e-1  0.210   0.157  0.150  -0.0245   0.446   1.00      39.1
#> 18 shape         1.05e+1 10.2     2.25   2.22    7.30    13.9     1.01      85.9
#> # ℹ 1 more variable: ess_tail <dbl>
# }