L.J
L.J

Reputation: 1084

Check the frequency of time series data

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?

Upvotes: 8

Views: 8759

Answers (3)

DataDancer
DataDancer

Reputation: 175

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.

Upvotes: 0

Joshua Ulrich
Joshua Ulrich

Reputation: 176648

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

Upvotes: 9

Vincent Zoonekynd
Vincent Zoonekynd

Reputation: 32351

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"

Upvotes: 9

Related Questions