Skip to contents

Introduction

This vignette provides a detailed guide to the fcox_bayes() function in the refundBayes package, which fits Bayesian Functional Cox Regression (FCR) models for time-to-event outcomes using Stan. The function extends the scalar-on-function regression framework to survival analysis with right censoring, and is designed with a syntax similar to mgcv::gam.

The methodology follows the tutorial by Jiang, Crainiceanu, and Cui (2025), Tutorial on Bayesian Functional Regression Using Stan, published in Statistics in Medicine.

Install the refundBayes Package

The refundBayes package can be installed from GitHub:

library(remotes)
remotes::install_github("https://github.com/ZirenJiang/refundBayes")

Statistical Model

The Functional Cox Regression Model

The Functional Cox Regression (FCR) model extends the classical Cox proportional hazards model to settings where one or more predictors are functional (i.e., curves or trajectories observed over a continuum).

For subject i=1,…,ni = 1, \ldots, n, let TiT_i be the event time and CiC_i the censoring time. The observed data consist of [Yi,Ξ΄i,𝐙i,{Wi(tm),tmβˆˆπ’―}][Y_i, \delta_i, \mathbf{Z}_i, \{W_i(t_m), t_m \in \mathcal{T}\}], where:

  • Yi=min(Ti,Ci)Y_i = \min(T_i, C_i) is the observed follow-up time,
  • Ξ΄i\delta_i is the censoring indicator with Ξ΄i=0\delta_i = 0 if the event is observed (Yi=TiY_i = T_i) and Ξ΄i=1\delta_i = 1 if the event is censored (Yi<TiY_i < T_i),
  • 𝐙i\mathbf{Z}_i is a pΓ—1p \times 1 vector of scalar predictors,
  • {Wi(tm),tmβˆˆπ’―}\{W_i(t_m), t_m \in \mathcal{T}\} for m=1,…,Mm = 1, \ldots, M is a functional predictor observed over a domain 𝒯\mathcal{T}.

The domain 𝒯\mathcal{T} is not restricted to [0,1][0,1]; it is determined by the actual time points in the data (e.g., 𝒯=[1,1440]\mathcal{T} = [1, 1440] for minute-level 24-hour activity data). See the section on tmat, lmat, and wmat below for details.

The model assumes a proportional hazards structure:

hi(t)=h0(t)exp(Ξ·i)h_i(t) = h_0(t) \exp(\eta_i)

where h0(t)h_0(t) is the baseline hazard function, and Ξ·i\eta_i is the linear predictor defined as:

Ξ·i=Ξ·0+βˆ«π’―Wi(s)Ξ²(s)ds+𝐙it𝛄\eta_i = \eta_0 + \int_{\mathcal{T}} W_i(s)\beta(s)\,ds + \mathbf{Z}_i^t \boldsymbol{\gamma}

Here Ξ·0\eta_0 is the intercept, Ξ²(β‹…)\beta(\cdot) is the functional coefficient, and 𝛄\boldsymbol{\gamma} is the vector of scalar regression coefficients. The integral is approximated by a Riemann sum using the time point matrix tmat and the integration weight matrix lmat (see below).

The log-likelihood for this model has the form:

β„“(𝐘,𝛅;h0,π›ˆ)=βˆ‘i=1n[(1βˆ’Ξ΄i){logh0(yi)+Ξ·iβˆ’H0(yi)exp(Ξ·i)}+Ξ΄i{βˆ’H0(yi)exp(Ξ·i)}]\ell(\mathbf{Y}, \boldsymbol{\delta}; h_0, \boldsymbol{\eta}) = \sum_{i=1}^n \left[ (1 - \delta_i)\left\{\log h_0(y_i) + \eta_i - H_0(y_i)\exp(\eta_i)\right\} + \delta_i\left\{-H_0(y_i)\exp(\eta_i)\right\} \right]

where H0(t)=∫0th0(u)duH_0(t) = \int_0^t h_0(u)\,du is the cumulative baseline hazard function.

Modeling the Baseline Hazard with M-Splines

