calc_scale | R Documentation |
RcppArmadillo
.Standardize (center and scale) the columns of a time series of data
in place, without copying the data in memory, using RcppArmadillo
.
calc_scale(tseries, center = TRUE, scale = TRUE, use_median = FALSE)
tseries |
A time series or matrix of data. |
center |
A Boolean argument: if |
scale |
A Boolean argument: if |
use_median |
A Boolean argument: if |
The function calc_scale()
standardizes (centers and scales) the
columns of a time series of data in place, without copying the data
in memory, using RcppArmadillo
.
If the arguments center
and scale
are both TRUE
and
use_median
is FALSE
(the defaults), then calc_scale()
performs the same calculation as the standard R
function
scale()
, and it calculates the centrality (central tendency) as the
mean and the dispersion as the standard deviation.
If the arguments center
and scale
are both TRUE
(the
defaults), then calc_scale()
standardizes the data.
If the argument center
is FALSE
then calc_scale()
only scales the data (divides it by the standard deviations).
If the argument scale
is FALSE
then calc_scale()
only demeans the data (subtracts the means).
If the argument use_median
is TRUE
, then it calculates the
centrality as the median and the dispersion as the median
absolute deviation (MAD).
If the number of rows of tseries
is less than 3
then it
does nothing and tseries
is not scaled.
The function calc_scale()
accepts a pointer to the argument
tseries
, and it overwrites the old data with the standardized data.
It performs the calculation in place, without copying the data in memory,
which can significantly increase the computation speed for large time
series.
The function calc_scale()
uses RcppArmadillo
C++
code, so on a typical time series it can be over 10 times faster
than the function scale()
.
Void (no return value - modifies the data in place).
## Not run:
# Calculate a time series of returns
retp <- zoo::coredata(na.omit(rutils::etfenv$returns[, c("IEF", "VTI")]))
# Demean the returns
demeaned <- apply(retp, 2, function(x) (x-mean(x)))
HighFreq::calc_scale(retp, scale=FALSE)
all.equal(demeaned, retp, check.attributes=FALSE)
# Calculate a time series of returns
retp <- zoo::coredata(na.omit(rutils::etfenv$returns[, c("IEF", "VTI")]))
# Standardize the returns
retss <- scale(retp)
HighFreq::calc_scale(retp)
all.equal(retss, retp, check.attributes=FALSE)
# Compare the speed of Rcpp with R code
library(microbenchmark)
summary(microbenchmark(
Rcode=scale(retp),
Rcpp=HighFreq::calc_scale(retp),
times=100))[, c(1, 4, 5)] # end microbenchmark summary
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.