diff --git a/R/smoothness.R b/R/smoothness.R index 28c2f4a0e..7adbc45e6 100644 --- a/R/smoothness.R +++ b/R/smoothness.R @@ -2,7 +2,7 @@ #' #' Functions to quantify the smoothness of a vector, which can be used in some cases #' as an index of "linearity". A smooth series is one that does not have abrupt changes in -#' its values. The smoothness of a series can be measured in different ways, such +#' its values. The smoothness of a series can be approximated in different ways, such #' as the standard deviation of the standardized differences or the lag-one #' autocorrelation. #' @@ -25,6 +25,10 @@ #' # When perfectly linear, the "smoothness" is 1 #' smoothness(1:10) #' +#' # And closer to zero for random +#' smoothness(rnorm(1000)) +#' smoothness(rnorm(1000), method = "diff") +#' #' @return Value of smoothness. #' @references https://stats.stackexchange.com/questions/24607/how-to-measure-smoothness-of-a-time-series-in-r #' @@ -54,7 +58,8 @@ smoothness.numeric <- function(x, if (method == "cor") { smooth <- stats::cor(utils::head(x, length(x) - lag), utils::tail(x, length(x) - lag)) } else { - smooth <- stats::sd(diff(x, lag = lag)) / abs(mean(diff(x, lag = lag))) + diff <- standardize(diff(x)) + smooth <- 1 - mean((diff(diff) ** 2) / 4) # Note the reversal to match the other method } if (!is.null(iterations)) { diff --git a/man/smoothness.Rd b/man/smoothness.Rd index 5b93db2b5..7d7d208be 100644 --- a/man/smoothness.Rd +++ b/man/smoothness.Rd @@ -26,7 +26,7 @@ Value of smoothness. \description{ Functions to quantify the smoothness of a vector, which can be used in some cases as an index of "linearity". A smooth series is one that does not have abrupt changes in -its values. The smoothness of a series can be measured in different ways, such +its values. The smoothness of a series can be approximated in different ways, such as the standard deviation of the standardized differences or the lag-one autocorrelation. } @@ -42,6 +42,10 @@ smoothness(x, iterations = 100) # When perfectly linear, the "smoothness" is 1 smoothness(1:10) +# And closer to zero for random +smoothness(rnorm(1000)) +smoothness(rnorm(1000), method = "diff") + } \references{ https://stats.stackexchange.com/questions/24607/how-to-measure-smoothness-of-a-time-series-in-r