Package 'transx'

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

Help Index


Rolling operations

Description

Apply rolling operations over a moving window for size n and increment step.

Usage

blck(x, fn, n = 1L, fill = NA, align = "left", ...)

blck_data(x, n = 1L)

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

fn

⁠[function]⁠

n

⁠[positive integer(1):1L]⁠

Window size.

fill

⁠[numeric or function: NA]⁠

Numeric value(s) or function used to fill observations.

align

⁠[character(1): "left"]⁠

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 fn.

Value

  • roll() returns a vector with the same class and attributes as the input vector.

  • roll_data() Returns a list of length length(x)/step.

Examples

x <- seq(10, 1, -1)

Functions used to calculate non-overlapping blocks

Description

Functions used to calculate non-overlapping blocks

Usage

blck_idx(x, n, align)

Arguments

x

data argument

n

size of the block

align

align


Non-Overapping Block Moment Calculations

Description

Non-Overapping Block Moment Calculations

Usage

blck_mean(x, n = 2L, fill = NA)

blck_median(x, n = 2L)

blck_modex(x, n = 2L)

blck_sd(x, n = 2L)

Arguments

x

numeric vector

n

block size

fill

⁠[numeric or function: NA]⁠

Numeric value(s) or function used to fill observations.


Removes measure of centrality from the series

Description

Maturing lifecycle

Removes the mean, the median or the mode from the series.

Usage

demean(x, na.rm = getOption("transx.na.rm"))

demedian(x, na.rm = getOption("transx.na.rm"))

demode(x, na.rm = getOption("transx.na.rm"))

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

na.rm

⁠[logical(1): getOption("transx.na.rm")]⁠

A value indicating whether NA values should be stripped before the computation proceeds.

Value

Returns a vector with the same class and attributes as the input vector.

Examples

x <- c(2,5,10,20,30)
summary(x)

demean(x)
demedian(x)
demode(x)

Compute lagged differnces

Description

Maturing lifecycle

Returns suitably lagged and iterated difference

  • diffx computes simple differences.

  • rdffix computes percentage differences.

  • ldiffx computes logged differences.

Usage

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)

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

n

⁠[positive integer(1): 1L]⁠

Value indicating which lag to use.

order

⁠[positive integer(1): 1L]⁠

Value indicating the order of the difference.

rho

⁠[numeric(1): NULL]⁠

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 or function: NA]⁠

Numeric value(s) or function used to fill observations.

Examples

x <- c(2, 4, 8, 20)
diffx(x)
rdiffx(x)
ldiffx(x)

Deterministic Trend

Description

Stable lifecycle

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.

Usage

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"))

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

bp

⁠[positive integer(1)]⁠

Break points to define piecewise segments of the data.

na.rm

⁠[logical(1): getOption("transx.na.rm")]⁠

A value indicating whether NA values should be stripped before the computation proceeds.

degree

⁠[positive integer(1)]⁠

Value indicating the degree of polynomial

Value

Returns a vector with the same class and attributes as the input vector.

Examples

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)))

Fill with locf and nocb

Description

Maturing lifecycle

Usage

fill_both(body, idx, first = c("locf", "nocb"))

Arguments

body

⁠[numeric vector]⁠

The body of the vector.

idx

⁠[integer vector]⁠

the index to replace with.

first

⁠[character: "locf"]⁠

Select which filling algorithms will occur first "locf" or "nocb".

Value

Returns a vector with the same class and attributes as the input vector.

Examples

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 with "linear approximation"

Description

Maturing lifecycle

Usage

fill_linear(body, idx, ...)

Arguments

body

⁠[numeric vector]⁠

The body of the vector.

idx

⁠[integer vector]⁠

the index to replace with.

...

Further arguments passed to ⁠\link[stats]{approx}⁠

Value

Returns a vector with the same class and attributes as the input vector.

Examples

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 with "Last Observation Carried Forward"

Description

Maturing lifecycle

Usage

fill_locf(body, idx, fail = NA)

Arguments

body

⁠[numeric vector]⁠

The body of the vector.

idx

⁠[integer vector]⁠

the index to replace with.

fail

⁠[numeric(1) or numeric vector: fill]⁠

In case it fails to fill some values.

Value

Returns a vector with the same class and attributes as the input vector.

Examples

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 with "Next observation carried backwards"

Description

Maturing lifecycle

Usage

fill_nocb(body, idx, fail = NA)

