How to check if a time series is stationary in R?

R ProgrammingServer Side ProgrammingProgramming

To check if a time series is stationary, we can use Dickey-Fuller test using adf.test function of tseries package. For example, if we have a time series object say TimeData then to check whether this time series is stationary or not we can use the command adf.test(TimeData).

Example1

 Live Demo

x1<-ts(rpois(240,5),frequency=12)
x1

Output

   Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1  3   7   7   1   1   6   5   4   8   5   2   3
2  5   0   3   4   4   3   2   4   6   5   5   4
3  5   8   6   2   3   9   5   4   7   4   4   7
4  2   5   4   5   7   9   9   2   3   2   7   7
5  3   5   3   3   8   2   2   3   5   4   3   7
6  6   6   5   4   6   6   5   6   4   2   7   5
7  5   2   3   7   3   7   7   3   7   4   5   6
8  2   5   6   4   6   5   6   3   5   4   9   3
9  4   4   3   7   8   5   1   7   3   5   4   7
10 4   4   7   9   6   6   8   4   4   4   3   3
11 5   2   5   1   2   6   2   2   5   4   3   6
12 4   7   9   2   8   5   6   3   3   5   4   2
13 7  12   2   3   7   3   1   7   6   8   6   4
14 4   4   6  10   8   9   4   2   7   3   6   9
15 5   5   6  11   2   2   6   5   8  10   6   9
16 5   4   2   3   5  14   4   5   3   9   7  11
17 4   5   6   6   4  10   5   6   3   7   7   3
18 8   7   4   9   4   6   1   2   2   7   5   8
19 8   6   5   2   3   8   8   4   5   3   3   8
20 3   4   4   2   4   2   8   3   5   6   4   4

Loading tseries package and testing for stationarity of x1 −

library(tseries)
adf.test(x1)
Augmented Dickey-Fuller Test
data: x1
Dickey-Fuller = -5.3203, Lag order = 6, p-value = 0.01
alternative hypothesis: stationary
Warning message:
In adf.test(x1) : p-value smaller than printed p-value

Example2

 Live Demo

x2<-ts(rnorm(80),frequency=4)
x2

Output

       Qtr1           Qtr2       Qtr3        Qtr4
1   0.60770751  1.51860885   0.42749703   0.68703506
2   0.42218841  0.32858411   0.17599648   0.87994947
3  -1.33761406  0.31770494  -0.47925768   0.97272742
4  -0.37615238 -0.22445651   0.84534205   1.62197957
5  -0.89668820  1.73200007  -0.08857733   1.00659180
6   0.43669956 -1.73037915  -0.20687863  -1.00296218
7   0.63507344 -1.19167403   0.10682158  -0.35904410
8  -0.23533581  1.73153525   0.30313272   0.79616948
9   0.14177702  1.05396871   0.10664840   1.00615408
10 -0.75120735  1.40710169   1.03382332  0.84761125
11 -0.09219708 -2.90114437   0.86964912   0.75207072
12 -1.98357706  0.92872611  -0.59287936   1.10598932
13 -0.24146436 -0.44565952  -0.63415612  -0.28752626
14 -1.98998974 -0.53391601   1.14723658  -1.17779097
15 -0.55899287  0.91836259   0.17393828   2.37323800
16 -0.73209001  0.18996601  -0.60507321   1.48610650
17 -1.38941874 -0.12928990   0.66282081   0.76798462
18  0.27651434  0.36685046   1.35956161  -1.53075985
19 -2.67204336  0.21251039  -1.75949873  -0.33758449
20  1.20913418  0.68987712   2.28974722   1.37072301

Testing for stationarity of x2 −

adf.test(x2)
Augmented Dickey-Fuller Test
data: x2
Dickey-Fuller = -3.6685, Lag order = 4, p-value = 0.03288
alternative hypothesis: stationary
raja
Published on 06-Mar-2021 11:54:18
Advertisements