Skip to contents

Generic functions for computation of autocorrelations, autocovariances and related quantities. The idea is to free the user from the need to look for specific functions that compute the desired property for their object.

Usage

autocovariances(x, maxlag, ...)

autocorrelations(x, maxlag, lag_0, ...)

partialAutocorrelations(x, maxlag, lag_0 = TRUE, ...)

partialAutocovariances(x, maxlag, ...)

partialVariances(x, ...)

Arguments

x

an object for which the requested property makes sense.

maxlag

the maximal lag to include in the result.

lag_0

if TRUE include lag zero.

...

further arguments for methods.

Details

autocorrelations is a generic function for computation of autocorrelations. It deduces the appropriate type of autocorrelation from the class of the object. For example, for models it computes theoretical autocorrelations, while for time series it computes sample autocorrelations.

The other functions described are similar for other second order properties of x.

These functions return objects from suitable classes, all inheriting from "Lagged". The latter means that indexing starts from zero, so the value for lag zero is accessed by r[0]). Subscripting always returns the underlying data unclassed (i.e. ordinary vectors or arrays). In particular, "[]" extracts the underlying data.

Functions computing autocorrelations and partial autocorrelations have argument lag_0 --- if it is set to FALSE, the value for lag zero is dropped from the result and the returned object is an ordinary vector or array, as appropriate.

See the individual methods for the format of the result and further details.

There are plot methods for sample autocorrelations and sample partial autocorrelations with overlaid significance limits under null hypotheses for independence or weak white noise, see plot-methods and the examples there. More details can be found in the vignettes, see section ‘See also’ below.

Value

an object from a class suitable for the requested property and x

Author

Georgi N. Boshnakov

See also

plot-methods for plotting with significance limits computed under strong white noise and weak white noise hypotheses;

autocorrelations-methods, partialAutocorrelations-methods for details on individual methods;

vignette("white_noise_tests", package = "sarima") and
vignette("garch_tests_example", package = "sarima") for extensive worked examples.

armaccf_xe, armaacf

Examples

set.seed(1234)
v1 <- rnorm(100)
autocorrelations(v1)
#> An object of class "SampleAutocorrelations"
#>       Lag_0       Lag_1       Lag_2       Lag_3       Lag_4       Lag_5 
#>  1.00000000  0.12957682  0.04402061  0.06106303  0.05826444  0.11109644 
#>       Lag_6       Lag_7       Lag_8       Lag_9      Lag_10      Lag_11 
#>  0.13135298  0.26913701 -0.08548724  0.12722915 -0.01484862 -0.01438639 
#>      Lag_12      Lag_13      Lag_14      Lag_15      Lag_16      Lag_17 
#>  0.09282460  0.11136127  0.04431534 -0.14147363 -0.02991080 -0.02545693 
#>      Lag_18      Lag_19      Lag_20 
#>  0.11824060 -0.01218368 -0.10847365 
#> Slot n:
#> [1] 100
#> Slot varnames:   <not set>
#> Slot objectname:  x
v1.acf <- autocorrelations(v1, maxlag = 10)

v1.acf[1:10] # drop lag zero value (and the class)
#>  [1]  0.12957682  0.04402061  0.06106303  0.05826444  0.11109644  0.13135298
#>  [7]  0.26913701 -0.08548724  0.12722915 -0.01484862
autocorrelations(v1, maxlag = 10, lag_0 = FALSE) # same
#>  [1]  0.12957682  0.04402061  0.06106303  0.05826444  0.11109644  0.13135298
#>  [7]  0.26913701 -0.08548724  0.12722915 -0.01484862

partialAutocorrelations(v1)
#> An object of class "SamplePartialAutocorrelations"
#>       Lag_0       Lag_1       Lag_2       Lag_3       Lag_4       Lag_5 
#>  1.00000000  0.12957682  0.02769547  0.05285558  0.04377385  0.09714201 
#>       Lag_6       Lag_7       Lag_8       Lag_9      Lag_10      Lag_11 
#>  0.10383731  0.24233653 -0.16783708  0.14648145 -0.10189429 -0.02638830 
#>      Lag_12      Lag_13      Lag_14      Lag_15      Lag_16      Lag_17 
#>  0.04045013  0.07193251 -0.04576705 -0.10297684 -0.09458730  0.04627240 
#>      Lag_18      Lag_19      Lag_20 
#>  0.09892282 -0.08337618 -0.11082180 
#> Slot n:
#> [1] 100
#> Slot varnames:   <not set>
#> Slot objectname:  x
partialAutocorrelations(v1, maxlag = 10)
#> An object of class "SamplePartialAutocorrelations"
#>       Lag_0       Lag_1       Lag_2       Lag_3       Lag_4       Lag_5 
#>  1.00000000  0.12957682  0.02769547  0.05285558  0.04377385  0.09714201 
#>       Lag_6       Lag_7       Lag_8       Lag_9      Lag_10 
#>  0.10383731  0.24233653 -0.16783708  0.14648145 -0.10189429 
#> Slot n:
#> [1] 100
#> Slot varnames:   <not set>
#> Slot objectname:  x

