Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve documentation of smoothness() #519

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: datawizard
Title: Easy Data Wrangling and Statistical Transformations
Version: 0.13.0.15
Version: 0.13.0.16
Authors@R: c(
person("Indrajeet", "Patil", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0003-1995-6531")),
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ BREAKING CHANGES
* Argument `drop_na` in `data_match()` is deprecated now. Please use `remove_na`
instead.

* The `"diff"` method in `smoothness()` was revised and now has a reversed
interpretation. Documentation was updated accordingly. (#374).

CHANGES

* The `select` argument, which is available in different functions to select
Expand Down
51 changes: 34 additions & 17 deletions R/smoothness.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#' Quantify the smoothness of a vector
#' Series smoothness
#'
#' 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 approximated in different ways, such
#' as the standard deviation of the standardized differences or the lag-one
#' autocorrelation.
#'
#' @param x Numeric vector (similar to a time series).
#' @param method Can be `"diff"` (the standard deviation of the standardized
Expand All @@ -12,6 +18,17 @@
#' plot(x)
#' smoothness(x, method = "cor")
#' smoothness(x, method = "diff")
#'
#' # A bootstrapped value can also be computed
#' smoothness(x, iterations = 100)
#'
#' # When perfectly linear, the "smoothness" is 1
#' smoothness(1:10)
strengejacke marked this conversation as resolved.
Show resolved Hide resolved
#'
#' # 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
Comment on lines 32 to 33
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be useful to have some details on the range of the output. Based on the examples I thought it would be between 0 and 1 but one of the answers in the CrossValidated post linked says it can go to -1. What does it mean to have a smoothness of -0.5?

#'
Expand Down Expand Up @@ -39,15 +56,14 @@
}

if (method == "cor") {
smooth <- stats::cor(utils::head(x, length(x) - lag), utils::tail(x, length(x) - lag))
smooth_value <- 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))

Check warning on line 61 in R/smoothness.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/smoothness.R,line=61,col=5,[object_overwrite_linter] 'diff' is an exported object from package 'base'. Avoid re-using such symbols.

Check warning on line 61 in R/smoothness.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/smoothness.R,line=61,col=5,[object_overwrite_linter] 'diff' is an exported object from package 'base'. Avoid re-using such symbols.
smooth_value <- 1 - mean((diff(diff) ** 2) / 4) # Note the reversal to match the other method
}

if (!is.null(iterations)) {
if (!requireNamespace("boot", quietly = TRUE)) {
insight::format_warning("Package 'boot' needed for bootstrapping SEs.")
} else {
if (requireNamespace("boot", quietly = TRUE)) {
results <- boot::boot(
data = x,
statistic = .boot_smoothness,
Expand All @@ -56,12 +72,14 @@
lag = lag
)
out_se <- stats::sd(results$t, na.rm = TRUE)
smooth <- data.frame(Smoothness = smooth, SE = out_se)
smooth_value <- data.frame(Smoothness = smooth_value, SE = out_se)
} else {
insight::format_warning("Package 'boot' needed for bootstrapping SEs.")
}
}

class(smooth) <- unique(c("parameters_smoothness", class(smooth)))
smooth
class(smooth_value) <- unique(c("parameters_smoothness", class(smooth_value)))
smooth_value
}


Expand All @@ -71,14 +89,13 @@
lag = 1,
iterations = NULL,
...) {
.smoothness <-
lapply(
x,
smoothness,
method = method,
lag = lag,
iterations = iterations
)
.smoothness <- lapply(
x,
smoothness,
method = method,
lag = lag,
iterations = iterations
)
.smoothness <- cbind(Parameter = names(.smoothness), do.call(rbind, .smoothness))
class(.smoothness) <- unique(c("parameters_smoothness", class(.smoothness)))
.smoothness
Expand Down
19 changes: 17 additions & 2 deletions man/smoothness.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test-distributions.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ test_that("distributions", {

expect_equal(kurtosis(x)$Kurtosis, -0.1119534, tolerance = 0.01)
expect_equal(skewness(x)$Skewness, -5.881466e-17, tolerance = 0.01)
expect_equal(as.numeric(smoothness(x, "diff")), 1.183699, tolerance = 0.01)
expect_equal(as.numeric(smoothness(x, "diff")), 0.9409494, tolerance = 0.01)
expect_equal(as.numeric(smoothness(x, "cor")), 0.9979799, tolerance = 0.01)
})
4 changes: 2 additions & 2 deletions tests/testthat/test-smoothness.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ test_that("smoothness works", {
set.seed(123)
x <- (-10:10)^3 + rnorm(21, 0, 100)
expect_equal(smoothness(x)[[1]], 0.9030014, tolerance = 0.001)
expect_equal(smoothness(x, method = "auto")[[1]], 1.750452, tolerance = 0.001)
expect_equal(smoothness(x, method = "auto")[[1]], 0.5131369, tolerance = 0.001)
})

test_that("smoothness works with iterations", {
Expand All @@ -11,7 +11,7 @@ test_that("smoothness works with iterations", {
set.seed(123)
x <- (-10:10)^3 + rnorm(21, 0, 100)
expect_equal(smoothness(x, iterations = 100)[[1]], 0.9030014, tolerance = 0.001)
expect_equal(smoothness(x, method = "auto", iterations = 100)[[1]], 1.750452, tolerance = 0.001)
expect_equal(smoothness(x, method = "auto", iterations = 100)[[1]], 0.5131369, tolerance = 0.001)
})


Expand Down
Loading