8

Assume: I have a time series data, either a zoo or xts object.

Question: Is there any convenient function or method so that I can check whether the time series is monthly, quarterly or yearly?

rcs
  • 67,191
  • 22
  • 172
  • 153
L.J
  • 1,066
  • 2
  • 13
  • 28
  • Not sure if this helps, but with a base R time series derived from `ts`, this can easily be achieved with `frequency`. – vitale232 Oct 07 '13 at 05:46
  • Thanks for your response, @vitale232. I have tried frequency() actually. Unfortunately, when applied to all times data I grabbed form FRED, it only gives 1, no matter whether the data itself is quarterly or monthly. – L.J Oct 07 '13 at 06:18
  • This question appears to be off-topic because it is about statistics rather than programming. Besides, it has already been answered at http://stats.stackexchange.com/a/1214/159 – Rob Hyndman Oct 07 '13 at 08:32
  • Hi, @RobHyndman, thanks for the comment. What I was initially thinking is to seek for some already established functions (if they ever existed). Thanks for referring me to the interesting discussion via the link. – L.J Oct 07 '13 at 10:39

3 Answers3

9

You can compute the average difference between the timestamps, and check if it is closer to 1 (daily data), 7 (weekly), etc.

guess_period <- function(x) { 
  average_period <- as.double( mean(diff(index(x))), units="days" )
  difference <- abs(log( average_period / c(
    daily = 1,
    business_days = 7/5,
    weekly = 7,
    monthly = 30,
    quarterly = 365/4,
    annual = 365
  ) ) )
  names( which.min( difference ) )
}

# Examples
library(quantmod)
getSymbols("^GSPC")
guess_period( GSPC )
# [1] "business_days"

getSymbols('CPIAUCNS',src='FRED')
guess_period( CPIAUCNS )
# [1] "monthly"
Vincent Zoonekynd
  • 31,893
  • 5
  • 69
  • 78
9

The xts package has the function periodicity for this purpose.

library(quantmod)
getSymbols("^GSPC")
periodicity(GSPC)
# Daily periodicity from 2007-01-03 to 2013-10-04
getSymbols("CPIAUCNS", src="FRED")
periodicity(CPIAUCNS)
# Monthly periodicity from 1913-01-01 to 2013-08-01
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
0

The one things that all the above techniques assume is that you dont care about the value of each data point, all are treated equally. Does anyone know how to incorporate the value of each data point? In effect finding the period of district data points. One could imagine that a single time series could have multiple periodicities.

DataDancer
  • 175
  • 1
  • 2
  • 11