Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fourier() vs fourierf() function in R

I'm using the fourier() and fourierf() functions in Ron Hyndman's excellent forecast package in R. Looking to verify whether the same terms are selected and used in fourier() and fourierf(), I plotted a few of the output terms.

Below is the original data using ts.plot(data). There's a frequency of 364 in the time series, FYI. data

Below is the plot of the terms using fourier(data,3). Basically, it looks like mirror images of the existing data.

fourier

Looking at just the sin1 term of the output, again, we get some variation that shows similar 364-day seasonality in line with the data above.

fourier2

However, when I plot the results of the Fourier forecast using fourierf(data,3, 410) I see the below data. It appears far more smooth than the terms provided by the original fourier function. fourierf

So, I wonder how the results of fourier() and fourierf() are related. Is it possible to just see one consolidated Fourier result, so that you can see the sin or cosine result moving through existing data and then through the forecasting period? If not, how can I confirm that the terms created by fourierf() fit the in-sample data?

I want to use it in an auto.arima or glm function with other external regressors like this:

 trainFourier<-fourier(data,3)

 trainFourier<-as.data.frame(trainFourier)
 trainFourier$exogenous<-exogenousData
 arima.object<-auto.arima(data, xreg=trainFourier)

 futureFourier<-fourierf(data,3, 410)

 fourierForecast<-forecast(arima.object, xreg=futureFourier, h=410)

and want to be completely sure that the auto.arima has the proper fitting (using the terms from fourier()) to what I'll put in under xreg for forecast (which has terms from a different function, i.e. ffourier()).

like image 854
Bryan Avatar asked Jul 31 '13 18:07

Bryan


1 Answers

Figured out the problem. I was using both the fda and forecast packages. fda, which is for functional data analysis and regression, has its own fourier() function. If I detach fda, my S1 term from fourier(data,3) looks like this:

fourier()

which lines up nicely with the Fourier forecast if I use ts.plot(c(trainFourier$S1,futureFourier$S1))

Moral of the story -- watch what your packages supress, folks!

like image 81
Bryan Avatar answered Nov 02 '22 08:11

Bryan