cohorttools for cohort data analyses

Jari Haukka

2019-10-22

Simple example with ‘boxesLx’

library(cohorttools)
library(DiagrammeR)
library(survival)
library(Epi)
library(mstate)
data(ebmt3)
bmt <- Lexis(exit = list(tft = rfstime/365.25),
             exit.status = factor(rfsstat, labels = c("Tx", "RD")),
                          data = ebmt3)
## NOTE: entry.status has been set to "Tx" for all.
## NOTE: entry is assumed to be 0 on the tft timescale.
bmtr <- cutLexis(bmt, cut = bmt$prtime/365.25, precursor.states = "Tx",
                                           new.state = "PR")
summary(bmtr)
##      
## Transitions:
##      To
## From   Tx   PR  RD  Records:  Events: Risk time:  Persons:
##   Tx  577 1169 458      2204     1627    2439.43      2204
##   PR    0  786 383      1169      383    3173.03      1169
##   Sum 577 1955 841      3373     2010    5612.46      2204
DiagrammeR::grViz(boxesLx(bmtr))

Simple figure

DiagrammeR::grViz(boxesLx(bmtr,layout="dot"))

Simple figure with ‘dot’ engine

DiagrammeR::grViz(boxesLx(bmtr,node.attr='shape=hexagon color=navy style=filled fillcolor=lightblue',
edge.attr = ' color=steelblue ',layout="circo",prop.penwidth=TRUE))

More colors and shapes in figure

Simple example with ‘mkratetable’

library(survival)
tmp.lt1<-mkratetable(Surv(time,status)~ sex,data=lung)
tmp.lt2<-mkratetable(Surv(time,status)~ sex,data=lung,add.RR=TRUE,lowest.N=60)
## Warning in mkratetable(Surv(time, status) ~ sex, data = lung, add.RR =
## TRUE, : lowest cell frequency under 60 , replaced by 999999
knitr::kable(tmp.lt1, caption="Incidence rates with 95% CI")
Incidence rates with 95% CI
sex pyears event rate low high
1 107.01164 112 1.0466152 0.8617797 1.2593502
2 83.52361 53 0.6345511 0.4753222 0.8300083
knitr::kable(tmp.lt2, caption="Incidence rates and incidence rate ratios with 95% CI. Number of cases under 60 replaced with '999999'")
Incidence rates and incidence rate ratios with 95% CI. Number of cases under 60 replaced with ‘999999’
sex pyears event rate low high RR 2.5p 97.5p
1 107.01164 112 1.0466152 0.8617797 1.2593502 1.0000000 1.0000000 1.0000000
2 83.52361 9999999 0.6345511 0.4753222 0.8300083 0.6062888 0.4372854 0.8406093

Simple example with ‘estim.hazard’

require(survival)

