Description Usage Arguments Details Value Examples
A function for computing the rolling and expanding covariances of time-series data.
1 2 3 |
x |
vector or matrix. Rows are observations and columns are variables. |
y |
vector or matrix. Rows are observations and columns are variables. |
width |
integer. Window size. |
weights |
vector. Weights for each observation within a window. |
center |
logical. If |
scale |
logical. If |
min_obs |
integer. Minimum number of observations required to have a value within a window,
otherwise result is |
complete_obs |
logical. If |
na_restore |
logical. Should missing values be restored? |
online |
logical. Process observations using an online algorithm. |
The denominator used gives an unbiased estimate of the covariance,
so if the weights are the default then the divisor n - 1
is obtained.
A cube with each slice the rolling and expanding covariances.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | n <- 15
x <- rnorm(n)
y <- rnorm(n)
weights <- 0.9 ^ (n:1)
# rolling covariances with complete windows
roll_cov(x, y, width = 5)
# rolling covariances with partial windows
roll_cov(x, y, width = 5, min_obs = 1)
# expanding covariances with partial windows
roll_cov(x, y, width = n, min_obs = 1)
# expanding covariances with partial windows and weights
roll_cov(x, y, width = n, min_obs = 1, weights = weights)
|
[1] NA NA NA NA -0.148443706
[6] -0.435311670 0.009127784 -0.304442237 -0.123305421 -0.294421320
[11] 0.023070498 0.526500549 0.507564633 0.364083836 -0.024936922
[1] NA -1.233339553 -0.458575201 -0.359099334 -0.148443706
[6] -0.435311670 0.009127784 -0.304442237 -0.123305421 -0.294421320
[11] 0.023070498 0.526500549 0.507564633 0.364083836 -0.024936922
[1] NA -1.23333955 -0.45857520 -0.35909933 -0.14844371 -0.36675005
[7] -0.37287208 -0.38691664 -0.24841381 -0.20407509 -0.17468675 -0.04929041
[13] -0.04328891 -0.04036279 -0.15632287
[1] NA -1.23333955 -0.42345204 -0.32900730 -0.08671242 -0.34920522
[7] -0.33746846 -0.41999786 -0.19851055 -0.13967601 -0.10496687 0.09145458
[13] 0.08379417 0.07170882 -0.15019006
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.