## compute 2nd order properties from raw data
autocovariances(v1)
#> An object of class "SampleAutocovariances"
#>       Lag_0       Lag_1       Lag_2       Lag_3       Lag_4       Lag_5 
#>  0.99874171  0.12941377  0.04396522  0.06098619  0.05819112  0.11095664 
#>       Lag_6       Lag_7       Lag_8       Lag_9      Lag_10      Lag_11 
#>  0.13118770  0.26879836 -0.08537967  0.12706906 -0.01482994 -0.01436829 
#>      Lag_12      Lag_13      Lag_14      Lag_15      Lag_16      Lag_17 
#>  0.09270780  0.11122115  0.04425957 -0.14129562 -0.02987317 -0.02542490 
#>      Lag_18      Lag_19      Lag_20 
#>  0.11809182 -0.01216835 -0.10833715 
#> Slot n:
#> [1] 100
#> Slot varnames:   <not set>
#> Slot objectname:  x
autocovariances(v1, maxlag = 10)
#> An object of class "SampleAutocovariances"
#>       Lag_0       Lag_1       Lag_2       Lag_3       Lag_4       Lag_5 
#>  0.99874171  0.12941377  0.04396522  0.06098619  0.05819112  0.11095664 
#>       Lag_6       Lag_7       Lag_8       Lag_9      Lag_10 
#>  0.13118770  0.26879836 -0.08537967  0.12706906 -0.01482994 
#> Slot n:
#> [1] 100
#> Slot varnames:   <not set>
#> Slot objectname:  x
partialAutocovariances(v1, maxlag = 6)
#> An object of class "SamplePartialAutocovariances"
#>          0          1          2          3          4          5          6 
#> 0.99874171 0.12724089 0.02717533 0.05171803 0.04274969 0.09397397 0.09936784 
#> Slot n:
#> [1] 100
#> Slot varnames:   <not set>
#> Slot objectname:  x
partialAutocovariances(v1)
#> An object of class "SamplePartialAutocovariances"
#>           0           1           2           3           4           5 
#>  0.99874171  0.12724089  0.02717533  0.05171803  0.04274969  0.09397397 
#>           6           7           8           9          10          11 
#>  0.09936784  0.21828652 -0.14692191  0.12547617 -0.08637655 -0.02235398 
#>          12          13          14          15          16          17 
#>  0.03420994  0.06052078 -0.03842568 -0.08554178 -0.07786971  0.03801254 
#>          18          19          20 
#>  0.08046935 -0.06735137 -0.08842251 
#> Slot n:
#> [1] 100
#> Slot varnames:   <not set>
#> Slot objectname:  x
partialVariances(v1, maxlag = 6)
#> An object of class "SamplePartialVariances"
#>         0         1         2         3         4         5         6 
#> 0.9987417 0.9819727 0.9812195 0.9784782 0.9766033 0.9673875 0.9569570 
#> Slot n:
#> [1] 100
#> Slot varnames:   <not set>
#> Slot objectname:  x
pv1 <- partialVariances(v1)

## compute 2nd order properties from raw data
autocovariances(AirPassengers, maxlag = 6)
#> An object of class "SampleAutocovariances"
#>     Lag_0     Lag_1     Lag_2     Lag_3     Lag_4     Lag_5     Lag_6 
#> 14291.973 13549.467 12513.692 11529.066 10756.502 10201.181  9743.318 
#> Slot n:
#> [1] 144
#> Slot varnames:   <not set>
#> Slot objectname:  x
autocorrelations(AirPassengers, maxlag = 6)
#> An object of class "SampleAutocorrelations"
#>     Lag_0     Lag_1     Lag_2     Lag_3     Lag_4     Lag_5     Lag_6 
#> 1.0000000 0.9480473 0.8755748 0.8066812 0.7526254 0.7137700 0.6817336 
#> Slot n:
#> [1] 144
#> Slot varnames:   <not set>
#> Slot objectname:  x
partialAutocorrelations(AirPassengers, maxlag = 6)
#> An object of class "SamplePartialAutocorrelations"
#>        Lag_0        Lag_1        Lag_2        Lag_3        Lag_4        Lag_5 
#>  1.000000000  0.948047341 -0.229421874  0.038147781  0.093785438  0.073606698 
#>        Lag_6 
#>  0.007727603 
#> Slot n:
#> [1] 144
#> Slot varnames:   <not set>
#> Slot objectname:  x
partialAutocovariances(AirPassengers, maxlag = 6)
#> An object of class "SamplePartialAutocovariances"
#>           0           1           2           3           4           5 
#> 14291.97333  1371.29064  -314.37784    52.19801   127.19886    99.29006 
#>           6 
#>    10.42335 
#> Slot n:
#> [1] 144
#> Slot varnames:   <not set>
#> Slot objectname:  x
partialVariances(AirPassengers, maxlag = 6)
#> An object of class "SamplePartialVariances"
#>         0         1         2         3         4         5         6 
#> 14291.973  1446.437  1370.305  1368.310  1356.275  1348.927  1348.846 
#> Slot n:
#> [1] 144
#> Slot varnames:   <not set>
#> Slot objectname:  x

