[R] Data decomposition

Gabor Grothendieck ggrothendieck at gmail.com
Sun Mar 29 18:36:09 CEST 2009


Its not clear what it means for the sum of
the weeks in a month to equal the month
since the weeks don't evenly divide
a month but if we apportion them pro
rata then here is a possibility.

We create the input monthly series z and
then produce a series of Date class days
d covering the period.  Merging the
input and d leaves a series with the
input values at the first of the month
and NAs on each other day.  We use ave
to distribute that first value among all
days of the month and then defining
weeks to end in Friday (or the last day
of the input even if its not Friday) we
use the nextfri function given in the zoo
vignette and aggregate over the days in
each week.

library(zoo)

# input
z <- zoo(1:12, as.yearmon(2000) + 0:11/12)
tt <- time(z)

# calculate sequence of Dates
rng <- c(as.Date(tt[1]), as.Date(tt[length(z)], frac = 1))
d <- seq(rng[1], rng[2], by = "day")

# place input into 1st of each month with NAs for other days
m <- merge(aggregate(z, as.Date, force), zoo(, d))

# distribute 1st of month across entire month
m0 <- ave(m, as.yearmon(time(m)), FUN = function(x) x[1]/length(x))

# calculate the next fri for each day
# This one line function can be found in the zoo-quickref vignette.
nextfri <- function(x) 7 * ceiling(as.numeric(x - 5 + 4)/7) + as.Date(5 - 4)
w <- pmin(nextfri(time(m0)), rng[2])

# sum all days in each week
aggregate(m0, w, sum)



On Sun, Mar 29, 2009 at 9:28 AM, Pele <drdionc at yahoo.com> wrote:
>
> Hi R users,
>
> I have a time series variable that is only available at a monthly level for
> 1 years  that  I need to decompose to a weekly time series level - can
> anyone recommend a R function that I can use to decompose this series?
>
> eg. if month1 = 1200 I would to decompose so that  the sum of the weeks for
> month1 equals 1200, etc..
>
> Many thanks in advance for any help.
> --
> View this message in context: http://www.nabble.com/Data-decomposition-tp22767614p22767614.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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