rtmb_corr: Fit a Correlation Model using RTMB

View source: R/wrapper_corr.R

rtmb_corrR Documentation

Fit a Correlation Model using RTMB

Description

'rtmb_corr' fits a correlation model to estimate means, standard deviations, and correlation structures. It supports simple correlation, multilevel correlation, and classical frequentist estimation.

Usage

rtmb_corr(
  x = NULL,
  data = NULL,
  ID = NULL,
  covariates = NULL,
  method = c("pearson", "spearman", "reml"),
  prior = prior_flat(),
  y_range = NULL,
  init = NULL,
  fixed = NULL,
  missing = c("listwise", "fiml", "pairwise"),
  WAIC = FALSE
)

Arguments

x

A matrix, data frame, formula, or expression (e.g., cbind(V1, V2)) of response variables.

data

An optional data frame containing the variables.

ID

A character string or expression specifying the group ID variable for multilevel models.

covariates

Optional numeric matrix or data frame of covariates to be included in the joint MVN model.

method

Correlation method for classic(): "pearson", "spearman", or "reml".

prior

Prior configuration object: 'prior_flat()', 'prior_normal()', or 'prior_weak()'. Default is 'prior_flat()'. For 'prior_normal()', correlation models use the aliases 'mean_sd' and 'sd_rate', which override the common arguments 'mu_sd' and 'sigma_rate'. An LKJ prior with 'lkj_eta = 1' is added automatically unless 'lkj_eta' is supplied explicitly.

y_range

Optional numeric vector or matrix defining the theoretical range (min, max) of response variables. Required when using prior_weak(). Can be a vector of length 2 (applies to all variables) or a matrix/list of length P.

init

Optional list of initial values.

fixed

Optional named list of fixed values for specific parameters.

missing

Missing value handling strategy: "listwise" (default), "pairwise", or "fiml" (Full Information Maximum Likelihood).

WAIC

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

Examples


  # Estimate the correlation between two variables in the debate dataset
  data(debate, package = "BayesRTMB")

  fit_corr <- rtmb_corr(cbind(sat, perf), data = debate)

  
  mcmc_corr <- fit_corr$sample(sampling = 500, warmup = 500, chains = 2)
  mcmc_corr$summary()

  bf_corr <- mcmc_corr$bayes_factor(fixed = list(corr = 0))
  print(bf_corr)
  

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