# Solution for non-stationarity in time series analysis in STATA

The previous article based on the Dickey Fuller test established that GDP time series data is non-stationary. This prevented time series analysis from proceeding further. Therefore, in this article possible solution to non-stationarity is explained.

## First differencing series

If a time series has a unit root problem, the first difference of such time series is ‘stationary’. Therefore, the solution here is to take the first difference of the GDP time series. The first difference of a time series is the series of changes from one period to the next. If Yt denotes the value of the time series Y at period t, then the first difference of Y at period t is equal to Yt-Yt-1. In STATA, the first difference of Y is expressed as DIFF(Y) or D of time series variable. Therefore, to generate the difference between current and previous values use the “D” operator. In the present case, to create a difference of time series GDP:

1. Click on ‘Data’ (ribbon).
2. Select ‘Create’ or ‘Change Data’.
3. Click in ‘Create New Variable’.
4. Dialogue box will appear as below (Figure 1).

OR

Use STATA command:

`generate gap_d1 = d1.gdp`

Fill the dialogue box with Variable name ‘gdp_d1’ and specify a value or an expression. Remember for differencing for one time, use prefix ‘d1’. Similarly, when differencing is to be done for second time then use prefix ‘d2’. For instance, the name for variable GDP is given ‘gdp_d1’ and in content of variable option, applied prefix ‘d1 to variable ‘gdp’ for first differencing.

After clicking ‘OK’  as shown in the figure above, the new variable appears on the Data editor i.e. ‘gdp_d1’ as differencing of variable ‘gdp’. The figure below shows the resulting window.

Thus first order differencing of time series variable GDP has been made. Now check again if it is stationary or not. In order to do so, perform both graphical and Dickey Fuller test. The procedure to construct the graph is explained in the previous article.

### Graphical representation

The figure below shows that the variable ‘gdp_d1’ is still reflecting an upward trend. However, the trend decreases when compared to original GDP variable. Therefore, GDP time series has been diminished to around zero. This implies that this time series can have a constant mean and variance. However, a mere graphical representation is an intuitive step thus, perform the formal tests of stationarity.

### Dickey Fuller test

As mentioned previously, Dickey Fuller test is conducted to examine stationarity in time series data. To examine the stationarity of differenced GDP time series, follow the steps mentioned in previous article:

1. Click on ‘Statistics’ (in ribbon of Output Window).
2. Select ‘Time Series’.
3. Select ‘Tests’.
4. Select ‘Augmented Dicky Fuller Test’.

OR

Use STATA Command:

`dfuller gap_d1, trend lags(0)`

Based on the equation entered, a dialogue box will appear as shown in the figure below. Select the variable ‘gdp_d1’ in ‘Variable’ option, mark on ‘Include trend term in regression’. Click on ‘OK’.

The results for Dickey Fuller test for gdp_d1 will appear in output window. The figure below reflects the results of the Dickey Fuller test of stationarity.

The output window of STATA reflects on the Dickey Fuller test results for variable ‘gdp_d1’. To examine the presence of stationarity, review two value; ‘Z(t)’ and Mackinnon p-value for ‘Z(t)’. Here it can be seen that, ‘Z(t)’ value is -7.073 which is a large negative number (as compared to ‘z(t)’ for ‘gdp’) and p-value is also found significant. Thus, null hypothesis of Dickey Fuller test is rejected.  Therefore,  the first differenced time series GDP is stationary.

Optional: Check the stationarity by taking lags as done in previous article.

## Second differencing series

There can be cases when the first differencing of such time series also turns out as non-stationary. Therefore, the solution here is to take the second difference of the GDP time series. In STATA, the second difference of ‘Y’ is expressed as ‘D2(Y)’. Similar to the above case, second differencing of GDP can be calculated as:

1. Click on ‘Data’ (ribbon).
2. Select ‘Create’ or ‘Change Data’.
3. Click in ‘Create New Variable’.
4. Dialogue box will appear as shown below.

OR

Use STATA command:

` generate gap_d2 = d2.gdp`

Fill the below dialogue box with variable name ‘gdp_d2’ and specify a value or an expression. For differencing for second time, use prefix ‘d2’. For instance, the variable has been named as ‘gdp_d2’ and in content of variable option, applied prefix ‘d2’ for second differencing.

