在R中检测季节性 [英] Detecting Seasonality in R

查看:406
本文介绍了在R中检测季节性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Problem: Detecting cyclical patterns in daily data using periodogram and FFT in R.

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

The issue is how to code in R the periodogram to detect monthly, quarterly, semi-annual, annual..etc cyclical patterns in the data. In other words I need to detect the existence of cyclical patterns for low frequencies ( ie: 1 year=> 2*pi/365, 6 months = > 4*pi/365, etc)

可复制的示例:

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

我的问题是什么意思?我们有季节性周期吗?

My question is what does it all mean? Do we have a seasonality cycle?

推荐答案

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

If you are looking for a long periods (365 days) you will find it under very low frequency

> 1/365
[1] 0.002739726

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

You can actually see a peak on the left of your first image at this value. Filter to lower frequencies if you want to zoom-in:

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

Another way to search for periodicity is auto-correlation estimation (acf):

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

另请参见季节性分解:

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

这篇关于在R中检测季节性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