Skip to content

Commit

Permalink
add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
GlenMartin31 committed Apr 24, 2023
1 parent 8a85d3a commit 60c5d80
Show file tree
Hide file tree
Showing 21 changed files with 723 additions and 34 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/R-CMD-check.yaml
Expand Up @@ -2,9 +2,9 @@
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master, develop]
branches: [main, master]
pull_request:
branches: [main, master, develop]
branches: [main, master]

name: R-CMD-check

Expand Down
50 changes: 50 additions & 0 deletions .github/workflows/test-coverage.yaml
@@ -0,0 +1,50 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: test-coverage

jobs:
test-coverage:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v3

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::covr
needs: coverage

- name: Test coverage
run: |
covr::codecov(
quiet = FALSE,
clean = FALSE,
install_path = file.path(Sys.getenv("RUNNER_TEMP"), "package")
)
shell: Rscript {0}

- name: Show testthat output
if: always()
run: |
## --------------------------------------------------------------------
find ${{ runner.temp }}/package -name 'testthat.Rout*' -exec cat '{}' \; || true
shell: bash

- name: Upload test results
if: failure()
uses: actions/upload-artifact@v3
with:
name: coverage-test-failures
path: ${{ runner.temp }}/package
4 changes: 3 additions & 1 deletion DESCRIPTION
Expand Up @@ -21,6 +21,7 @@ URL: https://github.com/GlenMartin31/predRupdate,
https://glenmartin31.github.io/predRupdate/
BugReports: https://github.com/GlenMartin31/predRupdate/issues
Suggests:
covr,
knitr,
rmarkdown,
testthat (>= 3.0.0)
Expand All @@ -30,7 +31,8 @@ Imports:
survival,
pROC,
ggplot2,
ggExtra
ggExtra,
rlang
Depends:
R (>= 2.10)
VignetteBuilder: knitr
1 change: 1 addition & 0 deletions NAMESPACE
Expand Up @@ -27,3 +27,4 @@ export(pred_predict)
export(pred_stacked_regression)
export(pred_update)
export(pred_validate)
importFrom(rlang,.data)
4 changes: 4 additions & 0 deletions R/map_newdata.R
Expand Up @@ -182,6 +182,10 @@ map_newdata.predinfo_survival <- function(x,
stop("'new_data' contains factor variables - convert to dummy/indicator variables first \n dummayvar() can help with this")
}

if (any(sapply(new_data, function(x) is.character(x)))) {
warning("'new_data' contains character variables - should these be indicator variables (see dummy_vars())?")
}

if (!is.null(binary_outcome)) {
stop("'binary_outcome' should be set to NULL if model_type=survival")
}
Expand Down
7 changes: 7 additions & 0 deletions R/predRupdate-package.R
@@ -0,0 +1,7 @@
#' @keywords internal
"_PACKAGE"

## usethis namespace: start
#' @importFrom rlang .data
## usethis namespace: end
NULL
18 changes: 6 additions & 12 deletions R/pred_validate.R
Expand Up @@ -330,12 +330,6 @@ print.predvalidate_survival <- function(x, ...) {
round((x$harrell_C - (stats::qnorm(0.975)*x$harrell_C_SE)), 4),
round((x$harrell_C + (stats::qnorm(0.975)*x$harrell_C_SE)), 4))
print(results)
# cat("\n")
# cat("\nOverall Performance Measures \n",
# "================================= \n", sep = "")
# cat("Cox-Snell R-squared: ", round(x$R2_CoxSnell, 4), "\n", sep = "")
# cat("Nagelkerke R-squared: ", round(x$R2_Nagelkerke, 4), "\n", sep = "")
# cat("Brier Score: ", round(x$BrierScore, 4), "\n", sep = "")

