Title: | Transform Univariate Time Series |
---|---|
Description: | Univariate time series operations that follow an opinionated design. The main principle of 'transx' is to keep the number of observations the same. Operations that reduce this number have to fill the observations gap. |
Authors: | Kostas Vasilopoulos [aut, cre] |
Maintainer: | Kostas Vasilopoulos <[email protected]> |
License: | GPL-3 |
Version: | 0.0.1.9000 |
Built: | 2024-11-02 03:12:20 UTC |
Source: | https://github.com/kvasilopoulos/transx |
Apply rolling operations over a moving window for size n
and increment step
.
blck(x, fn, n = 1L, fill = NA, align = "left", ...) blck_data(x, n = 1L)
blck(x, fn, n = 1L, fill = NA, align = "left", ...) blck_data(x, n = 1L)
x |
Univariate vector, numeric or ts object with only one dimension. |
fn |
|
n |
Window size. |
fill |
Numeric value(s) or function used to fill observations. |
align |
Specifying whether the index of the result should be left- or right-aligned or centered (default) compared to the rolling window of observations. |
... |
Additional arguments passed to the function |
roll()
returns a vector with the same class and attributes as the input vector.
roll_data()
Returns a list of length length(x)/step.
x <- seq(10, 1, -1)
x <- seq(10, 1, -1)
Functions used to calculate non-overlapping blocks
blck_idx(x, n, align)
blck_idx(x, n, align)
x |
data argument |
n |
size of the block |
align |
align |
Non-Overapping Block Moment Calculations
blck_mean(x, n = 2L, fill = NA) blck_median(x, n = 2L) blck_modex(x, n = 2L) blck_sd(x, n = 2L)
blck_mean(x, n = 2L, fill = NA) blck_median(x, n = 2L) blck_modex(x, n = 2L) blck_sd(x, n = 2L)
x |
numeric vector |
n |
block size |
fill |
Numeric value(s) or function used to fill observations. |
Removes the mean, the median or the mode from the series.
demean(x, na.rm = getOption("transx.na.rm")) demedian(x, na.rm = getOption("transx.na.rm")) demode(x, na.rm = getOption("transx.na.rm"))
demean(x, na.rm = getOption("transx.na.rm")) demedian(x, na.rm = getOption("transx.na.rm")) demode(x, na.rm = getOption("transx.na.rm"))
x |
Univariate vector, numeric or ts object with only one dimension. |
na.rm |
A value indicating whether NA values should be stripped before the computation proceeds. |
Returns a vector with the same class and attributes as the input vector.
x <- c(2,5,10,20,30) summary(x) demean(x) demedian(x) demode(x)
x <- c(2,5,10,20,30) summary(x) demean(x) demedian(x) demode(x)
Returns suitably lagged and iterated difference
diffx
computes simple differences.
rdffix
computes percentage differences.
ldiffx
computes logged differences.
diffx(x, n = 1L, order = 1L, rho = 1, fill = NA) rdiffx(x, n = 1L, order = 1L, rho = NULL, fill = NA) ldiffx(x, n = 1L, order = 1L, rho = 1, fill = NA)
diffx(x, n = 1L, order = 1L, rho = 1, fill = NA) rdiffx(x, n = 1L, order = 1L, rho = NULL, fill = NA) ldiffx(x, n = 1L, order = 1L, rho = 1, fill = NA)
x |
Univariate vector, numeric or ts object with only one dimension. |
n |
Value indicating which lag to use. |
order |
Value indicating the order of the difference. |
rho |
Value indicating the autocorrelation parameter. The purpose of this parameter is to provide quasi-differencing assuming the value falls within 0 and 1. |
fill |
Numeric value(s) or function used to fill observations. |
x <- c(2, 4, 8, 20) diffx(x) rdiffx(x) ldiffx(x)
x <- c(2, 4, 8, 20) diffx(x) rdiffx(x) ldiffx(x)
Remove global deterministic trend information from the series.
dt_lin
removes the linear trend.
dt_quad
removes the quadratic trend.
dt_poly
removes the nth-degree polynomial trend.
dtrend_lin(x, bp = NULL, na.rm = getOption("transx.na.rm")) dtrend_quad(x, bp = NULL, na.rm = getOption("transx.na.rm")) dtrend_poly(x, degree, bp = NULL, na.rm = getOption("transx.na.rm"))
dtrend_lin(x, bp = NULL, na.rm = getOption("transx.na.rm")) dtrend_quad(x, bp = NULL, na.rm = getOption("transx.na.rm")) dtrend_poly(x, degree, bp = NULL, na.rm = getOption("transx.na.rm"))
x |
Univariate vector, numeric or ts object with only one dimension. |
bp |
Break points to define piecewise segments of the data. |
na.rm |
A value indicating whether NA values should be stripped before the computation proceeds. |
degree |
Value indicating the degree of polynomial |
Returns a vector with the same class and attributes as the input vector.
set.seed(123) t <- 1:20 # Linear trend x <- 3*sin(t) + t plotx(cbind(x, dtrend_lin(x))) # Quadratic trend x2 <- 3*sin(t) + t + t^2 plotx(cbind(raw = x2, quad = dtrend_quad(x2))) # Introduce a breaking point at point = 10 xbp <- 3*sin(t) + t xbp[10:20] <- x[10:20] + 15 plotx(cbind(raw = xbp, lin = dtrend_lin(xbp), lin_bp = dtrend_lin(xbp, bp = 10)))
set.seed(123) t <- 1:20 # Linear trend x <- 3*sin(t) + t plotx(cbind(x, dtrend_lin(x))) # Quadratic trend x2 <- 3*sin(t) + t + t^2 plotx(cbind(raw = x2, quad = dtrend_quad(x2))) # Introduce a breaking point at point = 10 xbp <- 3*sin(t) + t xbp[10:20] <- x[10:20] + 15 plotx(cbind(raw = xbp, lin = dtrend_lin(xbp), lin_bp = dtrend_lin(xbp, bp = 10)))
locf
and nocb
fill_both(body, idx, first = c("locf", "nocb"))
fill_both(body, idx, first = c("locf", "nocb"))
body |
The body of the vector. |
idx |
the index to replace with. |
first |
Select which filling algorithms will occur first "locf" or "nocb". |
Returns a vector with the same class and attributes as the input vector.
leadx(1:4, fill = fill_both) leadx(1:4, fill = ~ fill_both(.x,.y, first = "nocb")) lagx(1:4, fill = fill_both) lagx(1:4, fill = ~ fill_both(.x,.y, first = "nocb")) set.seed(123) x <- rnorm(10) smooth_ma(x, 4, fill = fill_both)
leadx(1:4, fill = fill_both) leadx(1:4, fill = ~ fill_both(.x,.y, first = "nocb")) lagx(1:4, fill = fill_both) lagx(1:4, fill = ~ fill_both(.x,.y, first = "nocb")) set.seed(123) x <- rnorm(10) smooth_ma(x, 4, fill = fill_both)
fill_linear(body, idx, ...)
fill_linear(body, idx, ...)
body |
The body of the vector. |
idx |
the index to replace with. |
... |
Further arguments passed to |
Returns a vector with the same class and attributes as the input vector.
x <- c(5,3,2,2,5) xlen <- length(x) n <- 2 n <- pmin(n, xlen) idx <- 1:n body <- x[seq_len(xlen - n)] fill_linear(body, idx)
x <- c(5,3,2,2,5) xlen <- length(x) n <- 2 n <- pmin(n, xlen) idx <- 1:n body <- x[seq_len(xlen - n)] fill_linear(body, idx)
fill_locf(body, idx, fail = NA)
fill_locf(body, idx, fail = NA)
body |
The body of the vector. |
idx |
the index to replace with. |
fail |
In case it fails to fill some values. |
Returns a vector with the same class and attributes as the input vector.
x <- c(5,3,2,2,5) lagx(x, n = 2, fill = fill_locf) # A not so very neat way to deal with NA when `fill_locf` fails is (WIP) lagx(x, n = 2, fill = ~ fill_locf(.x,.y, fail = 0)) leadx(x, n = 2, fill = fill_locf) lagx(x, n = 2, fill = fill_nocb) leadx(x, n = 2, fill = fill_nocb) leadx(x, n = 2, fill = ~ fill_nocb(.x,.y, fail = 0))
x <- c(5,3,2,2,5) lagx(x, n = 2, fill = fill_locf) # A not so very neat way to deal with NA when `fill_locf` fails is (WIP) lagx(x, n = 2, fill = ~ fill_locf(.x,.y, fail = 0)) leadx(x, n = 2, fill = fill_locf) lagx(x, n = 2, fill = fill_nocb) leadx(x, n = 2, fill = fill_nocb) leadx(x, n = 2, fill = ~ fill_nocb(.x,.y, fail = 0))
fill_nocb(body, idx, fail = NA)
fill_nocb(body, idx, fail = NA)
body |
The body of the vector. |
idx |
the index to replace with. |
fail |
In case it fails to fill some values. |
Returns a vector with the same class and attributes as the input vector.
x <- c(5,3,2,2,5) leadx(x, n = 2, fill = fill_locf) xlen <- length(x) n <- 2 n <- pmin(n, xlen) idx <- (xlen - n + 1):xlen body <- x[-seq_len(n)] fill_nocb(body, idx, NA) fill_both(body, idx, first = "nocb")
x <- c(5,3,2,2,5) leadx(x, n = 2, fill = fill_locf) xlen <- length(x) n <- 2 n <- pmin(n, xlen) idx <- (xlen - n + 1):xlen body <- x[-seq_len(n)] fill_nocb(body, idx, NA) fill_both(body, idx, first = "nocb")
fill_spline(body, idx, ...)
fill_spline(body, idx, ...)
body |
The body of the vector. |
idx |
the index to replace with. |
... |
Further arguments passed to |
Returns a vector with the same class and attributes as the input vector.
x <- c(5,3,NA,2,5) fill_spline(x, 3)
x <- c(5,3,NA,2,5) fill_spline(x, 3)
fill_vec(vec)
fill_vec(vec)
vec |
Numeric vector of the same length |
lagx(c(1:5), fill = fill_vec(1:5)) ## Not run: lagx(c(1:5), fill = fill_window(roll_mean(.x))) ## End(Not run)
lagx(c(1:5), fill = fill_vec(1:5)) ## Not run: lagx(c(1:5), fill = fill_window(roll_mean(.x))) ## End(Not run)
fill_window(fn, ...)
fill_window(fn, ...)
fn |
Window function, usually of the |
... |
Further arguments passed to |
## Not run: lagx(c(1:5), fill = fill_window(rec_mean)) ## End(Not run)
## Not run: lagx(c(1:5), fill = fill_window(rec_mean)) ## End(Not run)
This function computes the cyclical component of the Baxter-King filter.
filter_bk(x, fill = NA, ...)
filter_bk(x, fill = NA, ...)
x |
Univariate vector, numeric or ts object with only one dimension. |
fill |
Numeric value(s) or function used to fill observations. |
... |
Further arguments passed to |
unemp <- ggplot2::economics$unemploy unemp_cycle <- filter_bk(unemp) plotx(cbind(unemp, unemp_cycle))
unemp <- ggplot2::economics$unemploy unemp_cycle <- filter_bk(unemp) plotx(cbind(unemp, unemp_cycle))
This function computes the cyclical component of the Boosted Hodrick-Prescot filter.
filter_boosted_hp( x, lambda = 1600, stopping = "nonstop", sig_p = 0.05, max_iter = 100 )
filter_boosted_hp( x, lambda = 1600, stopping = "nonstop", sig_p = 0.05, max_iter = 100 )
x |
Univariate vector, numeric or ts object with only one dimension. |
lambda |
Smoothness penalty parameter. |
stopping: |
|
sig_p: |
The significance level of the ADF test as the stopping criterion. It is used only when stopping == "adf". |
max_iter: |
The maximum number of iterations. |
Returns a vector with the same class and attributes as the input vector.
This function has been retrieved and rewritten from https://github.com/zhentaoshi/Boosted_HP_filter/blob/master/R/BoostedHP.R
Phillips, P.C.B. and Shi, Z. (2021), BOOSTING: WHY YOU CAN USE THE HP FILTER. International Economic Review. https://doi.org/10.1111/iere.12495
unemp <- ggplot2::economics$unemploy unemp_cycle <- filter_boosted_hp(unemp) plotx(cbind(unemp, unemp_cycle))
unemp <- ggplot2::economics$unemploy unemp_cycle <- filter_boosted_hp(unemp) plotx(cbind(unemp, unemp_cycle))
This function computes the cyclical component of the Butterworth filter.
filter_bw(x, ...)
filter_bw(x, ...)
x |
Univariate vector, numeric or ts object with only one dimension. |
... |
Further arguments passed to |
unemp <- ggplot2::economics$unemploy unemp_cycle <- filter_bw(unemp, freq = 10) plotx(cbind(unemp, unemp_cycle))
unemp <- ggplot2::economics$unemploy unemp_cycle <- filter_bw(unemp, freq = 10) plotx(cbind(unemp, unemp_cycle))
This function computes the cyclical component of the Christiano-Fitzgerald filter.
filter_cf(x, ...)
filter_cf(x, ...)
x |
Univariate vector, numeric or ts object with only one dimension. |
... |
Further arguments passed to |
unemp <- ggplot2::economics$unemploy unemp_cycle <- filter_cf(unemp) plotx(cbind(unemp, unemp_cycle))
unemp <- ggplot2::economics$unemploy unemp_cycle <- filter_cf(unemp) plotx(cbind(unemp, unemp_cycle))
This function computes the cyclical component of the Hamilton filter.
filter_hamilton(x, p = 4, horizon = 8, fill = NA)
filter_hamilton(x, p = 4, horizon = 8, fill = NA)
x |
Univariate vector, numeric or ts object with only one dimension. |
p |
A value indicating the number of lags |
horizon |
A value indicating the number of periods to look ahead. |
fill |
Numeric value(s) or function used to fill observations. |
Returns a vector with the same class and attributes as the input vector.
unemp <- ggplot2::economics$unemploy unemp_cycle <- filter_hamilton(unemp) plotx(cbind(unemp, unemp_cycle))
unemp <- ggplot2::economics$unemploy unemp_cycle <- filter_hamilton(unemp) plotx(cbind(unemp, unemp_cycle))
This function computes the cyclical component of the Hodrick-Prescot filter.
filter_hp(x, ...)
filter_hp(x, ...)
x |
Univariate vector, numeric or ts object with only one dimension. |
... |
Further arguments passed to |
select_lambda
unemp <- ggplot2::economics$unemploy unemp_cycle <- filter_hp(unemp, freq = select_lambda("monthly")) plotx(cbind(unemp, unemp_cycle))
unemp <- ggplot2::economics$unemploy unemp_cycle <- filter_hp(unemp, freq = select_lambda("monthly")) plotx(cbind(unemp, unemp_cycle))
This function computes the cyclical component of the trigonometric regression filter.
filter_tr(x, ...)
filter_tr(x, ...)
x |
Univariate vector, numeric or ts object with only one dimension. |
... |
Further arguments passed to |
unemp <- ggplot2::economics$unemploy unemp_cycle <- filter_tr(unemp, pl=8, pu=40) plotx(cbind(unemp, unemp_cycle))
unemp <- ggplot2::economics$unemploy unemp_cycle <- filter_tr(unemp, pl=8, pu=40) plotx(cbind(unemp, unemp_cycle))
Compute the sample geometric mean.
gmean(x, na.rm = getOption("transx.na.rm"))
gmean(x, na.rm = getOption("transx.na.rm"))
x |
Univariate vector, numeric or ts object with only one dimension. |
na.rm |
A value indicating whether NA values should be stripped before the computation proceeds. |
Returns a vector with the same class and attributes as the input vector.
Find the "previous" (lagx()
) or "next" (leadx()
) values in a vector.
Useful for comparing values behind of or ahead of the current values.
lagx(x, n = 1L, fill = NA) leadx(x, n = 1L, fill = NA)
lagx(x, n = 1L, fill = NA) leadx(x, n = 1L, fill = NA)
x |
Univariate vector, numeric or ts object with only one dimension. |
n |
Value indicating the number of positions to lead or lag by. |
fill |
Numeric value(s) or function used to fill observations. |
This functions has been taken and modified from the dplyr
package,
however, to reduce dependencies they are not imported.
Returns a vector with the same class and attributes as the input vector.
x <- c(5,3,2,2,5) lagx(x) lagx(x, fill = mean) lagx(x, fill = fill_nocb) leadx(x) leadx(x, fill = fill_locf)
x <- c(5,3,2,2,5) lagx(x) lagx(x, fill = mean) lagx(x, fill = fill_nocb) leadx(x) leadx(x, fill = fill_locf)
Compute the sample median.
modex(x, na.rm = getOption("transx.na.rm")) modex_int(x, na.rm = getOption("transx.na.rm"))
modex(x, na.rm = getOption("transx.na.rm")) modex_int(x, na.rm = getOption("transx.na.rm"))
x |
Univariate vector, numeric or ts object with only one dimension. |
na.rm |
A value indicating whether NA values should be stripped before the computation proceeds. |
out_iqr(x, cutoff = 1.5, fill = NA, ...)
out_iqr(x, cutoff = 1.5, fill = NA, ...)
x |
Univariate vector, numeric or ts object with only one dimension. |
cutoff |
Cutoff point that determines the number of score quantities after which an observation is considered outlier. |
fill |
Numeric value(s) or function used to fill observations. |
... |
further arguments passed to |
out_iqr(c(0,1,3,4,20))
out_iqr(c(0,1,3,4,20))
out_pt(x, pt_low = 0.1, pt_high = 0.9, fill = NA)
out_pt(x, pt_low = 0.1, pt_high = 0.9, fill = NA)
x |
Univariate vector, numeric or ts object with only one dimension. |
pt_low |
the lowest quantile |
pt_high |
the highest quantile |
fill |
Numeric value(s) or function used to fill observations. |
x <- c(1, 3, -1, 5, 10, 100) out_pt(x)
x <- c(1, 3, -1, 5, 10, 100) out_pt(x)
out_score_z(x, cutoff = 3, fill = NA, ...)
out_score_z(x, cutoff = 3, fill = NA, ...)
x |
Univariate vector, numeric or ts object with only one dimension. |
cutoff |
Cutoff point that determines the number of score quantities after which an observation is considered outlier. |
fill |
Numeric value(s) or function used to fill observations. |
... |
Further arguments passed to |
out_score_z(c(0,0.1,2,1,3,2.5,2,.5,6,4,100))
out_score_z(c(0,0.1,2,1,3,2.5,2,.5,6,4,100))
out_score_zrob(x, cutoff = 3.5, fill = NA, ...)
out_score_zrob(x, cutoff = 3.5, fill = NA, ...)
x |
Univariate vector, numeric or ts object with only one dimension. |
cutoff |
Cutoff point that determines the number of score quantities after which an observation is considered outlier. |
fill |
Numeric value(s) or function used to fill observations. |
... |
further arguments passed to |
out_score_zrob(c(0,0.1,2,1,3,2.5,2,.5,6,4,100))
out_score_zrob(c(0,0.1,2,1,3,2.5,2,.5,6,4,100))
out_threshold(x, tlow = NULL, thigh = NULL, fill = NA)
out_threshold(x, tlow = NULL, thigh = NULL, fill = NA)
x |
Univariate vector, numeric or ts object with only one dimension. |
tlow |
The lower threshold. |
thigh |
The upper threshold. |
fill |
Numeric value(s) or function used to fill observations. |
Returns a vector with the same class and attributes as the input vector.
x <- c(1, 3, -1, 5, 10, 100) out_threshold(x, tlow = 0, fill = 0) out_threshold(x, thigh = 9, fill = function(x) quantile(x, 0.9))
x <- c(1, 3, -1, 5, 10, 100) out_threshold(x, tlow = 0, fill = 0) out_threshold(x, thigh = 9, fill = function(x) quantile(x, 0.9))
Replace extremely values that are defined by min
and max
.
out_winsorise(x, min = quantile(x, 0.05), max = quantile(x, 0.95)) out_winsorize(x, min = quantile(x, 0.05), max = quantile(x, 0.95))
out_winsorise(x, min = quantile(x, 0.05), max = quantile(x, 0.95)) out_winsorize(x, min = quantile(x, 0.05), max = quantile(x, 0.95))
x |
Univariate vector, numeric or ts object with only one dimension. |
min |
The lower bound, all values lower than this will be replaced by this value. |
max |
The upper bound, all values above than this will be replaced by this value. |
Returns a vector with the same class and attributes as the input vector.
x <- c(1, 3, -1, 5, 10, 100) out_winsorise(x)
x <- c(1, 3, -1, 5, 10, 100) out_winsorise(x)
pow(x, pow = NULL, modulus = FALSE)
pow(x, pow = NULL, modulus = FALSE)
x |
Univariate vector, numeric or ts object with only one dimension. |
pow |
The nth power. |
modulus |
positive |
Returns a vector with the same class and attributes as the input vector.
pow(2, 2) pow(-2, 2) pow(-2,2, TRUE)
pow(2, 2) pow(-2, 2) pow(-2,2, TRUE)
pow_boxcox(x, lambda = NULL, lambda2 = NULL, ...)
pow_boxcox(x, lambda = NULL, lambda2 = NULL, ...)
x |
Univariate vector, numeric or ts object with only one dimension. |
lambda |
Transformation exponent, |
lambda2 |
Transformation exponent, |
... |
Further arguments passed to |
Returns a vector with the same class and attributes as the input vector.
Box, G. E., & Cox, D. R. (1964). An analysis of transformations. Journal of the Royal Statistical Society. Series B (Methodological), 211-252. https://www.jstor.org/stable/2984418
set.seed(123) x <- runif(10) pow_boxcox(x, 3)
set.seed(123) x <- runif(10) pow_boxcox(x, 3)
The transformation was reported to be successful in transform unimodal skewed distribution into normal distribution, but is not quite useful for bimodal or U-shaped distribution.
pow_manly(x, lambda = NULL)
pow_manly(x, lambda = NULL)
x |
Univariate vector, numeric or ts object with only one dimension. |
lambda |
Transformation exponent, |
Returns a vector with the same class and attributes as the input vector.
set.seed(123) x <- runif(10) pow_manly(x, 3)
set.seed(123) x <- runif(10) pow_manly(x, 3)
pow_tukey(x, lambda = NULL, ...)
pow_tukey(x, lambda = NULL, ...)
x |
Univariate vector, numeric or ts object with only one dimension. |
lambda |
Transformation exponent, |
... |
Further arguments passed to |
Returns a vector with the same class and attributes as the input vector.
set.seed(123) x <- runif(10) pow_tukey(x, 2)
set.seed(123) x <- runif(10) pow_tukey(x, 2)
pow_yj(x, lambda = NULL, ...)
pow_yj(x, lambda = NULL, ...)
x |
Univariate vector, numeric or ts object with only one dimension. |
lambda |
Transformation exponent, |
... |
Further arguments passed to |
Returns a vector with the same class and attributes as the input vector.
Yeo, I., & Johnson, R. (2000). A New Family of Power Transformations to Improve Normality or Symmetry. Biometrika, 87(4), 954-959. http://www.jstor.org/stable/2673623
set.seed(123) x <- runif(10) pow_yj(x, 3)
set.seed(123) x <- runif(10) pow_yj(x, 3)
Change the base year.
rebase(x, n = NULL) rebase_origin(x)
rebase(x, n = NULL) rebase_origin(x)
x |
Univariate vector, numeric or ts object with only one dimension. |
n |
The index of the new base year. |
Returns a vector with the same class and attributes as the input vector.
x <- 3:10 # New base would be 5 rebase(x, 5) # Or the origin rebase_origin(x) # Fro the base to be 100 or 0 then: rebase(x, 5)*100 rebase(x, 5) - 1
x <- 3:10 # New base would be 5 rebase(x, 5) # Or the origin rebase_origin(x) # Fro the base to be 100 or 0 then: rebase(x, 5)*100 rebase(x, 5) - 1
Apply rolling operations over a moving window for size n
and increment step
.
roll_idx( x, n = 1, step = 1, align = c("left", "center", "right"), complete = TRUE ) roll( x, fn, n = 1L, step = 1L, fill = NA, align = c("left", "center", "right"), ... ) roll_data(x, n = 1L, step = 1L, align = c("left", "center", "right"))
roll_idx( x, n = 1, step = 1, align = c("left", "center", "right"), complete = TRUE ) roll( x, fn, n = 1L, step = 1L, fill = NA, align = c("left", "center", "right"), ... ) roll_data(x, n = 1L, step = 1L, align = c("left", "center", "right"))
x |
Univariate vector, numeric or ts object with only one dimension. |
n |
Window size. |
step |
Rolling window step. |
align |
Specifying whether the index of the result should be left- or right-aligned or centered (default) compared to the rolling window of observations. |
fn |
|
fill |
Numeric value(s) or function used to fill observations. |
... |
Further arguments passed to the function |
roll()
: returns a vector with the same class and attributes as the input vector.
roll_idx()
: Returns the index that is used to calculate the subsets.
roll_data()
: Returns a list subsets of length length(x)/step.
# x is odd roll_idx(1:9, 2, 1, align = "left") roll_idx(1:9, 2, 1, align = "center") roll_idx(1:9, 2, 1, align = "right") # reduces # This works roll_idx(1:9, 3, 1, align = "left") roll_idx(1:9, 3, 1, align = "center") roll_idx(1:9, 3, 1, align = "right") roll_idx(1:9, 2, 2, align = "left") roll_idx(1:9, 2, 2, align = "center") roll_idx(1:9, 2, 2, align = "right") roll_idx(1:9, 3, 2, align = "left") roll_idx(1:9, 3, 2, align = "center") roll_idx(1:9, 3, 2, align = "right") # reduces # x is even roll_idx(1:8, 2, 2, align = "left") roll_idx(1:8, 2, 2, align = "center") roll_idx(1:8, 2, 2, align = "right") # reduces roll_idx(1:8, 2, 1, align = "left") roll_idx(1:8, 2, 1, align = "center") roll_idx(1:8, 2, 1, align = "right") roll_idx(1:8, 3, 1, align = "left") roll_idx(1:8, 3, 1, align = "center") roll_idx(1:8, 3, 1, align = "right") # reduces roll_idx(1:8, 3, 1, align = "left") roll_idx(1:8, 3, 1, align = "center") roll_idx(1:8, 3, 1, align = "right") # reduces x <- seq(10, 1, -1) roll_data(x, 2, align = "left") roll_data(x, 2, align = "right") roll(x, max, 3) roll(x, max, 3, align = "right") x <- 1:6 roll_data(x, 2) roll(x, mean, 2) roll_data(x, 2, 2) roll(x, mean, 2, 2)
# x is odd roll_idx(1:9, 2, 1, align = "left") roll_idx(1:9, 2, 1, align = "center") roll_idx(1:9, 2, 1, align = "right") # reduces # This works roll_idx(1:9, 3, 1, align = "left") roll_idx(1:9, 3, 1, align = "center") roll_idx(1:9, 3, 1, align = "right") roll_idx(1:9, 2, 2, align = "left") roll_idx(1:9, 2, 2, align = "center") roll_idx(1:9, 2, 2, align = "right") roll_idx(1:9, 3, 2, align = "left") roll_idx(1:9, 3, 2, align = "center") roll_idx(1:9, 3, 2, align = "right") # reduces # x is even roll_idx(1:8, 2, 2, align = "left") roll_idx(1:8, 2, 2, align = "center") roll_idx(1:8, 2, 2, align = "right") # reduces roll_idx(1:8, 2, 1, align = "left") roll_idx(1:8, 2, 1, align = "center") roll_idx(1:8, 2, 1, align = "right") roll_idx(1:8, 3, 1, align = "left") roll_idx(1:8, 3, 1, align = "center") roll_idx(1:8, 3, 1, align = "right") # reduces roll_idx(1:8, 3, 1, align = "left") roll_idx(1:8, 3, 1, align = "center") roll_idx(1:8, 3, 1, align = "right") # reduces x <- seq(10, 1, -1) roll_data(x, 2, align = "left") roll_data(x, 2, align = "right") roll(x, max, 3) roll(x, max, 3, align = "right") x <- 1:6 roll_data(x, 2) roll(x, mean, 2) roll_data(x, 2, 2) roll(x, mean, 2, 2)
root
: nth root
root_sqrt
: square root
root_cubic
: cubic root
root(x, root = NULL, modulus = FALSE) root_sq(x, ...) root_cubic(x, ...)
root(x, root = NULL, modulus = FALSE) root_sq(x, ...) root_cubic(x, ...)
x |
Univariate vector, numeric or ts object with only one dimension. |
root |
The nth root. |
modulus |
Transformation will work for data with both positive and negative |
... |
Further arguments passed to |
root(4, 2) root(-4, 2) root(-4, 2, TRUE)
root(4, 2) root(-4, 2) root(-4, 2, TRUE)
scale_range(x, to, na.rm = getOption("transx.na.rm")) scale_minmax(x, na.rm = getOption("transx.na.rm")) scale_unit_len(x, na.rm = getOption("transx.na.rm"))
scale_range(x, to, na.rm = getOption("transx.na.rm")) scale_minmax(x, na.rm = getOption("transx.na.rm")) scale_unit_len(x, na.rm = getOption("transx.na.rm"))
x |
Univariate vector, numeric or ts object with only one dimension. |
to |
Values that will determine the output range. |
na.rm |
A value indicating whether NA values should be stripped before the computation proceeds. |
To rescale a range between an arbitrary set of values [a, b], the formula becomes:
Returns a vector with the same class and attributes as the input vector.
x <- c(10,5,1,-2) scale_range(x, c(-1, 2)) scale_minmax(x)
x <- c(10,5,1,-2) scale_range(x, c(-1, 2)) scale_minmax(x)
These functions calculate the scores according to:
score_z
: Normal(z) distribution
score_mad
: Mean absolute deviation
score_t
: t-distribution
score_chi
: chi-distribution
score_z(x, na.rm = getOption("transx.na.rm")) score_mad(x, na.rm = getOption("transx.na.rm")) score_t(x, na.rm = getOption("transx.na.rm")) score_chisq(x, na.rm = getOption("transx.na.rm"))
score_z(x, na.rm = getOption("transx.na.rm")) score_mad(x, na.rm = getOption("transx.na.rm")) score_t(x, na.rm = getOption("transx.na.rm")) score_chisq(x, na.rm = getOption("transx.na.rm"))
x |
Univariate vector, numeric or ts object with only one dimension. |
na.rm |
A value indicating whether NA values should be stripped before the computation proceeds. |
Because function are known with different names:
score_z
is identical to std_mean
score_mad
is identical to std_median
Returns a vector with the same class and attributes as the input vector.
x <- seq(-3,3,0.5) score_z(x) score_mad(x) score_t(x)
x <- seq(-3,3,0.5) score_z(x) score_mad(x) score_t(x)
Approaches to selecting lambda.
select_lambda( freq = c("quarterly", "annual", "monthly", "weekly"), type = c("rot", "ru2002") )
select_lambda( freq = c("quarterly", "annual", "monthly", "weekly"), type = c("rot", "ru2002") )
freq |
The frequency of the dataset. |
type |
The methodology to select lambda. |
Rule of thumb is from Hodrick and Prescot (1997):
Lambda = 100*(number of periods in a year)^2
Annual data = 100 x 1^2 = 100
Quarterly data = 100 x 4^2 = 1,600
Monthly data = 100 x 12^2 = 14,400
Weekly data = 100 x 52^2 = 270,400
Daily data = 100 x 365^2 = 13,322,500
Ravn and Uhlig (2002) state that lambda should vary by the fourth power of the frequency observation ratio;
Lambda = 6.25 x (number of periods in a year)^4
Thus, the rescaled default values for lambda are:
Annual data = 1600 x 1^4 = 6.25
Quarterly data = 1600 x 4^4= 1600
Monthly data = 1600 x 12^4= 129,600
Weekly data = 1600 x 12^4 = 33,177,600
Hodrick, R. J., & Prescott, E. C. (1997). Postwar US business cycles: an empirical investigation. Journal of Money, credit, and Banking, 1-16.
Ravn, M. O., & Uhlig, H. (2002). On adjusting the Hodrick-Prescott filter for the frequency of observations. Review of economics and statistics, 84(2), 371-376.
Compute the sample skewness/kurtosis
skewness(x, na.rm = getOption("transx.na.rm")) kurtosis(x, na.rm = getOption("transx.na.rm"))
skewness(x, na.rm = getOption("transx.na.rm")) kurtosis(x, na.rm = getOption("transx.na.rm"))
x |
Univariate vector, numeric or ts object with only one dimension. |
na.rm |
A value indicating whether NA values should be stripped before the computation proceeds. |
smooth_kernel(x, ...)
smooth_kernel(x, ...)
x |
Univariate vector, numeric or ts object with only one dimension. |
... |
Further arguments passed to |
x <- co2 plotx(smooth_kernel(x))
x <- co2 plotx(smooth_kernel(x))
Locally-weighted polynomial regression.
smooth_loess(x, ...)
smooth_loess(x, ...)
x |
Univariate vector, numeric or ts object with only one dimension. |
... |
Further arguments passed to |
x <- co2 plotx(smooth_loess(x))
x <- co2 plotx(smooth_loess(x))
Computes a simple moving average smoother.
smooth_ma(x, order = NULL, centre = TRUE, fill = NA)
smooth_ma(x, order = NULL, centre = TRUE, fill = NA)
x |
Univariate vector, numeric or ts object with only one dimension. |
order |
Order of moving average smoother. |
centre |
Centers the moving average for even orders. |
fill |
Numeric value(s) or function used to fill observations. |
Returns a vector with the same class and attributes as the input vector.
x <- co2 x <- c(1:3, 5, 4, 7:3, 2*(2:5), rep(10, 4))#sin(1:100) plotx(x) lines(smooth_ma(x, 4), col = "red") lines(smooth_spline(x), col = "purple") lines(smooth_loess(x), col = "green")
x <- co2 x <- c(1:3, 5, 4, 7:3, 2*(2:5), rep(10, 4))#sin(1:100) plotx(x) lines(smooth_ma(x, 4), col = "red") lines(smooth_spline(x), col = "purple") lines(smooth_loess(x), col = "green")
smooth_spline(x, ...)
smooth_spline(x, ...)
x |
Univariate vector, numeric or ts object with only one dimension. |
... |
Further arguments passed to |
x <- co2 plotx(smooth_spline(x))
x <- co2 plotx(smooth_spline(x))
Convert number of standard deviations by which the value of a raw score is above or below the mean value of what is being observed or measured.
std_mean(x, na.rm = getOption("transx.na.rm")) std_median(x, na.rm = getOption("transx.na.rm"))
std_mean(x, na.rm = getOption("transx.na.rm")) std_median(x, na.rm = getOption("transx.na.rm"))
x |
Univariate vector, numeric or ts object with only one dimension. |
na.rm |
A value indicating whether NA values should be stripped before the computation proceeds. |
Returns a vector with the same class and attributes as the input vector.
x <- c(10,2,5,3) std_mean(x) scale(x) std_median(x)
x <- c(10,2,5,3) std_mean(x) scale(x) std_median(x)