[R] Embed function strips out date index

Gabor Grothendieck ggrothendieck at gmail.com
Wed Jun 30 23:48:19 CEST 2010


On Wed, Jun 30, 2010 at 4:32 PM, Manussawee Sukunta
<msukunta at illinoisalumni.org> wrote:
> Hi,
>
> I'm having especially hard time today and couldn't find any
> clue/answer through the internet.  I hope you can help.
>
> I'm in a process of writing a script to estimate error correction
> model, and I was following an example in Bernhard Pfaff's Analysis of
> Integrated and Cointegrated Time Series with R.  I have the following
> price data:
>
>> head(series,15)
>            PX_SETTLE PX_SETTLE.1
> 2009-01-02    4515.0      925.50
> 2009-01-05    4540.5      927.50
> 2009-01-06    4603.5      930.50
> 2009-01-07    4470.5      905.25
> 2009-01-08    4474.5      906.75
> 2009-01-09    4430.5      885.50
> 2009-01-12    4402.0      868.00
> 2009-01-13    4343.5      868.50
> 2009-01-14    4130.5      839.75
> 2009-01-15    4070.5      839.25
> 2009-01-16    4129.5      848.50
> 2009-01-20    4032.0      806.00
> 2009-01-21    4018.0      836.75
> 2009-01-22    4011.0      825.50
> 2009-01-23    3998.0      823.50
>
> Then I defined
> series.d = embed(diff(series),dim=2)
>
> which resulted in
>> head(series.d,15)
>         [,1]   [,2]   [,3]   [,4]
>  [1,]   25.5   2.00     NA     NA
>  [2,]   63.0   3.00   25.5   2.00
>  [3,] -133.0 -25.25   63.0   3.00
>  [4,]    4.0   1.50 -133.0 -25.25
>  [5,]  -44.0 -21.25    4.0   1.50
>  [6,]  -28.5 -17.50  -44.0 -21.25
>  [7,]  -58.5   0.50  -28.5 -17.50
>  [8,] -213.0 -28.75  -58.5   0.50
>  [9,]  -60.0  -0.50 -213.0 -28.75
> [10,]   59.0   9.25  -60.0  -0.50
> [11,]  -97.5 -42.50   59.0   9.25
> [12,]  -14.0  30.75  -97.5 -42.50
> [13,]   -7.0 -11.25  -14.0  30.75
> [14,]  -13.0  -2.00   -7.0 -11.25
> [15,]  169.0   7.25  -13.0  -2.00
>
> The new data series.d now has no date index.  I'm not sure how to get
> it back.  I tried to xts --> order.by = index(series), but the vector
> lengths are now not the same.  I feel like the answer might be
> obvious, but I just can't see it.  Again, I tried searching various
> forums and sites, but I couldn't find my answer.  I feel like I'm just
> going around a circle.  I hope someone can help me and shed some light
> on this problem.
>

Cannot tell exactly what you are doing without reproducible data and
code but you should be able to do it with zoo or xts.  Try this as an
example:

> Lines <- "Date PX_SETTLE PX_SETTLE.1
+ 2009-01-02    4515.0      925.50
+ 2009-01-05    4540.5      927.50
+ 2009-01-06    4603.5      930.50
+ 2009-01-07    4470.5      905.25
+ 2009-01-08    4474.5      906.75
+ 2009-01-09    4430.5      885.50
+ 2009-01-12    4402.0      868.00
+ 2009-01-13    4343.5      868.50
+ 2009-01-14    4130.5      839.75
+ 2009-01-15    4070.5      839.25
+ 2009-01-16    4129.5      848.50
+ 2009-01-20    4032.0      806.00
+ 2009-01-21    4018.0      836.75
+ 2009-01-22    4011.0      825.50
+ 2009-01-23    3998.0      823.50"
>
> library(zoo)
>
> # z <- read.zoo("myfile.txt", header = TRUE)
> z <- read.zoo(textConnection(Lines), header = TRUE)
> z
           PX_SETTLE PX_SETTLE.1
2009-01-02    4515.0      925.50
2009-01-05    4540.5      927.50
2009-01-06    4603.5      930.50
2009-01-07    4470.5      905.25
2009-01-08    4474.5      906.75
2009-01-09    4430.5      885.50
2009-01-12    4402.0      868.00
2009-01-13    4343.5      868.50
2009-01-14    4130.5      839.75
2009-01-15    4070.5      839.25
2009-01-16    4129.5      848.50
2009-01-20    4032.0      806.00
2009-01-21    4018.0      836.75
2009-01-22    4011.0      825.50
2009-01-23    3998.0      823.50
> diff(z)
           PX_SETTLE PX_SETTLE.1
2009-01-05      25.5        2.00
2009-01-06      63.0        3.00
2009-01-07    -133.0      -25.25
2009-01-08       4.0        1.50
2009-01-09     -44.0      -21.25
2009-01-12     -28.5      -17.50
2009-01-13     -58.5        0.50
2009-01-14    -213.0      -28.75
2009-01-15     -60.0       -0.50
2009-01-16      59.0        9.25
2009-01-20     -97.5      -42.50
2009-01-21     -14.0       30.75
2009-01-22      -7.0      -11.25
2009-01-23     -13.0       -2.00



More information about the R-help mailing list