# what is K in fourier function of R

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.

• From which package? – nico Jun 14 '14 at 21:03
• It is in package 'forecast'. Here is the link -inside-r.org/packages/cran/forecast/docs/fourier – Arushi Jun 14 '14 at 21:19
• K: is the Maximum order of Fourier terms. Alternatively, you could ask what the discrete time Fourier decomposition is. – user603 Jun 14 '14 at 21:37
• 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 – 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:

library(forecast)
library(McSpatial)
y<-ldeaths
n<-length(y)
x<-1:n
qmax<-floor(n/2)-1


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:

fit_g<-McSpatial::fourier(y~x,minq=1,maxq=qmax-1,crit="gcv")
fit_g\$q


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:

fit_f<-forecast::fourier(y,21)

• What's"n" in qmax<-floor(n/2)-1? – Ritesh Sinha Apr 16 '19 at 17:21