tmp.hz<-estim.hazard(time=lung$time,status=lung$status)
## NOTE: entry.status has been set to 0 for all.
## NOTE: entry is assumed to be 0 on the fu.time timescale.
# Few lines from resultin data.frame 'tmp.hz'
head(tmp.hz,4)
##   time.eval  Estimate    StdErr         haz       haz.lo      haz.hi
## 1   0.00000 -6.784785 0.2734077 0.001130851 0.0006617291 0.001932548
## 2   5.00000 -6.759449 0.2606975 0.001159868 0.0006958289 0.001933369
## 3  15.27273 -6.707461 0.2353176 0.001221763 0.0007703429 0.001937714
## 4  25.54545 -6.655671 0.2112780 0.001286704 0.0008504300 0.001946789
# Estimation parameters
attributes(tmp.hz)$estim.hazard.param 
## $breaks
##   [1]    0.00000    5.00000   15.27273   25.54545   35.81818   46.09091
##   [7]   56.36364   66.63636   76.90909   87.18182   97.45455  107.72727
##  [13]  118.00000  128.27273  138.54545  148.81818  159.09091  169.36364
##  [19]  179.63636  189.90909  200.18182  210.45455  220.72727  231.00000
##  [25]  241.27273  251.54545  261.81818  272.09091  282.36364  292.63636
##  [31]  302.90909  313.18182  323.45455  333.72727  344.00000  354.27273
##  [37]  364.54545  374.81818  385.09091  395.36364  405.63636  415.90909
##  [43]  426.18182  436.45455  446.72727  457.00000  467.27273  477.54545
##  [49]  487.81818  498.09091  508.36364  518.63636  528.90909  539.18182
##  [55]  549.45455  559.72727  570.00000  580.27273  590.54545  600.81818
##  [61]  611.09091  621.36364  631.63636  641.90909  652.18182  662.45455
##  [67]  672.72727  683.00000  693.27273  703.54545  713.81818  724.09091
##  [73]  734.36364  744.63636  754.90909  765.18182  775.45455  785.72727
##  [79]  796.00000  806.27273  816.54545  826.81818  837.09091  847.36364
##  [85]  857.63636  867.90909  878.18182  888.45455  898.72727  909.00000
##  [91]  919.27273  929.54545  939.81818  950.09091  960.36364  970.63636
##  [97]  980.90909  991.18182 1001.45455 1011.72727 1022.00000
## 
## $knots
## [1] 174.5 344.0 513.5 683.0 852.5
require(ggplot2)
ggplot(tmp.hz,
       aes(x = time.eval, y = haz)) +
  # Add a ribbon with the confidence band
  geom_smooth(
    aes(
      # lower and upper bound of the ribbon
      ymin = haz.lo, ymax = haz.hi), stat = "identity") +
  xlab("Time (d)") +ylab("Hazard")+ylim(c(0,0.01))+xlim(c(0,2*365))
Hazard with 95% confidence interval

Hazard with 95% confidence interval

# Define knots for spline
tmp.kn<-data.frame(knt=c(100,200,400,600))

tmp.hz1<-estim.hazard(time=lung$time,status=lung$status,knots=tmp.kn$knt)
## NOTE: entry.status has been set to 0 for all.
## NOTE: entry is assumed to be 0 on the fu.time timescale.
# Estimation parameters
attributes(tmp.hz1)$estim.hazard.param 
## $breaks
##   [1]    0.00000    5.00000   15.27273   25.54545   35.81818   46.09091
##   [7]   56.36364   66.63636   76.90909   87.18182   97.45455  107.72727
##  [13]  118.00000  128.27273  138.54545  148.81818  159.09091  169.36364
##  [19]  179.63636  189.90909  200.18182  210.45455  220.72727  231.00000
##  [25]  241.27273  251.54545  261.81818  272.09091  282.36364  292.63636
##  [31]  302.90909  313.18182  323.45455  333.72727  344.00000  354.27273
##  [37]  364.54545  374.81818  385.09091  395.36364  405.63636  415.90909
##  [43]  426.18182  436.45455  446.72727  457.00000  467.27273  477.54545
##  [49]  487.81818  498.09091  508.36364  518.63636  528.90909  539.18182
##  [55]  549.45455  559.72727  570.00000  580.27273  590.54545  600.81818
##  [61]  611.09091  621.36364  631.63636  641.90909  652.18182  662.45455
##  [67]  672.72727  683.00000  693.27273  703.54545  713.81818  724.09091
##  [73]  734.36364  744.63636  754.90909  765.18182  775.45455  785.72727
##  [79]  796.00000  806.27273  816.54545  826.81818  837.09091  847.36364
##  [85]  857.63636  867.90909  878.18182  888.45455  898.72727  909.00000
##  [91]  919.27273  929.54545  939.81818  950.09091  960.36364  970.63636
##  [97]  980.90909  991.18182 1001.45455 1011.72727 1022.00000
## 
## $knots
## [1] 100 200 400 600
ggplot(tmp.hz1,aes(x = time.eval, y = haz)) +
  geom_smooth(aes(ymin = haz.lo, ymax = haz.hi), stat = "identity") +
  xlab("Time (d)") +ylab("Hazard")+ylim(c(0,0.01))+xlim(c(0,2*365))+
  geom_rug(data=tmp.kn,aes(x = knt,y = NULL),sides="b") 
Hazard with 95% confidence interval. User defined knots for spline.

Hazard with 95% confidence interval. User defined knots for spline.