首页 > 解决方案 > 在 R 中拟合和绘制时间序列的谐波回归模型

问题描述

在 R 中,使用 HarmonicRegression 库,我试图将谐波回归模型与趋势拟合,但我正在努力弄清楚如何调用harmonic.regression 函数并绘制拟合模型。

library(TSA)    
library(HarmonicRegression)
data(tempdub)

har_model <- harmonic.regression(tempdub, time(tempdub), Tau = 24, normalize = TRUE, norm.pol = FALSE, norm.pol.degree = 1, trend.eliminate = FALSE, trend.degree = 1)

plot(ts(fitted(har_model), freq=1, start=c(1964,1)), type='l', ylim=range(c(fitted(har_model), tempdub))) 

points(tempdub)

我遇到的第一个错误是:

Error in if (nrow(inputts) != length(inputtime)) stop(paste("Length of time series (inputts):",  : 
  argument is of length zero

知道如何实现吗?

标签: rtime-seriesmodeling

解决方案


library("TSA")    
library("HarmonicRegression")
data(tempdub)

har_model <- harmonic.regression(as.vector(tempdub), 
                             1:length(tempdub), Tau = 12, 
                             normalize = TRUE, norm.pol = FALSE, norm.pol.degree = 1, 
                             trend.eliminate = FALSE, trend.degree = 1)


plot(tempdub)
lines(ts(har_model$fit.vals, freq=12, start=c(1964,1)), lty=2, col=3)

https://cran.r-project.org/web/packages/HarmonicRegression/HarmonicRegression.pdf

手册解释说,您必须将 inputts 和 inputtime 作为向量或矩阵提供给harmonic.regression().


推荐阅读