knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
The rap package contains functions for generating statistical metrics and visual means to assess the improvement in risk prediction of one risk model over another. It includes the Risk Assessment Plot (hence rap).
You can install the development version or rap from GitHub with:
# install.packages("devtools") devtools::install_github("JohnPickering/rap")
rap began as Matlab code in 2012 after I wrote a paper (1{target="_blank"}) for the Nephrology community on assessing the added value of one biomarker to a clinical prediction model. I worked with Professor Zoltan Endre on that paper. Dr David Cairns kindly provided some R code for the Risk Assessment Plot. This formed the basis of versions 0.1 to 0.4. Importantly, for those versions and the current version all errors are mine (sorry) and not those of Professor Endre or Dr Cairns. Since writing that paper I've come to consider some metrics as not helpful. So, for the current version I have dropped some statistical metrics that I believe are poor or wrongly applied. In particularly, I dropped providing the total NRI (Net Reclassification Improvement) and total IDI (Integrated Discrimination Improvement) metrics. These should never be presented because they inappropriately add together two fractions with differing denominators (NRI) or two means (IDI). Instead, these the NRIs and IDIs for those with and without the event of interest should be provided. Third, I have provided the change in AUCs rather than a p-value because the change is much more meaningful.
Version 1.03 were major changes:
allowed as input logistic regression models from glm (stats) and lrm (rms) as well as risk predictions calculated elsewhere.
provided as outputs in addition to the Risk Assessment Plot, a form of calibration plot and decision curve.
* the output from the main functions CI.raplot, and CI.classNRI are now lists that include the metrics for each bootstrap sample as well as the summary metrics. CI.classNRI also produces confusion matrices for those with and without the event of interest (separately). Bootstrapping is used to determine confidence intervals.
Version 1.10 :
addition of ROC plot.
calibration plot now uses (best practice) continuous curves (the old format is now "ggcalibrate_original()").
addition of precision recall curves.
all plots can be for one or two models.
Version 1.11:
made NRI metrics for models optional (use NRI_return = TRUE) to get them.
changed behaviour to that "x2 = NULL" is possible for CI.raplot. It has the effect of creating a model where every probability is 0.5.
* bug fix.
Version 1.22 (current):
addition of ggcontribute graph.
changed from geom_line to the geom_step for the ROC plot (because it represents the data better).
* bug fix.
This is a basic example for assessing the difference between two logistic regression models:
library(dplyr) library(rap) ## basic example code #### First make sure that data used has no missing values df <- data_risk %>% dplyr::filter(!is.na(baseline))%>% dplyr::filter(!is.na(new))%>% dplyr::filter(!is.na(outcome)) baseline_risk <- df$baseline # or the baseline glm model itself new_risk <- df$new # or the new glm model itself outcome <- df$outcome assessment <- CI.raplot(x1 = baseline_risk, x2 = new_risk, y = outcome, n.boot = 20, dp = 2) # Note the default is 1000 bootstraps (n.boot = 1000). This can take quite some time to run, so when testing I use a smaller number of bootstraps. # View results ## meta data (assessment$meta_data) ## exact point estimates (assessment$Metrics) ## bootstrap derived metrics with confidence intervals (assessment$Summary_metrics)
ggrap(x1 = baseline_risk, x2 = new_risk, y = outcome) # for Single risks x2 = NULL
ggcalibrate(x1 = baseline_risk, x2 = new_risk, y = outcome)
ggcalibrate_original(x1 = baseline_risk, x2 = new_risk, y = outcome, cut_type = "interval")
ggdecision(x1 = baseline_risk, x2 = new_risk, y = outcome)
ggprerec(x1 = baseline_risk, x2 = new_risk, y = outcome)
ggroc(x1 = baseline_risk, x2 = new_risk, y = outcome, carrington_line = TRUE)
Note, there are additional options for the ROC plot including labelling points and distinguishing areas of the plot that are diagnostic from those that are not.
Thanks to Professor Frank Harrell for these plots.
load("inst/extdata/fit_example") ggcontribute(x1 = eg_fit.glm)
This is a basic example for assessing the difference in the results of reclassification:
## basic example code baseline_class <- data_class$base_class new_class <- data_class$new_class outcome_class <- data_class$outcome class_assessment <- CI.classNRI(c1 = baseline_class, c2 = new_class, y = outcome_class, n.boot = 20, dp = 2) # Note the default is 2000 bootstraps (n.boot = 2000). This can take quite some time to run, so when testing I use a smaller number of bootstraps. # View results ## meta data (class_assessment$meta_data) ## exact point estimates and confusion matrices (class_assessment$Metrics) ## bootstrap derived metrics with confidence intervals (class_assessment$Summary_metrics)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.