cat("\n Also examine the histogram of predicted risks. \n")
}
Expand Down Expand Up @@ -409,7 +403,7 @@ validate_logistic <- function(ObservedOutcome,
if (cal_plot == FALSE){
plot_df <- data.frame("Prob" = Prob)
print(ggplot2::ggplot(plot_df,
ggplot2::aes_string(x = "Prob")) +
ggplot2::aes(x = .data$Prob)) +
ggplot2::geom_histogram(bins = 30,
colour = "black") +
ggplot2::ggtitle("Histogram of the Probability Distribution") +
Expand Down Expand Up @@ -506,7 +500,7 @@ validate_survival <- function(ObservedOutcome,
if (cal_plot == FALSE){
plot_df <- data.frame("Prob" = Prob)
print(ggplot2::ggplot(plot_df,
ggplot2::aes_string(x = "Prob")) +
ggplot2::aes(x = .data$Prob)) +
ggplot2::geom_histogram(bins = 30,
colour = "black") +
ggplot2::ggtitle("Histogram of the Probability Distribution") +
Expand Down Expand Up @@ -571,8 +565,8 @@ flex_calplot <- function(model_type = c("logistic", "survival"),
"o" = spline_preds$fit)

print(ggExtra::ggMarginal(ggplot2::ggplot(plot_df,
ggplot2::aes_string(x = "p",
y = "o")) +
ggplot2::aes(x = .data$p,
y = .data$o)) +
ggplot2::geom_line(ggplot2::aes(linetype = "Calibration Curve",
colour = "Calibration Curve")) +
ggplot2::xlim(xlim) +
Expand Down Expand Up @@ -613,8 +607,8 @@ flex_calplot <- function(model_type = c("logistic", "survival"),
plot_df$observed_risk <- 1 - (exp(-bh[(max(which(bh[,2] <= time_horizon))),1])^(exp(stats::predict(vcal, type = "lp"))))

print(ggExtra::ggMarginal(ggplot2::ggplot(plot_df,
ggplot2::aes_string(x = "Prob",
y = "observed_risk")) +
ggplot2::aes(x = .data$Prob,
y = .data$observed_risk)) +
ggplot2::geom_line(ggplot2::aes(linetype = "Calibration Curve",
colour = "Calibration Curve")) +
ggplot2::xlim(xlim) +
Expand Down
1 change: 1 addition & 0 deletions README.Rmd
Expand Up @@ -17,6 +17,7 @@ knitr::opts_chunk$set(

<!-- badges: start -->
[![R-CMD-check](https://github.com/GlenMartin31/predRupdate/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/GlenMartin31/predRupdate/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/GlenMartin31/predRupdate/branch/master/graph/badge.svg)](https://app.codecov.io/gh/GlenMartin31/predRupdate?branch=master)
<!-- badges: end -->

The goal of predRupdate is to provide a suite of functions for validating a existing (i.e. previously developed) prediction/ prognostic model, and for applying model updating methods to said model, according to an available dataset.
Expand Down
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -6,6 +6,8 @@
<!-- badges: start -->

[![R-CMD-check](https://github.com/GlenMartin31/predRupdate/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/GlenMartin31/predRupdate/actions/workflows/R-CMD-check.yaml)
[![Codecov test
coverage](https://codecov.io/gh/GlenMartin31/predRupdate/branch/master/graph/badge.svg)](https://app.codecov.io/gh/GlenMartin31/predRupdate?branch=master)
<!-- badges: end -->

The goal of predRupdate is to provide a suite of functions for
Expand Down
14 changes: 14 additions & 0 deletions codecov.yml
@@ -0,0 +1,14 @@
comment: false

coverage:
status:
project:
default:
target: auto
threshold: 1%
informational: true
patch:
default:
target: auto
threshold: 1%
informational: true
32 changes: 32 additions & 0 deletions man/predRupdate-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 0 additions & 19 deletions tests/testthat/_snaps/pm_input_info.md

This file was deleted.

16 changes: 16 additions & 0 deletions tests/testthat/_snaps/pred_stacked_regression.md
@@ -0,0 +1,16 @@
# pred_stacked_regression has expected output

Code
summary(SR)
Output
Information about 1 existing model(s) of type 'logistic'
Model Coefficients
=================================
Intercept Age SexM Smoking_Status Diabetes Creatine
1 -2.67512 0.005346913 0.1589209 0.5232909 0.2542794 0.4553747
Model Functional Form
=================================
Age + SexM + Smoking_Status + Diabetes + Creatine

32 changes: 32 additions & 0 deletions tests/testthat/_snaps/pred_validate_logistic.md
@@ -0,0 +1,32 @@
# output of pred_validate is as expected - single models

Code
print(val_results)
Output
Calibration Measures
=================================
Estimate Std. Err Lower 95% Confidence Interval
Calibration-in-the-large 0.7323 0.0206 0.6921
Calibration Slope 0.6484 0.0463 0.5576
Upper 95% Confidence Interval
Calibration-in-the-large 0.7726
Calibration Slope 0.7392
Also examine the calibration plot, if produced.
Discrimination Measures
=================================
Estimate Std. Err Lower 95% Confidence Interval
AUC 0.5814 0.0057 0.5702
Upper 95% Confidence Interval
AUC 0.5927
Overall Performance Measures
=================================
Cox-Snell R-squared: -0.0481
Nagelkerke R-squared: -0.0863
Brier Score: 0.1249
Also examine the histogram of predicted risks.

25 changes: 25 additions & 0 deletions tests/testthat/_snaps/pred_validate_survival.md
@@ -0,0 +1,25 @@
# output of pred_validate is as expected - single models

Code
print(val_results)
Output
Calibration Measures
=================================
Estimate Std. Err Lower 95% Confidence Interval
Observed:Expected Ratio 0.6319 0.0113 0.6181
Calibration Slope 1.0757 0.0389 0.9994
Upper 95% Confidence Interval
Observed:Expected Ratio 0.6461
Calibration Slope 1.1519
Also examine the calibration plot, if produced.
Discrimination Measures
=================================
Estimate Std. Err Lower 95% Confidence Interval
Harrell C 0.5869 0.0032 0.5806
Upper 95% Confidence Interval
Harrell C 0.5932
Also examine the histogram of predicted risks.

0 comments on commit 60c5d80

Please sign in to comment.