Compute posterior distributions of ratios from a fitted ratio model. Ratios are derived quantities: E[numerator] / E[denominator], computed in Stan's generated quantities block with full uncertainty propagation.
Arguments
- object
A
ratiod_fitobject- newdata
Optional data frame for prediction. If NULL, uses original data.
- type
Scale for ratio: "response" (default), "log", or "logit".
- by
Optional grouping variable name to aggregate ratios by group.
- summary
Logical; if TRUE, return summary statistics instead of full posterior draws.
- probs
Quantiles to compute if
summary = TRUE.- ...
Ignored
Examples
# Create a simple family
fam <- ratiod_poisson_gamma()
# \donttest{
# Generate synthetic data
set.seed(123)
n <- 50
df <- data.frame(
count = rpois(n, lambda = 20),
effort = rgamma(n, shape = 10, rate = 1),
depth = rnorm(n),
site = sample(letters[1:5], 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: 50
#> Iterations: 200 (warmup: 100)
#> Running NUTS sampler...
#> Parameters: 11
#> Iterations: 200 (warmup: 100)
#> Chains: 1 (cores: 1)
# Extract all observation-level ratios
r <- ratio(fit)
summary(r)
#> tulpaRatio ratio summary (response scale)
#> ===========================================
#>
#> obs mean sd q2.5 q50 q97.5
#> 1 2.095471 0.16912117 1.817258 2.104386 2.462139
#> 2 1.994679 0.11988040 1.805203 1.991035 2.240366
#> 3 1.977096 0.11269653 1.802464 1.971168 2.201110
#> 4 2.035048 0.13819919 1.809832 2.035061 2.331845
#> 5 2.358317 0.32900598 1.820225 2.332228 3.054777
#> 6 2.036532 0.13891409 1.810017 2.036979 2.335021
#> 7 1.750532 0.09967037 1.549673 1.753678 1.923319
#> 8 1.778376 0.09260133 1.599152 1.779017 1.940970
#> 9 2.080306 0.16104306 1.815418 2.088542 2.429237
#> 10 1.853390 0.08450116 1.705722 1.854757 2.002505
#>
#> ... and 40 more rows
# Log-ratio scale
r_log <- ratio(fit, type = "log")
# Group-level ratios
r_site <- ratio(fit, by = "site")
# }