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 4 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
22 changes: 20 additions & 2 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)
Comment on lines +25 to +26
Copy link
Member

Choose a reason for hiding this comment

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

The examples in the current docs report smoothness < 1 with "cor" and > 1 with "diff". Can you add some explanations on how to interpret those?

Copy link
Member Author

Choose a reason for hiding this comment

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

indeed, actually the "diff" method had a reversed interpretation. I fixed accordingly (but that's a breaking change)

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, this requires a bullet point in NEWS and fixing the tests

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, this requires a bullet point in NEWS and fixing the tests

image

#'
#' # 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
#'
Expand Down Expand Up @@ -39,13 +56,14 @@
}

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

Check warning on line 59 in R/smoothness.R

View workflow job for this annotation

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

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

Check warning on line 59 in R/smoothness.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/smoothness.R,line=59,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)))
diff <- standardize(diff(x))

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.

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.
smooth <- 1 - mean((diff(diff) ** 2) / 4) # Note the reversal to match the other method

Check warning on line 62 in R/smoothness.R

View workflow job for this annotation

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

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

Check warning on line 62 in R/smoothness.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/smoothness.R,line=62,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 66 in R/smoothness.R

View workflow job for this annotation

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

file=R/smoothness.R,line=66,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 66 in R/smoothness.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/smoothness.R,line=66,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 +74,7 @@
lag = lag
)
out_se <- stats::sd(results$t, na.rm = TRUE)
smooth <- data.frame(Smoothness = smooth, SE = out_se)

Check warning on line 77 in R/smoothness.R

View workflow job for this annotation

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

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

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.

Loading