-
Notifications
You must be signed in to change notification settings - Fork 1
/
coxtv_example.Rmd
76 lines (63 loc) · 2.71 KB
/
coxtv_example.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
---
title: "coxtv example"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{coxtv_example}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
There are a few ways to represent time-varying covariates. In this example I will use the Stanford Heart Transplant data to demonstrate how to use this package when the time-varying covariate is specified by a operation (such as a surgery). Another form of time-varying covariate are same variables measured over time, which is easier to specify with this package.
```{r, message=FALSE}
library(survival)
library(dplyr)
N = nrow(jasa)
jasa$ID = 1:N
jasa$y = pmax(0.5, as.numeric(jasa$fu.date - jasa$accept.dt))
jasa$age = jasa$age - 48
jasa$year = as.numeric(jasa$accept.dt - as.Date("1967-10-01"))/365.25
jasa$txtime= with(jasa, ifelse(tx.date== fu.date,
(tx.date -accept.dt) -.5,
(tx.date - accept.dt)))
jasa$status = jasa$fustat
```
Notice that:
- The response variable y here is the time from accept date to last followup, which is possibly adeath time.
- The status variable is 1 if death happend before last followup, 0 otherwise.
- txtime is the waiting time of heart transplant after the patient us accepted.
- In this example, the time varying covariates are the transplant indicator, and the interaction between transplant and age.
The jasa data frame can be readily fed into the coxtv function. Now we put the time varying covariates into a format suitable for this package.
```{r, message=FALSE}
# at the beginning no-one has transplant
tv_trans = data.frame(ID=1:N, time=0, value = 0)
tmp = jasa %>% select(c(ID, txtime)) %>% filter(!is.na(txtime))
tmp$time = tmp$txtime
tmp$value = 1
tmp = select(tmp, c(ID, time, value))
# At the transplant, the transplant indicator becomes 1
tv_trans = rbind(tv_trans, tmp)
# The interaction between age and transplant
tv_inter = tv_trans
tv_inter$value = tv_inter$value * jasa$age[match(tv_inter$ID, jasa$ID)]
tv_list = list(transplant = tv_trans, transplant_age = tv_inter)
```
Now we can fit a model
```{r setup}
library(coxtv)
result = coxtv(jasa, tv_list,c('surgery', 'year', 'age'),c(0.01, 0.0))
result
```
Compute the training C-index:
```{r}
cindex_tv(jasa, tv_list,c('surgery', 'year', 'age'), result[[1]])
cindex_tv(jasa, tv_list,c('surgery', 'year', 'age'), result[[2]])
```
We can also draw a Kaplan-Meier curve based on a time varying covariate. We use the definition defined in section four of [this paper](https://www.jstor.org/stable/pdf/27643698.pdf?refreqid=excelsior%3A9a359cba87b05649fc535fe7fea9430e).
```{r}
KM_curve_tv(jasa, tv_list[[1]], ngroup=2)
```