-
-
Notifications
You must be signed in to change notification settings - Fork 17
Improve documentation of smoothness()
#519
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
base: main
Are you sure you want to change the base?
Changes from 9 commits
ce47b60
e0e9894
e605941
442689d
1ea2c3d
6a91882
feb39af
309d929
b420e97
542736e
941046b
8302277
55743f7
ff8caed
969bdf0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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")), | ||
|
|
||
| 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 | ||
|
|
@@ -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) | ||
| #' | ||
| #' # 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤷 |
||
| #' | ||
|
|
@@ -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
|
||
| 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, | ||
|
|
@@ -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 | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -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 | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.