You are on page 1of 18

Time Series Tricks http://econometricstutorial.

com/2015/05/time-series-tricks/

HOME ABOUT ME

ECONOMETRICS TUTORIAL for


STATA Search Here...

You are here: Home » Time CHECK OUT MY RECENT ARTICLES:


Series » Time Series Tricks

Time Series on Stata: Forecasting by


Time Smoothing
July 28, 2015

Series A multi- variate way of modeling time series:


VAR

Tricks July 12, 2015

Updated on May 16, 2015 Model stationary and non-stationary series


By Michela Leave a on Stata
comment June 14, 2015

When you
deal with YOUR OPINIONS

time MICHELA on Time Series on Stata: Forecasting by


Smoothing
series data,
MICHELA on Instrumental Variables: Find the Bad
whatever Guys on Stata

data you Gatsby on Time Series on Stata: Forecasting by


Smoothing
have, this is
ALL you have
ALL YOU NEED TO KNOW
to know to

1 of 18 26/1/20, 8:45 pm
Time Series Tricks http://econometricstutorial.com/2015/05/time-series-tricks/

handle it in
This blog is intented for students that want to
July 2015 (2)

Stata. At
learn Stata in a nutshell. June 2015 (1)

least, for
Michela Guicciardi (c) Copyright 2015
May 2015 (1) Mansar Theme. Powered by WordPress.

April 2015 (3)


now.
March 2015 (10)

Good morning guys! February 2015 (2)

Today I am going to

talk again about time


KNOWLEDGE BY TOPIC
series data but in a
Data Mining (7)
more practical and

useful way. As I Econometrics (7)


remembered you
Time Series (5)
there, in order to

work on time series,

you first must tsset


FIND WHAT YOU NEED
your data to advice

Stata to recognize
AIC append arima autocorrelation
them.
BIC change format cluster command

Let’s open a proper correlogram difference dummy variables


dataset: econometrics forecast format GUI
editors fe
Hausman heteroskedasticity IV
use http://www.stata-

press.com/data/r12 ivreg2 label lag long missing data


/wpi1.dta OLS panel panel data predict properties

The first thing you


reg regression results review robust Stata
have to control for,
sysuse tables time series tsset twoway
variable varsoc vce WLS
once you have tsset

your data, is that

2 of 18 26/1/20, 8:45 pm
Time Series Tricks http://econometricstutorial.com/2015/05/time-series-tricks/

there are no gaps in [DO YOU WANT TO BE UPDATED? -----> SIGN UP]

the time series (like a


Hi guys!
missing quarter or If you are tired to go on the blog and discover I didn't posted

month). You can anything new from your last visit (my fault!), this is your best
solution:
easily notice it, if this
Sign up, subscribe and be notified when I create new
is the case in your
contents.
data, because Stata No more wonderings!

will inform you that


Join 25 other subscribers
the time variable has

gaps once you Email Address

called the tsset
Subscribe
command. How can

you fix this? Use the

command tsfill to fill

in the gap in the

time series.

..Then you can start

play a bit!

Autocorrelation
and Cross-
Correlation
We can start by

exploring

autocorrelation and

cross-correlation.

Autocorrelation

3 of 18 26/1/20, 8:45 pm
Time Series Tricks http://econometricstutorial.com/2015/05/time-series-tricks/

Autocorrelation

represents the

correlation between

a variable and its

previous values,  you

can use the ac and

pac commands to

investigate it. ac

produces a

correlogram (a graph

of autocorrelations)

with pointwise

confidence intervals

that is based on

Bartlett’s formula for

MA(q) processes. In

our case, it shows

the correlation

between the current

value of the

logarithmic

transformation of wpi

and its value ten lags

ago. It can be used

to define the q in

MA(q) only in

stationary series. pac

produces a partial

correlogram (a graph

of partial

autocorrelations) with

4 of 18 26/1/20, 8:45 pm
Time Series Tricks http://econometricstutorial.com/2015/05/time-series-tricks/

confidence intervals

calculated using a

standard error of

1/sqrt(n). The

residual variances for

each lag may

optionally be

included on the

graph. It shows also

the correlation

between the current

value of the series

and its value ten

quarters ago, but

without the effect of

the nine previous

lags. It can be used

to define the p in

AR(p) only in

stationary series. So

if you want you can

type:

ac ln_wpi, lags(10)

And below you can

find  the output you

will obtain.. I

personally dislike and

find confusing this

way to visualize

autocorrelations.

5 of 18 26/1/20, 8:45 pm
Time Series Tricks http://econometricstutorial.com/2015/05/time-series-tricks/

If you agree with me,

I suggest you to use

the corrgram

command that

creates a table in

which are

shown both ac and

pac, graphically and

numerically. If you

don’t want to see the

graphs you can add

the noplot options. If

you want to reduce

the number of lags

displayed, the lags()

option is what you

need instead. In our

example, I typed:

corrgram ln_wpi,

lags(10)

Apart for AC and

PAC, this command

6 of 18 26/1/20, 8:45 pm
Time Series Tricks http://econometricstutorial.com/2015/05/time-series-tricks/

displays the Box-

Pierce’Q statistic,

which tests the null

hypothesis that all

correlation up to lag

k are equal to 0. This

series shows

significant

autocorrelation given

that the p-value is

less than 0.05.

therefore, we can

reject the null that all

lags are not

autocorrelated. The

graphic view of the

AC shows a slow

decay in the trend,

suggesting non-

stationarity. The

graphic view of the

PAC instead shows

no spikes after the

third lag, suggesting

that all other lags are

mirrors of the third

one. Another easy

way to see that the

series is non-

stationary is by

plotting it.

7 of 18 26/1/20, 8:45 pm
Time Series Tricks http://econometricstutorial.com/2015/05/time-series-tricks/

twoway (tsline ln_wpi)

As we can observe,

the series displays a

clear increasing trend

so it cannot be

stationary at all.

Cross-
correlation

If you want to

explore the

relationship between

two time series, use

the command xcorr,

making sure that you

always list the

independent variable

first and the

dependent variable

second. You can

specify several

options for this

command that allow

you to graphically

visualize better the

relationship. I, for

example, typed:

8 of 18 26/1/20, 8:45 pm
Time Series Tricks http://econometricstutorial.com/2015/05/time-series-tricks/

xcorr ln_wpi wpi,

lags(10)

xlabel(-10(1)10,grid)

If you need to see

the table, just put the

table option instead

of the xlabel() one.

Of course, given that

I only have one time

series and its

logarithmic

transformation, the

cross correlation is

almost useless

because, as we can

expect, the

relationship across

the two is positive

and reaches a peak

in zero.

Lag Selection
Criteria
There is something

you cannot

9 of 18 26/1/20, 8:45 pm
Time Series Tricks http://econometricstutorial.com/2015/05/time-series-tricks/

underestimate when

using time series

data that   is the lag

selection. Too many

lags could indeed

increase the error in

the forecast whereas

too few could leave

out relevant

information. There

are some information

criterion procedures

to help come up with

a proper selection of

lags. The most

commonly used are:

Schwarz’s Bavesian

info criteria (BIC), the

Akaike’s information

criteria (AIC) and the

Hannan and Quin.

The criterion is that

you select the model

with the smaller BIC

or AIC. I usually

trusted   the BIC nore

to choose the lag but

there’s a useful

command in Stata

that allows you to

plot them all in the

10 of 18 26/1/20, 8:45 pm
Time Series Tricks http://econometricstutorial.com/2015/05/time-series-tricks/

same table and to

freely choose among

them.

In order to explore

this issue, let’s use

the following, more

complete, dataset:

use http://www.stata-

press.com/data/r11

/lutkepohl2.dta

