rtmb_mixture: Mixture Model Wrapper for RTMB

View source: R/wrapper_mixture.R

rtmb_mixtureR Documentation

Mixture Model Wrapper for RTMB

Description

Provides a user-friendly interface for fitting Gaussian mixture models with optional covariates on class membership probabilities and various covariance structures.

Usage

rtmb_mixture(
  formula,
  k = 2,
  data = NULL,
  covariance = c("diagonal", "diagonal_equal", "full", "full_equal", "full_equal_corr"),
  prior = prior_flat(),
  y_range = NULL,
  fixed = NULL,
  WAIC = FALSE,
  ...
)

Arguments

formula

A formula specifying the response variable(s). For multivariate, use cbind(y1, y2) ~ 1.

k

Number of mixture components.

data

A data frame containing the variables in the model.

covariance

Covariance structure: "diagonal" (default), "diagonal_equal", "full", "full_equal", or "full_equal_corr".

prior

Prior configuration: 'prior_flat()', 'prior_normal()', 'prior_weak()', 'prior_rhs()', or 'prior_ssp()'. Default is 'prior_flat()'. If 'y_range' is supplied with the default flat prior, the wrapper automatically switches to 'prior_weak()'.

y_range

Optional theoretical range of the response variables. Use a numeric vector 'c(min, max)' for a common range, or a P x 2 matrix/list for response-specific ranges. Specifying this automatically enables 'prior_weak()' when 'prior' is 'prior_flat()' and calibrates the component mean and residual standard-deviation priors.

fixed

Optional named list of fixed values for specific parameters.

WAIC

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

...

Reserved; unused arguments are rejected.

Value

A RTMB_Model object.

Examples


  # Simulate 1D mixture data (2 components)
  set.seed(123)
  N <- 100
  group <- rbinom(N, 1, 0.4)
  y <- ifelse(group == 1, rnorm(N, 5, 1), rnorm(N, 0, 1))
  dat <- data.frame(y)

  # Fit a 1D Gaussian mixture model with 2 components
  fit_mix <- rtmb_mixture(y ~ 1, k = 2, data = dat)
  
  # MAP estimation
  map_mix <- fit_mix$optimize()
  map_mix$summary()

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