acv <- autocovariances(AirPassengers, maxlag = 6)
autocovariances(acv) # no-op
#> An object of class "SampleAutocovariances"
#>     Lag_0     Lag_1     Lag_2     Lag_3     Lag_4     Lag_5     Lag_6 
#> 14291.973 13549.467 12513.692 11529.066 10756.502 10201.181  9743.318 
#> Slot n:
#> [1] 144
#> Slot varnames:   <not set>
#> Slot objectname:  x
autocovariances(acv, maxlag = 4) # trim the available lags
#> An object of class "SampleAutocovariances"
#>    Lag_0    Lag_1    Lag_2    Lag_3    Lag_4 
#> 14291.97 13549.47 12513.69 11529.07 10756.50 
#> Slot n:
#> [1] 144
#> Slot varnames:   <not set>
#> Slot objectname:  x

## compute 2nd order properties from sample autocovariances
acr <- autocorrelations(acv)
acr
#> An object of class "SampleAutocorrelations"
#>     Lag_0     Lag_1     Lag_2     Lag_3     Lag_4     Lag_5     Lag_6 
#> 1.0000000 0.9480473 0.8755748 0.8066812 0.7526254 0.7137700 0.6817336 
#> Slot n:
#> [1] 144
#> Slot varnames:   <not set>
#> Slot objectname:  x
partialAutocorrelations(acv)
#> An object of class "SamplePartialAutocorrelations"
#>        Lag_0        Lag_1        Lag_2        Lag_3        Lag_4        Lag_5 
#>  1.000000000  0.948047341 -0.229421874  0.038147781  0.093785438  0.073606698 
#>        Lag_6 
#>  0.007727603 
#> Slot n:
#> [1] 144
#> Slot varnames:   <not set>
#> Slot objectname:  x
partialAutocovariances(acv)
#> An object of class "SamplePartialAutocovariances"
#>           0           1           2           3           4           5 
#> 14291.97333  1371.29064  -314.37784    52.19801   127.19886    99.29006 
#>           6 
#>    10.42335 
#> Slot n:
#> [1] 144
#> Slot varnames:   <not set>
#> Slot objectname:  x
partialVariances(acv)
#> An object of class "SamplePartialVariances"
#>         0         1         2         3         4         5         6 
#> 14291.973  1446.437  1370.305  1368.310  1356.275  1348.927  1348.846 
#> Slot n:
#> [1] 144
#> Slot varnames:   <not set>
#> Slot objectname:  x

## compute 2nd order properties from sample autocorrelations
acr
#> An object of class "SampleAutocorrelations"
#>     Lag_0     Lag_1     Lag_2     Lag_3     Lag_4     Lag_5     Lag_6 
#> 1.0000000 0.9480473 0.8755748 0.8066812 0.7526254 0.7137700 0.6817336 
#> Slot n:
#> [1] 144
#> Slot varnames:   <not set>
#> Slot objectname:  x
partialAutocorrelations(acr)
#> An object of class "SamplePartialAutocorrelations"
#>        Lag_0        Lag_1        Lag_2        Lag_3        Lag_4        Lag_5 
#>  1.000000000  0.948047341 -0.229421874  0.038147781  0.093785438  0.073606698 
#>        Lag_6 
#>  0.007727603 
#> Slot n:
#> [1] 144
#> Slot varnames:   <not set>
#> Slot objectname:  x

## These cannot be computed, since the variance is needed but unknown:
##     autocovariances(acr)
##     partialAutocovariances(acr)
##     partialVariances(acr)

## to treat autocorrelations as autocovariances, 
## convert them to autocovariances explicitly:
as(acr, "Autocovariances")
#> An object of class "Autocovariances"
#>     Lag_0     Lag_1     Lag_2     Lag_3     Lag_4     Lag_5     Lag_6 
#> 1.0000000 0.9480473 0.8755748 0.8066812 0.7526254 0.7137700 0.6817336 
as(acr, "SampleAutocovariances")
#> An object of class "SampleAutocovariances"
#>     Lag_0     Lag_1     Lag_2     Lag_3     Lag_4     Lag_5     Lag_6 
#> 1.0000000 0.9480473 0.8755748 0.8066812 0.7526254 0.7137700 0.6817336 
#> Slot n:
#> [1] 144
#> Slot varnames:   <not set>
#> Slot objectname:  x

partialVariances(as(acr, "Autocovariances"))
#> An object of class "PartialVariances"
#>          0          1          2          3          4          5          6 
#> 1.00000000 0.10120624 0.09587931 0.09573978 0.09489768 0.09438353 0.09437790 
partialVariances(as(acr, "SampleAutocovariances"))
#> An object of class "SamplePartialVariances"
#>          0          1          2          3          4          5          6 
#> 1.00000000 0.10120624 0.09587931 0.09573978 0.09489768 0.09438353 0.09437790 
#> Slot n:
#> [1] 144
#> Slot varnames:   <not set>
#> Slot objectname:  x