date - Extend monthly data series to weekly data series in R -
i have monhtly data series , extend weekly series, using r.
i created empty weekly data frame , merged time series monthly data. used na.locf
function replaces na
values value preceding it.
weekly.data <- merge(monthlyts, enmptyweekly) weekly.data <- na.locf(weekly.data)
this works well. however, there weeks belong 2 months: e.g. week starting 2010-09-28
. week, don't want september value, weighted average of september , octover value (3/7*septvalue + 4/7*octvalue
).
is there formula result?
my immediate thought extend daily , there take weekly averages. seems work, there might simpler/quicker solutions around.
library(zoo) par(mar=c(2, 2, 2, 1), mgp=c(3, 0.6, 0), mfrow=c(3, 1), cex=0.8) mo <- seq(as.date(1), as.date(100), by="month") zoo.mo <- zoo(seq_along(mo), mo) plot(zoo.mo, type="b", pch=16, cex=0.5, main="monthly data") zoo.da <- merge(zoo.mo, zoo(, seq(start(zoo.mo), end(zoo.mo)+15, by="day"))) zoo.da <- na.locf(zoo.da) plot(zoo.da, type="b", pch=16, cex=0.5, main="daily data, constant interpolation") zoo.we <- rollapply(zoo.da, 7, mean, by=7) plot(zoo.we, type="b", pch=16, cex=0.5, main="weekly average") weekdays(start(zoo.da))
it take little tweaking want it. example in case first day friday, , week taken begin on friday. little trimming change monday, sunday or whatever. +15 bit in merge command there make sure final value (4) got carried on weekly data.
Comments
Post a Comment