rtmb_loglinear: RTMB-based Log-linear analysis (Poisson regression)

View source: R/wrapper_loglinear.R

rtmb_loglinearR Documentation

RTMB-based Log-linear analysis (Poisson regression)

Description

Performs Bayesian or Frequentist log-linear analysis (Poisson regression) on a contingency table or raw data.

Usage

rtmb_loglinear(
  formula,
  data,
  prior = prior_flat(),
  y_range = NULL,
  fixed = NULL,
  WAIC = FALSE,
  ...
)

Arguments

formula

A formula (e.g., '~ A + B + A:B') or a contingency table.

data

A data frame (required if 'formula' is used).

prior

An object of class "rtmb_prior" specifying the prior distribution.

y_range

Optional theoretical minimum and maximum values of the count response. If supplied with the default flat prior, 'prior_weak()' is used.

fixed

Optional named list of fixed values for specific parameters.

WAIC

Logical; if TRUE, add pointwise 'log_lik' to the generate block for WAIC.

...

Additional arguments passed to 'rtmb_glm()'.

Value

An 'RTMB_Model' object.

Examples


  # Create a contingency table
  tab <- matrix(c(10, 20, 30, 40), nrow = 2)
  dimnames(tab) <- list(A = c("A1", "A2"), B = c("B1", "B2"))
  
  # Fit a log-linear model (independence model: ~ A + B)
  fit_log <- rtmb_loglinear(~ A + B, data = tab)
  
  # MAP estimation
  map_log <- fit_log$optimize()
  map_log$summary()

BayesRTMB documentation built on Nov. 24, 2026, 9:06 a.m.