Learn R Programming

clmstan

Cumulative Link Models with CmdStanR

Overview

clmstan fits cumulative link models (CLMs) for ordinal categorical data using CmdStanR. It supports 11 link functions including standard links (logit, probit, cloglog) and flexible parametric links (GEV, AEP, Symmetric Power).

Models are pre-compiled using the instantiate package for fast execution without runtime compilation.

Documentation

Full documentation is available at: https://t-momozaki.github.io/clmstan/

Installation

Prerequisites

This package requires:

  1. CmdStan - Stan's command-line interface
  2. cmdstanr - R interface to CmdStan (not on CRAN)

Step 1: Install cmdstanr

# Install cmdstanr from r-universe (recommended)
install.packages("cmdstanr",
                 repos = c("https://stan-dev.r-universe.dev",
                           getOption("repos")))

Step 2: Install CmdStan

library(cmdstanr)
install_cmdstan()  # Only needed once

Step 3: Install clmstan

# From CRAN (when available)
install.packages("clmstan")

# From GitHub (development version)
# install.packages("devtools")
devtools::install_github("t-momozaki/clmstan")

Note: During package installation, Stan models are compiled automatically. This may take a few minutes on first install.

Quick Start

library(clmstan)

# Example data
set.seed(123)
n <- 100
x <- rnorm(n)
latent <- 1.0 * x + rlogis(n)
y <- cut(latent, breaks = c(-Inf, -1, 0, 1, Inf), labels = 1:4)
data <- data.frame(y = y, x = x)

# Fit a cumulative link model with logit link
fit <- clm_stan(y ~ x, data = data, link = "logit",
                chains = 4, iter = 2000, warmup = 1000)

# View results
fit$fit$summary(variables = c("beta", "c_transformed", "beta0"))

Supported Link Functions

Standard Links (5)

LinkDistributionUse Case
logitLogisticDefault, proportional odds
probitNormalSymmetric, latent variable interpretation
cloglogGumbel (max)Asymmetric, proportional hazards
loglogGumbel (min)Asymmetric
cauchitCauchyHeavy tails

Flexible Links with Parameters (6)

LinkParameterDescription
tlink$\nu > 0$$t$-distribution, adjustable tail weight
aranda_ordaz$\lambda > 0$Generalized asymmetric link
sp$r > 0$, baseSymmetric Power, adjustable skewness
log_gamma$\lambda \in \mathbb{R}$Continuous symmetric/asymmetric adjustment
gev$\xi \in \mathbb{R}$Generalized Extreme Value
aep$\theta_1, \theta_2 > 0$Asymmetric Exponential Power

Using Flexible Links

# Fixed parameter
fit_t <- clm_stan(y ~ x, data = data, link = "tlink",
                  link_param = list(df = 8))

# Estimate parameter with Bayesian inference
fit_gev <- clm_stan(y ~ x, data = data, link = "gev",
                    link_param = list(xi = "estimate"))

Threshold Structures

StructureDescription
flexibleFree thresholds (default)
equidistantEqual spacing between thresholds
symmetricSymmetric around center
# Equidistant thresholds
fit_equi <- clm_stan(y ~ x, data = data, threshold = "equidistant")

Prior Specification

Default Priors

clmstan uses weakly informative default priors:

ParameterDefault Prior
Regression coefficients ($\beta$)normal(0, 2.5)
Thresholds ($c$)normal(0, 10)
Equidistant spacing ($d$)gamma(2, 0.5)

For link parameters estimated via Bayesian inference:

LinkParameterDefault Prior
tlink$\nu$gamma(2, 0.1)
aranda_ordaz$\lambda$gamma(0.5, 0.5)
sp$r$gamma(0.5, 0.5)
log_gamma$\lambda$normal(0, 1)
gev$\xi$normal(0, 2)
aep$\theta_1, \theta_2$gamma(2, 1)

Custom Priors

Use the prior() function with distribution helpers:

# Tighter prior on regression coefficients
fit <- clm_stan(y ~ x, data = data,
                prior = prior(normal(0, 1), class = "b"))

# Multiple priors
fit <- clm_stan(y ~ x, data = data,
                prior = c(
                  prior(normal(0, 1), class = "b"),
                  prior(normal(0, 5), class = "Intercept")
                ))

Prior for Link Parameters

When estimating link parameters, you can specify custom priors:

# Custom prior for t-link df parameter
fit <- clm_stan(y ~ x, data = data, link = "tlink",
                link_param = list(df = "estimate"),
                prior = prior(gamma(3, 0.2), class = "df"))

# Custom prior for GEV xi parameter
fit <- clm_stan(y ~ x, data = data, link = "gev",
                link_param = list(xi = "estimate"),
                prior = prior(normal(0, 0.5), class = "xi"))

Available Distribution Functions

FunctionParametersExample
normal(mu, sigma)$\mu$: mean, $\sigma$: SDnormal(0, 2.5)
gamma(alpha, beta)$\alpha$: shape, $\beta$: rategamma(2, 0.1)
student_t(df, mu, sigma)$\nu$: df, $\mu$: location, $\sigma$: scalestudent_t(3, 0, 2.5)
cauchy(mu, sigma)$\mu$: location, $\sigma$: scalecauchy(0, 2.5)
flat()noneflat()

Note: flat() creates an improper uniform prior. Use with caution as it may lead to improper posteriors if the data does not provide sufficient information. For thresholds with ordered constraints, Stan's internal transformation provides implicit regularization.

Prior Classes

ClassDescriptionCompatible Distributions
bRegression coefficients ($\beta$)normal, student_t, cauchy, flat
InterceptThresholds ($c$, flexible)normal, student_t, cauchy, flat
c1First threshold ($c_1$, equidistant)normal, student_t, cauchy, flat
cposPositive thresholds (symmetric)normal, student_t, cauchy, flat
dEquidistant spacing ($d$)gamma
dft-link degrees of freedom ($\nu$)gamma
lambda_aoAranda-Ordaz $\lambda$gamma
rSymmetric Power $r$gamma
lambda_lgLog-gamma $\lambda$normal, student_t, cauchy
xiGEV $\xi$normal, student_t, cauchy
theta1, theta2AEP shape ($\theta_1, \theta_2$)gamma

License

MIT

Copy Link

Version

Install

install.packages('clmstan')

Version

0.1.1

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Tomotaka Momozaki

Last Published

February 12th, 2026

Functions in clmstan (0.1.1)

get_default_threshold_prior

Get default priors for threshold parameters
get_link_param_for_draw

Get link parameters for a specific draw
get_link_type

Get link type number for Stan
link_functions

Available Link Functions
flat

Flat (Improper Uniform) Prior Distribution
gamma

Gamma Distribution for Prior Specification
coef.clmstan

Extract coefficients from clmstan objects
compute_probs_all

Compute category probabilities for all draws
get_link_params

Get required parameters for a link function
get_base_link_params

Get base (fixed) link parameters from object
get_model_name

Get Stan model name based on threshold and full model flag
is_flexible_link

Check if a link function requires parameters
get_sp_base_type

Get base type number for SP link
get_threshold_names

Generate threshold names in ordinal::clm style
get_threshold_params

Get threshold parameter information
format_dist

Format a distribution object as a string
is.clmstan

Check if object is clmstan
map_class_to_params

Map Parameter Class and Distribution to Legacy Parameter Names
parse_clm_formula

Parse formula for CLM
cloglog_cdf

Complementary log-log CDF
gev_cdf

Generalized Extreme Value (GEV) link CDF
extract_posterior_summary

Extract posterior summary for specified parameters
extract_prediction_draws

Extract posterior draws needed for prediction
get_clmstan_draws

Get draws for plotting with bayesplot
predict.clmstan

Predict method for clmstan objects
prepare_prediction_matrix

Prepare design matrix for prediction
print.clm_prior_list

Print method for clm_prior_list objects
posterior_predict.clmstan

Posterior predictive distribution for clmstan objects
print.clm_prior_spec

Print method for clm_prior_spec objects
student_t

Student-t Distribution for Prior Specification
prepare_link_params_full

Prepare link parameters for full Stan model
prepare_link_params

Prepare link parameters for Stan
prepare_stan_data_symmetric

Prepare data for symmetric threshold Stan model
needs_full_model

Check if any link parameter requires estimation
prepare_stan_data_equidistant

Prepare data for equidistant threshold Stan model
loggamma_cdf

Log-gamma link CDF
prepare_stan_data_full

Prepare data for full Stan model with link parameter inference
prepare_stan_data_dispatch

Dispatch to appropriate data preparation function
loglog_cdf

Log-log CDF
get_coef_names

Get coefficient names from design matrix
print.summary.clmstan

Print method for summary.clmstan objects
print.clmstan

Print method for clmstan objects
prior

Specify Prior Distributions
validate_full_params

Validate full model parameters
summarize_class_draws

Summarize class prediction draws
supported_links

Get supported link functions
validate_threshold

Validate threshold specification
validate_prior_values

Validate prior values
plot.clmstan

Plot method for clmstan objects
supported_thresholds

Get supported threshold structures
summary.clmstan

Summary method for clmstan objects
extract_link_params_summary

Extract posterior summary for link parameters
validate_link

Check if a link function is valid
prepare_stan_data

Prepare data for Stan model
summarize_probs_draws

Summarize probability draws
extract_log_lik

Extract log-likelihood matrix from clmstan object
waic.clmstan

Widely Applicable Information Criterion for clmstan objects
print.clm_dist

Print method for clm_dist objects
normal

Normal Distribution for Prior Specification
print.clm_prior

Print method for clm_prior objects
loo.clmstan

Leave-One-Out Cross-Validation for clmstan objects
make_design_matrix

Create design matrix
get_default_link_prior

Get default prior for a link parameter
validate_link_params

Validate link parameters
get_default_priors

Get default priors for a threshold structure
sp_cdf

Symmetric Power (SP) link CDF
new_clmstan

Create a clmstan object (internal constructor)
validate_prediction_input

Validate prediction inputs
validate_prior

Validate prior specification for a model
validate_prior_class_dist

Validate that the distribution is compatible with the parameter class
clm_cdf

Unified CDF dispatcher for clmstan
clm_stan

Fit a Cumulative Link Model using CmdStanR
clmstan-class

clmstan S3 Class
clm_prior

Prior Specification for clmstan
cauchy

Cauchy Distribution for Prior Specification
c.clm_prior_spec

Combine Multiple Prior Specifications
diagnostics

MCMC Diagnostics for clmstan objects
extract_acf

Extract ACF values from clmstan object
compute_probs_single

Compute category probabilities for a single draw
clmstan-package

clmstan: Cumulative Link Models with CmdStanR
convert_prior_spec_to_legacy

Convert Prior Specification to Internal Format
aranda_ordaz_cdf

Aranda-Ordaz asymmetric link CDF
apply_priors_to_stan_data

Apply user priors to Stan data
aep_cdf

Asymmetric Exponential Power (AEP) link CDF
apply_custom_link_priors

Apply custom prior specifications for link parameters
extract_link_param_draws

Extract link parameter draws for full model
fitted.clmstan

Fitted values for clmstan objects
extract_y_rep

Extract y_rep from Stan fit
compute_r_eff

Compute relative effective sample size for log-likelihood