Arguments

body

⁠[numeric vector]⁠

The body of the vector.

idx

⁠[integer vector]⁠

the index to replace with.

fail

⁠[numeric(1) or numeric vector: fill]⁠

In case it fails to fill some values.

Value

Returns a vector with the same class and attributes as the input vector.

Examples

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 with "cubic spline interpolation"

Description

Maturing lifecycle

Usage

fill_spline(body, idx, ...)

Arguments

body

⁠[numeric vector]⁠

The body of the vector.

idx

⁠[integer vector]⁠

the index to replace with.

...

Further arguments passed to ⁠\link[stats]{spline}⁠

Value

Returns a vector with the same class and attributes as the input vector.

Examples

x <- c(5,3,NA,2,5)
fill_spline(x, 3)

Fill with values

Description

Experimental lifecycle

Usage

fill_vec(vec)

Arguments

vec

⁠[numeric]⁠

Numeric vector of the same length

Examples

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 functions

Description

Experimental lifecycle

Usage

fill_window(fn, ...)

Arguments

fn

⁠[function]⁠

Window function, usually of the roll, rec or blck families.

...

Further arguments passed to fn.

Examples

## Not run: 
lagx(c(1:5), fill = fill_window(rec_mean))

## End(Not run)

Baxter-King Filter

Description

Maturing lifecycle

This function computes the cyclical component of the Baxter-King filter.

Usage

filter_bk(x, fill = NA, ...)

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

fill

⁠[numeric or function: NA]⁠

Numeric value(s) or function used to fill observations.

...

Further arguments passed to bkfilter.

Examples

unemp <- ggplot2::economics$unemploy
unemp_cycle <- filter_bk(unemp)
plotx(cbind(unemp, unemp_cycle))

Boosted HP filter

Description

Experimental lifecycle

This function computes the cyclical component of the Boosted Hodrick-Prescot filter.

Usage

filter_boosted_hp(
  x,
  lambda = 1600,
  stopping = "nonstop",
  sig_p = 0.05,
  max_iter = 100
)

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

lambda

⁠[numeric(1): 1600]⁠

Smoothness penalty parameter.

stopping:

⁠[character: "nonstop"]⁠

  • If stopping = "adf" or "BIC", used stopping criteria.

  • If stopping = "nonstop", iterated until max_iter

sig_p:

⁠[numeric(1): 0.05]⁠

The significance level of the ADF test as the stopping criterion. It is used only when stopping == "adf".

max_iter:

⁠[numeric(1): 100]⁠

The maximum number of iterations.

Value

Returns a vector with the same class and attributes as the input vector.

Source

This function has been retrieved and rewritten from https://github.com/zhentaoshi/Boosted_HP_filter/blob/master/R/BoostedHP.R

References

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

Examples

unemp <- ggplot2::economics$unemploy
unemp_cycle <- filter_boosted_hp(unemp)
plotx(cbind(unemp, unemp_cycle))

Butterworth Filter

Description

Maturing lifecycle

This function computes the cyclical component of the Butterworth filter.

Usage

filter_bw(x, ...)

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

...

Further arguments passed to bwfilter.

Examples

unemp <- ggplot2::economics$unemploy
unemp_cycle <- filter_bw(unemp, freq = 10)
plotx(cbind(unemp, unemp_cycle))

Christiano-Fitzgerald Filter

Description

Maturing lifecycle

This function computes the cyclical component of the Christiano-Fitzgerald filter.

Usage

filter_cf(x, ...)

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

...

Further arguments passed to cffilter.

Examples

unemp <- ggplot2::economics$unemploy
unemp_cycle <- filter_cf(unemp)
plotx(cbind(unemp, unemp_cycle))

Hamilton Filter

Description

Maturing lifecycle

This function computes the cyclical component of the Hamilton filter.

Usage

filter_hamilton(x, p = 4, horizon = 8, fill = NA)

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

p

⁠[integer(1): 4]⁠

A value indicating the number of lags

horizon

⁠[integer(1): 8]⁠

A value indicating the number of periods to look ahead.

fill

⁠[numeric or function: NA]⁠

Numeric value(s) or function used to fill observations.

Value

Returns a vector with the same class and attributes as the input vector.

Examples

unemp <- ggplot2::economics$unemploy
unemp_cycle <- filter_hamilton(unemp)
plotx(cbind(unemp, unemp_cycle))

