From 5fcc1093db73939983bdd3119be381ed14e1db7b Mon Sep 17 00:00:00 2001 From: "Steven Paul Sanderson II, MPH" Date: Sun, 7 Jan 2024 10:58:33 -0500 Subject: [PATCH] Fixes #374 --- NEWS.md | 2 ++ R/vec-cumulative-functions.R | 27 ++++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index ff131933..385b12d0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -26,6 +26,8 @@ dplyr in favor of using `dplyr::pick()` 5. Fix #385 - For `tidy_multi_dist_autoplot()` the `.plot_type = "quantile"` did not work. 6. Fix #383 - Update all autoplot functions to use linewidth instead of size. +7. Fix #375 - Update `cskewness()` to take advantage of vectorization with a speedup +of 124x faster. # TidyDensity 1.2.6 diff --git a/R/vec-cumulative-functions.R b/R/vec-cumulative-functions.R index 2c97765c..2ab6575d 100644 --- a/R/vec-cumulative-functions.R +++ b/R/vec-cumulative-functions.R @@ -62,10 +62,31 @@ cvar <- function(.x) { #' cskewness <- function(.x) { - skewness <- function(.x) { - sqrt(length(.x)) * sum((.x - mean(.x))^3) / (sum((.x - mean(.x))^2)^(3 / 2)) + n <- length(.x) + + if (n == 0L) { + return(.x) + } else if (n == 1L) { + return(0) + } + + m2 <- m3 <- term1 <- 0 + out <- numeric(n) + out[1] <- NaN + m1 <- .x[1] + + for (i in 2:n) { + n0 <- i - 1 + delta <- x[i] - m1 + delta_n <- delta/i + m1 <- m1 + delta_n + term1 <- delta*delta_n*n0 + m3 <- m3 + term1*delta_n*(n0 - 1) - 3*delta_n*m2 + m2 <- m2 + term1 + out[i] <- sqrt(i)*m3/m2^1.5 } - sapply(seq_along(.x), function(k, z) skewness(z[1:k]), z = .x) + + out } #' Cumulative Kurtosis