r - Check the frequency of time series data -
assume: have time series data, either zoo or xts object.
question: there convenient function or method can check whether time series monthly, quarterly or yearly?
you can compute average difference between timestamps, , check if closer 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"
Comments
Post a Comment