R-listers, I am using xts with a yearmon index, but am getting some inconsistent results with the date index when i drop observations (for example by using na.omit). The issue is illustrated in the example below. If I start with a monthly zooreg series starting in 2009, yearmon converts this to "Dec-2008". Not such a worry for my example, but strange. Having converted to xts, i drop the first observation. The index shows jan 2009. But if i create a new variable with this index, it shifts the series back to dec 2008. No doubt i am doing something wrong. very grateful for any tips library(xts) z <- zooreg(1:24,frequency=12,start=c(2009,1)) # monthly data starting 2009 x <- xts(z,as.yearmon(index(z))) # starts Dec 2008 xx <- x[-1, ] # drop first obs (eg through na.omit) index(xx) # starts jan 2009 xxx <- xts(NA[1:length(xx)],index(xx)) # back to dec 2008 periodicity(x) periodicity(xx) periodicty(xxx) [[alternative HTML version deleted]]
On Sun, Apr 18, 2010 at 8:25 AM, simeon duckworth <simeonduckworth at gmail.com> wrote:> R-listers, > > I am using xts with a yearmon index, but am getting some inconsistent > results with the date index when i drop observations (for example by using > na.omit). > > The issue is illustrated in the example below. ?If I start with a monthly > zooreg series starting in 2009, yearmon converts this to "Dec-2008". ?Not > such a worry for my example, but strange. ?Having converted to xts, i drop > the first observation. ?The index shows jan 2009. ?But if i create a new > variable with this index, it shifts the series back to dec 2008. > > No doubt i am doing something wrong. ?very grateful for any tips > > library(xts) > > z <- zooreg(1:24,frequency=12,start=c(2009,1)) ?# monthly data starting 2009 > x <- xts(z,as.yearmon(index(z))) ? ? ? ? ? ? ? ? ? ? ? ?# starts Dec 2008Not for me. It starts in January 2009 for me. Also please show your code in such a way that it can be pasted into a session. Either comment out the output using # or else preface input lines with > so its clear what is input and what is output. And show what versions of the software and R you are using and what platform.> z <- zooreg(1:24,frequency=12,start=c(2009,1)) # monthly data starting 2009 > head(z)2009(1) 2009(2) 2009(3) 2009(4) 2009(5) 2009(6) 1 2 3 4 5 6> x <- xts(z,as.yearmon(index(z))) > head(x)x Jan 2009 1 Feb 2009 2 Mar 2009 3 Apr 2009 4 May 2009 5 Jun 2009 6> R.version.string[1] "R version 2.10.1 (2009-12-14)"> win.version()[1] "Windows Vista (build 6002) Service Pack 2"> packageDescription("zoo")$Version[1] "1.6-3"> packageDescription("xts")$Version[1] "0.7-0" I also tried older versions zoo 1.6-0, xts 0.6-8 and R 2.9.2 and got the same result as I got here.
Hi Gabor Thats odd. I still get the same problem with the same versions of the software in your mail ... viz "as.yearmon" converts 2009(1) to "Dec-2008" and although xts is indexed at "Jan 2009" in xx, using it to create another xts object with that index reverts to "Dec-2008". grateful for any suggestions ## code ## library(xts) z <- zooreg(1:24,frequency=12,start=c(2009,1)) x <- xts(z,as.yearmon(index(z))) xx <- x[-1, ] index(xx) xxx <- xts(NA[1:length(xx)],index(xx)) periodicity(x) periodicity(xx) periodicty(xxx)b ## results ###> periodicity(x)Monthly periodicity from Dec 2008 to Nov 2010> periodicity(xx)Monthly periodicity from Jan 2009 to Nov 2010> periodicity(xxx)Monthly periodicity from Dec 2008 to Oct 2010> > R.version.string[1] "R version 2.10.1 (2009-12-14)"> win.version()[1] "Windows XP (build 2600) Service Pack 3"> packageDescription("xts")$Version[1] "0.7-0"> Sys.time()[1] "2010-04-18 19:37:26 BST" On Sun, Apr 18, 2010 at 1:25 PM, simeon duckworth <simeonduckworth@gmail.com> wrote:> R-listers, > > I am using xts with a yearmon index, but am getting some inconsistent > results with the date index when i drop observations (for example by using > na.omit). > > The issue is illustrated in the example below. If I start with a monthly > zooreg series starting in 2009, yearmon converts this to "Dec-2008". Not > such a worry for my example, but strange. Having converted to xts, i drop > the first observation. The index shows jan 2009. But if i create a new > variable with this index, it shifts the series back to dec 2008. > > No doubt i am doing something wrong. very grateful for any tips > > library(xts) > > z <- zooreg(1:24,frequency=12,start=c(2009,1)) # monthly data starting > 2009 > x <- xts(z,as.yearmon(index(z))) # starts Dec 2008 > xx <- x[-1, ] # drop > first obs (eg through na.omit) > index(xx) # starts > jan 2009 > xxx <- xts(NA[1:length(xx)],index(xx)) # back to dec 2008 > > periodicity(x) > periodicity(xx) > periodicty(xxx) >[[alternative HTML version deleted]]
On Sun, Apr 18, 2010 at 2:51 PM, simeon duckworth <simeonduckworth at gmail.com> wrote:> Hi Gabor > > Thats odd. I still get the same problem with the same versions of the > software in your mail ... viz "as.yearmon" converts 2009(1) to "Dec-2008"We can`t conclude that its in as.yearmon based on the output shown. What is the output of: index(z) as.yearmon(index(z)) x This is what I get:> index(z)[1] 2009.000 2009.083 2009.167 2009.250 2009.333 2009.417 2009.500 2009.583 [9] 2009.667 2009.750 2009.833 2009.917 2010.000 2010.083 2010.167 2010.250 [17] 2010.333 2010.417 2010.500 2010.583 2010.667 2010.750 2010.833 2010.917> as.yearmon(index(z))[1] "Jan 2009" "Feb 2009" "Mar 2009" "Apr 2009" "May 2009" "Jun 2009" [7] "Jul 2009" "Aug 2009" "Sep 2009" "Oct 2009" "Nov 2009" "Dec 2009" [13] "Jan 2010" "Feb 2010" "Mar 2010" "Apr 2010" "May 2010" "Jun 2010" [19] "Jul 2010" "Aug 2010" "Sep 2010" "Oct 2010" "Nov 2010" "Dec 2010"> head(x)x Jan 2009 1 Feb 2009 2 Mar 2009 3 Apr 2009 4 May 2009 5 Jun 2009 6> and although xts is indexed at "Jan 2009" in xx, using it to create another > xts object with that index reverts to "Dec-2008". > > grateful for any suggestions > > ## code ## > library(xts) > z <- zooreg(1:24,frequency=12,start=c(2009,1)) > x <- xts(z,as.yearmon(index(z))) > xx <- x[-1, ] > index(xx) > xxx <- xts(NA[1:length(xx)],index(xx)) > periodicity(x) > periodicity(xx) > periodicty(xxx)b > > ## results ### >> periodicity(x) > Monthly periodicity from Dec 2008 to Nov 2010 >> periodicity(xx) > Monthly periodicity from Jan 2009 to Nov 2010 >> periodicity(xxx) > Monthly periodicity from Dec 2008 to Oct 2010 >> >> R.version.string > [1] "R version 2.10.1 (2009-12-14)" >> win.version() > [1] "Windows XP (build 2600) Service Pack 3" >> packageDescription("xts")$Version > [1] "0.7-0" >> Sys.time() > [1] "2010-04-18 19:37:26 BST" > > > > On Sun, Apr 18, 2010 at 1:25 PM, simeon duckworth <simeonduckworth at gmail.com >> wrote: > >> R-listers, >> >> I am using xts with a yearmon index, but am getting some inconsistent >> results with the date index when i drop observations (for example by using >> na.omit). >> >> The issue is illustrated in the example below. ?If I start with a monthly >> zooreg series starting in 2009, yearmon converts this to "Dec-2008". ?Not >> such a worry for my example, but strange. ?Having converted to xts, i drop >> the first observation. ?The index shows jan 2009. ?But if i create a new >> variable with this index, it shifts the series back to dec 2008. >> >> No doubt i am doing something wrong. ?very grateful for any tips >> >> library(xts) >> >> z <- zooreg(1:24,frequency=12,start=c(2009,1)) ?# monthly data starting >> 2009 >> x <- xts(z,as.yearmon(index(z))) ? ? ? ? ? ? ? ? ? ? ? ?# starts Dec 2008 >> xx <- x[-1, ?] ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# drop >> first obs (eg through na.omit) >> index(xx) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # starts >> jan 2009 >> xxx <- xts(NA[1:length(xx)],index(xx)) ? ? ? ? ? ? ? ?# back to dec 2008 >> >> periodicity(x) >> periodicity(xx) >> periodicty(xxx) >> > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >
... forgot to post this back to the r-list. it seems that the problem is with xts rather than zoo and yearmon per se ie using yearmon to index xts gives inconsistent results. grateful for any help anyone can offer. thanks On Sun, Apr 18, 2010 at 8:15 PM, simeon duckworth <simeonduckworth@gmail.com> wrote:> Hi gabor > > It seems asthough the issue is in working with yearmon in xts. the command > as.yearmon(index(z)) works in the same way as yours, but not when used to > index the xts object. > > > > > ## code > library(xts) > z <- zooreg(1:24,frequency=12,start=c(2009,1)) > x <- xts(z,as.yearmon(index(z))) > xx <- x[-1, ] > index(xx) > xxx <- xts(NA[1:length(xx)],index(xx)) > > index(z) > as.yearmon(index(z)) > head(x,3) > head(xx,3) > head(xxx,3) > > > ## output > > > index(z) > [1] 2009.000 2009.083 2009.167 2009.250 2009.333 2009.417 2009.500 > 2009.583 > [9] 2009.667 2009.750 2009.833 2009.917 2010.000 2010.083 2010.167 > 2010.250 > [17] 2010.333 2010.417 2010.500 2010.583 2010.667 2010.750 2010.833 > 2010.917 > > as.yearmon(index(z)) > [1] "Jan 2009" "Feb 2009" "Mar 2009" "Apr 2009" "May 2009" "Jun 2009" > [7] "Jul 2009" "Aug 2009" "Sep 2009" "Oct 2009" "Nov 2009" "Dec 2009" > [13] "Jan 2010" "Feb 2010" "Mar 2010" "Apr 2010" "May 2010" "Jun 2010" > [19] "Jul 2010" "Aug 2010" "Sep 2010" "Oct 2010" "Nov 2010" "Dec 2010" > > head(x,3) > x > Dec 2008 1 > Jan 2009 2 > Feb 2009 3 > > head(xx,3) > x > Jan 2009 2 > Feb 2009 3 > Mar 2009 4 > > head(xxx,3) > [,1] > Dec 2008 NA > Jan 2009 NA > Feb 2009 NA > > > > > > On Sun, Apr 18, 2010 at 8:00 PM, Gabor Grothendieck < > ggrothendieck@gmail.com> wrote: > >> On Sun, Apr 18, 2010 at 2:51 PM, simeon duckworth >> <simeonduckworth@gmail.com> wrote: >> > Hi Gabor >> > >> > Thats odd. I still get the same problem with the same versions of the >> > software in your mail ... viz "as.yearmon" converts 2009(1) to >> "Dec-2008" >> >> We can`t conclude that its in as.yearmon based on the output shown. >> What is the output of: >> >> index(z) >> as.yearmon(index(z)) >> x >> >> This is what I get: >> >> > index(z) >> [1] 2009.000 2009.083 2009.167 2009.250 2009.333 2009.417 2009.500 >> 2009.583 >> [9] 2009.667 2009.750 2009.833 2009.917 2010.000 2010.083 2010.167 >> 2010.250 >> [17] 2010.333 2010.417 2010.500 2010.583 2010.667 2010.750 2010.833 >> 2010.917 >> > as.yearmon(index(z)) >> [1] "Jan 2009" "Feb 2009" "Mar 2009" "Apr 2009" "May 2009" "Jun 2009" >> [7] "Jul 2009" "Aug 2009" "Sep 2009" "Oct 2009" "Nov 2009" "Dec 2009" >> [13] "Jan 2010" "Feb 2010" "Mar 2010" "Apr 2010" "May 2010" "Jun 2010" >> [19] "Jul 2010" "Aug 2010" "Sep 2010" "Oct 2010" "Nov 2010" "Dec 2010" >> > head(x) >> x >> Jan 2009 1 >> Feb 2009 2 >> Mar 2009 3 >> Apr 2009 4 >> May 2009 5 >> Jun 2009 6 >> >> >> >> > and although xts is indexed at "Jan 2009" in xx, using it to create >> another >> > xts object with that index reverts to "Dec-2008". >> > >> > grateful for any suggestions >> > >> > ## code ## >> > library(xts) >> > z <- zooreg(1:24,frequency=12,start=c(2009,1)) >> > x <- xts(z,as.yearmon(index(z))) >> > xx <- x[-1, ] >> > index(xx) >> > xxx <- xts(NA[1:length(xx)],index(xx)) >> > periodicity(x) >> > periodicity(xx) >> > periodicty(xxx)b >> > >> > ## results ### >> >> periodicity(x) >> > Monthly periodicity from Dec 2008 to Nov 2010 >> >> periodicity(xx) >> > Monthly periodicity from Jan 2009 to Nov 2010 >> >> periodicity(xxx) >> > Monthly periodicity from Dec 2008 to Oct 2010 >> >> >> >> R.version.string >> > [1] "R version 2.10.1 (2009-12-14)" >> >> win.version() >> > [1] "Windows XP (build 2600) Service Pack 3" >> >> packageDescription("xts")$Version >> > [1] "0.7-0" >> >> Sys.time() >> > [1] "2010-04-18 19:37:26 BST" >> > >> > >> > >> > On Sun, Apr 18, 2010 at 1:25 PM, simeon duckworth < >> simeonduckworth@gmail.com >> >> wrote: >> > >> >> R-listers, >> >> >> >> I am using xts with a yearmon index, but am getting some inconsistent >> >> results with the date index when i drop observations (for example by >> using >> >> na.omit). >> >> >> >> The issue is illustrated in the example below. If I start with a >> monthly >> >> zooreg series starting in 2009, yearmon converts this to "Dec-2008". >> Not >> >> such a worry for my example, but strange. Having converted to xts, i >> drop >> >> the first observation. The index shows jan 2009. But if i create a >> new >> >> variable with this index, it shifts the series back to dec 2008. >> >> >> >> No doubt i am doing something wrong. very grateful for any tips >> >> >> >> library(xts) >> >> >> >> z <- zooreg(1:24,frequency=12,start=c(2009,1)) # monthly data starting >> >> 2009 >> >> x <- xts(z,as.yearmon(index(z))) # starts Dec >> 2008 >> >> xx <- x[-1, ] # >> drop >> >> first obs (eg through na.omit) >> >> index(xx) # >> starts >> >> jan 2009 >> >> xxx <- xts(NA[1:length(xx)],index(xx)) # back to dec >> 2008 >> >> >> >> periodicity(x) >> >> periodicity(xx) >> >> periodicty(xxx) >> >> >> > >> > [[alternative HTML version deleted]] >> > >> > ______________________________________________ >> > 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]]