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

as_draws(x, ...)

# 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

For tidybayes integration, use this function to extract draws, then apply tidybayes functions for posterior analysis.

See also

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

Examples

# \donttest{
# Generate synthetic data
set.seed(111)
n <- 45
df <- data.frame(
  count = rpois(n, lambda = 22),
  effort = rgamma(n, shape = 11, rate = 1),
  depth = rnorm(n),
  site = sample(letters[1:4], n, replace = TRUE)
)

# Fit model
fit <- tratio(
  count | effort ~ depth + (1 | site),
  data = df,
  family = ratiod_poisson_gamma(),
  mode = "hmc",
  control = list(iter = 200, warmup = 100, chains = 1)
)
#> Inference: Exact (Tier 1)
#>   Backend: hmc
#> Fitting ratio model...
#>   Family: poisson_gamma
#>   Observations: 45
#>   Iterations: 200 (warmup: 100)
#> Running NUTS sampler...
#>   Parameters: 10
#>   Iterations: 200 (warmup: 100)
#>   Chains: 1 (cores: 1)
#> Warning: 3 divergent transition(s) after warmup. Increase max_treedepth or reparameterize.

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

# Use with posterior package
if (requireNamespace("posterior", quietly = TRUE)) {
  posterior::summarise_draws(draws)
}
#> # A tibble: 10 × 10
#>    variable         mean    median     sd    mad       q5     q95  rhat ess_bulk
#>    <chr>           <dbl>     <dbl>  <dbl>  <dbl>    <dbl>   <dbl> <dbl>    <dbl>
#>  1 beta_num[1]   3.09     3.09 e+0 0.0424 0.0455  3.02e+0  3.16   1.04     15.7 
#>  2 beta_num[2]  -0.0502  -4.54 e-2 0.0209 0.0232 -8.22e-2 -0.0231 0.999    51.5 
#>  3 beta_denom[…  2.41     2.42 e+0 0.0597 0.0543  2.34e+0  2.51   1.01     25.9 
#>  4 beta_denom[… -0.0125  -2.40 e-2 0.0432 0.0370 -7.79e-2  0.0633 1.00     53.5 
#>  5 sigma_re      0.0358   2.48 e-2 0.0335 0.0331  4.74e-4  0.110  1.03     14.1 
#>  6 re[1]        -0.00500  1.11 e-4 0.0324 0.0172 -7.66e-2  0.0343 1.06     23.7 
#>  7 re[2]        -0.00405 -2.01 e-3 0.0311 0.0102 -5.07e-2  0.0406 1.01     17.9 
#>  8 re[3]        -0.0265  -9.93 e-3 0.0480 0.0238 -1.49e-1  0.0182 1.02     17.7 
#>  9 re[4]        -0.00729  1.000e-4 0.0384 0.0217 -7.81e-2  0.0352 1.20      4.48
#> 10 shape         9.04     9.02 e+0 1.60   2.18    6.52e+0 11.3    1.07     18.8 
#> # ℹ 1 more variable: ess_tail <dbl>
# }