ulima2_ ulima2_ - 3 months ago 11
R Question

Calculate weekly mean from time series with missing data in R

I have a time series object with daily values that starts in the 19th century and reaches into the 20th century. There are a lot of missing values in there.

I'm trying to calculate weekly means and here's a minimal example:

library(zoo)
library(xts)

# Create time series that starts in 19th century
T <- 100 # number of days
myTS <- xts(rnorm(T), as.Date(1:T, origin="1899-11-05"))

# Insert some missing values
myTS[4:7] <- NA
myTS[33:34] <- NA
myTS[67:87] <- NA

# Try calculating weekly means
weekData <- apply.weekly(myTS, colMeans, na.rm = TRUE)


which only returns the weekly mean for the last week:


1900-02-13 [some value]


I use
colMeans
instead of just
mean
because I'm operating on a larger dataset with several variables.

I would like the mean for all the weeks. Does somebody have an idea what I'm doing wrong?

Answer

Updated based on your comment to use week-year combination:

library(zoo)
library(xts)

# Create time series that starts in 19th century
T <- 100 # number of days
myTS  <- xts(rnorm(T), as.Date(1:T, origin="1899-11-05"))

# Insert some missing values
myTS[4:7] <- NA
myTS[33:34] <- NA
myTS[67:87] <- NA

# Let's use a flexible class
myTS <- data.frame(dates=index(myTS),v1=myTS[,1])

# Here's an easy way to transform dates to weeks
require(lubridate)
week_num <- week(myTS[,1])
year_num <- year(myTS[,1])
week_yr  <- paste(week_num, year_num)

# Weekly means
aggregate(myTS$v1,by=list(week_yr),mean,na.rm=T)
   Group.1           x
1   1 1900  0.05405322
2   2 1900  0.31981319
3   3 1900         NaN
4   4 1900         NaN
5  45 1899  0.85081053
6  46 1899  0.34064255
7  47 1899  0.02880424
8  48 1899 -0.34408119
9  49 1899 -0.38089026
10  5 1900  0.62292188
11 50 1899 -0.59666955
12 51 1899  0.57756987
13 52 1899 -0.41325485
14 53 1899  0.88013634
15  6 1900  0.01514668
16  7 1900 -0.50863942