[R] weird to me interaction between time() and %%, %/%
David Scott
d.scott at auckland.ac.nz
Wed Sep 1 04:25:16 CEST 2010
Steve is undoubtedly right about this being a floating point problem.
One comment I would make is that I think floor is a bit nicer for this,
both conceptually and for readability. Also you don't need a really
small value to add, half the sampling interval will work:
> junk <- ts(1:125, start=c(1999,2), frequency=12)
> floor(time(junk))
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1999 1999 1999 1999 1999 1999 1999 1999 1999 1999 1999 1999
2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000
2001 2001 2001 2001 2001 2001 2001 2001 2001 2001 2001 2001 2001
2002 2002 2002 2002 2002 2002 2002 2002 2002 2002 2002 2002 2002
2003 2003 2003 2003 2003 2003 2003 2003 2003 2003 2003 2003 2003
2004 2004 2004 2004 2004 2004 2004 2004 2004 2004 2004 2004 2004
2005 2004 2005 2005 2005 2005 2005 2005 2005 2005 2005 2005 2005
2006 2005 2006 2006 2006 2006 2006 2006 2006 2006 2006 2006 2006
2007 2006 2007 2007 2007 2007 2007 2007 2007 2007 2007 2007 2007
2008 2007 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008
2009 2008 2009 2009 2009 2009 2009
> floor(time(junk) + 1/24)
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1999 1999 1999 1999 1999 1999 1999 1999 1999 1999 1999 1999
2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000
2001 2001 2001 2001 2001 2001 2001 2001 2001 2001 2001 2001 2001
2002 2002 2002 2002 2002 2002 2002 2002 2002 2002 2002 2002 2002
2003 2003 2003 2003 2003 2003 2003 2003 2003 2003 2003 2003 2003
2004 2004 2004 2004 2004 2004 2004 2004 2004 2004 2004 2004 2004
2005 2005 2005 2005 2005 2005 2005 2005 2005 2005 2005 2005 2005
2006 2006 2006 2006 2006 2006 2006 2006 2006 2006 2006 2006 2006
2007 2007 2007 2007 2007 2007 2007 2007 2007 2007 2007 2007 2007
2008 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008
2009 2009 2009 2009 2009 2009 2009
David Scott
On 1/09/2010 11:04 a.m., Alistair Gray wrote:
> Dear List,
> I'm getting weird and unexpected behaviour using time and %%, or %/%. It's
> likely I'm not appreciating the nuances of floating point arithmetic. Or it
> could be a bug.
>
> I'm running
> > R.version
> _
> platform x86_64-redhat-linux-gnu kernel 2.6.33.8-149.fc13.x86_64
> arch x86_64
> os linux-gnu
> system x86_64, linux-gnu
> status
> major 2
> minor 11.1
> year 2010
> month 05
> day 31
> svn rev 52157
> language R
> version.string R version 2.11.1 (2010-05-31)
>
> I was trying to extract the year from a ts object using time and %/%. It has
> always seemed to work previously but today I got this peculiar result.
>
> > junk<- ts(1:125, start=c(1999,2), frequency=12)
> > time(junk)
> Jan Feb Mar Apr May Jun Jul Aug
> 1999 1999.083 1999.167 1999.250 1999.333 1999.417 1999.500 1999.583
> 2000 2000.000 2000.083 2000.167 2000.250 2000.333 2000.417 2000.500 2000.583
> 2001 2001.000 2001.083 2001.167 2001.250 2001.333 2001.417 2001.500 2001.583
> 2002 2002.000 2002.083 2002.167 2002.250 2002.333 2002.417 2002.500 2002.583
> 2003 2003.000 2003.083 2003.167 2003.250 2003.333 2003.417 2003.500 2003.583
> 2004 2004.000 2004.083 2004.167 2004.250 2004.333 2004.417 2004.500 2004.583
> 2005 2005.000 2005.083 2005.167 2005.250 2005.333 2005.417 2005.500 2005.583
> 2006 2006.000 2006.083 2006.167 2006.250 2006.333 2006.417 2006.500 2006.583
> 2007 2007.000 2007.083 2007.167 2007.250 2007.333 2007.417 2007.500 2007.583
> 2008 2008.000 2008.083 2008.167 2008.250 2008.333 2008.417 2008.500 2008.583
> 2009 2009.000 2009.083 2009.167 2009.250 2009.333 2009.417
> Sep Oct Nov Dec
> 1999 1999.667 1999.750 1999.833 1999.917
> 2000 2000.667 2000.750 2000.833 2000.917
> 2001 2001.667 2001.750 2001.833 2001.917
> 2002 2002.667 2002.750 2002.833 2002.917
> 2003 2003.667 2003.750 2003.833 2003.917
> 2004 2004.667 2004.750 2004.833 2004.917
> 2005 2005.667 2005.750 2005.833 2005.917
> 2006 2006.667 2006.750 2006.833 2006.917
> 2007 2007.667 2007.750 2007.833 2007.917
> 2008 2008.667 2008.750 2008.833 2008.917
> 2009
>
> This looks fine but
> > time(junk)%/%1L
> Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
> 1999 1999 1999 1999 1999 1999 1999 1999 1999 1999 1999 1999
> 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000
> 2001 2001 2001 2001 2001 2001 2001 2001 2001 2001 2001 2001 2001
> 2002 2002 2002 2002 2002 2002 2002 2002 2002 2002 2002 2002 2002
> 2003 2003 2003 2003 2003 2003 2003 2003 2003 2003 2003 2003 2003
> 2004 2004 2004 2004 2004 2004 2004 2004 2004 2004 2004 2004 2004
> 2005 2004 2005 2005 2005 2005 2005 2005 2005 2005 2005 2005 2005
> 2006 2005 2006 2006 2006 2006 2006 2006 2006 2006 2006 2006 2006
> 2007 2006 2007 2007 2007 2007 2007 2007 2007 2007 2007 2007 2007
> 2008 2007 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008
> 2009 2008 2009 2009 2009 2009 2009
>
> Note the incorrect year for January after 2004.
>
> Also
> > time(junk)%%1L
> Jan Feb Mar Apr May Jun
> 1999 0.08333333 0.16666667 0.25000000 0.33333333 0.41666667
> 2000 0.00000000 0.08333333 0.16666667 0.25000000 0.33333333 0.41666667
> 2001 0.00000000 0.08333333 0.16666667 0.25000000 0.33333333 0.41666667
> 2002 0.00000000 0.08333333 0.16666667 0.25000000 0.33333333 0.41666667
> 2003 0.00000000 0.08333333 0.16666667 0.25000000 0.33333333 0.41666667
> 2004 0.00000000 0.08333333 0.16666667 0.25000000 0.33333333 0.41666667
> 2005 1.00000000 0.08333333 0.16666667 0.25000000 0.33333333 0.41666667
> 2006 1.00000000 0.08333333 0.16666667 0.25000000 0.33333333 0.41666667
> 2007 1.00000000 0.08333333 0.16666667 0.25000000 0.33333333 0.41666667
> 2008 1.00000000 0.08333333 0.16666667 0.25000000 0.33333333 0.41666667
> 2009 1.00000000 0.08333333 0.16666667 0.25000000 0.33333333 0.41666667
> Jul Aug Sep Oct Nov Dec
> 1999 0.50000000 0.58333333 0.66666667 0.75000000 0.83333333 0.91666667
> 2000 0.50000000 0.58333333 0.66666667 0.75000000 0.83333333 0.91666667
> 2001 0.50000000 0.58333333 0.66666667 0.75000000 0.83333333 0.91666667
> 2002 0.50000000 0.58333333 0.66666667 0.75000000 0.83333333 0.91666667
> 2003 0.50000000 0.58333333 0.66666667 0.75000000 0.83333333 0.91666667
> 2004 0.50000000 0.58333333 0.66666667 0.75000000 0.83333333 0.91666667
> 2005 0.50000000 0.58333333 0.66666667 0.75000000 0.83333333 0.91666667
> 2006 0.50000000 0.58333333 0.66666667 0.75000000 0.83333333 0.91666667
> 2007 0.50000000 0.58333333 0.66666667 0.75000000 0.83333333 0.91666667
> 2008 0.50000000 0.58333333 0.66666667 0.75000000 0.83333333 0.91666667
> 2009
>
>
> After some testing, it seems to be an interaction between the start in February
> and the length of the series. E.g. I get the same problem if the length of the
> series is 137 or 113.
>
> E.g.
> > time(ts(1:126, start=c(1999,2), frequency=12))%/%1L
> Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
> 1999 1999 1999 1999 1999 1999 1999 1999 1999 1999 1999 1999
> 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000
> 2001 2001 2001 2001 2001 2001 2001 2001 2001 2001 2001 2001 2001
> 2002 2002 2002 2002 2002 2002 2002 2002 2002 2002 2002 2002 2002
> 2003 2003 2003 2003 2003 2003 2003 2003 2003 2003 2003 2003 2003
> 2004 2004 2004 2004 2004 2004 2004 2004 2004 2004 2004 2004 2004
> 2005 2005 2005 2005 2005 2005 2005 2005 2005 2005 2005 2005 2005
> 2006 2006 2006 2006 2006 2006 2006 2006 2006 2006 2006 2006 2006
> 2007 2007 2007 2007 2007 2007 2007 2007 2007 2007 2007 2007 2007
> 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008
> 2009 2009 2009 2009 2009 2009 2009 2009
>
> is OK
>
> as is
> > time(ts(1:125, start=c(1999,3), frequency=12))%/%1L
> Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
> 1999 1999 1999 1999 1999 1999 1999 1999 1999 1999 1999
> 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000 2000
> 2001 2001 2001 2001 2001 2001 2001 2001 2001 2001 2001 2001 2001
> 2002 2002 2002 2002 2002 2002 2002 2002 2002 2002 2002 2002 2002
> 2003 2003 2003 2003 2003 2003 2003 2003 2003 2003 2003 2003 2003
> 2004 2004 2004 2004 2004 2004 2004 2004 2004 2004 2004 2004 2004
> 2005 2005 2005 2005 2005 2005 2005 2005 2005 2005 2005 2005 2005
> 2006 2006 2006 2006 2006 2006 2006 2006 2006 2006 2006 2006 2006
> 2007 2007 2007 2007 2007 2007 2007 2007 2007 2007 2007 2007 2007
> 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008 2008
> 2009 2009 2009 2009 2009 2009 2009 2009
>
> Any explanations would be gratefully accepted. If it is a floating point problem
> then any more robust method of extracting year from a ts object would also be
> gratefully accepted.
>
> thanks
> Alistair
>
> ______________________________________________
> 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.
--
_________________________________________________________________
David Scott Department of Statistics
The University of Auckland, PB 92019
Auckland 1142, NEW ZEALAND
Phone: +64 9 923 5055, or +64 9 373 7599 ext 85055
Email: d.scott at auckland.ac.nz, Fax: +64 9 373 7018
Director of Consulting, Department of Statistics
More information about the R-help
mailing list