# Check for stationarity of a time series and check Granger causality test

I am new to time series analysis and related statistical tools. I am struggling with a time series for more than a week. I tried to check the Granger causality for the below data.

serdata <- read.csv("serdata.csv")
serdata
serdata$date = as.Date(serdata$date,format="%m/%d/%Y")
plot.ts(serdata)

## I could not interpret acf and pacf.
library(tseries)
acf(serdata$actual) pacf(serdata$actual)
acf(serdata$digital) pacf(serdata$digital)


adf.test(serdata$digital, alternative = "stationary")  Augmented Dickey-Fuller Test data: serdata$digital Dickey-Fuller = -2.1558, Lag order = 7, p-value = 0.5114 alternative hypothesis: stationary

kpss.test(serdata$digital)  KPSS Test for Level Stationarity data: serdata$digital KPSS Level = 5.5528, Truncation lag parameter = 4, p-value = 0.01

Warning message: In kpss.test(serdata$digital) : p-value smaller than printed p-value Box.test(serdata$digital, lag=20, type = "Ljung-Box")


Box-Ljung test

data: serdata$digital X-squared = 4242.103, df = 20, p-value < 2.2e-16 ## Grangertest gives very small p-value for both the series. library(lmtest) grangertest(serdata$digital~serdata$actual, order = 7, data = serdata) grangertest(serdata$actual~serdata$digital, order = 7, data = serdata) ## Tried SMA library("TTR") SMAdigital<-SMA(serdata$digital,n=10)
SMAactual <-SMA(serdata$actual,n=10) plot.ts(SMAactual) grangertest(SMAdigital~SMAactual, order = 7, data = serdata) grangertest(SMAactual~SMAdigital, order = 7, data = serdata)  I think the data is not stationary. Can some one help me to confirm ? How can I remove the stationary ? I tried to use deompose() but got an error Error in decompose(beonic$digital) :
time series has no or less than 2 periods


I am totally confused. Can someone guide me to check the causality ?

The data: serdata.csv