After clicking ‘OK’ as shown in the above figure, check the ‘Data Editor’ window for new variable ‘gdp_d2’ as second differencing of variable ‘gdp’. This is shown in the figure below.

After performing second order differencing of time series variable GDP, test if it is stationary or not. Check this with the graph as well as Dickey Fuller test.

### Graphical representation

As the figure below shows, ‘gdp_d2’ is not reflecting a upward trend anymore. Thus, GDP time series data has been diminished to around zero. This implies that this time series definitely has a constant mean and variance. Further, to prove this point, perform the Dickey Fuller test to test stationarity.

### Dickey Fuller test

To examine the stationarity of second differenced GDP time series, again follow the steps mentioned in previous article.

1. Click on ‘Statistics’ (in ribbon of Output Window).
2. Select ‘Time Series’.
3. Select ‘Tests’.
4. Select ‘Augmented Dicky Fuller Test’.

OR

Use STATA Command:

`dfuller gap_d2, trend lags(0)`

In the dialog box below, select the variable ‘gdp_d2’ in ‘Variable’ option, select ‘Include trend term in regression’. Click on ‘OK’. The results for Dickey Fuller test for ‘gdp_d2’ will appear in output window. Figure 9: Dialogue box for Augmented Dickey Fuller unit root test

The figure below shows the results of the unit root test. Figure 10: Augmented Dickey Fuller unit root test results

Based on the previous interpretations, reviewing the ‘Z(t)’ and Mackinnon p-value for ‘Z(t)’, it can be stated that ‘Z(t)’ value is a large negative number (as compared to ‘z(t)’ for ‘gdp’ and ‘gdp_d1’) and p-value for the same is also significant. Therefore, the null hypothesis is rejected.

## Further time series analyses in STATA

This article explained the procedure to remove non-stationarity from time series data. It also tested the data using graphical representation and Dickey Fuller Unit Root test. The next article discusses about the concept of ARIMA and how to process it in STATA.

### Priya Chetty

Partner at Project Guru
Priya is a master in business administration with majors in marketing and finance. She is fluent with data modelling, time series analysis, various regression models, forecasting and interpretation of the data. She has assisted data scientists, corporates, scholars in the field of finance, banking, economics and marketing.

#### Latest posts by Priya Chetty (see all)

##### 9 thoughts on “Solution for non-stationarity in time series analysis in STATA”
1. Djamel 1 year ago

Hi! Priya. Thank you For this article which is of great use. Besides, I would like to know why you applied a second differenced to the serie while the Dickey Fuller test said it was stationary. Thanks

• Priya 1 year & 4 months ago

The second differencing has been done just to show how to do it in STATA, that is for demonstrating the required steps. Since GDP series turned out to be stationary at the first difference, there is no need to check for stationarity at second differences. But we often encounter time series data for variables which remain non stationary even after first differencing. In those cases, we need to proceed one step further and perform Dickey-Fuller test after differencing the data series at the first difference

• Djamel 1 year & 4 months ago

Thank you!

2. Anna 1 year & 2 months ago

i like your article, it’s so much helpful, very few writers can explain so well as you’ve done. Thank you very much.

3. Shamika 1 year & 2 months ago

after correcting non stationary how do we proceed the regression

4. Brian 1 year ago

5. Yoshida 1 year ago

Tried both but they all increase in the same proportion.

6. Nina 8 months ago

Hi! Priya. Thank you For this article which is of great use. Besides, I would like to know why you add the trend option while doing the Dickey Fuller test for d2 of GDP as from the figure it seems the upward trend disaprear. Thanks. Another question is the figure shows that the data seems to be much more volatile during financial crisis. Why it is still tested to be stationary? Thanks again.

7. Divya 8 months ago

The trend disappears only after the second differentiation as indicated by the graphical representation of second differencing of GDP. However, the graphical representation of the first differencing of GDP depicts an upward trend. The dickey fuller test for second difference is based upon the trend depicted in the first differencing of GDP.

Also, applying the stationarity test is important because sample dataset includes the economic variables of the Indian economy from 1996 to 2016. The period of financial crisis is just a sub-sample of this dataset. Stationarity is used to analyse the movement of the variables over a period of time. Moreover, the time series need to be stationary for correlation and regression analysis. Otherwise, these results would be misleading.