Unlike the classical Cox model, which leaves the baseline hazard unspecified and uses partial likelihood, the Bayesian approach requires a full likelihood specification. The fcox_bayes() function models the baseline hazard function h0(t)h_0(t) using M-splines:

h0(t)=βˆ‘l=1LclMl(t;𝐀,Ο„)h_0(t) = \sum_{l=1}^L c_l M_l(t; \mathbf{k}, \tau)

where Ml(t;𝐀,Ο„)M_l(t; \mathbf{k}, \tau) denotes the ll-th M-spline basis function with knots 𝐀\mathbf{k} and degree Ο„\tau, and 𝐜=(c1,…,cL)t\mathbf{c} = (c_1, \ldots, c_L)^t are the spline coefficients. The constraints clβ‰₯0c_l \geq 0 and βˆ‘l=1Lcl>0\sum_{l=1}^L c_l > 0 ensure that the baseline hazard is non-negative and non-trivial. M-splines are a family of non-negative, piecewise polynomial basis functions that integrate to one over their support.

The cumulative baseline hazard function is modeled using I-splines, which are the integrated forms of M-splines:

H0(t)=βˆ‘l=1LclIl(t;𝐀,Ο„)H_0(t) = \sum_{l=1}^L c_l I_l(t; \mathbf{k}, \tau)

where Il(t;𝐀,Ο„)=∫0tMl(u;𝐀,Ο„)duI_l(t; \mathbf{k}, \tau) = \int_0^t M_l(u; \mathbf{k}, \tau)\,du. Because M-splines are non-negative, the resulting I-spline cumulative hazard is non-decreasing by construction.

Identifiability Constraint

The I-spline coefficients 𝐜\mathbf{c} and the intercept Ξ·0\eta_0 are not simultaneously identifiable: for any a>0a > 0, the parameter pairs (𝐜,Ξ·0)(\mathbf{c}, \eta_0) and (a𝐜,Ξ·0βˆ’loga)(a\mathbf{c}, \eta_0 - \log a) yield the same hazard function. To resolve this, the model imposes the constraint βˆ‘l=1Lcl=1\sum_{l=1}^L c_l = 1 by assigning a non-informative Dirichlet prior D(𝐜;𝛂)D(\mathbf{c}; \boldsymbol{\alpha}) with 𝛂=(1,…,1)\boldsymbol{\alpha} = (1, \ldots, 1) to the coefficients. Consequently, the intercept defaults to FALSE in fcox_bayes().

Functional Coefficient via Penalized Splines

As in the SoFR model, the functional coefficient Ξ²(s)\beta(s) is represented using KK spline basis functions ψ1(s),…,ψK(s)\psi_1(s), \ldots, \psi_K(s):

Ξ²(s)=βˆ‘k=1Kbkψk(s)\beta(s) = \sum_{k=1}^K b_k \psi_k(s)

The spline coefficients 𝐛\mathbf{b} are reparametrised using the spectral decomposition of the penalty matrix 𝐒\mathbf{S} (the Wahba-O’Sullivan smoothing penalty) to obtain transformed coefficients 𝐛̃=(𝐛̃rt,𝐛̃ft)t\tilde{\mathbf{b}} = (\tilde{\mathbf{b}}_r^t, \tilde{\mathbf{b}}_f^t)^t, where:

  • 𝐛̃r\tilde{\mathbf{b}}_r (random effects) receive a N(𝟎,Οƒb2𝐈)N(\mathbf{0}, \sigma_b^2 \mathbf{I}) prior,
  • 𝐛̃f\tilde{\mathbf{b}}_f (fixed effects) receive non-informative priors.

This reparametrisation ensures numerical stability and efficient sampling in Stan.

The Role of tmat, lmat, and wmat

