From 129bd405215325bd07be647cef88390b5b9238ad Mon Sep 17 00:00:00 2001 From: Hugo Gruson Date: Mon, 26 Jul 2021 17:03:27 +0200 Subject: [PATCH] Remove unnecessary vapply() --- R/bias.R | 12 ++---------- R/pit.R | 12 ++---------- 2 files changed, 4 insertions(+), 20 deletions(-) diff --git a/R/bias.R b/R/bias.R index 588b757b4..0fb4620c6 100644 --- a/R/bias.R +++ b/R/bias.R @@ -92,22 +92,14 @@ bias <- function(true_values, predictions) { n_pred <- ncol(predictions) # empirical cdf - P_x <- vapply(seq_along(true_values), - function(i) { - sum(predictions[i,] <= true_values[i]) / n_pred - }, - .0) + P_x <- rowSums(predictions <= true_values) / n_pred if (continuous_predictions) { res <- 1 - 2 * P_x return(res) } else { # for integer case also calculate empirical cdf for (y-1) - P_xm1 <- vapply(seq_along(true_values), - function(i) { - sum(predictions[i,] <= true_values[i] - 1) / n_pred - }, - .0) + P_xm1 <- rowSums(predictions <= (true_values - 1)) / n_pred res <- 1 - (P_x + P_xm1) return(res) diff --git a/R/pit.R b/R/pit.R index 81001bef2..fd4f154d7 100644 --- a/R/pit.R +++ b/R/pit.R @@ -166,11 +166,7 @@ pit <- function(true_values, # calculate emipirical cumulative distribution function as # Portion of (y_true <= y_predicted) - P_x <- vapply(seq_along(true_values), - function(i) { - sum(predictions[i, ] <= true_values[i]) / n_pred - }, - .0) + P_x <- rowSums(predictions <= true_values) / n_pred # calculate PIT for continuous predictions case if (continuous_predictions) { @@ -190,11 +186,7 @@ pit <- function(true_values, # calculate PIT for integer predictions case if (!continuous_predictions) { # empirical cdf for (y-1) for integer-valued predictions - P_xm1 <- vapply(seq_along(true_values), - function(i) { - sum(predictions[i,] <= true_values[i] - 1) / n_pred - }, - .0) + P_xm1 <- rowSums(predictions <= (true_values - 1)) / n_pred # do n_replicates times for randomised PIT u <- replicate(n_replicates, P_xm1 + stats::runif(n) * (P_x - P_xm1)) # apply Anderson Darling test on u values