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 10 commits into
base: main
Choose a base branch
from
15 changes: 14 additions & 1 deletion 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 measured 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,13 @@
#' plot(x)
#' smoothness(x, method = "cor")
#' smoothness(x, method = "diff")
#'
#' # A boootstrapped value can also be computed
strengejacke marked this conversation as resolved.
Show resolved Hide resolved
#' smoothness(x, iterations = 100)
#'
#' # When perfectly linear, the "smoothness" is 1
#' smoothness(1:10)
strengejacke marked this conversation as resolved.
Show resolved Hide resolved
#'
#' @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?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

🤷

#'
Expand Down Expand Up @@ -39,13 +52,13 @@
}

if (method == "cor") {
smooth <- stats::cor(utils::head(x, length(x) - lag), utils::tail(x, length(x) - lag))

Check warning on line 55 in R/smoothness.R

View workflow job for this annotation

GitHub Actions / lint / lint

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

Check warning on line 55 in R/smoothness.R

View workflow job for this annotation

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

file=R/smoothness.R,line=55,col=5,[object_overwrite_linter] 'smooth' is an exported object from package 'stats'. Avoid re-using such symbols.
} else {
smooth <- stats::sd(diff(x, lag = lag)) / abs(mean(diff(x, lag = lag)))

Check warning on line 57 in R/smoothness.R

View workflow job for this annotation

GitHub Actions / lint / lint

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

Check warning on line 57 in R/smoothness.R

View workflow job for this annotation

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

file=R/smoothness.R,line=57,col=5,[object_overwrite_linter] 'smooth' is an exported object from package 'stats'. Avoid re-using such symbols.
}

if (!is.null(iterations)) {
if (!requireNamespace("boot", quietly = TRUE)) {

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=9,[if_not_else_linter] Prefer `if (A) x else y` to the less-readable `if (!A) y else x` in a simple if/else statement.

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=9,[if_not_else_linter] Prefer `if (A) x else y` to the less-readable `if (!A) y else x` in a simple if/else statement.
insight::format_warning("Package 'boot' needed for bootstrapping SEs.")
} else {
results <- boot::boot(
Expand All @@ -56,7 +69,7 @@
lag = lag
)
out_se <- stats::sd(results$t, na.rm = TRUE)
smooth <- data.frame(Smoothness = smooth, SE = out_se)

Check warning on line 72 in R/smoothness.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/smoothness.R,line=72,col=7,[object_overwrite_linter] 'smooth' is an exported object from package 'stats'. Avoid re-using such symbols.

Check warning on line 72 in R/smoothness.R

View workflow job for this annotation

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

file=R/smoothness.R,line=72,col=7,[object_overwrite_linter] 'smooth' is an exported object from package 'stats'. Avoid re-using such symbols.
}
}

Expand Down
15 changes: 13 additions & 2 deletions man/smoothness.Rd

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

Loading