I am using fourier() function of R which has arguments x,h,K. Can any body please explain me what is 'K' in this function and what is the use of it.

Thanks in advance.

  • $\begingroup$ From which package? $\endgroup$ – nico Jun 14 '14 at 21:03
  • $\begingroup$ It is in package 'forecast'. Here is the link -inside-r.org/packages/cran/forecast/docs/fourier $\endgroup$ – Arushi Jun 14 '14 at 21:19
  • $\begingroup$ K: is the Maximum order of Fourier terms. Alternatively, you could ask what the discrete time Fourier decomposition is. $\endgroup$ – user603 Jun 14 '14 at 21:37
  • $\begingroup$ I am trying to understand, what value should I give to K while using this function in R. Does the value of K depends on the type of data. By type, means it is daily, weekly,monthly or yearly data. Please help me in understanding this part. Thanks $\endgroup$ – Arushi Jun 14 '14 at 21:58

You could use any feature selection approach to find an optimal value of $k$.

Feature selection is an important subject in statistics. Explaining it is, however, outside the scope of the question and well covered elsewhere. You can find good references for this on the internet. For example consider chapter 7 of The Elements of Statistical Learning (2nd edition) a very good book on the subject that also happens to be available for free download at the author's website (see link).

There are many tools to perform feature selection. Perhaps the simplest, most intuitive is cross-validation. In the context of the model you try to fit (discrete fourier decomposition), cross-validation can be performed as so:


The forecast package doesn't include a tool to perform gcv on Fourier decomposition fit of a time series. To do that, you can use the fourier function in the McSpatial package:


According to the gcv criterion, the optimal value of K (this parameter is called q in the McSpatial) for the ldeaths dataset is 21. Now you can re-run the Fourier decomposition of the ldeaths dataset with this optimal value of K to obtain you final fit that is a valid forecast object:

| cite | improve this answer | |
  • $\begingroup$ What's"n" in qmax<-floor(n/2)-1? $\endgroup$ – Ritesh Sinha Apr 16 '19 at 17:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.