hello, i cant find a solution on this (might be) easy problem: i have a time serie by carlandar weeks, so for every carlendar week i have a value. now i would like to use the functions for time series, so i change structur to a time serie with cam <- ts(number,start=c(2001,1),deltat=7/365) or cam <- ts(number,start=c(2001,1),frequency=52) the problem on it is, that 2004 had 53 calendar weeks, which is not recognized there. it follows with using a saisonal structure the weeks are shifting. f.e. first week in 2005 is conected with the second week in 2004. with the first function leap years are not recognized. is there another function which is recognizing irregularities in the calendar? thanks a lot, collonil -- View this message in context: http://www.nabble.com/time-series-by-calendar-week-tp18340479p18340479.html Sent from the R help mailing list archive at Nabble.com.
I don't know if this will help, but look at the zoo, chron, and Posix Date Time packages/classes. On Tue, Jul 8, 2008 at 10:25 AM, collonil <manitz.juliane@web.de> wrote:> > hello, > > i cant find a solution on this (might be) easy problem: > > i have a time serie by carlandar weeks, so for every carlendar week i have > a > value. now i would like to use the functions for time series, so i change > structur to a time serie with > > cam <- ts(number,start=c(2001,1),deltat=7/365) > or > cam <- ts(number,start=c(2001,1),frequency=52) > > the problem on it is, that 2004 had 53 calendar weeks, which is not > recognized there. > it follows with using a saisonal structure the weeks are shifting. f.e. > first week in 2005 is conected with the second week in 2004. > with the first function leap years are not recognized. > > is there another function which is recognizing irregularities in the > calendar? > > thanks a lot, collonil > > -- > View this message in context: > http://www.nabble.com/time-series-by-calendar-week-tp18340479p18340479.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods. We are mammals, and have not exhausted the annoying little problems of being mammals. -K. Mullis [[alternative HTML version deleted]]
On Tue, Jul 8, 2008 at 10:25 AM, collonil <manitz.juliane at web.de> wrote:> > hello, > > i cant find a solution on this (might be) easy problem: > > i have a time serie by carlandar weeks, so for every carlendar week i have a > value. now i would like to use the functions for time series, so i change > structur to a time serie with > > cam <- ts(number,start=c(2001,1),deltat=7/365) > or > cam <- ts(number,start=c(2001,1),frequency=52) > > the problem on it is, that 2004 had 53 calendar weeks, which is not > recognized there. > it follows with using a saisonal structure the weeks are shifting. f.e. > first week in 2005 is conected with the second week in 2004. > with the first function leap years are not recognized. > > is there another function which is recognizing irregularities in the > calendar? >I assume the main problem is that you want to convert it to a series that has an integral number of cycles per year. To do that some approximation will be required such as: - omit anything past 52 weeks from each year, or - create a grid of freq points per year and then take the last point in each grid section or the mean of all points in each grid section Here is an example using a grid of 52 points per year: # Suppose we have this data: library(zoo) set.seed(1) z <- zooreg(1:100 + rnorm(100), start = as.Date("2001-01-01"), deltat = 7) # new.freq() converts dates to a grid of freq points per year # yd is sequence of dates of firsts of years # yy is years of the same sequence # last line interpolates so dates, d, are transformed to year + frac of year new.freq <- function(d, freq = 52) { y <- as.Date(cut(range(d), "years")) + c(0, 367) yd <- seq(y[1], y[2], "year") yy <- as.numeric(format(yd, "%Y")) ceiling(freq * approx(yd, yy, xout = d)$y) / freq } # take last point in each period aggregate(z, new.freq, tail, 1) # or, take mean of all points in each period aggregate(z, new.freq, mean)