Search code examples
rcycletrigonometry

Fitting a harmonic function into a time series in R


enter image description here

I got the following time series of residuals from another regression. One index is a day. You can directly observe the year cycle.

Aim is to fit a harmonic function through it to expalain further part of the underlying time series.

I really appreciate your ideas about which function to use for estimating the right parameters! From acf we learn that there is also a week cycle. However, this issue i will adress later with sarima.


Solution

  • This seems to be the sort of thing a fourier transform is designed for.

    Try

    fftobj = fft(x)
    plot(Mod(fftobj)[1:floor(length(x)/2)])
    

    The peaks in this plot corresponds to frequencies with high coefficients in the fit. Arg(fftobj) will give you the phases.