检测R中的季节性

时间:2015-03-02 19:21:39

标签: r signals time-series signal-processing fft

问题:使用周期图和R中的FFT检测日常数据中的周期性模式。

问题是如何在R中编码周期图以检测数据中的月度,季度,半年度,年度......周期性模式。换句话说,我需要检测低频周期模式的存在(即:1年=> 2 * pi / 365,6个月=> 4 * pi / 365等)

可重复的例子:

 library(weatherData)
 w2009=getWeatherForYear("sfo",2009)
 w2010=getWeatherForYear("sfo",2010)
 w2011=getWeatherForYear("sfo",2011)
 w2012=getWeatherForYear("sfo",2012)
 w2013=getWeatherForYear("sfo",2013)
 w2014=getWeatherForYear("sfo",2014)
 w=rbind(w2009,w2010); w=rbind(w,w2011); w=rbind(w,w2012) 
 w=rbind(w,w2013); w=rbind(w,w2014)

 # Next we analyze the periodograms
 # This is IMAGE 1
 TSA::periodogram(w$Max_TemperatureF)
 # Next: I dont really know to use this information
 GeneCycle::periodogram(w$Max_TemperatureF)
 # Next THIS IS IMAGE 2
 stats::spectrum(w$Max_TemperatureF)
 # I also tried . This is IMAGE 3
 f.data <- GeneCycle::periodogram(tmax)
 harmonics <- 1:365
 plot(f.data$freq[harmonics]*length(tmax),]
      f.data$spec[harmonics]/sum(f.data$spec),
      xlab="Harmonics (Hz)", ylab="Amplitute Density", type="h")

TSA Periodogram

Spectrum Periodogram

GeneCyle

读完答案后,我做了:

 per <- TSA::periodogram(w$Max_TemperatureF,lwd = 1)
 x <- which(per$freq < 0.01)
 plot(x = per$freq[x], y = per$spec[x], type="s")

Low Freq

我的问题是这一切意味着什么?我们有季节性周期吗?

1 个答案:

答案 0 :(得分:0)

如果您正在寻找很长一段时间(365天),您会发现它的频率非常低

> 1/365
[1] 0.002739726

您实际上可以在此值的第一张图片左侧看到一个峰值。如果要放大,请过滤到较低频率:

per <- TSA::periodogram(w$Max_TemperatureF,lwd = 1)
x <- which(per$freq < 0.01)
plot(x = per$freq[x], y = per$spec[x], type="s")

搜索周期性的另一种方法是自相关估计(acf):

acf(w$Max_TemperatureF, lag.max = 365*3)

另见季节性分解:

ts1 <- ts(data = w$Max_TemperatureF, frequency = 365)
plot( stl(ts1, s.window = "periodic"))