calc_mean | R Documentation |
RcppArmadillo
.Calculate the mean (location) of the columns of a time series or a
matrix using RcppArmadillo
.
calc_mean(tseries, method = "moment", confl = 0.75)
tseries |
A time series or a matrix of data. |
method |
A character string specifying the type of the
mean (location) model (the default is |
confl |
The confidence level for calculating the quantiles of
returns (the default is |
The function calc_mean()
calculates the mean (location) values of
the columns of the time series tseries
using C++
RcppArmadillo
code.
If method = "moment"
(the default) then calc_mean()
calculates the location as the mean - the first moment of the data.
If method = "quantile"
then it calculates the location
\bar{r}
as the average of the quantiles as follows:
\bar{r} = \frac{q_{\alpha} + q_{1-\alpha}}{2}
Where \alpha
is the confidence level for calculating the quantiles
(argument confl
).
If method = "nonparametric"
then it calculates the location as the
median.
The code examples below compare the function calc_mean()
with the
mean (location) calculated using R
code.
A single-row matrix with the mean (location) of the columns of
tseries
.
## Not run:
# Calculate historical returns
retp <- na.omit(rutils::etfenv$returns[, c("XLP", "VTI")])
# Calculate the column means in RcppArmadillo
HighFreq::calc_mean(retp)
# Calculate the column means in R
sapply(retp, mean)
# Compare the values
all.equal(drop(HighFreq::calc_mean(retp)),
sapply(retp, mean), check.attributes=FALSE)
# Compare the speed of RcppArmadillo with R code
library(microbenchmark)
summary(microbenchmark(
Rcpp=HighFreq::calc_mean(retp),
Rcode=sapply(retp, mean),
times=10))[, c(1, 4, 5)] # end microbenchmark summary
# Calculate the quantile mean (location)
HighFreq::calc_mean(retp, method="quantile", confl=0.9)
# Calculate the quantile mean (location) in R
colSums(sapply(retp, quantile, c(0.9, 0.1), type=5))
# Compare the values
all.equal(drop(HighFreq::calc_mean(retp, method="quantile", confl=0.9)),
colSums(sapply(retp, quantile, c(0.9, 0.1), type=5)),
check.attributes=FALSE)
# Compare the speed of RcppArmadillo with R code
summary(microbenchmark(
Rcpp=HighFreq::calc_mean(retp, method="quantile", confl=0.9),
Rcode=colSums(sapply(retp, quantile, c(0.9, 0.1), type=5)),
times=10))[, c(1, 4, 5)] # end microbenchmark summary
# Calculate the column medians in RcppArmadillo
HighFreq::calc_mean(retp, method="nonparametric")
# Calculate the column medians in R
sapply(retp, median)
# Compare the values
all.equal(drop(HighFreq::calc_mean(retp, method="nonparametric")),
sapply(retp, median), check.attributes=FALSE)
# Compare the speed of RcppArmadillo with R code
summary(microbenchmark(
Rcpp=HighFreq::calc_mean(retp, method="nonparametric"),
Rcode=sapply(retp, median),
times=10))[, 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.