Skip to contents

Compute posterior distributions of ratios from a fitted ratio model. Ratios are derived quantities: E[numerator] / E[denominator], computed from each posterior draw, so they carry the full posterior uncertainty.

Usage

ratio(
  object,
  newdata = NULL,
  type = c("response", "log", "logit"),
  by = NULL,
  summary = FALSE,
  probs = c(0.025, 0.5, 0.975),
  ...
)

# S3 method for class 'ratiod_ratio'
summary(object, probs = c(0.025, 0.5, 0.975), ...)

# S3 method for class 'ratiod_ratio'
print(x, ...)

# S3 method for class 'ratiod_ratio_summary'
print(x, n = 10, ...)

Arguments

object

A ratiod_fit object

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

x

A ratiod_ratio object, or the ratiod_ratio_summary its summary() returns.

n

Number of rows to print

Value

A ratiod_ratio object containing posterior draws or summaries.

Examples

# Create a simple family
fam <- ratiod_poisson_gamma()

# \donttest{
# Generate synthetic data: a site effect shared by count and effort
set.seed(123)
n <- 60
site <- rep(letters[1:6], each = 10)
u <- rnorm(6, sd = 0.5)[match(site, letters[1:6])]
depth <- rnorm(n)
df <- data.frame(
  count = rpois(n, exp(3 + 0.3 * depth + u)),
  effort = rgamma(n, shape = 10, 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)
)

# 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.523396 0.12525588 2.287559 2.522919 2.774121
#>    2 1.344486 0.13403784 1.085993 1.350169 1.617813
#>    3 1.657526 0.10756786 1.453498 1.662806 1.838949
#>    4 1.809619 0.09564595 1.642603 1.811728 1.983654
#>    5 3.348648 0.30409800 2.714406 3.358616 3.951932
#>    6 2.431067 0.11173986 2.223885 2.428394 2.650132
#>    7 2.468042 0.11689360 2.242487 2.464920 2.693725
#>    8 2.218183 0.09041575 2.047706 2.218036 2.380397
#>    9 1.738422 0.10093632 1.555407 1.743632 1.913923
#>   10 4.133050 0.52405818 3.144541 4.123865 5.223773
#> 
#> ... and 50 more rows

# Log-ratio scale
r_log <- ratio(fit, type = "log")

# Group-level ratios
r_site <- ratio(fit, by = "site")
# }