knitr::opts_chunk$set(echo = TRUE,
                      results='asis',
                      warning = FALSE,
                      message = FALSE)

help_console <- function(topic, format=c("text", "html", "latex", "Rd"),
                         lines=NULL, before=NULL, after=NULL) {  
  format=match.arg(format)
  if (!is.character(topic)) topic <- deparse(substitute(topic))
  helpfile = utils:::.getHelpFile(help(topic))

  hs <- capture.output(switch(format, 
                              text=tools:::Rd2txt(helpfile),
                              html=tools:::Rd2HTML(helpfile),
                              latex=tools:::Rd2latex(helpfile),
                              Rd=tools:::prepare_Rd(helpfile)
                              )
                      )
  if(!is.null(lines)) hs <- hs[lines]
  hs <- c(before, hs, after)
  cat(hs, sep="\n")
  invisible(hs)
}

Install packages

rm(list =ls())

options(knitr.kable.NA = '.')

packs <- c("knitr"
           ,"survival"
           ,"devtools"
           ,"ggplot2"
           ,"htmlTable"
           ,"pammtools"
           )

install <- lapply(packs, function(x) require(x, character.only = T))

install_github("lisaerein/nicesurv")

library(nicesurv)

help(ggsurv)

Load the veteran dataset from the survival R package

data(veteran)

head(veteran)

Generate survival estimates using the survfit function

mysfit <- survfit(Surv(time, status) ~ trt, data = veteran)

plot(mysfit)

Generate a default ggsurv KM plot

ggsurv(sfit = mysfit)

Add ggsurv optional parameters:

Label and reorder groups, change colors and line types

ggsurv(sfit = mysfit,
       grname = "Treatment",
       groups = c(2,1),
       grlabs = c("Experimental", "Standard"),
       surv.col = c("black", "darkgray"),
       surv.lty = c(2,1))

Change the x axis limits/breaks to show 1 year and change y axis to percentage scale

ggsurv(sfit = mysfit,
       grname = "Treatment",
       groups = c(2,1),
       grlabs = c("Experimental", "Standard"),
       surv.col = c("black", "darkgray"),
       surv.lty = c(2,1),
       xlab = c("Days"),
       xlim = c(0, 360),
       xby = 30,
       perc = TRUE)

Remove 95% CI shading

ggsurv(sfit = mysfit,
       grname = "Treatment",
       groups = c(2,1),
       grlabs = c("Experimental", "Standard"),
       surv.col = c("black", "darkgray"),
       surv.lty = c(2,1),
       xlab = c("Days"),
       xlim = c(0, 360),
       xby = 30,
       perc = TRUE,
       ci = FALSE)

Add a risk table

ggsurv(sfit = mysfit,
       grname = "Treatment",
       groups = c(2,1),
       grlabs = c("Experimental", "Standard"),
       surv.col = c("black", "darkgray"),
       surv.lty = c(1,1),
       xlab = c("Days"),
       xlim = c(0, 360),
       xby = 30,
       perc = TRUE,
       ci = FALSE,
       grid = FALSE,
       risktab = TRUE,
       risktab.margins = c(0,0.01,0.2,0.15),
       risktab.times = c(0,30,60,120,180,240,300,360))

Create a non-stratified survival curve with risk table

mysfit2 <- survfit(Surv(time, status) ~ 1, data= veteran)

ggsurv(sfit = mysfit2,
       grid = c(FALSE, TRUE),
       ci.ribbon = FALSE,
       ci.lty = 2,
       ci.size = 0.5,
       cens = FALSE,
       xlab = "\nMonths",
       xlim = c(0,3)*360,
       xby = 90,
       xbrlabs = seq(0,36,3),
       risktab = T,
       risktab.margins = c(0,0.01,0.2,0.15),
       risktab.times = seq(0,360*3,90))

Other functions in the 'nicesurv' R package

'medtable' generates a table for median survival estimates

mt <- medtab(mysfit,
             printorig = FALSE)

mt <- medtab(mysfit,
             groups = c(2,1),
             grlabs = c("Experimental", "Standard"),
             printorig = FALSE)

'survtab' generates a table of survival estimates

st <- survtab(mysfit,
              times = c(0,30,120,360,500),
              surv.dec = 2,
              groups = c(1,2),
              grlabs = c("Std.", "Exp."),
              printorig = FALSE)

'nicecoxph' formats regression results from coxph models

Multiple regression

coxmod <- coxph(Surv(time, status) ~ trt + celltype + karno, data= veteran)

coxtbl <- nicecoxph(coxmod)

Univariate regression

coxmod <- coxph(Surv(time, status) ~ trt + celltype + karno, data= veteran)

coxtbl <- nicecoxph(df = veteran,
                    covs = c("trt", "celltype", "karno"),
                    ttevent = "time",
                    event = "status",
                    type3 = TRUE,
                    regtype = "uni")


lisaerein/nicesurv documentation built on Dec. 9, 2024, 12:17 a.m.