检查时间序列数据的频率

L.J*_*L.J 4 r time-series zoo xts

假设:我有时间序列数据,动物园或xts对象.

问题:是否有任何方便的功能或方法,以便我可以检查时间序列是月度,季度还是年度?

Jos*_*ich 6

xts包具有periodicity用于此目的的功能.

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
Run Code Online (Sandbox Code Playgroud)


Vin*_*ynd 5

您可以计算时间戳之间的平均差异,并检查它是否接近1(每日数据),7(每周)等.

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"
Run Code Online (Sandbox Code Playgroud)