Ask me anything
Ithy Logo
Sign in to save

Stan Code for Bayesian Censored Quantile Regression

A detailed guide and example implementation using a generalized asymmetric Laplace distribution

stan code, regression model, data visualization

Key Highlights

  • Generalized Distribution Definition: Implementation of a generalized asymmetric Laplace (GAL) distribution via a custom log-density function.
  • Censoring Mechanism: Incorporation of censoring indicators to treat censored and uncensored observations distinctly.
  • Bayesian Framework: Use of appropriate priors, latent variable treatment, and likelihood construction to support quantile regression modeling.

Introduction

In Bayesian analysis, quantile regression is frequently implemented by modeling the conditional quantiles of the response variable using the asymmetric Laplace distribution (ALD). When data are censored, modifications are needed to accurately capture the underlying data-generating process. A generalized asymmetric Laplace (GAL) distribution extends the capabilities of the standard ALD by providing extra flexibility with parameters to adjust skewness and tail behavior.

This guide presents a comprehensive Stan code example integrating censoring and the GAL distribution for quantile regression. It discusses model components, the customized log-density function, and the handling of censored observations. The code provided is meant as a starting point and may be adjusted to fit specific modeling contexts.


Model Overview and Components

The model comprises several key elements:

1. Defining the GAL Distribution

The GAL distribution is defined as a generalization of the asymmetric Laplace distribution. For example, one may define the probability density function (pdf) as:

\( \displaystyle f(y \mid \mu, \sigma, \kappa) = \frac{1}{\sigma} \cdot \frac{\kappa}{1+\kappa^2} \exp\left(-\frac{\kappa \, |y-\mu|}{\sigma}\right) \)

where μ is a location parameter, σ is the scale parameter, and κ is a skewness parameter. This formulation conveniently decouples the tail behavior from the central tendencies, allowing the modeler to capture the dynamics of the response variable when particular quantiles are of interest.

2. Incorporating Censoring

In many practical scenarios, the observed data are censored. Censoring implies that for a subset of the data, only partial information on the response is available. The Stan code must therefore condition the likelihood computation on whether an observation is censored. This is typically done by defining a censoring indicator and by using truncated log-likelihood functions for censored observations.

3. Bayesian Framework and Prior Specifications

The Bayesian setup requires prior distributions for all parameters involved in the model. For quantile regression, the regression coefficients, scale parameter, skewness parameter, and any intercept are assigned priors to reflect the prior beliefs or to regularize the model. The flexibility of the Bayesian software, Stan, is then used to incorporate the likelihood contributions from both censored and uncensored data.


Detailed Stan Code Example

Below is an example of the Stan code that integrates the above components. Please note that this code example is simplified for clarity and should be adapted as needed to your specific application.

Stan Code


// Define the GAL distribution function
functions {
  // Log-density for the generalized asymmetric Laplace (GAL) distribution
  real gal_lpdf(real y | real mu, real sigma, real kappa) {
    // The GAL log pdf is defined as:
    // log( kappa / (sigma * (1+kappa^2)) ) - kappa * fabs(y - mu) / sigma
    return log(kappa) - log(sigma) - log(1 + kappa * kappa)
           - kappa * fabs(y - mu) / sigma;
  }
}

data {
  int<lower=1> N;                   // Number of observations
  int<lower=1> K;                   // Number of predictors
  matrix[N, K] X;                   // Design matrix for predictors
  vector[N] y_obs;                  // Response variable (observed)
  int<lower=0,upper=1> censor[N];   // Censoring indicator: 1=censored, 0=observed
  // Hyperparameters for priors
  real<lower=0> sigma_mu;
  real<lower=0> sigma_beta;
  real<lower=0> sigma_sigma;
  real<lower=0> sigma_kappa;
}

parameters {
  real mu;                          // Intercept term
  vector[K] beta;                   // Regression coefficients
  real<lower=0> sigma;              // Scale parameter for the GAL distribution
  real<lower=0> kappa;              // Skewness parameter for the GAL distribution
}

model {
  // Prior distributions
  mu ~ normal(0, sigma_mu);
  beta ~ normal(0, sigma_beta);
  sigma ~ normal(0, sigma_sigma);   // Can replace with Cauchy for heavier tails if needed
  kappa ~ normal(0, sigma_kappa);

  // Likelihood: Censored Data Modeling
  for (i in 1:N) {
    // Compute the location parameter for observation i
    real location = mu + dot_product(X[i], beta);
    
    if (censor[i] == 1) {
      // For censored observations, use the custom GAL log density
      target += gal_lpdf(y_obs[i] | location, sigma, kappa);
    } else {
      // For uncensored observations,
      // one alternative is to use the normal approximation;
      // here, for demonstration, we continue with the GAL formulation
      target += gal_lpdf(y_obs[i] | location, sigma, kappa);
    }
  }
}

