Skip to content

Commit

Permalink
#8: remove yardstick (#9)
Browse files Browse the repository at this point in the history
Close #8
  • Loading branch information
slamao authored Feb 17, 2025
2 parents 306ba82 + 4be0e01 commit 43f542e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 30 deletions.
5 changes: 2 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: stats4phc
Title: Performance Evaluation for the Prognostic Value of Predictive Models Intended to Support
Personalized Healthcare Through Predictiveness Curves and Positive / Negative Predictive Values
Version: 0.1.1
Version: 0.1.2
Authors@R: c(
person("Ondrej", "Slama", email = "[email protected]", role = c("aut", "cre")),
person("Darrick", "Shen", email = "[email protected]", role = "aut"),
Expand Down Expand Up @@ -36,8 +36,7 @@ Imports:
isotone (>= 1.1.0),
mgcv (>= 1.8.41),
pracma (>= 2.4.2),
tidyr (>= 1.3.0),
yardstick (>= 1.1.0)
tidyr (>= 1.3.0)
Suggests:
knitr,
rmarkdown,
Expand Down
40 changes: 14 additions & 26 deletions R/PV.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,19 @@ nonParametricPV <- function(outcome, score) {
ppv <- vapply(
thresh.predictions,
function(x) {
yardstick::ppv_vec(
truth = factor(outcome, levels = c("1", "0")),
estimate = factor(x, levels = c("1", "0")),
event_level = "first"
)
tp <- sum(outcome == 1 & x == 1)
fp <- sum(outcome == 0 & x == 1)
tp / (tp + fp)
},
numeric(1)
)

npv <- vapply(
thresh.predictions,
function(x) {
yardstick::npv_vec(
truth = factor(outcome, levels = c("1", "0")),
estimate = factor(x, levels = c("1", "0")),
event_level = "first"
)
tn <- sum(outcome == 0 & x == 0)
fn <- sum(outcome == 1 & x == 0)
tn / (tn + fn)
},
numeric(1)
)
Expand Down Expand Up @@ -136,35 +132,27 @@ nonParametricTR <- function(outcome, score) {

# Calc sensitivities and specificities at each risk percentile threshold
senses <- vapply(
thresh.predictions[1:(length(score) - 1)],
thresh.predictions,
function(x) {
yardstick::sens_vec(
truth = factor(outcome, levels = c("1", "0")),
estimate = factor(x, levels = c("1", "0")),
event_level = "first"
)
sum(outcome == 1 & x == 1) / sum(outcome == 1)
},
numeric(1)
)

specs <- vapply(
thresh.predictions[1:(length(score) - 1)],
thresh.predictions,
function(x) {
yardstick::spec_vec(
truth = factor(outcome, levels = c("1", "0")),
estimate = factor(x, levels = c("1", "0")),
event_level = "first"
)
sum(outcome == 0 & x == 0) / sum(outcome == 0)
},
numeric(1)
)

# Create a data.frame
dat <- data.frame(
score = c(min(score), score),
percentile = c(0, ecdf(score)(score)),
Sensitivity = c(1, senses, 0),
Specificity = c(0, specs, 1)
score = score,
percentile = ecdf(score)(score),
Sensitivity = senses,
Specificity = specs
) %>%
tidyr::pivot_longer(
cols = c("Sensitivity", "Specificity"),
Expand Down
4 changes: 4 additions & 0 deletions R/sensSpec.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ sensSpec <- function(outcome,
dat <- split(dat, dat$method) %>%
lapply(\(d) nonParametricTR(outcome = d$outcome, score = d$estimate)) %>%
bind_rows(.id = "method")

if (!plot.raw) {
dat <- add0thPercTR(dat)
}

# Plot
p <- ggplot(dat) +
Expand Down
24 changes: 24 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,30 @@ add0thPercPV <- function(x) {
}


add0thPercTR <- function(x) {
bind_rows(
x,
x %>%
group_by(.data$method) %>%
summarise(
score = NA,
percentile = 0,
pf = "Sensitivity",
value = 1
),
x %>%
group_by(.data$method) %>%
summarise(
score = NA,
percentile = 0,
pf = "Specificity",
value = 0
)
) %>%
arrange(.data$method, .data$pf, .data$percentile)
}


#' For snapshot testing of graphs
#'
#' @param code Code to create a graph
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ remotes::install_github(repo = "genentech/stats4phc")
For reproducibility, refer to a specific version tag, for example

``` r
remotes::install_github(repo = "genentech/stats4phc", ref = "v0.1.1")
remotes::install_github(repo = "genentech/stats4phc", ref = "v0.1.2")
```


Expand Down

0 comments on commit 43f542e

Please sign in to comment.