[R] Lags and Differences of zoo Objects

Gabor Grothendieck ggrothendieck at gmail.com
Sat Jul 31 07:54:59 CEST 2010


On Sat, Jul 31, 2010 at 1:44 AM, pdb <philb at philbrierley.com> wrote:
>
> Thanks for the response.
>
> I can figure out the 'lag' parameter to the function, but I dont understand
> the 'differences' parameter.
>
> differences - an integer indicating the order of the difference
>
> What does the 'order of the difference' mean in English?
>
> How are these numbers calculated?
>
>> x <- iris
>> x$Species = NULL
>> x$Petal.Width = NULL
>> x$Sepal.Width = NULL
>> x$Sepal.Length = NULL
>>
>> x <- zoo(x)
>>
>> x <-
> + merge(orig = x
> + ,l1d1 = diff(x, lag = 1, differences = 1, arithmetic = TRUE, na.pad =
> TRUE)
> + ,l1d2 = diff(x, lag = 1, differences = 2, arithmetic = TRUE, na.pad =
> TRUE)
> + ,l2d1 = diff(x, lag = 2, differences = 1, arithmetic = TRUE, na.pad =
> TRUE)
> + ,l2d2 = diff(x, lag = 2, differences = 2, arithmetic = TRUE, na.pad =
> TRUE)
> + )
>>
>> x
>    Petal.Length.orig Petal.Length.l1d1 Petal.Length.l1d2 Petal.Length.l2d1
> Petal.Length.l2d2
> 1                 1.4                NA                NA                NA
> NA
> 2                 1.4               0.0                NA                NA
> NA
> 3                 1.3              -0.1     -1.000000e-01              -0.1
> NA
> 4                 1.5               0.2      3.000000e-01               0.1
> NA
> 5                 1.4              -0.1     -3.000000e-01               0.1
> 2.000000e-01
> 6                 1.7               0.3      4.000000e-01               0.2
> 1.000000e-01
> 7                 1.4              -0.3     -6.000000e-01               0.0
> -1.000000e-01
> 8                 1.5               0.1      4.000000e-01              -0.2
> -4.000000e-01
> 9                 1.4              -0.1     -2.000000e-01               0.0
> 0.000000e+00
> 10                1.5               0.1      2.000000e-01               0.0
> 2.000000e-01
> 11                1.5               0.0     -1.000000e-01               0.1
> 1.000000e-01
> 12                1.6               0.1      1.000000e-01               0.1
> 1.000000e-01
> --
> View this message in context: http://r.789695.n4.nabble.com/Lags-and-Differences-of-zoo-Objects-tp2308666p2308681.html
> Sent from the R help mailing list archive at Nabble.com.
>

A kth order difference is analogous to a kth derivative and will
annihilate and (k-1)-degree polynomial. It is just repeatedly applies
diff.

z2 <- zoo((1:5)^2)

diff(z2) # linear
diff(z2, diff = 2) # constant
diff(z2, diff = 3) # all zeros

# compare
diff(z2, diff = 2)
diff(diff(z2))



More information about the R-help mailing list