R中检测季节性(Detecting Seasonality in R)

2019-09-28 14:40发布

问题:使用周期图和FFT在R.日常数据检测循环模式

问题是如何在R代码里面的周期图检测数据月度,季度,半年度,annual..etc循环模式。 换句话说,我需要检测为低频率周期性图案的存在(即:11个月=> 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")

阅读的答案后,我做的:

 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")

我的问题是到底有什么意思呢? 我们有一个季节性周期?

Answer 1:

如果你正在寻找一个长期(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"))


文章来源: Detecting Seasonality in R