ts_diff: Derivative of time series

ts_diffR Documentation

Derivative of time series

Description

Iteratively differenced series up to order. The same length as the original series is recovered by calculating the mean of two vectors for each iteration: One with a duplicated first value and one with a duplicated last value.

Usage

ts_diff(
  y,
  order = 1,
  addColumns = TRUE,
  keepDerivatives = FALSE,
  maskEdges = NULL,
  silent = TRUE
)

Arguments

y

A timeseries object or numeric vector or a matrix in which columns are variables and rows are numeric values observed over time.

order

How many times should the difference iteration be applied? (default = 1)

addColumns

Should the derivative(s) be added to the input vector/matrix as columns? (default = TRUE)

keepDerivatives

If TRUE and order > 1, all derivatives from 1:order will be returned as a matrix )default = FALSE)

maskEdges

Mask the values at the edges of the derivatives by any numeric type that is not NULL (default = NULL)

silent

Silent-ish mode

Value

Depending on the setting of addColumns and the object type passed as y, a vector of equal length as y iteratively differenced by order times; a matrix with derivatives, or a matrix with original(s) and derivative(s).

Note

The values at the edges of the derivatives represent endpoint averages and should be excluded from any subsequent analyses. Set argument maskEdges to a value of your choice.

See Also

Other Time series operations: ts_center(), ts_changeindex(), ts_checkfix(), ts_detrend(), ts_discrete(), ts_duration(), ts_embed(), ts_integrate(), ts_levels(), ts_peaks(), ts_permtest_block(), ts_permtest_transmat(), ts_rasterize(), ts_sd(), ts_slice(), ts_slopes(), ts_standardise(), ts_sumorder(), ts_symbolic(), ts_trimfill(), ts_windower()

Examples


# Here's an interesting numeric vector
y<-c(1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368)

# Return the first order derivative as a vector
ts_diff(y=y,addColumns=FALSE)

# Return original and derivative as a matrix
plot(stats::ts(ts_diff(y=y, addColumns=TRUE)))

# Works on multivariate data objects with mixed variable types
df <- data.frame(x=letters, y=1:26, z=sin(1:26))

# Returns only derivatives of the numeric colunmns
ts_diff(y=df,addColumns=FALSE)

# Returns original data with derivatives of the numeric columns
ts_diff(y=df, order=4, addColumns=TRUE)

# Plot logistic S-curve and derivatives 1 to 3
S <- stats::plogis(seq(-5,5,.1))
plot(stats::ts(ts_diff(S, order=3, keepDerivatives = TRUE)))
abline(v=which(seq(-5,5,.1)==0), col= "red3", lwd=3)

# Plot again, but with masked edges
(maskEdge <- ts_diff(S, order=3, keepDerivatives = TRUE, maskEdges = NA))
plot(stats::ts(maskEdge))
abline(v=which(seq(-5,5,.1)==0), col= "red3", lwd=3)


FredHasselman/casnet documentation built on April 20, 2024, 3:05 p.m.