Solved – fitting a cubic polynomial to a trend component of time series

rtime seriestrend

I have 295 observations of two variables, of which here are a few:

 Date             Close price  
   1/04/14 0:00     478.72   
    2/04/14 0:00    437.51   
    3/04/14 0:00    447.08  
    4/04/14 0:00    448.88  
    5/04/14 0:00    464.83  
    6/04/14 0:00    460.70  
    7/04/14 0:00    446.22  
    8/04/14 0:00    450.46   
    9/04/14 0:00    440.20  
    10/04/14 0:00   360.84  
    11/04/14 0:00   420.06  
    12/04/14 0:00   420.66  
    13/04/14 0:00   414.95  
    14/04/14 0:00   457.63  
    15/04/14 0:00   520.12  
    16/04/14 0:00   529.16    

The first variable is the date plus time stamp "0:00" and the second variable is the price at that date. My whole data set spans 1/4/2014 till 20/1/2015 with daily observations.

I want to decompose this data into two components, trend and errors. I know stl is mainly for seasonal data, but I use the following code, setting the value of s.window to be large to negate the seasonality component.

btc.ts<-ts(btc.csv$Close.Price,frequency=12)
decomp<-stl(btc.ts,s.window=10000)
plot(decomp)

There are a few issues here, the frequency of my data is daily, and frequency = 12 is not correct, but my hopes are that I just want to extract the trend component of the stl and use the zoo package:

trend<-decomp$time.series[,"trend"]
td<-seq(as.Date("2014/4/1"),as.Date("2015/1/15"),"days")
trendz<-zoo(x=trend,order.by=td)

The reason for this is that the trend component found in stl looks the best out of all my approaches, as shown below:

enter image description here

Now, what I want to do is fit a polynomial to model the trend, the issue I am having here is that I cannot use 'td' in the following command (natural splines):

trendz.fit<-lm(trendz~ns(td+I(td^2)+I(td^3)))

as td is a 'date' object. I just want to have something like : trend (t) = a + bt + c(t^2) + d(t^3)..

What can I do to achieve this? How does one usually treat regressions with one of the variables being time/date?

Any suggestions are appreciated, thanks

Best Answer

Regardless of which package you use, you would like to model a 'trend' as a polynomial. In your particular case, trend will be just what is left after seasonality and some 'random' noise were removed from the series. In order to model/check a trend, you proceed as any other regression. First, you need make your 'td' a vector of numbers i.e. $td.new=seq(1:length(td))$. For the $lm$ function to work it needs to be just plain integers. If there was a non-linear contribution, the $I(td.new^2)$ would just be significant as usual in linear regression...Hope this helps.

Related Question