rstpm2 v1.5.1


Monthly downloads



Smooth Survival Models, Including Generalized Survival Models

R implementation of generalized survival models (GSMs), smooth accelerated failure time (AFT) models and Markov multi-state models. For the GSMs, g(S(t|x))=eta(t,x) for a link function g, survival S at time t with covariates x and a linear predictor eta(t,x). The main assumption is that the time effect(s) are smooth <doi:10.1177/0962280216664760>. For fully parametric models with natural splines, this re-implements Stata's 'stpm2' function, which are flexible parametric survival models developed by Royston and colleagues. We have extended the parametric models to include any smooth parametric smoothers for time. We have also extended the model to include any smooth penalized smoothers from the 'mgcv' package, using penalized likelihood. These models include left truncation, right censoring, interval censoring, gamma frailties and normal random effects <doi:10.1002/sim.7451>. For the smooth AFTs, S(t|x) = S_0(t*eta(t,x)), where the baseline survival function S_0(t)=exp(-exp(eta_0(t))) is modelled for natural splines for eta_0, and the time-dependent cumulative acceleration factor eta(t,x)=\int_0^t exp(eta_1(u,x)) du for log acceleration factor eta_1(u,x). The Markov multi-state models allow for a range of models with smooth transitions to predict transition probabilities, length of stay, utilities and costs, with differences, ratios and standardisation.


NOTE: versions 1.4.1 and 1.4.2 of rstpm2 included a critical bug in the predict function for type in "hr", "sdiff", "hdiff", "meansurvdiff", "meanhr", "or", "marghr" or "uncured".


This package provides link-based survival models that extend the Royston-Parmar models, a family of flexible parametric models. There are two main classes included in this package:

A. The class stpm2 is an R version of stpm2 in Stata with some extensions, including:

  1. Multiple links (log-log, -probit, -logit);

  2. Left truncation and right censoring (with experimental support for interval censoring);

  3. Relative survival;

  4. Cure models (where we introduce the nsx smoother, which extends the ns smoother);

  5. Predictions for survival, hazards, survival differences, hazard differences, mean survival, etc;

  6. Functional forms can be represented in regression splines or other parametric forms;

  7. The smoothers for time can use any transformation of time, including no transformation or log(time).

B. Another class pstpm2 is the implementation of the penalised models and corresponding penalized likelihood estimation methods. The main aim is to represent another way to deal with non-proportional hazards and adjust for potential continuous confounders in functional forms, not limited to proportional hazards and linear effect forms for all covariates. Functional forms can be represented in penalized regression splines (all mgcv smoothers ) or other parametric forms.

Some examples

The default for the parametric model is to use the Royston Parmar model, which uses a natural spline for the transformed baseline for log(time) with a log-log link.

fit <- stpm2(Surv(rectime,censrec==1)~hormon,data=brcancer,df=3)

(Hazard plot)

The default for the penalised model is similar, using a thin-plate spline for the transformed baseline for log(time) with a log-log link. The advantage of the penalised model is that there is no need to specify the knots or degrees of freedom for the baseline smoother.

fit <- pstpm2(Surv(rectime,censrec==1)~hormon,data=brcancer)

(Hazard plot 2)

Functions in rstpm2

Name Description
coef<- Generic method to update the coef in an object.
cox.tvc Test for a time-varying effect in the coxph model
eform.stpm2 S3 method for to provide exponentiated coefficents with confidence intervals.
stpm2-class Class "stpm2" ~~~
markov_msm Predictions for continuous time, nonhomogeneous Markov multi-state models using parametric and penalised survival models.
lines.stpm2 S3 methods for lines
tvcCoxph-class Class "tvcCoxph"
brcancer German breast cancer data from Stata.
colon Colon cancer.
aft Parametric accelerated failure time model with smooth time functions
incrVar Utility that returns a function to increment a variable in a data-frame.
predictnl Estimation of standard errors using the numerical delta method.
legendre.quadrature.rule.200 Legendre quadrature rule for n=200.
grad gradient function (internal function)
pstpm2-class Class "pstpm2"
aft-class Class "stpm2" ~~~
numDeltaMethod Calculate numerical delta method for non-linear predictions.
Rstpm2-package Flexible parametric survival models.
plot-methods plots for an stpm2 fit
rstpm2-internal Internal functions for the rstpm2 package.
nsx Generate a Basis Matrix for Natural Cubic Splines (with eXtensions)
nsxD Generate a Basis Matrix for the first derivative of Natural Cubic Splines (with eXtensions)
residuals-methods Residual values for an stpm2 or pstpm2 fit
vuniroot Vectorised One Dimensional Root (Zero) Finding
gsm Parametric and penalised generalised survival models
popmort Background mortality rates for the colon dataset.
predict.nsx Evaluate a Spline Basis
predict-methods Predicted values for an stpm2 or pstpm2 fit
predictnl-methods ~~ Methods for Function predictnl ~~
No Results!

Vignettes of rstpm2

No Results!

Last month downloads


Type Package
Date 2019-10-28
LinkingTo Rcpp,RcppArmadillo
License GPL-2 | GPL-3
LazyData yes
NeedsCompilation yes
Packaged 2019-11-05 07:48:56 UTC; marcle
Repository CRAN
Date/Publication 2019-11-05 23:00:05 UTC

Include our badge in your README