Create objects inheriting from Lagged.

Lagged(data, ...)

Arguments

data

suitable data, see Details.

...

further arguments passed on to new().

Details

Lagged creates an object inheriting from "Lagged". The exact class depends on argument data. This is the easiest way to create lagged objects.

If data is a vector, matrix or 3D array, the result is "Lagged1d", "Lagged2d" and "Lagged3d", respectively. If data inherits from "Lagged", the result is "FlexibleLagged".

Value

a suitable "Lagged" object, as described in Details

Note

I am considering making Lagged generic.

See also

the specific classes Lagged1d, Lagged2d, Lagged3d

Examples

## adapted from examples for acf() ## acf of univariate ts acv1 <- acf(ldeaths, plot = FALSE) class(acv1) # "acf"
#> [1] "acf"
a1 <- drop(acv1$acf) class(a1) # numeric
#> [1] "numeric"
la1 <- Lagged(a1) # 1d lagged object from 'numeric': Lagged(acv1) # 1d lagged object from 'acf':
#> An object of class "Lagged1d" #> Slot *data*: #> Lag_0 Lag_1 Lag_2 Lag_3 Lag_4 Lag_5 #> 1.000000000 0.755051141 0.396956836 0.019395714 -0.355897989 -0.608566374 #> Lag_6 Lag_7 Lag_8 Lag_9 Lag_10 Lag_11 #> -0.681383469 -0.607909875 -0.378212377 -0.012975866 0.383252644 0.650206704 #> Lag_12 Lag_13 Lag_14 Lag_15 Lag_16 Lag_17 #> 0.723167071 0.638001465 0.371577811 0.009467461 -0.293699737 -0.496742216 #> Lag_18 #> -0.585558984
## acf of multivariate ts acv2 <- acf(ts.union(mdeaths, fdeaths), plot = FALSE) class(acv2) # "acf"
#> [1] "acf"
a2 <- acv2$acf class(a2) # 3d array
#> [1] "array"
dim(a2)
#> [1] 16 2 2
la2a <- acf2Lagged(acv2) # explicitly convert 'acf' to lagged object Lagged(acv2) # equivalently, just use Lagged()
#> An object of class "Lagged3d" #> Slot *data*: #> , , Lag_0 #> #> #> 1.0000000 0.9762413 #> 0.9762413 1.0000000 #> #> , , Lag_1 #> #> #> 0.7570591 0.7356685 #> 0.7443093 0.7295201 #> #> , , Lag_2 #> #> #> 0.4020525 0.3642418 #> 0.4052006 0.3808750 #> #> , , Lag_3 #> #> #> 0.03783542 -0.01067572 #> 0.01975943 -0.01833805 #> #> , , Lag_4 #> #> #> -0.3346538 -0.3829206 #> -0.3494684 -0.3889098 #> #> , , Lag_5 #> #> #> -0.5889291 -0.6223870 #> -0.6040312 -0.6305605 #> #> , , Lag_6 #> #> #> -0.6639578 -0.6885385 #> -0.6778035 -0.6959545 #> #> , , Lag_7 #> #> #> -0.5915389 -0.610584 #> -0.6117935 -0.617580 #> #> , , Lag_8 #> #> #> -0.3641284 -0.3833383 #> -0.3816714 -0.3960179 #> #> , , Lag_9 #> #> #> -0.002048197 -0.01811207 #> -0.024534195 -0.03473011 #> #> , , Lag_10 #> #> #> 0.3959546 0.3919831 #> 0.3400054 0.3487681 #> #> , , Lag_11 #> #> #> 0.6502747 0.6565921 #> 0.6218958 0.6281912 #> #> , , Lag_12 #> #> #> 0.7171089 0.7213972 #> 0.7082063 0.7158608 #> #> , , Lag_13 #> #> #> 0.6375408 0.6391044 #> 0.6154271 0.6193603 #> #> , , Lag_14 #> #> #> 0.3732288 0.3613526 #> 0.3656260 0.3584216 #> #> , , Lag_15 #> #> #> 0.01393135 -3.385423e-03 #> 0.01505498 -3.603698e-05 #>
identical(la2a, Lagged(acv2)) # TRUE
#> [1] TRUE
la2a[0] # R_0, indexing lagged object
#> , , 1 #> #> [,1] [,2] #> [1,] 1.0000000 0.9762413 #> [2,] 0.9762413 1.0000000 #>
a2[1, , ] # same, indexing array data from 'cf' object
#> [,1] [,2] #> [1,] 1.0000000 0.9762413 #> [2,] 0.9762413 1.0000000
acv2[0] # same, indexing 'acf' object
#> #> Autocorrelations of series ‘ts.union(mdeaths, fdeaths)’, by lag #> #> , , mdeaths #> #> mdeaths fdeaths #> 1.000 (0) 0.976 (0) #> #> , , fdeaths #> #> mdeaths fdeaths #> 0.976 (0) 1.000 (0) #>
la2a[1] # R_1
#> , , 1 #> #> [,1] [,2] #> [1,] 0.7570591 0.7356685 #> [2,] 0.7443093 0.7295201 #>
a2[2, , ] # same
#> [,1] [,2] #> [1,] 0.7570591 0.7356685 #> [2,] 0.7443093 0.7295201
acv2[1/12] # transposed, see end of this example section as to why use 1/12
#> #> Autocorrelations of series ‘ts.union(mdeaths, fdeaths)’, by lag #> #> , , mdeaths #> #> mdeaths fdeaths #> 0.757 ( 0.0833) 0.744 (-0.0833) #> #> , , fdeaths #> #> mdeaths fdeaths #> 0.736 ( 0.0833) 0.730 ( 0.0833) #>
la2a[2] # R_1
#> , , 1 #> #> [,1] [,2] #> [1,] 0.4020525 0.3642418 #> [2,] 0.4052006 0.3808750 #>
a2[3, , ] # same
#> [,1] [,2] #> [1,] 0.4020525 0.3642418 #> [2,] 0.4052006 0.3808750
acv2[2/12] # transposed, see end of this example section as to why use 1/12
#> #> Autocorrelations of series ‘ts.union(mdeaths, fdeaths)’, by lag #> #> , , mdeaths #> #> mdeaths fdeaths #> 0.402 ( 0.167) 0.405 (-0.167) #> #> , , fdeaths #> #> mdeaths fdeaths #> 0.364 ( 0.167) 0.381 ( 0.167) #>
## multiple lags la2a[0:1] # native indexing with lagged object
#> , , 1 #> #> [,1] [,2] #> [1,] 1.0000000 0.9762413 #> [2,] 0.9762413 1.0000000 #> #> , , 2 #> #> [,1] [,2] #> [1,] 0.7570591 0.7356685 #> [2,] 0.7443093 0.7295201 #>
a2[1:2, , ] # different ordering
#> , , 1 #> #> [,1] [,2] #> [1,] 1.0000000 0.9762413 #> [2,] 0.7570591 0.7443093 #> #> , , 2 #> #> [,1] [,2] #> [1,] 0.9762413 1.0000000 #> [2,] 0.7356685 0.7295201 #>
acv2$acf[1:2, ,] # also different ordering
#> , , 1 #> #> [,1] [,2] #> [1,] 1.0000000 0.9762413 #> [2,] 0.7570591 0.7443093 #> #> , , 2 #> #> [,1] [,2] #> [1,] 0.9762413 1.0000000 #> [2,] 0.7356685 0.7295201 #>
## '[' doesn't drop a dimension even when it is of length 1: la2a[0]
#> , , 1 #> #> [,1] [,2] #> [1,] 1.0000000 0.9762413 #> [2,] 0.9762413 1.0000000 #>
la2a[1]
#> , , 1 #> #> [,1] [,2] #> [1,] 0.7570591 0.7356685 #> [2,] 0.7443093 0.7295201 #>
## to get a singleton element, use '[[': la2a[[0]]
#> [,1] [,2] #> [1,] 1.0000000 0.9762413 #> [2,] 0.9762413 1.0000000
la2a[[1]]
#> [,1] [,2] #> [1,] 0.7570591 0.7356685 #> [2,] 0.7443093 0.7295201
## arithmetic and math operators -la1
#> An object of class "Lagged1d" #> Slot *data*: #> Lag_0 Lag_1 Lag_2 Lag_3 Lag_4 Lag_5 #> -1.000000000 -0.755051141 -0.396956836 -0.019395714 0.355897989 0.608566374 #> Lag_6 Lag_7 Lag_8 Lag_9 Lag_10 Lag_11 #> 0.681383469 0.607909875 0.378212377 0.012975866 -0.383252644 -0.650206704 #> Lag_12 Lag_13 Lag_14 Lag_15 Lag_16 Lag_17 #> -0.723167071 -0.638001465 -0.371577811 -0.009467461 0.293699737 0.496742216 #> Lag_18 #> 0.585558984
+la1
#> An object of class "Lagged1d" #> Slot *data*: #> Lag_0 Lag_1 Lag_2 Lag_3 Lag_4 Lag_5 #> 1.000000000 0.755051141 0.396956836 0.019395714 -0.355897989 -0.608566374 #> Lag_6 Lag_7 Lag_8 Lag_9 Lag_10 Lag_11 #> -0.681383469 -0.607909875 -0.378212377 -0.012975866 0.383252644 0.650206704 #> Lag_12 Lag_13 Lag_14 Lag_15 Lag_16 Lag_17 #> 0.723167071 0.638001465 0.371577811 0.009467461 -0.293699737 -0.496742216 #> Lag_18 #> -0.585558984
2*la1
#> An object of class "Lagged1d" #> Slot *data*: #> Lag_0 Lag_1 Lag_2 Lag_3 Lag_4 Lag_5 #> 2.00000000 1.51010228 0.79391367 0.03879143 -0.71179598 -1.21713275 #> Lag_6 Lag_7 Lag_8 Lag_9 Lag_10 Lag_11 #> -1.36276694 -1.21581975 -0.75642475 -0.02595173 0.76650529 1.30041341 #> Lag_12 Lag_13 Lag_14 Lag_15 Lag_16 Lag_17 #> 1.44633414 1.27600293 0.74315562 0.01893492 -0.58739947 -0.99348443 #> Lag_18 #> -1.17111797
la1^2
#> An object of class "Lagged1d" #> Slot *data*: #> Lag_0 Lag_1 Lag_2 Lag_3 Lag_4 Lag_5 #> 1.000000e+00 5.701022e-01 1.575747e-01 3.761937e-04 1.266634e-01 3.703530e-01 #> Lag_6 Lag_7 Lag_8 Lag_9 Lag_10 Lag_11 #> 4.642834e-01 3.695544e-01 1.430446e-01 1.683731e-04 1.468826e-01 4.227688e-01 #> Lag_12 Lag_13 Lag_14 Lag_15 Lag_16 Lag_17 #> 5.229706e-01 4.070459e-01 1.380701e-01 8.963282e-05 8.625954e-02 2.467528e-01 #> Lag_18 #> 3.428793e-01
la1 + la1^2
#> An object of class "Lagged1d" #> Slot *data*: #> Lag_0 Lag_1 Lag_2 Lag_3 Lag_4 Lag_5 #> 2.000000000 1.325153365 0.554531566 0.019771908 -0.229234610 -0.238213342 #> Lag_6 Lag_7 Lag_8 Lag_9 Lag_10 Lag_11 #> -0.217100037 -0.238355459 -0.235167775 -0.012807492 0.530135234 1.072975461 #> Lag_12 Lag_13 Lag_14 Lag_15 Lag_16 Lag_17 #> 1.246137683 1.045047335 0.509647880 0.009557094 -0.207440201 -0.249989387 #> Lag_18 #> -0.242679660
abs(la1)
#> An object of class "Lagged1d" #> Slot *data*: #> Lag_0 Lag_1 Lag_2 Lag_3 Lag_4 Lag_5 #> 1.000000000 0.755051141 0.396956836 0.019395714 0.355897989 0.608566374 #> Lag_6 Lag_7 Lag_8 Lag_9 Lag_10 Lag_11 #> 0.681383469 0.607909875 0.378212377 0.012975866 0.383252644 0.650206704 #> Lag_12 Lag_13 Lag_14 Lag_15 Lag_16 Lag_17 #> 0.723167071 0.638001465 0.371577811 0.009467461 0.293699737 0.496742216 #> Lag_18 #> 0.585558984
sinpi(la1)
#> An object of class "Lagged1d" #> Slot *data*: #> Lag_0 Lag_1 Lag_2 Lag_3 Lag_4 #> 5.665539e-16 6.957974e-01 9.480588e-01 6.089573e-02 -8.992651e-01 #> Lag_5 Lag_6 Lag_7 Lag_8 Lag_9 #> -9.423969e-01 -8.419911e-01 -9.430847e-01 -9.276945e-01 -4.075359e-02 #> Lag_10 Lag_11 Lag_12 Lag_13 Lag_14 #> 9.334896e-01 8.907115e-01 7.641331e-01 9.074825e-01 9.197119e-01 #> Lag_15 Lag_16 Lag_17 Lag_18 #> 2.973852e-02 -7.972253e-01 -9.999476e-01 -9.640925e-01
sqrt(abs(la1))
#> An object of class "Lagged1d" #> Slot *data*: #> Lag_0 Lag_1 Lag_2 Lag_3 Lag_4 Lag_5 Lag_6 #> 1.00000000 0.86893679 0.63004511 0.13926850 0.59657186 0.78010664 0.82545955 #> Lag_7 Lag_8 Lag_9 Lag_10 Lag_11 Lag_12 Lag_13 #> 0.77968575 0.61498974 0.11391166 0.61907402 0.80635396 0.85039230 0.79874994 #> Lag_14 Lag_15 Lag_16 Lag_17 Lag_18 #> 0.60957183 0.09730088 0.54194071 0.70479942 0.76521826
## Summary group max(la1)
#> [1] 1
min(la1)
#> [1] -0.6813835
range(la1)
#> [1] -0.6813835 1.0000000
prod(la1)
#> [1] -8.84397e-11
sum(la1)
#> [1] 0.92613
any(la1 < 0)
#> [1] TRUE
all(la1 >= 0)
#> [1] FALSE
## Math2 group round(la1)
#> An object of class "Lagged1d" #> Slot *data*: #> Lag_0 Lag_1 Lag_2 Lag_3 Lag_4 Lag_5 Lag_6 Lag_7 Lag_8 Lag_9 Lag_10 #> 1 1 0 0 0 -1 -1 -1 0 0 0 #> Lag_11 Lag_12 Lag_13 Lag_14 Lag_15 Lag_16 Lag_17 Lag_18 #> 1 1 1 0 0 0 0 -1
round(la1, 2)
#> An object of class "Lagged1d" #> Slot *data*: #> Lag_0 Lag_1 Lag_2 Lag_3 Lag_4 Lag_5 Lag_6 Lag_7 Lag_8 Lag_9 Lag_10 #> 1.00 0.76 0.40 0.02 -0.36 -0.61 -0.68 -0.61 -0.38 -0.01 0.38 #> Lag_11 Lag_12 Lag_13 Lag_14 Lag_15 Lag_16 Lag_17 Lag_18 #> 0.65 0.72 0.64 0.37 0.01 -0.29 -0.50 -0.59
signif(la1)
#> An object of class "Lagged1d" #> Slot *data*: #> Lag_0 Lag_1 Lag_2 Lag_3 Lag_4 Lag_5 #> 1.00000000 0.75505100 0.39695700 0.01939570 -0.35589800 -0.60856600 #> Lag_6 Lag_7 Lag_8 Lag_9 Lag_10 Lag_11 #> -0.68138300 -0.60791000 -0.37821200 -0.01297590 0.38325300 0.65020700 #> Lag_12 Lag_13 Lag_14 Lag_15 Lag_16 Lag_17 #> 0.72316700 0.63800100 0.37157800 0.00946746 -0.29370000 -0.49674200 #> Lag_18 #> -0.58555900
signif(la1, 4)
#> An object of class "Lagged1d" #> Slot *data*: #> Lag_0 Lag_1 Lag_2 Lag_3 Lag_4 Lag_5 Lag_6 Lag_7 #> 1.000000 0.755100 0.397000 0.019400 -0.355900 -0.608600 -0.681400 -0.607900 #> Lag_8 Lag_9 Lag_10 Lag_11 Lag_12 Lag_13 Lag_14 Lag_15 #> -0.378200 -0.012980 0.383300 0.650200 0.723200 0.638000 0.371600 0.009467 #> Lag_16 Lag_17 Lag_18 #> -0.293700 -0.496700 -0.585600
## The remaining examples below are only relevant ## for users comparing to indexing of 'acf' ## ## indexing in base R acf() is somewhat misterious, so an example with ## DIY computation of lag_1 autocovariance matrix n <- length(mdeaths) tmpcov <- sum((mdeaths - mean(mdeaths)) * (fdeaths - mean(fdeaths)) ) / n msd <- sqrt(sum((mdeaths - mean(mdeaths))^2)/n) fsd <- sqrt(sum((fdeaths - mean(fdeaths))^2)/n) tmpcov1 <- sum((mdeaths - mean(mdeaths))[2:n] * (fdeaths - mean(fdeaths))[1:(n-1)] ) / n tmpcov1 / (msd * fsd)
#> [1] 0.7356685
la2a[[1]][1,2] - tmpcov1 / (msd * fsd) # only numerically different
#> [1] 2.220446e-16
## the raw acf in the 'acf' object is not surprising: la2a[[1]][1,2] == acv2$acf[2, 1, 2] # TRUE
#> [1] TRUE
## ... but this probably is: acv2[1]
#> #> Autocorrelations of series ‘ts.union(mdeaths, fdeaths)’, by lag #> #> , , mdeaths #> #> mdeaths fdeaths #> 0.717 ( 1) 0.708 (-1) #> #> , , fdeaths #> #> mdeaths fdeaths #> 0.721 ( 1) 0.716 ( 1) #>
## the ts here is monthly but has unit of lag 'year' ## so acv2[1] asks for lag 1 year = 12 months, thus acv2[1/12]
#> #> Autocorrelations of series ‘ts.union(mdeaths, fdeaths)’, by lag #> #> , , mdeaths #> #> mdeaths fdeaths #> 0.757 ( 0.0833) 0.744 (-0.0833) #> #> , , fdeaths #> #> mdeaths fdeaths #> 0.736 ( 0.0833) 0.730 ( 0.0833) #>
all( acv2$acf[13, , ] == drop(acv2[1]$acf) ) # TRUE
#> [1] TRUE
all( acv2$acf[2, , ] == drop(acv2[1/12]$acf) ) # TRUE
#> [1] TRUE
all( la2a[[1]] == drop(acv2[1/12]$acf) ) # TRUE
#> [1] TRUE