Learn R Programming

Multivariate Event Times (mets)

Implementation of various statistical models for multivariate event history data doi:10.1007/s10985-013-9244-x. Including multivariate cumulative incidence models doi:10.1002/sim.6016, and bivariate random effects probit models (Liability models) doi:10.1016/j.csda.2015.01.014. Modern methods for survival analysis, including regression modelling (Cox, Fine-Gray, Ghosh-Lin, Binomial regression) with fast computation of influence functions. Restricted mean survival time regression and years lost for competing risks. Average treatment effects and G-computation. All functions can be used with clusters and will work for large data.

Installation

install.packages("mets")

The development version may be installed directly from github (requires Rtools on windows and development tools (+Xcode) for Mac OS X):

remotes::install_github("kkholst/mets", dependencies="Suggests")

or to get development version

remotes::install_github("kkholst/mets",ref="develop")

Citation

To cite the mets package please use one of the following references

Thomas H. Scheike and Klaus K. Holst and Jacob B. Hjelmborg (2013). Estimating heritability for cause specific mortality based on twin studies. Lifetime Data Analysis. http://dx.doi.org/10.1007/s10985-013-9244-x

Klaus K. Holst and Thomas H. Scheike Jacob B. Hjelmborg (2015). The Liability Threshold Model for Censored Twin Data. Computational Statistics and Data Analysis. http://dx.doi.org/10.1016/j.csda.2015.01.014

BibTeX:

