问题描述
我正在使用R中预测包中的auto.arima来确定傅立叶级数的最佳K项.
I am using the auto.arima from the forecast package in R to determine the optimal K-terms for fourier series.
执行完此操作后,我想计算季节性并将该一个季节性变量插入多元回归模型中.
After I do that, I want to then calculate the seasonality and plug that one seasonality variable into a multiple regression model.
使用预测包中的数据集,我能够提取最佳数量的傅立叶项:
Using the dataset from the forecast package, I was able to extract the optimal amount of fourier terms:
library(forecast)
##Public dataset from the forecast package
head(gas)
##Choose Optimal Amount of K-Terms
bestfit <- list(aicc=Inf)
for(i in 1:6)
{
fit <- auto.arima(gas, xreg=fourier(gas, K=i), seasonal=FALSE)
if(fit$aicc < bestfit$aicc)
bestfit <- fit
else break;
optimal_k_value<-max(i)
print(i)
}
##Extract Fourier Terms
seasonality<-data.frame(fourier(gas, K=optimal_k_value))
##Convert Gas TS Data to Dataframe
gas_df <- data.frame(gas, year = trunc(time(gas)),
month = month.abb[cycle(gas)])
##Extract True Seasonality by Taking Sum of Rows
seasonality$total<- rowSums(seasonality)
##Combine Seasonality to Month and Year
final_df<-cbind(gas_df, seasonality$total)
seasonality$total
列是否应被季节变量"考虑以用于以后的建模,还是需要在其中添加系数?
Would the seasonality$total
column be considered by "seasonality variable" for later modelling or do I need to add coefficients to it?
推荐答案
否,seasonality$total
不是季节性变量.要看到这一点,请注意fourier(gas, K = optimal_k_value)
的每一列都是从-1到1的季节性分量,因此它们只是sin(...)和cos(...),没有任何系数.显然,不同的季节成分必须具有不同的系数,所以您不应该将它们相加.
No, seasonality$total
is not the seasonality variable. To see that, note that each column of fourier(gas, K = optimal_k_value)
is just a seasonal component going from -1 to 1 so that they are just sin(...) and cos(...) without any coefficients. Clearly, different seasonal components must have different coefficients, so you shouldn't just sum them up.
侧面注释1 :由于i
始终只是一个数字,因此使用max(i)
毫无意义,只需optimal_k_value <- i
就足够了.
Side comment 1: since i
is always just a single number, there is no point in using max(i)
, just optimal_k_value <- i
is enough.
旁注2 :我建议检查
plot(resid(auto.arima(gas, xreg = fourier(gas, K = optimal_k_value), seasonal = FALSE)))
其中一个可能是季节性低于年度频率(似乎fourier
不允许考虑),尽管您可能会将其单独建模为趋势.另外,将数据拆分为1970年之前和之后的数据可能是个好主意.
For one, there may be seasonality of lower than yearly frequency (it seems like fourier
doesn't allow to consider that), although perhaps you are going to model it separately as a trend. Also, it may be a good idea to split the data to something like before and after 1970.
这篇关于了解傅立叶的季节性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!