Merge branch 'secmal'

* secmal:
  Rename plot_survival.R
  Add secondary malignancy plots
  Introduce initialization function
This commit is contained in:
Jens Sauer 2020-11-17 18:22:18 +01:00
commit 6c4f717793
6 changed files with 137 additions and 11 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
.RData
.Rhistory
survival_*
*.png
*.csv

View File

@ -10,10 +10,15 @@ survival.R
Source this file and call one of the plot functions to show plot in R.
`sma_plot_file_surv()` will save all plots to files.
plot_survival.R
---------------
secmal.R
--------
This file contains the secondary malignancy plots. You can call the
functions in R or `sma_plot_file_secmal()` to save them to files.
plots.R
-------
Special script to generate all plots and save them in files.
Run with `Rscript plot_survival.R`
Run with `Rscript plots.R`
Dependencies
------------

View File

@ -1,10 +1,12 @@
# secMalASCT survival plots
# secMalASCT plots
#
# License: GPL version 3
# Jens Mathis Sauer (c) 2020
source("survival.R")
source("secmal.R")
#
# Make all plots and save to file
sma_plot_file_surv()
sma_plot_file_secmal()

87
secmal.R Normal file
View File

@ -0,0 +1,87 @@
# Secondary malignancies calculations
#
# License: GPL version 3
# Jens Mathis Sauer (c) 2020
source("utils.R")
sma_init()
# Setup survival object
surv_sec <- Surv(time = secmal$time_at_risk, event = secmal$time_at_risk_status)
# plot cummulative events
sma_plot_secmal_event <- function() {
ggsurvplot(survfit(surv_sec ~ 1), data = secmal, xscale = "d_y",
title = "Secondary malignacies",
fun = "event",
break.time.by = sma_break.time.by,
surv.median.line = "hv",
risk.table = "nrisk_cumevents",
ggtheme = theme_bw())
}
# plot cummulative hazard
sma_plot_secmal_haz <- function() {
ggsurvplot(survfit(surv_sec ~ 1), data = secmal, xscale = "d_y",
title = "Secondary malignacies",
fun = "cumhaz",
break.time.by = sma_break.time.by,
risk.table = "abs_pct",
ggtheme = theme_bw())
}
# plot cummulative events per sex
sma_plot_secmal_event_sex <- function() {
ggsurvplot(survfit(surv_sec ~ sex, data = secmal), data = secmal, xscale = "d_y",
title = "Secondary malignacies",
fun = "event",
break.time.by = sma_break.time.by,
surv.median.line = "hv",
risk.table = "nrisk_cumevents",
pval = TRUE,
ggtheme = theme_bw())
}
# plot cummulative hazard per sex
sma_plot_secmal_haz_sex <- function() {
ggsurvplot(survfit(surv_sec ~ sex, data = secmal), data = secmal, xscale = "d_y",
title = "Secondary malignacies",
fun = "cumhaz",
surv.median.line = "hv",
break.time.by = sma_break.time.by,
risk.table = "abs_pct",
pval = TRUE,
ggtheme = theme_bw())
}
# plot cummulative events per diagnosis
sma_plot_secmal_event_dx <- function() {
ggsurvplot(survfit(surv_sec ~ diagnosis, data = secmal), data = secmal, xscale = "d_y",
title = "Secondary malignacies",
fun = "event",
break.time.by = sma_break.time.by,
risk.table = "nrisk_cumevents",
pval = TRUE,
ggtheme = theme_bw())
}
# plot cummulative hazard per diagnosis
sma_plot_secmal_haz_dx <- function() {
ggsurvplot(survfit(surv_sec ~ diagnosis, data = secmal), data = secmal, xscale = "d_y",
title = "Secondary malignacies",
fun = "cumhaz",
break.time.by = sma_break.time.by,
risk.table = "abs_pct",
pval = TRUE,
ggtheme = theme_bw())
}
# Write secondary malignancy plots to files
sma_plot_file_secmal <- function() {
sma_plot_file("secmal_event.png", png, sma_plot_secmal_event)
sma_plot_file("secmal_haz.png", png, sma_plot_secmal_haz)
sma_plot_file("secmal_event_sex.png", png, sma_plot_secmal_event_sex)
sma_plot_file("secmal_haz_sex.png", png, sma_plot_secmal_haz_sex)
sma_plot_file("secmal_event_dx.png", png, sma_plot_secmal_event_dx)
sma_plot_file("secmal_haz_dx.png", png, sma_plot_secmal_haz_dx)
}

View File

@ -3,11 +3,8 @@
# License: GPL version 3
# Jens Mathis Sauer (c) 2020
library(survival)
library(survminer)
source("utils.R")
secmal <- sma_load_data("current.csv")
sma_init()
# Setup survival object
surv_dx <- Surv(time = secmal$event_time_dx, event = secmal$event_status)

39
utils.R
View File

@ -3,8 +3,6 @@
# License: GPL version 3
# Jens Mathis Sauer (c) 2020
library(tidyverse)
#
# Write plot to filename
#
@ -86,3 +84,40 @@ sma_load_data <- function(file) {
# Global break.time.by value
#
sma_break.time.by = 5 * 365.25
#
# Initialize secMalASCT workspace
#
# This function can be called multiple times, the data will only
# be loaded once. To manually reload data call:
# > sma_init_force()
#
sma_init <- function() {
if (exists("sma_initialized") == FALSE) {
print("Initialize secMalASCT workspace")
print("Loading libraries")
library(survival)
library(survminer)
library(tidyverse)
print("Loading data")
#
# Use superassignment operator '<<-' to make
# secmal and sma_initialized with global scope.
#
secmal <<- sma_load_data("current.csv")
sma_initialized <<- TRUE
}
}
#
# Force reinitialization of workspace
#
# Forcefully calls sma_init() again.
#
sma_init_force <- function() {
print("Reloading secMalASCT data")
remove(sma_initialized, pos = ".GlobalEnv")
sma_init()
}