Hodrick-Prescot Filter

Description

Maturing lifecycle

This function computes the cyclical component of the Hodrick-Prescot filter.

Usage

filter_hp(x, ...)

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

...

Further arguments passed to hpfilter.

See Also

select_lambda

Examples

unemp <- ggplot2::economics$unemploy
unemp_cycle <- filter_hp(unemp, freq = select_lambda("monthly"))
plotx(cbind(unemp, unemp_cycle))

Trigonometric regression Filter

Description

Maturing lifecycle

This function computes the cyclical component of the trigonometric regression filter.

Usage

filter_tr(x, ...)

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

...

Further arguments passed to trfilter.

Examples

unemp <- ggplot2::economics$unemploy
unemp_cycle <- filter_tr(unemp, pl=8, pu=40)
plotx(cbind(unemp, unemp_cycle))

Geometric Mean value

Description

Compute the sample geometric mean.

Usage

gmean(x, na.rm = getOption("transx.na.rm"))

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

na.rm

⁠[logical(1): getOption("transx.na.rm")]⁠

A value indicating whether NA values should be stripped before the computation proceeds.

Value

Returns a vector with the same class and attributes as the input vector.


Compute lagged or leading values

Description

Stable lifecycle

Find the "previous" (lagx()) or "next" (leadx()) values in a vector. Useful for comparing values behind of or ahead of the current values.

Usage

lagx(x, n = 1L, fill = NA)

leadx(x, n = 1L, fill = NA)

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

n

⁠[positive integer(1): 1L]⁠

Value indicating the number of positions to lead or lag by.

fill

⁠[numeric or function: NA]⁠

Numeric value(s) or function used to fill observations.

Details

This functions has been taken and modified from the dplyr package, however, to reduce dependencies they are not imported.

Value

Returns a vector with the same class and attributes as the input vector.

Examples

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)

Mode value

Description

Compute the sample median.

Usage

modex(x, na.rm = getOption("transx.na.rm"))

modex_int(x, na.rm = getOption("transx.na.rm"))

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

na.rm

⁠[logical(1): getOption("transx.na.rm")]⁠

A value indicating whether NA values should be stripped before the computation proceeds.


Detect outliers with Tukey's method

Description

Maturing lifecycle

Usage

out_iqr(x, cutoff = 1.5, fill = NA, ...)

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

cutoff

⁠[numeric(1): 1.5]⁠

Cutoff point that determines the number of score quantities after which an observation is considered outlier.

fill

⁠[numeric or function: NA]⁠

Numeric value(s) or function used to fill observations.

...

further arguments passed to quantile.

Examples

out_iqr(c(0,1,3,4,20))

Detect outliers with Percentiles

Description

Maturing lifecycle

Usage

out_pt(x, pt_low = 0.1, pt_high = 0.9, fill = NA)

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

pt_low

the lowest quantile

pt_high

the highest quantile

fill

⁠[numeric or function: NA]⁠

Numeric value(s) or function used to fill observations.

Examples

x <- c(1, 3, -1, 5, 10, 100)
out_pt(x)

Detect outliers with zscore

Description

Maturing lifecycle

Usage

out_score_z(x, cutoff = 3, fill = NA, ...)

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

cutoff

⁠[numeric(1): 3]⁠

Cutoff point that determines the number of score quantities after which an observation is considered outlier.

fill

⁠[numeric or function: NA]⁠

Numeric value(s) or function used to fill observations.

...

Further arguments passed to score.

Examples

out_score_z(c(0,0.1,2,1,3,2.5,2,.5,6,4,100))

Detect outliers Iglewicz and Hoaglin (1993) robust z-score method

Description

Maturing lifecycle

Usage

out_score_zrob(x, cutoff = 3.5, fill = NA, ...)

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

cutoff

⁠[numeric(1): 3.5]⁠

Cutoff point that determines the number of score quantities after which an observation is considered outlier.

fill

⁠[numeric or function: NA]⁠

Numeric value(s) or function used to fill observations.

...

further arguments passed to score.

Examples

out_score_zrob(c(0,0.1,2,1,3,2.5,2,.5,6,4,100))

Detect outliers with upper and lower threshold

Description

Maturing lifecycle

Usage

out_threshold(x, tlow = NULL, thigh = NULL, fill = NA)

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

tlow

⁠[numeric(1): NULL]⁠

The lower threshold.

thigh

