Solved – Question about eliminating seasonality

I am trying to remove seasonality from data. I tried the non-linear trend using the code: trend=lm(NH3cH6~t+cos.t+sin.t). The plot was shown as following:
The dots are the data, the red line is the trend

However, as you can see, the second peak of the trend does not quite fit the data. I want to shift the second peak of the trend a little bit, but I don't know how to modify the code to achieve this. Can anyone help me to improve the fitting of the trend?

Your model fits a linear time trend plus a first order Fourier series approximation for the seasonality. Since you don't define cos.t or sin.t, it is not possible to tell what seasonal period you have used. You can fit a model with correct seasonal period using

t <- 1:length(NH3cH6) cos.t <- cos(2*pi*t/365) sin.t <- sin(2*pi*t/365) trend <- lm(NH3cH6 ~ t + cos.t + sin.t) 

However, you may need a higher order Fourier series approximation, and there are facilities in the forecast package in R for doing that.

First, make sure the data is a time series object of the correct frequency:

NH3cH6 <- ts(NH3cH6, frequency=365) 

Generate the Fourier terms for the regression (using 3 terms here, but choose the best number by minimizing the AIC of the fitted regression model).

library(forecast) X <- fourier(NH3cH6,3) 

Fit the regression model

fit  <- tslm(NH3cH6 ~ X + trend) 

The tslm command will understand what trend means, and will make sure the residuals and fitted values are ts objects with time series characteristics.

To see the result:

plot(NH3cH6) lines(fitted(fit), col="red") 

Similar Posts:

Rate this post

Leave a Comment