Now let’s use the

varsoc command to

see all the selection

criterion procedures:

Varsoc ln_inc

ln_consump,

maxlag(10)

When the three

criteria all agree, the

selection is clear. But

sometimes it can

happen to get

conflicting results. In

this case, HQIC and

SBIC suggest a lag of

11 of 18 26/1/20, 8:45 pm
Time Series Tricks http://econometricstutorial.com/2015/05/time-series-tricks/

2 whereas AIC a lag

of 2. There are

several papers on the

argument so, if you

need to be sure on

what to believe, you

are recommended to

consult them.

Unit Root Test


The most important

thing to check with

time series data is

the presence of a

unit root in the

series.   In this

section, we

demonstrate how to

evaluate if the series

has a unit root.

Having a unit root in

a series means that

there is more than

one trend. Let’s look

at logarithmic

transformation of

income across time

and test for unit

root.

line ln_inv qtr

12 of 18 26/1/20, 8:45 pm
Time Series Tricks http://econometricstutorial.com/2015/05/time-series-tricks/

One way to deal with

unit roots is by using

The Dickey-Fuller

test, one of the most

commonly used tests

for stationarity. The

null hypothesis is

that the series has a

unit root. The Stata

command is:

dfuller ln_inv, lag(5)

The test statistic

shows that the

investment series

have a unit root, it

lies within the

acceptance region.

One way to deal with

13 of 18 26/1/20, 8:45 pm
Time Series Tricks http://econometricstutorial.com/2015/05/time-series-tricks/

stochastic trends

(unit root) is by

taking the first

difference of the

variable (second test

above). In this case,

we still fail to reject

the null hypothesis!

Panicked? Of course,

not! If we reduce the

lags from 5 to 2, you

can see that the

series has no more a

unit root. Try to

believe it!

If you are curious

and you want to

observe the

associated regression

when doing the

Dickey-Fuller test,

you can specify the

regress option.

Another useful one is

trend, the trend

option allows you to

include a time trend

term in the

associated

regression. In our

case:

14 of 18 26/1/20, 8:45 pm
Time Series Tricks http://econometricstutorial.com/2015/05/time-series-tricks/

dfuller dln_inv, lag(2)

trend regress 

As you can observe,

we can reject the

hypothesis of a time

trend in the series.

Wait a second, how

could it be possible?

Well, remember that

now we are working

on the first

differenced series of

investments, which

plot is no more the

one above but this

one:

Does it make sense

now? I hope so.

Useful tip: For panel

data unit root tests,

see Stata’s

“xtunitroot”

15 of 18 26/1/20, 8:45 pm
Time Series Tricks http://econometricstutorial.com/2015/05/time-series-tricks/

command.

Ok guys, for today it’s

all. Next time we will

run into AR, MA,

ARIMA and VAR. Stay

tuned!

Michela Guicciardi

Tags: AC AIC autocorrelation


BIC correlogram

cross-correlation DF

Dickey Fuller difference graph

lag lag selection criteria PAC

Q test time series trend tsfill

tsset twoway unit root

unit root test varsoc xcorr

Tweet

By
Michela
https://www.linkedin.c
/pub/michela-
guicciardi
/82/839/6a1

http://www.econometric

16 of 18 26/1/20, 8:45 pm
Time Series Tricks http://econometricstutorial.com/2015/05/time-series-tricks/

You May Also Like

Time A multi-
Series on variate
Stata: way of
Forecastingmodeling
by time
Smoothing series:
VAR

Leave a Reply

Your email address will not


be published. Required fields
are marked *

Comment

Name *

17 of 18 26/1/20, 8:45 pm
Time Series Tricks http://econometricstutorial.com/2015/05/time-series-tricks/

Email *

Website

Post Comment

Notify me of follow-up
comments by email.

Notify me of new posts


by email.

18 of 18 26/1/20, 8:45 pm

You might also like