date    actual  digital
2/1/2016    5357    3205
2/2/2016    3717    4381
2/3/2016    3820    4379
2/4/2016    4566    4728
2/5/2016    4684    4494
2/6/2016    6563    4727
2/7/2016    6622    3606
2/8/2016    5734    3415
2/9/2016    4771    4373
2/10/2016   6107    4484
2/11/2016   4206    4448
2/12/2016   4845    4595
2/13/2016   6171    4464
2/14/2016   6883    3314
2/15/2016   4407    3565
2/16/2016   5271    3780
2/17/2016   6604    4312
2/18/2016   8226    4537
2/19/2016   9173    4350
2/20/2016   9143    4302
2/21/2016   8612    3343
2/22/2016   7252    3535
2/23/2016   5042    4245
2/24/2016   4832    4496
2/25/2016   5366    4456
2/26/2016   4916    4561
2/27/2016   7797    4519
2/28/2016   7760    3318
2/29/2016   4397    3393
3/1/2016    3845    4435
3/2/2016    4920    4537
3/3/2016    3976    4577
3/4/2016    5404    4661
3/5/2016    7341    4428
3/6/2016    5422    3358
3/7/2016    4488    3479
3/8/2016    3676    4400
3/9/2016    3968    4378
3/10/2016   4072    4609
3/11/2016   4311    4971
3/12/2016   6944    4755
3/13/2016   6394    3516
3/14/2016   3971    3678
3/15/2016   4308    4405
3/16/2016   3759    4386
3/17/2016   3652    4375
3/18/2016   4649    4472
3/19/2016   5979    4184
3/20/2016   5923    3375
3/21/2016   3822    3534
3/22/2016   2964    4325
3/23/2016   3505    4553
3/24/2016   4103    4344
3/25/2016   7998    4125
3/26/2016   8945    3351
3/27/2016   8162    2932
3/28/2016   6001    2828
3/29/2016   4277    3801
3/30/2016   3059    4020
3/31/2016   3243    4095
4/1/2016    4368    3884
4/2/2016    5335    3821
4/3/2016    5638    2984
4/4/2016    3586    3219
4/5/2016    2941    4085
4/6/2016    2731    4287
4/7/2016    3951    4425
4/8/2016    3460    4469
4/9/2016    6856    4086
4/10/2016   5136    3089
4/11/2016   3404    3200
4/12/2016   3033    4476
4/13/2016   3834    4165
4/14/2016   2886    4215
4/15/2016   3649    4008
4/16/2016   6266    3779
4/17/2016   5695    2898
4/18/2016   4740    2955
4/19/2016   4506    3872
4/20/2016   5324    4009
4/21/2016   4888    3971
4/22/2016   5555    4070
4/23/2016   7379    4066
4/24/2016   7688    2968
4/25/2016   5240    3083
4/26/2016   4672    4167
4/27/2016   4748    4258
4/28/2016   4779    4176
4/29/2016   4915    4288
4/30/2016   4936    4161
5/1/2016    5424    3155
5/2/2016    2240    3272
5/3/2016    2047    4296
5/4/2016    2411    4209
5/5/2016    2376    4242
5/6/2016    3058    4114
5/7/2016    4103    4071
5/8/2016    4229    2878
5/9/2016    2320    3039
5/10/2016   2292    4169
5/11/2016   2068    4419
5/12/2016   2443    4445
5/13/2016   2787    4769
5/14/2016   4595    4153
5/15/2016   4465    2912
5/16/2016   2082    3172
5/17/2016   2606    4235
5/18/2016   2547    4439
5/19/2016   2566    4433
5/20/2016   3321    4252
5/21/2016   5013    3776
5/22/2016   4883    3119
5/23/2016   2187    3157
5/24/2016   2705    4206
5/25/2016   2056    4407
5/26/2016   2193    4057
5/27/2016   2898    3941
5/28/2016   4744    3318
5/29/2016   4128    2814
5/30/2016   2537    3143
5/31/2016   1967    3099
6/1/2016    1903    3525
6/2/2016    2280    3709
6/3/2016    3195    3387
6/4/2016    5902    2969
6/5/2016    6553    2364
6/6/2016    4364    2577
6/7/2016    2171    3477
6/8/2016    2052    3578
6/9/2016    2274    3458
6/10/2016   2764    3432
6/11/2016   4334    3012
6/12/2016   4515    2723
6/13/2016   2244    3647
6/14/2016   1764    3127
6/15/2016   2287    3135
6/16/2016   2620    3154
6/17/2016   3539    3091
6/18/2016   6140    2496
6/19/2016   6020    2367
6/20/2016   2460    2975
6/21/2016   2019    3452
6/22/2016   2260    3153
6/23/2016   2357    2982
6/24/2016   3011    2786
6/25/2016   6115    2374
6/26/2016   5390    2412
6/27/2016   2294    2917
6/28/2016   2574    3117
6/29/2016   2210    3001
6/30/2016   2160    2826
7/1/2016    3164    2584
7/2/2016    4505    2256
7/3/2016    4611    2308
7/4/2016    4361    2427
7/5/2016    2433    2345
7/6/2016    2635    2577
7/7/2016    3293    2606
7/8/2016    2839    2602
7/9/2016    4455    2129
7/10/2016   5092    2081
7/11/2016   4109    2364
7/12/2016   4833    2408
7/13/2016   4955    2393
7/14/2016   5345    2416
7/15/2016   5266    2233
7/16/2016   7403    2049
7/17/2016   5792    2051
7/18/2016   4967    2206
7/19/2016   4634    2463
7/20/2016   4625    2439
7/21/2016   5005    2544
7/22/2016   5223    2398
7/23/2016   5172    2147
7/24/2016   4606    2253
7/25/2016   2407    2434
7/26/2016   2212    2604
7/27/2016   2155    2533
7/28/2016   2528    2672
7/29/2016   3446    2515
7/30/2016   5657    2175
7/31/2016   5198    2126
8/1/2016    2706    2462
8/2/2016    2299    2476
8/3/2016    2062    2633
8/4/2016    2231    2730
8/5/2016    2862    2554
8/6/2016    4005    2313
8/7/2016    4731    2249
8/8/2016    2177    2571
8/9/2016    2182    2585
8/10/2016   2285    2643
8/11/2016   2627    2476
8/12/2016   2885    2483
8/13/2016   6288    2449
8/14/2016   7410    2212
8/15/2016   2380    2615
8/16/2016   1823    2622
8/17/2016   1863    2573
8/18/2016   7   2723
8/19/2016   23  2580
8/20/2016   4329    2403
8/21/2016   4205    2369
8/22/2016   2162    2474
8/23/2016   2190    2754
8/24/2016   2041    2793
8/25/2016   2513    2791
8/26/2016   4491    2730
8/27/2016   5282    2724
8/28/2016   4737    2348
8/29/2016   2097    2629
8/30/2016   2110    3038
8/31/2016   2071    2901
9/1/2016    2473    2822
9/2/2016    2586    2862
9/3/2016    3551    2774
9/4/2016    4104    2539
9/5/2016    2025    2730
9/6/2016    2516    2937
9/7/2016    1983    3272
9/8/2016    1804    3270
9/9/2016    2283    2979
9/10/2016   4049    2780
9/11/2016   3792    2488
9/12/2016   2221    2777
9/13/2016   2337    3147
9/14/2016   2258    3105
9/15/2016   2276    3394
9/16/2016   2425    3043
9/17/2016   4567    3092
9/18/2016   3639    2761
9/19/2016   2616    2819
9/20/2016   2384    3365
9/21/2016   2735    3308
9/22/2016   2938    3164
9/23/2016   3386    3246
9/24/2016   4897    3261
9/25/2016   5788    2411
9/26/2016   4349    2297
9/27/2016   4451    3178
9/28/2016   4889    3326
9/29/2016   5002    3167
9/30/2016   5452    3102
10/1/2016   6223    3097
10/2/2016   6593    2575
10/3/2016   4574    2786
10/4/2016   4285    3188
10/5/2016   5323    3358
10/6/2016   4689    3537
10/7/2016   5499    3326
10/8/2016   5852    3198
10/9/2016   4987    2494
10/10/2016  2680    2834
10/11/2016  3128    3337
10/12/2016  2189    3686
10/13/2016  2532    3600
10/14/2016  3296    3630
10/15/2016  5019    3397
10/16/2016  4222    2666
10/17/2016  2482    2913
10/18/2016  2742    3746
10/19/2016  2611    3832
10/20/2016  2248    3716
10/21/2016  3463    3621
10/22/2016  5030    3530
10/23/2016  6164    2730
10/24/2016  4192    2774
10/25/2016  2879    4035
10/26/2016  2333    4054
10/27/2016  2710    3611
10/28/2016  3568    3846
10/29/2016  5415    3542
10/30/2016  5974    2684
10/31/2016  2314    2928
11/1/2016   2334    3108
11/2/2016   3379    3721
11/3/2016   2846    3884
11/4/2016   3183    4103
11/5/2016   4865    3797
11/6/2016   4948    3025
11/7/2016   3159    2651
11/8/2016   2724    1987
11/9/2016   2866    1833
11/10/2016  3600    2054
11/11/2016  5198    1862
11/12/2016  5192    1461
11/13/2016  4482    1311
11/14/2016  0   1557
11/15/2016  3   2014
11/16/2016  3284    2148
11/17/2016  2938    2033
11/18/2016  3710    1874
11/19/2016  4293    1572
11/20/2016  4239    1315
11/21/2016  4086    1715
11/22/2016  3054    1898
11/23/2016  3194    1797
11/24/2016  3033    1688
11/25/2016  3927    1462
11/26/2016  4824    1184
11/27/2016  4685    1237
11/28/2016  3072    1620
11/29/2016  3139    1916
11/30/2016  4363    1949
12/1/2016   2780    1907
12/2/2016   4075    1733
12/3/2016   5207    1453
12/4/2016   5327    1243
12/5/2016   4223    1569
12/6/2016   3179    1752
12/7/2016   3329    1697
12/8/2016   3747    1887
12/9/2016   3534    1797
12/10/2016  4673    1613
12/11/2016  5205    1229
12/12/2016  3600    1442
12/13/2016  4541    1871
12/14/2016  3823    1852
12/15/2016  3770    2155
12/16/2016  4370    2574
12/17/2016  4478    1434
12/18/2016  5396    1253
12/19/2016  5140    1306
12/20/2016  4089    1301
12/21/2016  4231    1333
12/22/2016  5657    1226
12/23/2016  6215    1074
12/24/2016  4211    841
12/25/2016  0   814
12/26/2016  5503    1035
12/27/2016  9483    1773
12/28/2016  6956    2030
12/29/2016  8992    1204
12/30/2016  6062    1135
12/31/2016  6326    1068
1/1/2017    7463    1067
1/2/2017    8426    1288
1/3/2017    8084    1322
1/4/2017    7026    1463
1/5/2017    6295    1480
1/6/2017    7179    1529
1/7/2017    5745    1474
1/8/2017    5690    1322
1/9/2017    5761    1404
1/10/2017   6332    1453
1/11/2017   6240    1056
1/12/2017   6710    1623
1/13/2017   5698    1714
1/14/2017   6156    1558
1/15/2017   7415    1365
1/16/2017   5742    1386
1/17/2017   5779    1671
1/18/2017   5784    1770
1/19/2017   5331    1680
1/20/2017   5476    1579
1/21/2017   7710    1290
1/22/2017   7187    1233
1/23/2017   7676    1352
1/24/2017   4742    1671
1/25/2017   5153    1745
1/26/2017   4817    1832
1/27/2017   5976    2170
1/28/2017   6145    2610
1/29/2017   5764    2238
1/30/2017   5305    2138
1/31/2017   4760    1898
2/1/2017    4752    2318
2/2/2017    4527    2223
2/3/2017    5206    6291
2/4/2017    6209    2040
2/5/2017    5729    1890
2/6/2017    7056    2225
2/7/2017    4955    2428
2/8/2017    4185    2302
2/9/2017    4090    2082
2/10/2017   3881    1994
2/11/2017   5280    1657
2/12/2017   6471    1768
2/13/2017   5840    1956
2/14/2017   4530    2148
2/15/2017   3993    2159
2/16/2017   4453    2188
2/17/2017   5570    2048
2/18/2017   7479    1959
2/19/2017   5349    1736
2/20/2017   4098    2041
2/21/2017   5032    2100
2/22/2017   4256    2253
2/23/2017   6215    2162
2/24/2017   4480    2055
2/25/2017   5995    1899
2/26/2017   6412    1836
2/27/2017   5450    2012
2/28/2017   3935    2243