The integral βˆ«π’―Wi(s)Ξ²(s)ds\int_{\mathcal{T}} W_i(s)\beta(s)\,ds is approximated by the Riemann sum βˆ‘m=1MLmWi(tm)ψk(tm)\sum_{m=1}^M L_m W_i(t_m)\psi_k(t_m), constructed from three user-supplied matrices:

  • tmat (an nΓ—Mn \times M matrix): contains the time points tmt_m at which the functional predictor is observed. The (i,m)(i, m)-th entry equals tmt_m. The range of values in tmat determines the domain of integration 𝒯\mathcal{T}. For example, if the functional predictor is observed at minutes 1,2,…,14401, 2, \ldots, 1440 within a day, then tmat has entries ranging from 11 to 14401440 and 𝒯=[1,1440]\mathcal{T} = [1, 1440]. There is no requirement that the domain be rescaled to [0,1][0, 1].

  • lmat (an nΓ—Mn \times M matrix): contains the integration weights Lm=tm+1βˆ’tmL_m = t_{m+1} - t_m for the Riemann sum approximation. For equally spaced time points with spacing Ξ”t\Delta t, every entry of lmat equals Ξ”t\Delta t. These weights, together with tmat, fully specify how the numerical integration is performed and over what domain.

  • wmat (an nΓ—Mn \times M matrix): contains the functional predictor values. The ii-th row contains the MM observed values Wi(t1),…,Wi(tM)W_i(t_1), \ldots, W_i(t_M) for subject ii.

In the formula s(tmat, by = lmat * wmat, bs = "cc", k = 10), the mgcv infrastructure uses tmat to construct the spline basis ψk(tm)\psi_k(t_m) at the observed time points, and the by = lmat * wmat argument provides the element-wise product Lmβ‹…Wi(tm)L_m \cdot W_i(t_m) that enters the Riemann sum. The basis functions are evaluated on the scale of tmat, and the integration weights in lmat ensure that the discrete sum correctly approximates the integral over the actual domain 𝒯\mathcal{T} β€” regardless of whether it is [0,1][0, 1], [1,1440][1, 1440], or any other interval.

Note that for all subjects, the time points are assumed to be identical so that tim=tmt_{im} = t_m for all i=1,…,ni = 1, \ldots, n. Thus every row of tmat is the same, and every row of lmat is the same. The matrices are replicated across rows to match the mgcv syntax, which expects all terms in the formula to have the same dimensions.

Full Bayesian Model

The complete Bayesian Functional Cox Regression model is:

{π˜βˆΌβ„“(𝐘,𝛅;h0,π›ˆ)π›ˆ=Ξ·0𝐉n+𝐗̃rt𝐛̃r+𝐗̃ft𝐛̃f+𝐙t𝛄𝐛̃r∼N(𝟎,Οƒb2𝐈)Ξ·0∼p(Ξ·0);𝐛̃f∼p(𝐛̃f);π›„βˆΌp(𝛄);Οƒb2∼p(Οƒb2)h0(t)=βˆ‘l=1LclMl(t;𝐀,Ο„)𝐜∼D(𝐜;𝛂)\begin{cases} \mathbf{Y} \sim \ell(\mathbf{Y}, \boldsymbol{\delta}; h_0, \boldsymbol{\eta}) \\ \boldsymbol{\eta} = \eta_0 \mathbf{J}_n + \tilde{\mathbf{X}}_r^t \tilde{\mathbf{b}}_r + \tilde{\mathbf{X}}_f^t \tilde{\mathbf{b}}_f + \mathbf{Z}^t \boldsymbol{\gamma} \\ \tilde{\mathbf{b}}_r \sim N(\mathbf{0}, \sigma_b^2 \mathbf{I}) \\ \eta_0 \sim p(\eta_0);\; \tilde{\mathbf{b}}_f \sim p(\tilde{\mathbf{b}}_f);\; \boldsymbol{\gamma} \sim p(\boldsymbol{\gamma});\; \sigma_b^2 \sim p(\sigma_b^2) \\ h_0(t) = \sum_{l=1}^L c_l M_l(t; \mathbf{k}, \tau) \\ \mathbf{c} \sim D(\mathbf{c}; \boldsymbol{\alpha}) \end{cases}

where most components are shared with the SoFR model, except for the Cox likelihood (first line) and the baseline hazard specification via M-splines with a Dirichlet prior on 𝐜\mathbf{c} (last two lines).

Joint Modeling with FPCA (Optional)

When functional predictors are observed with measurement error, the fcox_bayes() function supports a joint modeling approach that simultaneously estimates FPCA scores and fits the Cox regression model. In this case, the model regresses on the latent trajectory Di(t)D_i(t) rather than the observed (noisy) function Wi(t)=Di(t)+Ο΅i(t)W_i(t) = D_i(t) + \epsilon_i(t), properly accounting for the uncertainty in the score estimates.

The fcox_bayes() Function

Usage

fcox_bayes(
  formula,
  data,
  cens,
  joint_FPCA = NULL,
  intercept = FALSE,
  runStan = TRUE,
  niter = 3000,
  nwarmup = 1000,
  nchain = 3,
  ncores = 1
)

Arguments

Argument Description
formula Functional regression formula, using the same syntax as mgcv::gam. The left-hand side should be the observed survival time Yi=min(Ti,Ci)Y_i = \min(T_i, C_i). Functional predictors are specified using the s() term with by = lmat * wmat to encode the Riemann sum integration.
data A data frame containing all scalar and functional variables used in the model, as well as the survival time as the response variable.
cens A binary vector indicating censoring status for each subject, following the convention: Ξ΄i=0\delta_i = 0 if the event is observed and Ξ΄i=1\delta_i = 1 if censored. Must have the same length as the number of observations in data.
joint_FPCA A logical (TRUE/FALSE) vector of the same length as the number of functional predictors, indicating whether to jointly model FPCA for each functional predictor. Default is NULL, which sets all entries to FALSE (no joint FPCA).
intercept Logical. Whether to include an intercept term in the linear predictor. Default is FALSE, because the intercept is not identifiable simultaneously with the M-spline coefficients for the baseline hazard (see the Identifiability Constraint section above).
runStan Logical. Whether to run the Stan program. If FALSE, the function only generates the Stan code and data without sampling. Default is TRUE.
niter Total number of Bayesian posterior sampling iterations (including warmup). Default is 3000.
nwarmup Number of warmup (burn-in) iterations. Default is 1000.
nchain Number of Markov chains for posterior sampling. Default is 3.
ncores Number of CPU cores to use when executing the chains in parallel. Default is 1.

Return Value

The function returns a list of class "refundBayes" containing the following elements:

Element Description
stanfit The Stan fit object (class stanfit). Can be used for convergence diagnostics, traceplots, and additional summaries via the rstan package.
spline_basis Basis functions used to reconstruct the functional coefficients from the posterior samples.
stancode A character string containing the generated Stan model code.
standata A list containing the data passed to the Stan model.
int A vector of posterior samples for the intercept term Ξ·0\eta_0. NULL by default since intercept = FALSE.
scalar_coef A matrix of posterior samples for scalar coefficients 𝛄\boldsymbol{\gamma}, where each row is one posterior sample and each column corresponds to one scalar predictor. NULL if no scalar predictors are included.
func_coef A list of posterior samples for functional coefficients. Each element is a matrix where each row is one posterior sample and each column corresponds to one location on the functional domain.
baseline_hazard A list containing posterior samples of baseline hazard parameters: bhaz (baseline hazard values) and cbhaz (cumulative baseline hazard values).
family The family type: "Cox".

Formula Syntax

The formula follows the mgcv::gam syntax. The left-hand side is the observed survival time (not a Surv object). The key component for specifying functional predictors is:

s(tmat, by = lmat * wmat, bs = "cc", k = 10)

where:

  • tmat: an nΓ—Mn \times M matrix of time points defining the domain 𝒯\mathcal{T} over which the functional predictor is observed and the integral is computed. The domain adapts to the actual values in tmat (e.g., [0,1][0, 1], [1,1440][1, 1440], etc.).
  • lmat: an nΓ—Mn \times M matrix of Riemann sum weights (Lm=tm+1βˆ’tmL_m = t_{m+1} - t_m), controlling the numerical integration over 𝒯\mathcal{T}.
  • wmat: an nΓ—Mn \times M matrix of functional predictor values.
  • bs: the type of spline basis (e.g., "cr" for cubic regression splines, "cc" for cyclic cubic regression splines).
  • k: the number of basis functions.

Scalar predictors are included as standard formula terms. The censoring information is provided separately via the cens argument, not through the formula.

Example: Bayesian Functional Cox Regression

We demonstrate the fcox_bayes() function using a simulated example dataset with a time-to-event outcome and one functional predictor.

Load and Prepare Data

## Load the example data
Func_Cox_Data <- readRDS("data/example_data_Cox.rds")

## Prepare the functional predictor and censoring indicator
Func_Cox_Data$wmat <- Func_Cox_Data$MIMS
Func_Cox_Data$cens <- 1 - Func_Cox_Data$event

The example dataset contains:

  • survtime: the observed survival time Yi=min(Ti,Ci)Y_i = \min(T_i, C_i),
  • event: a binary event indicator (11 = event occurred, 00 = censored),
  • X1: a scalar predictor,
  • tmat: the nΓ—Mn \times M time point matrix (defines the domain 𝒯\mathcal{T}),
  • lmat: the nΓ—Mn \times M Riemann sum weight matrix (defines the integration weights over 𝒯\mathcal{T}),
  • MIMS: the nΓ—Mn \times M functional predictor matrix (physical activity data).

Note that the censoring indicator cens is constructed as 1 - event, following the convention where Ξ΄i=0\delta_i = 0 indicates an observed event and Ξ΄i=1\delta_i = 1 indicates censoring.

Fit the Bayesian Functional Cox Model

library(refundBayes)

refundBayes_FCox <- refundBayes::fcox_bayes(
  survtime ~ X1 + s(tmat, by = lmat * wmat, bs = "cc", k = 10),
  data = Func_Cox_Data,
  cens = Func_Cox_Data$cens,
  runStan = TRUE,
  niter = 1500,
  nwarmup = 500,
  nchain = 3,
  ncores = 3
)

In this call:

  • The formula specifies the observed survival time survtime as the response, with one scalar predictor X1 and one functional predictor encoded via s(tmat, by = lmat * wmat, bs = "cc", k = 10).
  • The spline basis is evaluated at the time points stored in tmat, and the integration over the domain 𝒯\mathcal{T} (determined by tmat) is approximated using the weights in lmat.
  • The censoring vector cens is supplied separately, with 00 = event observed and 11 = censored.
  • bs = "cc" uses cyclic cubic regression splines, appropriate for periodic functional predictors (e.g., 24-hour activity patterns).
  • k = 10 specifies 10 basis functions. In practice, 30–40 basis functions are recommended for moderately smooth functional data on dense grids.
  • The intercept argument is not specified and defaults to FALSE, which is appropriate for Cox models due to the identifiability constraint with the baseline hazard.
  • The sampler runs 3 chains in parallel, each with 1500 total iterations (500 warmup + 1000 posterior samples).

Visualization

The plot() method for refundBayes objects displays the estimated functional coefficient Ξ²Μ‚(t)\hat{\beta}(t) along with pointwise 95% credible intervals:

library(ggplot2)
plot(refundBayes_FCox)

Extracting Posterior Summaries

Posterior summaries of the functional coefficient can be computed directly from the func_coef element:

## Posterior mean of the functional coefficient
mean_curve <- apply(refundBayes_FCox$func_coef[[1]], 2, mean)

## Pointwise 95% credible interval
upper_curve <- apply(refundBayes_FCox$func_coef[[1]], 2,
                     function(x) quantile(x, prob = 0.975))
lower_curve <- apply(refundBayes_FCox$func_coef[[1]], 2,
                     function(x) quantile(x, prob = 0.025))

The posterior samples in func_coef[[1]] are stored as a QΓ—MQ \times M matrix, where QQ is the number of posterior samples and MM is the number of time points on the functional domain.

Comparison with Frequentist Results

The Bayesian results can be compared with frequentist estimates obtained via mgcv::gam, which handles Cox proportional hazards models through its cox.ph() family using partial likelihood:

library(mgcv)

## Fit frequentist functional Cox model using mgcv
fit_freq <- gam(
  survtime ~ s(tmat, by = lmat * wmat, bs = "cc", k = 10) + X1,
  data = Func_Cox_Data,
  family = cox.ph(),
  weights = Func_Cox_Data$event
)

## Extract frequentist estimates
freq_result <- plot(fit_freq)

Note that the frequentist approach uses cox.ph() family with partial likelihood and a Breslow-type nonparametric estimator for the baseline hazard, while the Bayesian approach models the baseline hazard parametrically using M-splines. Despite this difference, simulation studies in Jiang et al.Β (2025) show that both approaches achieve comparable performance in terms of estimation accuracy and coverage.

Inspecting the Generated Stan Code

Setting runStan = FALSE allows you to inspect or modify the Stan code before running the model:

## Generate Stan code without running the sampler
fcox_code <- refundBayes::fcox_bayes(
  survtime ~ X1 + s(tmat, by = lmat * wmat, bs = "cc", k = 10),
  data = Func_Cox_Data,
  cens = Func_Cox_Data$cens,
  runStan = FALSE
)

## Print the generated Stan code
cat(fcox_code$stancode)

The generated Stan code includes a functions block that defines custom log-likelihood functions for the Cox model (cox_log_lpdf for observed events and cox_log_lccdf for censored observations), in addition to the standard data, parameters, and model blocks.

Practical Recommendations

  • Number of basis functions (k): For illustrative purposes, k = 10 is often used. In practice, 30–40 basis functions are recommended for moderately smooth functional data observed on dense grids.
  • Spline type (bs): Use "cr" (cubic regression splines) for general functional predictors. Use "cc" (cyclic cubic regression splines) when the functional predictor is periodic (e.g., 24-hour activity patterns).
  • Intercept: The intercept defaults to FALSE for Cox models because it is not simultaneously identifiable with the baseline hazard M-spline coefficients. In most cases, this default should not be changed.
  • Censoring vector: Ensure the cens vector correctly follows the convention Ξ΄i=0\delta_i = 0 for observed events and Ξ΄i=1\delta_i = 1 for censored observations. If your data has an event indicator (1 = event, 0 = censored), use cens = 1 - event.
  • Number of iterations: Cox models may require more iterations than standard SoFR models. A common setup is niter = 3000 with nwarmup = 1000. Watch for divergent transitions in the Stan output; if they occur, consider increasing the number of warmup iterations or adjusting the adapt_delta parameter.
  • Convergence diagnostics: After fitting, examine traceplots and RΜ‚\hat{R} statistics using the rstan package (e.g., rstan::traceplot(refundBayes_FCox$stanfit)) to ensure that the Markov chains have converged.
  • Joint FPCA: When functional predictors are measured with substantial noise, consider setting joint_FPCA = TRUE for the relevant predictor to jointly estimate FPCA scores and regression coefficients within the Cox model.

References

  • Jiang, Z., Crainiceanu, C., and Cui, E. (2025). Tutorial on Bayesian Functional Regression Using Stan. Statistics in Medicine, 44(20–22), e70265.
  • Crainiceanu, C. M., Goldsmith, J., Leroux, A., and Cui, E. (2024). Functional Data Analysis with R. CRC Press.
  • Cui, E., Crainiceanu, C. M., and Leroux, A. (2021). Additive Functional Cox Model. Journal of Computational and Graphical Statistics, 30(3), 780–793.
  • Brilleman, S. L., Elci, E. M., Novik, J. B., and Wolfe, R. (2020). Bayesian Survival Analysis Using the rstanarm R Package. arXiv preprint, arXiv:2002.09633.
  • Cox, D. (1972). Regression Models and Life-Tables. Journal of the Royal Statistical Society, Series B, 34(2), 187–220.
  • Carpenter, B., Gelman, A., Hoffman, M. D., et al.Β (2017). Stan: A Probabilistic Programming Language. Journal of Statistical Software, 76(1), 1–32.