R小盐准备介绍R语言机器学习与预测模型的学习笔记
你想要的R语言学习资料都在这里, 快来收藏关注【科研私家菜】
01 什么是频谱分析
频谱分析是一种将复杂信号分解为较简单信号的技术。许多物理信号均可以表示为许多不同频率简单信号的和。找出一个信号在不同频率下的信息(如振幅、功率、强度或相位等)的做法即为频谱分析。
由傅里叶理论可知,时域中的任何信号都可以由一个或多个具有适当频率、幅度和相位的正弦
波叠加而成。也就是说,任何时域信号都可以变换成相应的频域信号,通过频域测量可以得到信号在某个特定频率.上的能量值。频谱分析就是在频域上分析时间序列的方法,它使用傅里叶分析方法,将时域信号转换到频域,并从频域中找出信号频谱的变化规律。
从右图中分析出信号中有哪几类主要频率的信号,并可进-步确定出主要信号的周期。R语言程序包stats中spec.pgram函数通过平滑的周期图来估计时间序列的谱密度,且周期图由快速傅里叶
变换计算而来。
spec.pgram(x, spans = NULL, kernel, taper = 0.1,
pad = 0, fast = TRUE, demean = FALSE, detrend = TRUE,
plot = TRUE, na.action = na.fail, ...)
02 R语言实现频谱分析
原始周期图不是谱密度的一致估计量,但相邻值是渐近独立的。因此,假设谱密度是光滑的,通过平滑原始周期图可以得到一致的估计量。为了帮助快速傅立叶变换,级数将自动填充零,直到级数长度是一个高度合数。这是由fast参数控制的,而不是pad参数。0点的周期图在理论上是0,因为去掉了级数的均值(但这可能会受到渐减的影响):在平滑过程中,它被相邻值的插值所取代,并且该频率不返回任何值。
require(graphics)
## Examples from Venables & Ripley
spectrum(ldeaths)
spectrum(ldeaths, spans = c(3,5))
spectrum(ldeaths, spans = c(5,7))
spectrum(mdeaths, spans = c(3,3))
spectrum(fdeaths, spans = c(3,3))
## bivariate example
mfdeaths.spc <- spec.pgram(ts.union(mdeaths, fdeaths), spans = c(3,3))
# plots marginal spectra: now plot coherency and phase
plot(mfdeaths.spc, plot.type = "coherency")
plot(mfdeaths.spc, plot.type = "phase")
## now impose a lack of alignment
mfdeaths.spc <- spec.pgram(ts.intersect(mdeaths, lag(fdeaths, 4)),
spans = c(3,3), plot = FALSE)
plot(mfdeaths.spc, plot.type = "coherency")
plot(mfdeaths.spc, plot.type = "phase")
stocks.spc <- spectrum(EuStockMarkets, kernel("daniell", c(30,50)),
plot = FALSE)
plot(stocks.spc, plot.type = "marginal") # the default type
plot(stocks.spc, plot.type = "coherency")
plot(stocks.spc, plot.type = "phase")
sales.spc <- spectrum(ts.union(BJsales, BJsales.lead),
kernel("modified.daniell", c(5,7)))
plot(sales.spc, plot.type = "coherency")
plot(sales.spc, plot.type = "phase")
specOut<-spec.pgram(as.integer(AirPassengers),taper=0,log='no')
maxSpecFreq<-specOut$freq[which.max(specOut$spec)]
abline(v=maxSpecFreq,lty=2,col='red')
period<-1/maxSpecFreq
period
效果如下:
另外,R语言包TSA中的periodogram可以做傅立叶分析或者其他频谱分析。
### ** Examples
data(star)
plot(star,xlab='Day',ylab='Brightness')
plot of chunk example-TSA-periodogram-1
periodogram(star,ylab='Variable Star Periodogram'); abline(h=0)
plot of chunk example-TSA-periodogram-1
参考资料
Bloomfield, P. (1976) Fourier Analysis of Time Series: An Introduction. Wiley.
Brockwell, P.J. and Davis, R.A. (1991) Time Series: Theory and Methods. Second edition. Springer.
Venables, W.N. and Ripley, B.D. (2002) Modern Applied Statistics with S. Fourth edition. Springer. (Especially pp. 392–7.)
关注R小盐,关注科研私家菜(VX_GZH: SciPrivate),有问题请联系R小盐。让我们一起来学习 R语言机器学习与临床预测模型















网友评论