• FWIW, the error you are getting from stats::decompose is due to the fact that your data is not of the ts (time series) type; you are using either a data.frame or a bare vector which carries neither date nor frequency information. You may get weird behavior from other functions as well. Apr 3, 2017 at 0:08
• Yes Chris, I was getting weird graph for date when I plotted plot.ts(serdata). Hope serdata$date = as.Date(serdata$date,format="%m/%d/%Y") would resolve it. Apr 3, 2017 at 4:45

The first thing you need to determine is whether your data is a trend stationary or difference stationary process.

A trend stationary process is one whereby, if the trend was removed from your data, then you would be left with a stationary dataset.

However, this would not be the case in data that has a unit root present, i.e. one where the null hypothesis of non-stationarity according to the Dickey-Fuller test cannot be rejected.

As well as the adf.test, you should also run a KPSS test to determine whether your data is trend stationary or not, with your null hypothesis being a trend stationary series, and the alternative being the presence of a unit root.

While we would need to see the acf and pacf plots to provide more clarity, in a non-stationary process you would expect to see a small decay in acf for each lag, whereas in a stationary process the same would drop sharply after the first lag.

You need to determine this since decomposing your data series and attempting to remove a trend would be incorrect if your data in fact is difference stationary, in which case differencing the series would be expected to transform your series into a stationary one.

• KPPS test returned a small p-value. So the series is difference stationary. How can I transform the difference stationary series to stationary ? Logarithmic transformation would be a solution? Apr 3, 2017 at 4:50
• If it is difference data, then obtain differences, i.e. diff(serdata$actual,1), diff(serdata$digital,1) Apr 3, 2017 at 8:09
• Sorry, I could not get it. How to proceed with diff() ? Apr 3, 2017 at 11:08
• You need to difference your series. i.e. variable=diff(serdata$actual,1), where you are differencing actual by its first difference, hence the value of 1. Apr 3, 2017 at 13:00 • Now KPPS returned a high p-value. I tried the following Granger tests grangertest(diff(serdata$actual,1)~diff(serdata$digital,1), order = 8, data = serdata) grangertest(diff(serdata$digital,1)~diff(serdata\$actual,1), order = 8, data = serdata)  Both the causality tests returns low p-value. I testes for different lags and all of them have very low p-values Apr 4, 2017 at 5:13