**PS602**

**Testing and Correcting for Autocorrelation in the residuals using Stata**

Serial correlation is a frequent problem in the analysis of time series data. Various factors can produce residuals that are correlated with each other, such as an omitted variable or the wrong functional form. If the problem cannot be resolved by improved model specification, then we need to correct for the influence of the autocorrelation through statistical means.

The basic steps are :

- Set the data set to be a time-series data set
- Run regression
- Examine for serial correlation
- Correct the regression for the serial correlation

For this example we will use the presidentail approval data set: presapp.dta

**Set the data set to be a time-series data
set**

Stata needs to know that the data set is a time series data set. Use the **TSSET **command.

First, create a time variable. Sinvce we have quartely data, and only ear and quarter as variables,
we need to create a variable coded 1,2,...,t for the quarters in the data set. Use the **Generate **command,

gen time = (year - 1949)*4 + quarter

Examine this data with the list command to ensure that it is the series 1 to 148 indicateing that there are 148 quarters in the period 1949-1985.

Now set this as the time variable

tsset time , q

Note that if you examine the data with the

tsset

command, the quarterly dating is off. Stata starts all data sets on at january 1960. This can be alterd, but not here!

Regress Presidential approval on real GNP

reg approval realgnp

**Examine for serial correlation**

In order to see what autocorrelation looks like, examine the residuals.

rvfplot

Since this is a plot of residuals against fitted values, we can see autocorrleation if we examine the rresiduals across time.

predict res, r

graph res time

In order to measure thi autocorrelation, we can calculate the Pearson's r between the residuals and their lagged values

predict res, r

gen lres = res[_n-1]

(to observe lagged data)list res lres

corr res lres

After running the regression, save the residuals and then enter the Durbin-Watson Statistic command.

dwstat

The DW shows strong autocorrelation. This is also reflected in the Runs or (Runs of Signs) test.

runtest res

In addition, you can examine the correlogram of the residuals. This will sho significant autocorrleation at fifferent lags. The correlogram is an essential tool of Bob-Jenkins style time series analysis.

ac res

Stata also provides the Portmanteau test for white noise, which is a Chi-square test.

wntestq res

**Correct the regression for the serial correlation**

Correcting for autocorrelation is easy with STATA. Run the analysis with the Prais-Winston command, specifying the Cochran-Orcutt option.

prais approval realgnp, corc

This gives us results that are substantially different from the original results.

Note the following points in the estimation:

- the number of iterations
- the final value for rho
- the difference in the estimates (both coefficients and t-values)
- the change in r-square
- the transformed Durbin-Watson

Additional Information on Stata commands. (Taken from the Stata help information)

For additional information about how Stata treats time series data, here is the Stata
help for **TSSET**

The Stata information on Prais-Winston estimation options.