[R] help with zoo

Gabor Grothendieck ggrothendieck at gmail.com
Mon Mar 1 18:13:42 CET 2010


I am not sure if you are looking for a method to relabel your time
points or a method to insert NAs.

If you are looking for relabeling of time points that gives an even
number of weeks per year which is the same every year then time
distortion is inevitable.  For example, we can assume 53 weeks a year
and add 0 to the year for the first week, add 1/53 for the second
week, 2/53 for the third week and so on.

# convert yyyyww to year + fraction as described above
# where ww is week number starting at 1
library(gsubfn)
tt <- strapply(time(z1), "(....)(..)", y + w ~ as.numeric(y) +
(as.numeric(w)-1)/53, simplify = c)
z2 <- z1
time(z2) <- tt

# If x is a year + fraction as would be produced by the above code
recover a date in that week like this:

x <- 2009 + 1/53 # example data is 2nd week in 2009
first.of.year <- function(x) as.Date(paste(floor(x), "1", "1", sep = "-"))
first.of.year(x) + round((first.of.year(x+1) - first.of.year(x)) * (x %% 1))

# which for the x here would give "2009-01-08"


On Mon, Mar 1, 2010 at 10:41 AM, ravi <rv15i at yahoo.se> wrote:
> Hi Gabor,
> Thanks for your quick reply. Yes, your solution works. But not quite in the way that I wanted.
> For example,
>> length(z1)
> [1] 174
>> length(index(z1reg))
> [1] 708
>  Instead of 52 weeks, the indexing continues all the way from 53 to the next 1000 level, leading to lots of NA's.
>
> What I am looking for is an indexing that brings about regularity, and can still be converted to dates for plotting purposes.
> Something on the lines of the following :
>> d1<-as.Date("2010-02-23")
>> yearWeek<-format(d1,"%Y-%W")
>> yearWeek
> [1] "2010-08"
>> yearWeekDate<-as.Date(yearWeek,format("%Y-%W"))
>> yearWeekDate
> [1] "2010-03-01"
>> yearWeekDate<-as.Date(yearWeek,format="%Y-%W")
>> yearWeekDate
> [1] "2010-03-01"
>
> My time series is regular in that the difference between any two points is 2 weeks with some breaks in between (with odd breaks, the regularity gets disturbed). In the above method, any date gets converted to the date corresponding to Monday in that week. This should partly give some regularity to the time series. But the same method fails to work when I try it on my data frame :
>
> pathW<-"W:\\Miljö\\Data_diagram\\Stoftnedfall äldre\\stofthistoriaFeb2010.txt"
> stoft<-read.table(pathW,header=TRUE,sep="\t",dec=",",colClasses=c("Date","numeric"))
> stoft$week<-format(stoft$datum,format("%Y%-%W"))
> stoft$weekNdat<-as.Date(stoft$week,format="%Y-%W")
> head(stoft)
>> head(stoft)
>        datum    stoftm   week weekNdat
> 1 2003-01-08 1.8037575 200301     <NA>
> 2 2003-01-21 0.7933268 200303     <NA>
> 3 2003-02-06 1.2891561 200305     <NA>
> 4 2003-02-18 0.7957754 200307     <NA>
> 5 2003-03-03 0.8447462 200309     <NA>
> 6 2003-03-17 1.7393376 200311     <NA>
>
> What is my mistake here? And, will it be possible to proceed in the lines that I have indicated?
> One method that I can follow is to get just week number from the date and then (manually)
> add the final week of a year to the weeks of the following year, and so on.
> I am hoping for a more elegant solution, preferably where the index can can be converted back to dates for plotting.
> Thanking you,
> Ravi
>
>
>
>
> ----- Original Message ----
> From: Gabor Grothendieck <ggrothendieck at gmail.com>
> To: ravi <rv15i at yahoo.se>
> Cc: r-help at r-project.org
> Sent: Mon, 1 March, 2010 15:08:53
> Subject: Re: [R] help with zoo
>
> This will give you a regular series with NAs:
>
> z1reg <- as.zooreg(as.ts(z1))
>
>
>
> On Mon, Mar 1, 2010 at 8:03 AM, ravi <rv15i at yahoo.se> wrote:
>> Hi,
>> I am interested in decomposing a time series and getting the trend, seasonal and irregular variations, as one can get with the "stl" command. My time series is fairly regular, but it has some breaks. From the zoo manual, I gather that it should be possible to convert it to a regular time series and then fill the NA entries by interpolation. I am not able to proceed beyond a certain point and would like some help. Here's my code :
>>
>> dput(stoft)
>> structure(list(datum = structure(c(12060, 12073, 12089, 12101,
>> 12114, 12128, 12143, 12157, 12170, 12184, 12198, 12213, 12226,
>> 12284, 12297, 12310, 12324, 12338, 12352, 12368, 12381, 12394,
>> 12409, 12425, 12436, 12451, 12464, 12478, 12489, 12507, 12535,
>> 12549, 12562, 12579, 12591, 12639, 12653, 12668, 12681, 12696,
>> 12710, 12724, 12737, 12751, 12765, 12779, 12793, 12807, 12821,
>> 12835, 12849, 12863, 12878, 12892, 12906, 12920, 12934, 12948,
>> 12962, 12976, 12998, 13011, 13025, 13038, 13046, 13063, 13074,
>> 13088, 13102, 13119, 13130, 13144, 13158, 13172, 13187, 13200,
>> 13213, 13227, 13241, 13256, 13270, 13283, 13297, 13311, 13325,
>> 13339, 13360, 13376, 13390, 13404, 13418, 13433, 13445, 13459,
>> 13472, 13486, 13502, 13515, 13530, 13544, 13558, 13572, 13584,
>> 13599, 13614, 13627, 13641, 13657, 13669, 13683, 13697, 13712,
>> 13731, 13740, 13754, 13769, 13782, 13797, 13810, 13825, 13838,
>> 13852, 13881, 13894, 13908, 13923, 13936, 13950, 13965, 13978,
>> 13992, 14006, 14020, 14034, 14048, 14062, 14090, 14104, 14118,
>> 14132, 14146, 14160, 14175, 14189, 14202, 14217, 14231, 14257,
>> 14271, 14286, 14300, 14315, 14327, 14348, 14362, 14376, 14393,
>> 14406, 14419, 14433, 14475, 14489, 14503, 14517, 14532, 14545,
>> 14559, 14573, 14586, 14599, 14622, 14636, 14651, 14664), class = "Date"),
>>     stoftm = c(1.803757545, 0.793326848, 1.289156128, 0.795775388,
>>     0.844746181, 1.739337633, 2.737467333, 4.174410319, 2.115538261,
>>     0.818511827, 1.94396559, 0.585690685, 0.455428376, 1.537438049,
>>     0.954930465, 1.469123793, 2.455535482, 1.677949246, 0.491107096,
>>     1.432395698, 0.910856751, 1.542579982, 1.470592916, 1.210374365,
>>     0.899370874, 0.241915718, 0.062437761, 1.091349103, 6.120236163,
>>     2.419157178, 3.60145204, 2.332758708, 2.0531005, 1.685171409,
>>     1.018592496, 0.429718709, 0.798049032, 0.896361397, 1.388321984,
>>     7.219274317, 1.364186379, 1.364186379, 1.469123793, 0.279658208,
>>     1.074296773, 1.418753834, 1.113176085, 1.309618924, 0.682093189,
>>     0.90036301, 1.309618924, 1.125453762, 5.793244822, 3.069419352,
>>     1.023139784, 1.125453762, 1.227767741, 0.545674552, 1.200484013,
>>     1.534709676, 1.969328791, 0.53476106, 2.216802866, 1.542579982,
>>     0.596831541, 1.887391978, 4.514216744, 4.092559136, 3.60145204,
>>     2.387326163, 2.083484651, 0.777586236, 0.072301878, 0.736660645,
>>     0.165521281, 0, 0.587649517, 0.272837276, 2.346400572, 2.54648124,
>>     2.018995841, 1.851095979, 0, 1.637023655, 2.387326163, 0.682093189,
>>     0.113682198, 1.957607454, 0, 1.568814336, 3.192196126, 1.591550775,
>>     0, 0, 0.843277057, 1.091349103, 1.193663081, 0.661105707,
>>     1.282335196, 0.341046595, 0.954930465, 0.368330322, 0.350141171,
>>     3.75605983, 1.718874837, 1.432395698, 1.568814336, 0.895247311,
>>     1.145916558, 0.532032688, 0.341046595, 0.541127264, 0.402075985,
>>     1.220188928, 1.023139784, 0.26738053, 0.899838323, 0.604789295,
>>     0.954930465, 1.298705433, 0, 0.682093189, 3.001210033, 0,
>>     1.637023655, 0.659538641, 2.05677331, 1.637023655, 1.018592496,
>>     1.285483318, 3.683303223, 0.954930465, 2.455535482, 1.780263224,
>>     1.159558422, 0.852616487, 0.170523297, 1.432395698, 0.668451326,
>>     0.518390824, 0.682093189, 0, 0.254648124, 0.255784946, 0,
>>     0, 0, 0.443360573, 0.627525734, 1.336902651, 0.184165161,
>>     0.725747154, 1.233451851, 3.001210033, 1.364186379, 0.600242007,
>>     1.606530077, 0.440737138, 0, 0, 0.318310155, 0, 0.375151254,
>>     0.682093189, 0.241915718, 0.514193327, 0.518390824, 0, 0.4260459,
>>     0, 0.368330322, 0.354688458, 0, 0)), .Names = c("datum",
>> "stoftm"), class = "data.frame", row.names = c(NA, -174L))
>>
>> stoft$week<-format(stoft$datum,format("%Y%W"))
>> library(zoo)
>> z1<-zoo(stoft$stoftm,stoft$week)
>> is.regular(z1)
>> z2<-as.ts(z1)
>>
>> I should like to have some help in going further. I can experiment even more, but it would be nice if I received some help before I resume my trials.
>> Should I transform the index to a better form? What is the frequency that I should choose (especially if the index has "%Y%W" form)?
>> What is the best way to go forward to the decomposition of the time series?
>> Thanking you,
>> Ravi
>>
>>
>> ______________________________________________
>> 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.
>>
>
>



More information about the R-help mailing list