@Article{,
  title={Estimating heritability for cause specific mortality based on twin studies},
  author={Scheike, Thomas H. and Holst, Klaus K. and Hjelmborg, Jacob B.},
  year={2013},
  issn={1380-7870},
  journal={Lifetime Data Analysis},
  doi={10.1007/s10985-013-9244-x},
  url={http://dx.doi.org/10.1007/s10985-013-9244-x},
  publisher={Springer US},
  keywords={Cause specific hazards; Competing risks; Delayed entry;
	    Left truncation; Heritability; Survival analysis},
  pages={1-24},
  language={English}

}

@Article{,
  title={The Liability Threshold Model for Censored Twin Data},
  author={Holst, Klaus K. and Scheike, Thomas H. and Hjelmborg, Jacob B.},
  year={2015},
  doi={10.1016/j.csda.2015.01.014},
  url={http://dx.doi.org/10.1016/j.csda.2015.01.014},
  journal={Computational Statistics and Data Analysis}
}

Examples: Twins Polygenic modelling

First considering standard twin modelling (ACE, AE, ADE, and more models)

 ace <- twinlm(y ~ 1, data=d, DZ="DZ", zyg="zyg", id="id")
 ace
 ## An AE-model could be fitted as
 ae <- twinlm(y ~ 1, data=d, DZ="DZ", zyg="zyg", id="id", type="ae")
 ## LRT:
 lava::compare(ae,ace)
 ## AIC
 AIC(ae)-AIC(ace)
 ## To adjust for the covariates we simply alter the formula statement
 ace2 <- twinlm(y ~ x1+x2, data=d, DZ="DZ", zyg="zyg", id="id", type="ace")
 ## Summary/GOF
 summary(ace2)

Examples: Twins Polygenic modelling time-to-events Data

In the context of time-to-events data we consider the "Liabilty Threshold model" with IPCW adjustment for censoring.

First we fit the bivariate probit model (same marginals in MZ and DZ twins but different correlation parameter). Here we evaluate the risk of getting cancer before the last double cancer event (95 years)

data(prt)
prt0 <-  force.same.cens(prt, cause="status", cens.code=0, time="time", id="id")
prt0$country <- relevel(prt0$country, ref="Sweden")
prt_wide <- fast.reshape(prt0, id="id", num="num", varying=c("time","status","cancer"))
prt_time <- subset(prt_wide,  cancer1 & cancer2, select=c(time1, time2, zyg))
tau <- 95
tt <- seq(70, tau, length.out=5) ## Time points to evaluate model in

b0 <- bptwin.time(cancer ~ 1, data=prt0, id="id", zyg="zyg", DZ="DZ", type="cor",
              cens.formula=Surv(time,status==0)~zyg, breaks=tau)
summary(b0)

Liability threshold model with ACE random effects structure

b1 <- bptwin.time(cancer ~ 1, data=prt0, id="id", zyg="zyg", DZ="DZ", type="ace",
           cens.formula=Surv(time,status==0)~zyg, breaks=tau)
summary(b1)

Examples: Twins Concordance for time-to-events Data


data(prt) ## Prostate data example (sim)

## Bivariate competing risk, concordance estimates
p33 <- bicomprisk(Event(time,status)~strata(zyg)+id(id),
                  data=prt, cause=c(2,2), return.data=1, prodlim=TRUE)

p33dz <- p33$model$"DZ"$comp.risk
p33mz <- p33$model$"MZ"$comp.risk

## Probability weights based on Aalen's additive model (same censoring within pair)
prtw <- ipw(Surv(time,status==0)~country+zyg, data=prt,
            obs.only=TRUE, same.cens=TRUE, 
            cluster="id", weight.name="w")

## Marginal model (wrongly ignoring censorings)
bpmz <- biprobit(cancer~1 + cluster(id), 
                 data=subset(prt,zyg=="MZ"), eqmarg=TRUE)

## Extended liability model
bpmzIPW <- biprobit(cancer~1 + cluster(id),
                    data=subset(prtw,zyg=="MZ"),
                    weights="w")
smz <- summary(bpmzIPW)

## Concordance
plot(p33mz,ylim=c(0,0.1),axes=FALSE, automar=FALSE,atrisk=FALSE,background=TRUE,background.fg="white")
axis(2); axis(1)

abline(h=smz$prob["Concordance",],lwd=c(2,1,1),col="darkblue")
## Wrong estimates:
abline(h=summary(bpmz)$prob["Concordance",],lwd=c(2,1,1),col="lightgray", lty=2)

Examples: Cox model, RMST

We can fit the Cox model and compute many useful summaries, such as restricted mean survival and stanardized treatment effects (G-estimation). First estimating the standardized survival

 data(bmt); bmt$time <- bmt$time+runif(408)*0.001
 bmt$event <- (bmt$cause!=0)*1
 dfactor(bmt) <- tcell.f~tcell

 ss <- phreg(Surv(time,event)~tcell.f+platelet+age,bmt) 
 summary(survivalG(ss,bmt,50))

 sst <- survivalGtime(ss,bmt,n=50)
 plot(sst,type=c("survival","risk","survival.ratio")[1])

Based on the phreg we can also compute the restricted mean survival time and years lost (via Kaplan-Meier estimates). The function does it for all times at once and can be plotted as restricted mean survival or years lost at the different time horizons

 out1 <- phreg(Surv(time,cause!=0)~strata(tcell,platelet),data=bmt)
 
 rm1 <- resmean.phreg(out1,times=50)
 summary(rm1)
 par(mfrow=c(1,2))
 plot(rm1,se=1)
 plot(rm1,years.lost=TRUE,se=1)

For competing risks the years lost can be decomposed into different causes and is based on the integrated Aalen-Johansen estimators for the different strata

 ## years.lost decomposed into causes
 drm1 <- cif.yearslost(Event(time,cause)~strata(tcell,platelet),data=bmt,times=10*(1:6))
 par(mfrow=c(1,2)); plot(drm1,cause=1,se=1); title(main="Cause 1"); plot(drm1,cause=2,se=1); title(main="Cause 2")
 summary(drm1)

Computations are again done for all time horizons at once as illustrated in the plot.

Examples: Cox model IPTW

We can fit the Cox model with inverse probabilty of treatment weights based on logistic regression. The treatment weights can be time-dependent and then mutiplicative weights are applied (see details and vignette).

library(mets)
 data(bmt); bmt$time <- bmt$time+runif(408)*0.001
 bmt$event <- (bmt$cause!=0)*1
 dfactor(bmt) <- tcell.f~tcell

 ss <- phreg_IPTW(Surv(time,event)~tcell.f+cluster(id),data=bmt,treat.model=tcell.f~platelet+age) 
 summary(ss)
 head(iid(ss))

Examples: Competing risks regression, Binomial Regression

We can fit the logistic regression model at a specific time-point with IPCW adjustment

 data(bmt); bmt$time <- bmt$time+runif(408)*0.001
 # logistic regresion with IPCW binomial regression 
 out <- binreg(Event(time,cause)~tcell+platelet,bmt,time=50)
 summary(out)
 head(iid(out))

 predict(out,data.frame(tcell=c(0,1),platelet=c(1,1)),se=TRUE)

Examples: Competing risks regression, Fine-Gray/Logistic link

We can fit the Fine-Gray model and the logit-link competing risks model (using IPCW adjustment). Starting with the logit-link model

 data(bmt)
 bmt$time <- bmt$time+runif(nrow(bmt))*0.01
 bmt$id <- 1:nrow(bmt)

 ## logistic link  OR interpretation
 or=cifreg(Event(time,cause)~strata(tcell)+platelet+age,data=bmt,cause=1)
 summary(or)
 par(mfrow=c(1,2))
 ## to see baseline 
 plot(or)

 # predictions 
 nd <- data.frame(tcell=c(1,0),platelet=0,age=0)
 pll <- predict(or,nd)
 plot(pll)

Similarly, the Fine-Gray model can be estimated using IPCW adjustment

 ## Fine-Gray model
 fg=cifreg(Event(time,cause)~strata(tcell)+platelet+age,data=bmt,cause=1,propodds=NULL)
 summary(fg)
 ## baselines 
 plot(fg)
 nd <- data.frame(tcell=c(1,0),platelet=0,age=0)
 pfg <- predict(fg,nd,se=1)
 plot(pfg,se=1)

 ## influence functions of regression coefficients
 head(iid(fg))

and we can get standard errors for predictions based on the influence functions of the baseline and the regression coefiicients (these are used in the predict function)

baseid <- iidBaseline(fg,time=40)
FGprediid(baseid,nd)

further G-estimation can be done

 dfactor(bmt) <- tcell.f~tcell
 fg1 <- cifreg(Event(time,cause)~tcell.f+platelet+age,bmt,cause=1,propodds=NULL)
 summary(survivalG(fg1,bmt,50))

Examples: Marginal mean for recurrent events

We can estimate the expected number of events non-parametrically and get standard errors for this estimator

data(hfactioncpx12)
dtable(hfactioncpx12,~status)

gl1 <- recurrentMarginal(Event(entry,time,status)~strata(treatment)+cluster(id),hfactioncpx12,cause=1,death.code=2)
summary(gl1,times=1:5)
plot(gl1,se=1)

Examples: Ghosh-Lin for recurrent events

We can fit the Ghosh-Lin model for the expected number of events observed before dying (using IPCW adjustment and get predictions)

data(hfactioncpx12)
dtable(hfactioncpx12,~status)

gl1 <- recreg(Event(entry,time,status)~treatment+cluster(id),hfactioncpx12,cause=1,death.code=2)
summary(gl1)

## influence functions of regression coefficients
head(iid(gl1))

and we can get standard errors for predictions based on the influence functions of the baseline and the regression coefiicients

 nd=data.frame(treatment=levels(hfactioncpx12$treatment),id=1)
 pfg <- predict(gl1,nd,se=1)
 summary(pfg,times=1:5)
 plot(pfg,se=1)

The influence functions of the baseline and regression coefficients at a specific time-point can be obtained

baseid <- iidBaseline(gl1,time=2)
dd <- data.frame(treatment=levels(hfactioncpx12$treatment),id=1)
GLprediid(baseid,dd)

Examples: Fixed time modelling for recurrent events

We can fit a log-link regression model at 2 years for the expected number of events observed before dying (using IPCW adjustment)

data(hfactioncpx12)

e2 <- recregIPCW(Event(entry,time,status)~treatment+cluster(id),hfactioncpx12,cause=1,death.code=2,time=2)
summary(e2)
head(iid(e2))

Examples: Regression for RMST/Restricted mean survival for survival and competing risks using IPCW

RMST can be computed using the Kaplan-Meier (via phreg) and the for competing risks via the cumulative incidence functions, but we can also get these estimates via IPCW adjustment and then we can do regression

 ### same as Kaplan-Meier for full censoring model 
 bmt$int <- with(bmt,strata(tcell,platelet))
 out <- resmeanIPCW(Event(time,cause!=0)~-1+int,bmt,time=30,
                         cens.model=~strata(platelet,tcell),model="lin")
 estimate(out)
 head(iid(out))
 ## same as 
 out1 <- phreg(Surv(time,cause!=0)~strata(tcell,platelet),data=bmt)
 rm1 <- resmean.phreg(out1,times=30)
 summary(rm1)
 
 ## competing risks years-lost for cause 1  
 out1 <- resmeanIPCW(Event(time,cause)~-1+int,bmt,time=30,cause=1,
                       cens.model=~strata(platelet,tcell),model="lin")
 estimate(out1)
 ## same as 
 drm1 <- cif.yearslost(Event(time,cause)~strata(tcell,platelet),data=bmt,times=30)
 summary(drm1)

Examples: Average treatment effects (ATE) for survival or competing risks

We can compute ATE for survival or competing risks data for the probabilty of dying

 bmt$event <- bmt$cause!=0; dfactor(bmt) <- tcell~tcell
 brs <- binregATE(Event(time,cause)~tcell+platelet+age,bmt,time=50,cause=1,
	  treat.model=tcell~platelet+age)
 summary(brs)
 head(brs$riskDR.iid)
 head(brs$riskG.iid)

or the the restricted mean survival or years-lost to cause 1

 out <- resmeanATE(Event(time,event)~tcell+platelet,data=bmt,time=40,treat.model=tcell~platelet)
 summary(out)
 head(out$riskDR.iid)
 head(out$riskG.iid)

 out1 <- resmeanATE(Event(time,cause)~tcell+platelet,data=bmt,cause=1,time=40,
                    treat.model=tcell~platelet)
 summary(out1)

Here event is 0/1 thus leading to restricted mean and cause taking the values 0,1,2 produces regression for the years lost due to cause 1.

Examples: While Alive estimands for recurrent events

We consider an RCT and aim to describe the treatment effect via while alive estimands

data(hfactioncpx12)

dtable(hfactioncpx12,~status)
dd <- WA_recurrent(Event(entry,time,status)~treatment+cluster(id),hfactioncpx12,time=2,death.code=2)
summary(dd)

dd <- WA_recurrent(Event(entry,time,status)~treatment+cluster(id),hfactioncpx12,time=2,
		   death.code=2,trans=.333)
summary(dd,type="log")

Copy Link

Version

Install

install.packages('mets')

Monthly Downloads

10,378

Version

1.3.7

License

GPL (>= 2)

Maintainer

Klaus Holst

Last Published

August 30th, 2025

Functions in mets (1.3.7)

aalenfrailty

Aalen frailty model
WA_recurrent

While-Alive estimands for recurrent events
FG_AugmentCifstrata

Augmentation for Fine-Gray model based on stratified NPMLE Cif (Aalen-Johansen)
Grandom.cif

Additive Random effects model for competing risks data for polygenetic modelling
basehazplot.phreg

Plotting the baselines of stratified Cox
bicomprisk

Estimation of concordance in bivariate competing risks data
aalenMets

Fast additive hazards model with robust standard errors
back2timereg

Convert to timereg object
TRACE

The TRACE study group of myocardial infarction
LinSpline

Simple linear spline
blocksample

Block sampling
binregCasewise

Estimates the casewise concordance based on Concordance and marginal estimate using binreg
binregRatio

Percentage of years lost due to cause regression
binreg

Binomial Regression for censored competing risks data
biprobit

Bivariate Probit model
binregG

G-estimator for binomial regression model (Standardized estimates)
binregATE

Average Treatment effect for censored competing risks data using Binomial Regression
binregTSR

2 Stage Randomization for Survival Data or competing Risks Data
bmt

The Bone Marrow Transplant Data
casewise

Estimates the casewise concordance based on Concordance and marginal estimate using prodlim but no testing
binomial.twostage

Fits Clayton-Oakes or bivariate Plackett (OR) models for binary data using marginals that are on logistic form. If clusters contain more than two times, the algoritm uses a compososite likelihood based on all pairwise bivariate models.
cluster.index

Finds subjects related to same cluster
cor.cif

Cross-odds-ratio, OR or RR risk regression for competing risks
count.history

Counts the number of previous events of two types for recurrent events processes
cif

Cumulative incidence with robust standard errors
cifreg

CIF regression
bptwin

Liability model for twin data
calgb8923

CALGB 8923, twostage randomization SMART design
dlag

Lag operator
divide.conquer.timereg

Split a data set and run function from timereg and aggregate
dermalridgesMZ

Dermal ridges data (monozygotic twins)
dermalridges

Dermal ridges data (families)
divide.conquer

Split a data set and run function
casewise.test

Estimates the casewise concordance based on Concordance and marginal estimate using timereg and performs test for independence
diabetes

The Diabetic Retinopathy Data
daggregate

aggregating for for data frames
concordanceCor

Concordance Computes concordance and casewise concordance
dcut

Cutting, sorting, rm (removing), rename for data frames
dcor

summary, tables, and correlations for data frames
dsort

Sort data frame
doubleFGR

Double CIF Fine-Gray model with two causes
dprint

list, head, print, tail
evalTerminal

Evaluates piece constant covariates at min(D,t) where D is a terminal event
easy.binomial.twostage

Fits two-stage binomial for describing depdendence in binomial data using marginals that are on logistic form using the binomial.twostage funcion, but call is different and easier and the data manipulation is build into the function. Useful in particular for family design data.
fast.pattern

Fast pattern
fast.reshape

Fast reshape
dspline

Simple linear spline
fast.approx

Fast approximation
familycluster.index

Finds all pairs within a cluster (family)
event.split

event.split (SurvSplit).
familyclusterWithProbands.index

Finds all pairs within a cluster (famly) with the proband (case/control)
glm_IPTW

IPTW GLM, Inverse Probaibilty of Treatment Weighted GLM
dtransform

Transform that allows condition
dtable

tables for data frames
haplo

haplo fun data
haplo.surv.discrete

Discrete time to event haplo type analysis
dby

Calculate summary statistics grouped by
gofM.phreg

GOF for Cox covariates in PH regression
gofZ.phreg

GOF for Cox covariates in PH regression
gofG.phreg

Stratified baseline graphical GOF test for Cox covariates in PH regression
gof.phreg

GOF for Cox PH regression
km

Kaplan-Meier with robust standard errors
ipw2

Inverse Probability of Censoring Weights
ipw

Inverse Probability of Censoring Weights
drelevel

relev levels for data frames
hfactioncpx12

hfaction, subset of block randmized study HF-ACtion from WA package
eventpois

Extract survival estimates from lifetable analysis
lifetable.matrix

Life table
dreg

Regression for data frames with dutility call
iidBaseline

Influence functions or IID decomposition of baseline for recrec/phreg/cifregFG
logitSurv

Proportional odds survival model
mets-package

mets: Analysis of Multivariate Event Times
interval.logitsurv.discrete

Discrete time to event interval censored data
mets.options

Set global options for mets
npc

For internal use
phregR

Fast Cox PH regression and calculations done in R to make play and adjustments easy
phreg

Fast Cox PH regression
phreg_rct

Lu-Tsiatis More Efficient Log-Rank for Randomized studies with baseline covariates
pmvn

Multivariate normal distribution function
plot_twin

Scatter plot function
phreg_IPTW

IPTW Cox, Inverse Probaibilty of Treatment Weighted Cox regression
mena

Menarche data set
melanoma

The Melanoma Survival Data
medweight

Computes mediation weights
mediatorSurv

Mediation analysis in survival context
prob.exceed.recurrent

Estimation of probability of more that k events for recurrent events process
migr

Migraine data
multcif

Multivariate Cumulative Incidence Function example data set
np

np data set
prt

Prostate data set
lifecourse

Life-course plot
mlogit

Multinomial regression based on phreg regression
plack.cif

plack Computes concordance for or.cif based model, that is Plackett random effects model
random.cif

Random effects model for competing risks data
rchaz

Simulation of Piecewise constant hazard model (Cox).
rpch

Piecewise constant hazard distribution
sim.cause.cox

Simulation of cause specific from Cox models.
plot.phreg

Plotting the baselines of stratified Cox
recurrentMarginal

Fast recurrent marginal mean when death is possible
recreg

Recurrent events regression with terminal event
print.casewise

prints Concordance test
predictRisk.binreg

Risk predictions to work with riskRegression package
simGLcox

Simulation of two-stage recurrent events data based on Cox/Cox or Cox/Ghosh-Lin structure
resmeanATE

Average Treatment effect for Restricted Mean for censored competing risks data using IPCW
simMultistate

Simulation of illness-death model
resmeanIPCW

Restricted IPCW mean for censored survival data
rcrisk

Simulation of Piecewise constant hazard models with two causes (Cox).
simClaytonOakes

Simulate from the Clayton-Oakes frailty model
predict.phreg

Predictions from proportional hazards model
rchazC

Piecewise constant hazard distribution
predictRisk

Risk predictions to work with riskRegression package
simRecurrentII

Simulation of recurrent events data based on cumulative hazards with two types of recurrent events
simRecurrentTS

Simulation of recurrent events data based on cumulative hazards: Two-stage model
simClaytonOakesWei

Simulate from the Clayton-Oakes frailty model
twinbmi

BMI data set
sim.cif

Simulation of output from Cumulative incidence regression model
twinlm

Classic twin model for quantitative traits
reexports

Objects exported from other packages
resmean.phreg

Restricted mean for stratified Kaplan-Meier or Cox model with martingale standard errors
sim.recurrent

Simulation of two-stage recurrent events data based on Cox/Cox or Cox/Ghosh-Lin structure
simAalenFrailty

Simulate from the Aalen Frailty model
summaryGLM

Reporting OR (exp(coef)) from glm with binomial link and glm predictions
summary.cor

Summary for dependence models for competing risks
ttpd

ttpd discrete survival data on interval form
twin.clustertrunc

Estimation of twostage model with cluster truncation in bivariate situation
twostageMLE

Twostage survival model fitted by pseudo MLE
survivalG

G-estimator for Cox and Fine-Gray model
survival.twostage

Twostage survival model for multivariate survival data
twinstut

Stutter data set
twinsim

Simulate twin data
test.conc

Concordance test Compares two concordance estimates
tetrachoric

Estimate parameters from odds-ratio
sim.cox

Simulation of output from Cox model.
EventSplit2

Event split with two time-scales, time and gaptime
ClaytonOakes

Clayton-Oakes model with piece-wise constant hazards
BinAugmentCifstrata

Augmentation for Binomial regression based on stratified NPMLE Cif (Aalen-Johansen)
Bootphreg

Wild bootstrap for Cox PH regression
Dbvn

Derivatives of the bivariate normal cumulative distribution function
Effbinreg

Efficient IPCW for binary data
CPH_HPN_CRBSI

Rates for HPN program for patients of Copenhagen Cohort
ACTG175

ACTG175, block randmized study from speff2trial package
Event

Event history object
EVaddGam

Relative risk for additive gamma model