generated quantities {
  vector[N] y_rep;     // Posterior predictive replicated outcomes
  for (i in 1:N) {
    // Simulate new outcome based on proposed linear predictor and noise
    y_rep[i] = mu + dot_product(X[i], beta)
               + // This uses a pseudo noise component of a normal distribution for illustration.
                 normal_rng(0, sigma);
  }
}
  

In the code above, the gal_lpdf function encapsulates the log density of the GAL distribution based on a simplified version of the density function. The model block uses the censoring indicator to differentiate between censored and uncensored observations. While the same log density function is applied to both scenarios in this example, a more refined model might involve a truncated distribution for observations determined to be censored.


Understanding the Code Components

Data Block

The data block defines the observed data elements required to build the model:

  • N: Total number of data points.
  • K: Number of predictors used in the regression model.
  • X: Design matrix holding the predictor variables.
  • y_obs: Observed response variable values.
  • censor: Censoring indicator assigning a value of 1 for censored observations, and 0 for uncensored.
  • Hyperparameters for the priors, such as sigma_mu, sigma_beta, etc., provide user-defined scales to tune prior behavior and regularization strength.

Parameters Block

The key parameters include:

  • mu: The intercept.
  • beta: A vector of regression coefficients corresponding to each predictor.
  • sigma: The scale parameter controlling the spread or dispersion of the GAL distribution.
  • kappa: A positive parameter governing skewness, with higher values dictating increased asymmetry in the distribution.

Model Block Explained

Within the model block:

  • Priors: Standard choices (e.g., normal and Cauchy distributions) are used to regularize parameter estimation.
  • Censoring Mechanism: The likelihood is computed using a loop that runs over all observations. The code distinguishes censored observations both via the indicator variable censor. For censored observations, the custom gal_lpdf function is applied ensuring the modeled likelihood adheres to the specified truncation or censoring limits.
  • Likelihood Contribution: Although this example applies a similar density function for both censored and uncensored observations for illustration, in a more detailed implementation one might:
    • Employ truncated density formulations for censored data.
    • Integrate mixture representations or additional layers if using a true generalized model.

Generated Quantities Block

The generated quantities block is used for posterior predictive checks:

  • y_rep: A vector of replicated outcomes based on the model fits that can be used to assess model adequacy through simulations.
  • The noise component added in the simulation is drawn from a normal distribution. This aspect can be extended or replaced if one wants to simulate directly from the GAL distribution or any other preferred formulation.

A Comparative Overview Table

The following table summarizes the key features and alternative approaches from the model and example code provided:

Component Description Alternative Considerations
Distribution Definition Custom log-density function for the GAL distribution using a mixture of parameters (μ, σ, κ). May use brms’ built-in functions or extend to the mixture representations for enhanced flexibility.
Censoring Handling Use of censoring indicators to choose between truncated and standard likelihoods. Can incorporate Tobit models or latent variable methods for more nuanced censoring.
Parameter Priors Normal and Cauchy priors to regularize mu, beta, sigma, and kappa. Priors can be customized based on domain expertise and data characteristics.
Posterior Predictive Checks Simulated replicated outcomes (y_rep) in the generated quantities block. Useful for assessing model performance and calibration against observed data.

Additional Considerations and Extensions

When developing a model for Bayesian censored quantile regression with the GAL distribution, consider the following extensions:

  • Model Complexity and Mixtures: The basic GAL formulation can be extended to a mixture model that accommodates different modes in the data, especially if heterogeneity is suspected.
  • Truncated Distributions: For censored observations, a refined approach may explicitly declare truncated versions of the GAL or an alternative distribution, ensuring the likelihood only integrates over the region where the observations are censored.
  • Efficient Sampling Algorithms: Using functions like map_rect or reparameterization techniques can improve the computational efficiency, especially in high-dimensional or large-sample problems.
  • Model Validation: Implementing robust posterior predictive checks to assess model adequacy is crucial. Simulated data should be compared with observed censored and uncensored data to verify the model’s performance.
  • Software Integration: Integrate the Stan model with R (via RStan or brms) or Python (through PyStan) to benefit from additional diagnostic tools and visualization libraries.

References


Recommended Related Queries

cran.r-project.org
PDF

Last updated March 22, 2025

AI-assisted research. Sources, methodology and corrections · Source-checked guides and tools · Explore published answers

Ask Ithy AI