⁠[numeric(1): NULL]⁠

The upper threshold.

fill

⁠[numeric or function: NA]⁠

Numeric value(s) or function used to fill observations.

Value

Returns a vector with the same class and attributes as the input vector.

Examples

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))

Winsorize

Description

Maturing lifecycle

Replace extremely values that are defined by min and max.

Usage

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))

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

min

⁠[numeric(1): quantile(x, 0.05)]⁠

The lower bound, all values lower than this will be replaced by this value.

max

⁠[numeric(1): quantile(x, 0.95)]⁠

The upper bound, all values above than this will be replaced by this value.

Value

Returns a vector with the same class and attributes as the input vector.

See Also

Winsorize

Examples

x <- c(1, 3, -1, 5, 10, 100)
out_winsorise(x)

nth Power Transformation

Description

Stable lifecycle

Usage

pow(x, pow = NULL, modulus = FALSE)

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

pow

⁠[numeric(1): NA]⁠

The nth power.

modulus

positive

Value

Returns a vector with the same class and attributes as the input vector.

Examples

pow(2, 2)
pow(-2, 2)
pow(-2,2, TRUE)

Box-Cox Transformations

Description

Maturing lifecycle

Usage

pow_boxcox(x, lambda = NULL, lambda2 = NULL, ...)

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

lambda

⁠[numeric(1): NULL]⁠

Transformation exponent, λ\lambda.

lambda2

⁠[numeric(1): NULL]⁠

Transformation exponent, λ2\lambda_2.

...

Further arguments passed to pow.

Value

Returns a vector with the same class and attributes as the input vector.

References

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

Examples

set.seed(123)
x <- runif(10)
pow_boxcox(x, 3)

Manly(1971) Transformations

Description

Maturing lifecycle

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.

Usage

pow_manly(x, lambda = NULL)

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

lambda

⁠[numeric(1): NULL]⁠

Transformation exponent, λ\lambda.

Value

Returns a vector with the same class and attributes as the input vector.

Examples

set.seed(123)
x <- runif(10)
pow_manly(x, 3)

Tukey Transformations Transformations

Description

Maturing lifecycle

Usage

pow_tukey(x, lambda = NULL, ...)

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

lambda

⁠[numeric(1): NULL]⁠

Transformation exponent, λ\lambda.

...

Further arguments passed to pow.

Value

Returns a vector with the same class and attributes as the input vector.

Examples

set.seed(123)
x <- runif(10)
pow_tukey(x, 2)

Yeo and Johnson(2000) Transformations

Description

Maturing lifecycle

Usage

pow_yj(x, lambda = NULL, ...)

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

lambda

⁠[numeric(1): NULL]⁠

Transformation exponent, λ\lambda.

...

Further arguments passed to pow.

Value

Returns a vector with the same class and attributes as the input vector.

References

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

Examples

set.seed(123)
x <- runif(10)
pow_yj(x, 3)

Change the base year

Description

Maturing lifecycle

Change the base year.

Usage

rebase(x, n = NULL)

rebase_origin(x)

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

n

⁠[numeric(1): NULL]⁠

The index of the new base year.

Value

Returns a vector with the same class and attributes as the input vector.

Examples

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

Recursive

Description

Recursive

Usage

rec_max(x)

Arguments

x

numeric vector


Recursive

Description

Recursive

Usage

rec_mean(x)

Arguments

x

numeric vector


Recursive

Description

Recursive

Usage

rec_median(x)

Arguments

x

numeric vector


Recursive

Description

Recursive

Usage

rec_min(x)

Arguments

x

numeric vector


Recursive

Description

Recursive

Usage

rec_mode(x)

Arguments

x

numeric vector


Recursive

Description

Recursive

Usage

rec_prod(x)

Arguments

x

numeric vector


Recursive

Description

Recursive

Usage

rec_sd(x)

Arguments

x

numeric vector


Recursive

Description

Recursive

Usage

rec_sum(x)

Arguments

x

numeric vector


Recursive

Description

Recursive

Usage

rec_var(x)

Arguments

x

numeric vector


Rolling operations

Description

Apply rolling operations over a moving window for size n and increment step.

Usage

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"))

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

n

⁠[positive integer(1):1L]⁠

Window size.

step

⁠[positive integer(1):1L]⁠

Rolling window step.

align

⁠[character(1): "left"]⁠

Specifying whether the index of the result should be left- or right-aligned or centered (default) compared to the rolling window of observations.

