Bayesian Functional Cox Regression with `refundBayes::fcox_bayes`
2026-03-03
Source:vignettes/fcox_bayes_vignette.Rmd
fcox_bayes_vignette.RmdIntroduction
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 , let be the event time and the censoring time. The observed data consist of , where:
- is the observed follow-up time,
- is the censoring indicator with if the event is observed () and if the event is censored (),
- is a vector of scalar predictors,
- for is a functional predictor observed over a domain .
The domain
is not restricted to
;
it is determined by the actual time points in the data (e.g.,
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:
where is the baseline hazard function, and is the linear predictor defined as:
Here
is the intercept,
is the functional coefficient, and
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:
where 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
using M-splines:
where denotes the -th M-spline basis function with knots and degree , and are the spline coefficients. The constraints and 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:
where . Because M-splines are non-negative, the resulting I-spline cumulative hazard is non-decreasing by construction.
Identifiability Constraint
The I-spline coefficients
and the intercept
are not simultaneously identifiable: for any
,
the parameter pairs
and
yield the same hazard function. To resolve this, the model imposes the
constraint
by assigning a non-informative Dirichlet prior
with
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 is represented using spline basis functions :
The spline coefficients are reparametrised using the spectral decomposition of the penalty matrix (the Wahba-OβSullivan smoothing penalty) to obtain transformed coefficients , where:
- (random effects) receive a prior,
- (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 is approximated by the Riemann sum , constructed from three user-supplied matrices:
tmat(an matrix): contains the time points at which the functional predictor is observed. The -th entry equals . The range of values intmatdetermines the domain of integration . For example, if the functional predictor is observed at minutes within a day, thentmathas entries ranging from to and . There is no requirement that the domain be rescaled to .lmat(an matrix): contains the integration weights for the Riemann sum approximation. For equally spaced time points with spacing , every entry oflmatequals . These weights, together withtmat, fully specify how the numerical integration is performed and over what domain.wmat(an matrix): contains the functional predictor values. The -th row contains the observed values for subject .
In the formula
s(tmat, by = lmat * wmat, bs = "cc", k = 10), the
mgcv infrastructure uses tmat to construct the
spline basis
at the observed time points, and the by = lmat * wmat
argument provides the element-wise product
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
β regardless of whether it is
,
,
or any other interval.
Note that for all subjects, the time points are assumed to be
identical so that
for all
.
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:
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 (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
rather than the observed (noisy) function
,
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
.
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:
if the event is observed and
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
.
NULL by default since intercept = FALSE. |
scalar_coef |
A matrix of posterior samples for scalar coefficients
,
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 matrix of time points defining the domain over which the functional predictor is observed and the integral is computed. The domain adapts to the actual values intmat(e.g., , , etc.). -
lmat: an matrix of Riemann sum weights (), controlling the numerical integration over . -
wmat: an 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$eventThe example dataset contains:
-
survtime: the observed survival time , -
event: a binary event indicator ( = event occurred, = censored), -
X1: a scalar predictor, -
tmat: the time point matrix (defines the domain ), -
lmat: the Riemann sum weight matrix (defines the integration weights over ), -
MIMS: the functional predictor matrix (physical activity data).
Note that the censoring indicator cens is constructed as
1 - event, following the convention where
indicates an observed event and
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
survtimeas the response, with one scalar predictorX1and one functional predictor encoded vias(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 (determined bytmat) is approximated using the weights inlmat. - The censoring vector
censis supplied separately, with = event observed and = censored. -
bs = "cc"uses cyclic cubic regression splines, appropriate for periodic functional predictors (e.g., 24-hour activity patterns). -
k = 10specifies 10 basis functions. In practice, 30β40 basis functions are recommended for moderately smooth functional data on dense grids. - The
interceptargument is not specified and defaults toFALSE, 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
along with pointwise 95% credible intervals:
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
matrix, where
is the number of posterior samples and
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 = 10is 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
FALSEfor 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
censvector correctly follows the convention for observed events and for censored observations. If your data has an event indicator (1 = event, 0 = censored), usecens = 1 - event. -
Number of iterations: Cox models may require more
iterations than standard SoFR models. A common setup is
niter = 3000withnwarmup = 1000. Watch for divergent transitions in the Stan output; if they occur, consider increasing the number of warmup iterations or adjusting theadapt_deltaparameter. -
Convergence diagnostics: After fitting, examine
traceplots and
statistics using the
rstanpackage (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 = TRUEfor 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.