user3184733
user3184733

Reputation: 65

Testing for stationarity of many time series

I have a series of data here. Now pd is an identifier that distinguishes between different elements (there are 2228 unique elements), Date is the date and excess is just a column of values. I want to test for stationarity of excess for each pd using Box.test, adf.test, and kpss.test within the fpp package. To illustrate what I mean, take pd = 1 as an example:

library(fpp)

pd1 <- read.delim("E:/something/something/pd1.txt") # Here I just extracted only the corresponding values for pd = 1 from the data file #

excessret <- matrix(0,dim(pd1),1)

excessret[,1] <- pd1[,3]

Box.test(excessret[,1], lag=20, type="Ljung-Box")

adf.test(excessret[,1], alternative="stationary")

kpss.test(excessret[,1])

The results of each of the three tests are, respectively:

Box-Ljung test

data:  excessret[, 1]
X-squared = 47.7202, df = 20, p-value = 0.0004656    


Augmented Dickey-Fuller Test

data:  excessret[, 1]
Dickey-Fuller = -3.2127, Lag order = 4, p-value = 0.09056
alternative hypothesis: stationary


KPSS Test for Level Stationarity

data:  excessret[, 1]
KPSS Level = 0.1942, Truncation lag parameter = 2, p-value = 0.1

Say I set a significance level of 5%. Then the rules are:

1) For the Box.test, if p-value < 0.05 => stationary

2) For the adf.test, if p-value < 0.05 => stationary

3) For the kpss.test, if p-value > 0.05 => stationary (note change of inequality)

So in this case, the Box.test and kpss.test suggest pd = 1 is stationary while the adf.test suggests pd = 1 is non stationary.

My question is, I wish to do this for every single pd, then count how many pd's are stationary, for all three tests. So for example, using the Box.test, I wish to apply Box.test on every pd and then see out of the 2228 unique elements, how many are classified as stationary. Then repeat this for the other two tests.

Thanks.

Upvotes: 3

Views: 7136

Answers (1)

agstudy
agstudy

Reputation: 121626

You can do this for example:

library(data.table)
DT <- as.data.table(returns)
DT[,Date := as.Date(Date,format='%d/%m/%Y')]
library(fpp)
library(xts)
DT[,{ x = xts(excess,Date)
      list(box= Box.test(x)$p.value <0.05 ,
           adf= adf.test(x)$p.value <0.05 ,
           kpss= kpss.test(x)$p.value >0.05)
},pd]

         pd   box  adf  kpss
   1:     1  TRUE TRUE FALSE
   2:    21  TRUE TRUE FALSE
   3:    26  TRUE TRUE FALSE
   4:    29  TRUE TRUE FALSE
   5:    31 FALSE TRUE FALSE
  ---                       
2224: 82840  TRUE TRUE FALSE
2225: 82848 FALSE TRUE FALSE
2226: 82850  TRUE TRUE FALSE
2227: 83053 FALSE TRUE FALSE
2228: 83273  TRUE TRUE FALSE

Upvotes: 5

Related Questions