fn

⁠[function]⁠

fill

⁠[numeric or function: NA]⁠

Numeric value(s) or function used to fill observations.

...

Further arguments passed to the function fn.

Value

  • 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.

Examples

# 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)

nth Root Transformation

Description

Stable lifecycle
  • root: nth root

  • root_sqrt: square root

  • root_cubic: cubic root

Usage

root(x, root = NULL, modulus = FALSE)

root_sq(x, ...)

root_cubic(x, ...)

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

root

⁠[numeric(1): NA]⁠

The nth root.

modulus

⁠[logical(1): FALSE]⁠

Transformation will work for data with both positive and negative root.

...

Further arguments passed to root.

Examples

root(4, 2)
root(-4, 2)

root(-4, 2, TRUE)

Rescale

Description

Maturing lifecycle

Usage

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"))

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

to

⁠[numeric(2): NULL]⁠

Values that will determine the output range.

na.rm

⁠[logical(1): getOption("transx.na.rm")]⁠

A value indicating whether NA values should be stripped before the computation proceeds.

Details

To rescale a range between an arbitrary set of values [a, b], the formula becomes:

Value

Returns a vector with the same class and attributes as the input vector.

Examples

x <- c(10,5,1,-2)
scale_range(x, c(-1, 2))
scale_minmax(x)

Score transformation

Description

Stable lifecycle

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

Usage

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"))

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

na.rm

⁠[logical(1): getOption("transx.na.rm")]⁠

A value indicating whether NA values should be stripped before the computation proceeds.

Details

Because function are known with different names:

  • score_z is identical to std_mean

  • score_mad is identical to std_median

Value

Returns a vector with the same class and attributes as the input vector.

See Also

scores

Examples

x <- seq(-3,3,0.5)
score_z(x)
score_mad(x)
score_t(x)

Selecting lambda

Description

Approaches to selecting lambda.

Usage

select_lambda(
  freq = c("quarterly", "annual", "monthly", "weekly"),
  type = c("rot", "ru2002")
)

Arguments

freq

⁠[character: "quarterly"]⁠

The frequency of the dataset.

type

⁠[character: "rot"]⁠

The methodology to select lambda.

Details

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

References

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.


Skewness/Kurtosis Value

Description

Compute the sample skewness/kurtosis

Usage

skewness(x, na.rm = getOption("transx.na.rm"))

kurtosis(x, na.rm = getOption("transx.na.rm"))

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

na.rm

⁠[logical(1): getOption("transx.na.rm")]⁠

A value indicating whether NA values should be stripped before the computation proceeds.


Kernel Regression Smoother

Description

Experimental lifecycle

Usage

smooth_kernel(x, ...)

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

...

Further arguments passed to smooth_kernel

Examples

x <- co2
plotx(smooth_kernel(x))

LOWESS smoother

Description

Experimental lifecycle

Locally-weighted polynomial regression.

Usage

smooth_loess(x, ...)

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

...

Further arguments passed to lowess

Examples

x <- co2
plotx(smooth_loess(x))

Moving-average smoothing

Description

Experimental lifecycle

Computes a simple moving average smoother.

Usage

smooth_ma(x, order = NULL, centre = TRUE, fill = NA)

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

order

⁠[integer(1): NULL]⁠

Order of moving average smoother.

centre

⁠[logical(1): TRUE]⁠

Centers the moving average for even orders.

fill

⁠[numeric or function: NA]⁠

Numeric value(s) or function used to fill observations.

Value

Returns a vector with the same class and attributes as the input vector.

Examples

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")

Fit a Smoothing Spline

Description

Experimental lifecycle

Usage

smooth_spline(x, ...)

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

...

Further arguments passed to smooth.spline

Examples

x <- co2
plotx(smooth_spline(x))

Standarization

Description

Maturing lifecycle

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.

Usage

std_mean(x, na.rm = getOption("transx.na.rm"))

std_median(x, na.rm = getOption("transx.na.rm"))

Arguments

x

⁠[univariate vector]⁠

Univariate vector, numeric or ts object with only one dimension.

na.rm

⁠[logical(1): getOption("transx.na.rm")]⁠

A value indicating whether NA values should be stripped before the computation proceeds.

Value

Returns a vector with the same class and attributes as the input vector.

Examples

x <- c(10,2,5,3)
std_mean(x)
scale(x)

std_median(x)