Welcome to ClientVPS Mirrors

README

ModalCens: Parametric Modal Regression with Right Censoring

CRAN status R-CMD-check License: GPL-3

ModalCens is an R package for fitting parametric modal regression models to continuous positive random variables. While traditional Generalized Linear Models (GLMs) target the conditional mean, ModalCens directly models the conditional mode \(M_i\), providing a more robust and meaningful measure of central tendency for asymmetric and heavy-tailed distributions — particularly under right censoring.

The methodology is grounded in the framework of modal linear regression introduced by Yao & Li (2014). It extends beyond traditional exponential family distributions to include highly flexible models such as the Log-Logistic and Birnbaum-Saunders distributions, as detailed in our preprint Galarza & Lachos (2026).


Key Features


Installation

# Development version from GitHub
# install.packages("devtools")
devtools::install_github("chedgala/ModalCens")

Quick Start: Multi-Family Comparison

Censoring is simulated for didactic purposes. Each family is fitted on its natural domain: long-tail families (Gamma, Weibull, InvGauss) on the original scale, and Beta on the \((0,1)\) scale.

library(ModalCens)
data(trees)
set.seed(007)

# Simulate 15% right-censoring (cens = 1: censored | cens = 0: observed)
q85      <- quantile(trees$Volume, 0.85)
cens_ind <- as.integer(trees$Volume > q85)

# (0,1) rescaling for Beta only
ys     <- (trees$Volume - min(trees$Volume) + 1) / (max(trees$Volume) - min(trees$Volume) + 2)
q85b   <- quantile(ys, 0.85)
cens_b <- as.integer(ys > q85b)

# Original scale for long-tail families; (0,1) scale for Beta
df_orig <- data.frame(y = pmin(trees$Volume, q85), cens = cens_ind,
                      Girth = trees$Girth, Height = trees$Height)
df_beta <- data.frame(y = pmin(ys, q85b), cens = cens_b,
                      Girth = trees$Girth, Height = trees$Height)

# Fit all families
datasets <- list(gamma = df_orig, weibull = df_orig,
                 invgauss = df_orig, lognormal = df_orig, beta = df_beta,
                 loglogistic = df_orig, bisa = df_orig)
models <- list(); aic_values <- list()

for (f in names(datasets)) {
  mod <- try(modal_cens(y ~ Girth + Height, data = datasets[[f]],
                        cens = datasets[[f]]$cens, family = f), silent = TRUE)
  if (!inherits(mod, "try-error")) { models[[f]] <- mod; aic_values[[f]] <- AIC(mod) }
}

# Select best model by AIC
best <- names(which.min(aic_values))
cat("Best family:", best, "| AIC =", round(aic_values[[best]], 3), "\n")

# Full analysis of the winner
fit <- models[[best]]
summary(fit)
plot(fit)
confint(fit)

Methodology

Mode Reparameterization

The package reparameterizes exponential family densities of the form

\[ f(y_i \mid \theta_i, \phi) = \exp\left\lbrace\frac{y_i \theta_i - b(\theta_i)}{a(\phi)} + c(y_i, \phi)\right\rbrace \]

by solving the first-order condition \(\left.\dfrac{\partial \log f(y_i)}{\partial y_i}\right|_{y_i = M_i} = 0\), which expresses the canonical parameter \(\theta_i\) as an explicit function of the conditional mode \(M_i\) and the dispersion \(\phi\). This yields the mode-link models:

Family Mode–parameter relation
Gamma \(\alpha = \phi^{-1} + 1\), \(\;\text{scale} = M_i \cdot \phi\)
Beta \(\alpha = \phi + 1.01\), \(\;\beta_p = (\alpha - 1)/M_i - \alpha + 2\)
Weibull \(k = \phi + 1.01\), \(\;\lambda = M_i \cdot (k/(k-1))^{1/k}\)
Inv. Gaussian \(\mu = \bigl[M_i^{-2} - 3/(\lambda M_i)\bigr]^{-1/2}\), \(\;\lambda > 3M_i\)
Log-Normal \(\mu_{LN} = \log(M_i) + \sigma^2\), \(\;\sigma = \sqrt{\phi}\)
Log-Logistic \(k = \phi + 1.01\), \(\;\alpha = M_i \cdot \bigl((k+1)/(k-1)\bigr)^{1/k}\)
Birnbaum-Saunders \(\alpha_{bs} = \sqrt{\phi}\), \(\;\beta_{bs} = M_i / \bigl(\sqrt{1+\alpha_{bs}^2/4} - \alpha_{bs}/2\bigr)^2\)

Censored Log-Likelihood

For a sample of \(n\) observations with censoring indicator \(\delta_i \in \{0, 1\}\) (where \(\delta_i = 1\) denotes right-censored):

\[\ell(\boldsymbol{\gamma}, \phi) = \sum_{i=1}^{n} \left[(1 - \delta_i) \log f(y_i \mid M_i, \phi) + \delta_i \log S(y_i \mid M_i, \phi)\right]\]

where \(S(\cdot) = 1 - F(\cdot)\) is the survival function. Maximization is performed via the BFGS algorithm.

Randomized Quantile Residuals

For each observation, define \(u_i = F(y_i \mid \hat{M}_i, \hat{\phi})\). For right-censored observations (\(\delta_i = 1\)), \(u_i\) is drawn uniformly on \([F(y_i), 1]\). The residual is \(r_i = \Phi^{-1}(u_i)\), which follows \(\mathcal{N}(0,1)\) under the correct model (Dunn & Smyth, 1996).


Main Function

modal_cens(formula, data, cens, family = "gamma")
Argument Description
formula A formula object, e.g., y ~ x1 + x2
data A data frame with no missing values
cens Binary vector: 1 = right-censored, 0 = fully observed. Must have no NAs.
family One of "gamma", "beta", "weibull", "invgauss", "lognormal", "loglogistic", "bisa"

The function stops with an error if any NA is detected in cens or in the variables referenced by formula. No imputation is performed.

The function returns an object of class "ModalCens" with methods for summary(), plot(), AIC(), BIC(), logLik(), coef(), vcov(), and residuals().


Authors


References

Need a high-speed mirror for your open-source project?
Contact our mirror admin team at info@clientvps.com.

This archive is provided as a free public service to the community.
Proudly supported by infrastructure from VPSPulse , RxServers , BuyNumber , UnitVPS , OffshoreName and secure payment technology by ArionPay.