简体   繁体   中英

How to remove seasonality and trend from GDP time series data in R

I am doing a time series analysis to forecast the GDP for the next years and in order to get a good forecasting model I need to remove the trend and the seasonality.

I have used the seasonally adjusted data but it did not completely remove the trend and seasonality of the data. I am using the multiplicative method to remove trend and seasonality.

Seasonally adjusted GDP

decmopose_GDP <- decompose(GDP, 'multiplicative')
adjustGDP <- GDP/decmopose_GDP$seasonal
plot(adjustGDP)

Does anyone know any other method to remove trend and seasonality from the time series?

You can try categorical variable of seasons and splines for time. For example, the model can be 式 . In the model, X contains indicator variable of seasons and also splines for time (you can give them specific degree of freedom). Then the GDP with seasonality and time trend removed will be obtained, ie, residuals of the model. The code can be as follows.

  ##Fitting a model with season and time variable
  model1 <- lm(gdp ~ cat_season + ns(time, df = n))
  ##Extract the GDP without time trend
  GDP_withouttrend <- resid(model1)
  ##Plot the GDP without trend
  plot(GDP_withouttrend)

In case it may be useful for those who still read this old question, there are many R packages (eg, stl )available to decompose a time series into seasonality, trend, and remainder. I believe what is being looked for here is the remainder component. Here I use a package called Rbeast developed by me as an example. Rbeast does time series decomposition and changepoint detection at the same time.

library(Rbeast)
Y = covid19$newcases    # covi19 is a daily time series coming with the Rbeast package
                        # the 'seasonality'/periodicity is 7 days (a week)
o = beast(Y)
plot(o)
remainder = Y - o$trend$Y -o$season$Y
library(Rbeast)
Ylog = log( covid19$newcases+0.001)    # treat covid19 using a multiplicative decomposition model
                                       # the first datapoint is 0; adding 0.001 to nudge it a bit to avoid getting a inf from the log
     
o = beast(Ylog,freq=7)
plot(o)
remainder = exp( Ylog-o$trend$Y -o$season$Y )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM