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. Thank you, Manussawee
Manussawee, What type of object is series? We could help you better if we could reproduce exactly what you are trying to do, which requires more information (you made a good start by including data and code, though). The output of diff is a vector (time series, ...) with length one less than the input. embed(..., 2) also returns an object with length one less than the input. This is why you noticed that series.d had a different length than series (shorter by exactly 2, I bet). You should be able to figure out what you want to do from there. Since I don't know how you want the dates to line up, I can't really help you anymore from here. Jonathan On Wed, Jun 30, 2010 at 2:32 PM, Manussawee Sukunta < msukunta@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. > > Thank you, > Manussawee > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
On 2010-06-30 14:32, Manussawee Sukunta 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.It looks like series is a dataframe with your dates as rownames. If you want series.d (a matrix) to have the same rownames: rownames(series.d) <- rownames(series) But I'm not sure how much sense that makes, especially for columns 3 and 4. -Peter Ehlers> > Thank you, > Manussawee >
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) > zPX_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