*! version 1.0.1
help hamiltonfilter

Title

Calculates the Hamilton Filter for a Single Time Series or for a Panel Dataset

Syntax

hamiltonfilter
varname
[if] [in]
, options

stub(string) designates a string name from which new variable names will be created

frequency(string) specifies the units of the time variable: monthly, quarterly or yearly

You must tsset or xtset your data before using hamiltonfilter;
see [TS] tsset and [XT] xtset.

The hamiltonfilter command works for both Time series and Panel data.

varname may contain time-series operators; see tsvarlist.

by is not allowed with hamiltonfilter; see [D] by for more details on by.

Description

hamiltonfilter calculates the Hamilton filter for a single time series or for a panel
dataset. The command uses the Hamilton filter to separate a time series into trend and
cyclical components. The Hamilton filter is utilized as an alternative to the Hodrick-Prescott
high-pass filter. The theory behind the command hamiltonfilter is provided by
Hamilton (2017). The paper also gives the criticisms of the Hodrick-Prescott filter
and explains why the Hamilton filter is a superior alternative.

Options

stub(string) designates a string name from which new variable names will be
created. To form this option, you put inside the brackets a string name (without the
double quotes). Then new variable names will be created from this string. You must
specify this option in order to get a result. Hence this option is required.

frequency(string) specifies the units of the time variable. To form this
option, you put inside the brackets one of the following string names (without the
double quotes): monthly, quarterly or yearly. If your data have a monthly
frequency, you indicate the frequency(string) option as
frequency(monthly), if you have quarterly data, you specify
frequency(quarterly) and if you have yearly data, you define
frequency(yearly). You must indicate this option in order to get a
result. Hence this option is required.

Stored results

hamiltonfilter stores the following in r():

Macros

r(cyclevar) variable containing estimates of the cyclical component

r(trendvar) variable containing estimates of the trend component

r(varlist) original time-series variable

r(frequency) units of the time variable

Examples

Before beginning the estimations, we use the set more off instruction to tell
Stata not to pause when displaying the output.

set more off

We illustrate the use of the command hamiltonfilter with the dataset hamiltonfilterdmonthly.dta.
This dataset contains monthly data for the USA from 1999m1 to 2017m9.

use http://fmwww.bc.edu/repec/bocode/h/hamiltonfilterdmonthly.dta, clear

Next we describe the dataset to see the definition of each variable. We see that we have monthly
seasonally adjusted data on Real Personal Consumption Expenditures (RPCE) for the USA.

describe

Now let us compute the Hamilton filter. We begin by indicating the name of the command hamiltonfilter, followed
by the variable for which we want to calculate the Hamilton filter. This variable is named
lgrpcecm in this dataset. Then we specify the option stub() in which we put the
string "hamfmo" without the double quotes. This option is required. We finish by indicating
the option frequency() in which we put the string "monthly" without the double quotes. This
option is also required.

hamiltonfilter lgrpcecm, stub(hamfmo) frequency(monthly)

To see the variables that the command has created, we type:

describe

We notice that two more variables have been generated in our dataset: hamfmo_trend
and hamfmo_cycle which respectively correspond to the trend and cycle
of lgrpcecm. By default, these variables are respectively labeled
as "lgrpcecm Trend from the Hamilton Filter, Monthly"
and "lgrpcecm Cycle from the Hamilton Filter, Monthly".

Next we summarize our variables of interest to see their descriptive statistics.

summarize lgrpcecm hamfmo_trend hamfmo_cycle

Now we plot the lgrpcecm variable with its Hamilton filtered trend hamfmo_trend by
using the command [TS] tsline. In this last command, we utilize the following
options: we put the legend at 6 o'clock, the legend in one column, specify the lines to be thick
and give a name to our graph. See the command [TS] tsline for more details.

tsline lgrpcecm hamfmo_trend, legend(pos(6)) legend(col(1)) lwidth(thick thick) name(trendmo, replace)

In this graph the variable lgrpcecm is in blue and the variable hamfmo_trend is in red as indicated
by the legend. There are some missing values at the beginning of the trend variable. These values were created in the
computation process of the Hamilton filter. If we have data with a long temporal depth, this is generally not a big problem.

Let us now plot the cyclical component named hamfmo_cycle of the variable lgrpcecm.

tsline hamfmo_cycle, lwidth(thick) name(cyclemo, replace)

If we want to close all the previously created graphs, we type:

graph close _all

Now we illustrate how to use of the command hamiltonfilter with quarterly data. We open the dataset hamiltonfilterdquarterly.dta.
This dataset contains quarterly data for the USA from 1947q1 to 2017q3.

use http://fmwww.bc.edu/repec/bocode/h/hamiltonfilterdquarterly.dta, clear

We describe the dataset to see the definition of each variable. We see that we have quarterly
seasonally adjusted data on the Real Gross Domestic Product (GDP) of the USA.

describe

We compute the Hamilton filter for this dataset.

hamiltonfilter lgrgdpusaq, stub(hamfqt) frequency(quarterly)

We plot the lgrgdpusaq variable with its Hamilton filtered trend hamfqt_trend.

tsline lgrgdpusaq hamfqt_trend, legend(pos(6)) legend(col(1)) lwidth(thick thick) name(trendqt, replace)

We see that the trend fits very well the variable.

Next we plot the cyclical component.

tsline hamfqt_cycle, lwidth(thick) name(cycleqt, replace)

Let us now compare the Hamilton filter with the Hodrick-Prescott filter. First, we calculate the
Hodrick-Prescott filter for our variable of interest named lgrgdpusaq. We compute both its cyclical
and trend components. We use the Official Stata command tsfilter hp to achieve this.

tsfilter hp lgrgdpusaq_hp_cylce = lgrgdpusaq, trend(lgrgdpusaq_hp_trend)

Second, we plot the trend components of the Hamilton filter and the Hodrick-Prescott filter respectively.

tsline hamfqt_trend lgrgdpusaq_hp_trend, legend(pos(6)) legend(col(1)) lwidth(thick thick) name(trendcomparqt, replace)

Third, we plot the cyclical components of the Hamilton filter and the Hodrick-Prescott filter respectively.

tsline hamfqt_cycle lgrgdpusaq_hp_cylce, legend(pos(6)) legend(col(1)) lwidth(thick thick) name(cylcecomparqt, replace)

Fourth, we close all the previously created graphs.

graph close _all

Now we demonstrate how to use of the command hamiltonfilter with yearly data. We open the dataset hamiltonfilterdyearly.dta.
This dataset contains yearly data for the USA from 1947 to 2016.

use http://fmwww.bc.edu/repec/bocode/h/hamiltonfilterdyearly.dta, clear

We describe the dataset to see the definition of each variable. We see that we have yearly
data on the Real Gross Domestic Product (GDP) of the USA.

describe

We compute the Hamilton filter for this dataset.

hamiltonfilter lgrgdpusay, stub(hamfyr) frequency(yearly)

We plot the lgrgdpusay variable with its Hamilton filtered trend hamfyr_trend.

tsline lgrgdpusay hamfyr_trend, legend(pos(6)) legend(col(1)) lwidth(thick thick) name(trendyr, replace)

We plot the cyclical component.

tsline hamfyr_cycle, lwidth(thick) name(cycleyr, replace)

Let us elucidate how to use the Hamilton filter with time-series operators; see tsvarlist for more
details. We compute the Hamilton filter for a 2-period lead of our variable of interest named lgrgdpusay.

hamiltonfilter F2.lgrgdpusay, stub(hamfyrtso) frequency(yearly)

We plot the F2.lgrgdpusay variable with its Hamilton filtered trend hamfyrtso_trend.

tsline F2.lgrgdpusay hamfyrtso_trend, legend(pos(6)) legend(col(1)) lwidth(thick thick) name(trendyrtso, replace)

We plot the cyclical component.

tsline hamfyrtso_cycle, lwidth(thick) name(cycleyrtso, replace)

If we want to close all the previously created graphs, we type:

graph close _all

Now we exhibit how to utilize the command hamiltonfilter with yearly panel data. We open the dataset hamiltonfilterdgseven.dta.
This dataset contains yearly panel data for the G7 countries from 1950 to 2014.

use http://fmwww.bc.edu/repec/bocode/h/hamiltonfilterdgseven.dta, clear

We describe the dataset to see the definition of each variable. We see that we have yearly panel
data on the Real Gross Domestic Product (GDP) of the G7 countries.

describe

We compute the Hamilton filter for this dataset.

hamiltonfilter lgrgdpolev, stub(hamfyrpan) frequency(yearly)

We plot the lgrgdpolev variable with its Hamilton filtered trend hamfyrpan_trend by
using the command [TS] tsline. In addition to our usual options for this last command, we
augment the following options: we specify that we want the graphs by country and indicate the major
ticks plus labels for the x and y axes. We can enlarge the figure window to see the graphs very well.

tsline lgrgdpolev hamfyrpan_trend, legend(pos(6)) legend(col(1)) lwidth(thick thick) name(trendyrpan, replace) by(country) ylabel(25(3)31) xlabel(1950(20)2014)

We plot the cyclical component.

tsline hamfyrpan_cycle, lwidth(thick) name(cycleyrpan, replace) by(country) xlabel(1950(20)2014)

Let us explain how to use the Hamilton filter with the if qualifier; see if for more
details. We compute the Hamilton filter for the G7 countries after the "first oil shock" of our variable of interest named lgrgdpolev.

hamiltonfilter lgrgdpolev if year > 1973, stub(hamfyrpanos) frequency(yearly)

We plot the lgrgdpolev variable with its Hamilton filtered trend hamfyrpanos_trend for the same period.

tsline lgrgdpolev hamfyrpanos_trend if year > 1973, legend(pos(6)) legend(col(1)) lwidth(thick thick) name(trendyrpanos, replace) by(country) ylabel(25(3)31) xlabel(1973(10)2014)

We plot the cyclical component for the same period.

tsline hamfyrpanos_cycle if year > 1973, lwidth(thick) name(cycleyrpanos, replace) by(country) xlabel(1973(10)2014)

If we want to close all the previously created graphs, we type:

graph close _all

The hamiltonfilter command works when there are missing values for an entire country in a panel data of countries. As
an illustration, suppose that the data for Japan are missing. Thus, we type:

use http://fmwww.bc.edu/repec/bocode/h/hamiltonfilterdgseven.dta, clear

`"replace lgrgdpolev = . if country == "Japan""'

We compute the Hamilton filter for this new dataset.

hamiltonfilter lgrgdpolev, stub(hamfyrpmiss) frequency(yearly)

We plot the lgrgdpolev variable with its Hamilton filtered trend hamfyrpmiss_trend. We notice
that the graphic for Japan is empty. This, because the Hamilton Filter was not computed for Japan.

tsline lgrgdpolev hamfyrpmiss_trend, legend(pos(6)) legend(col(1)) lwidth(thick thick) name(trendyrpmiss, replace) by(country) ylabel(25(3)31) xlabel(1950(20)2014)

We plot the cyclical component. We notice that the graphic for Japan is empty. This, because the Hamilton Filter
was not computed for Japan.

tsline hamfyrpmiss_cycle, lwidth(thick) name(cycleyrpmiss, replace) by(country) xlabel(1950(20)2014)

If we want to close all the previously created graphs, we type:

graph close _all

The hamiltonfilter command also works when the panel data are [TS] tsset or [XT] xtset, but one
or more entries of the panel identifier are dropped. As an illustration, assume that the data for the first country, in
the alphabetical order, of the G7 countries (Canada) are dropped. Hence, we type:

use http://fmwww.bc.edu/repec/bocode/h/hamiltonfilterdgseven.dta, clear

drop if id == 1

We compute the Hamilton filter for this new dataset.

hamiltonfilter lgrgdpolev, stub(hamfyrpdro) frequency(yearly)

We plot the lgrgdpolev variable with its Hamilton filtered trend hamfyrpdro_trend. We notice that the
graphic for Canada is missing. This, because the computation of the Hamilton Filter was not performed for Canada.

tsline lgrgdpolev hamfyrpdro_trend, legend(pos(6)) legend(col(1)) lwidth(thick thick) name(trendyrpdro, replace) by(country) ylabel(25(3)31) xlabel(1950(20)2014)

We plot the cyclical component. We notice that the graphic for Canada is missing. This, because the
computation of the Hamilton Filter was not performed for Canada.

tsline hamfyrpdro_cycle, lwidth(thick) name(cycleyrpdro, replace) by(country) xlabel(1950(20)2014)

If we want to close all the previously created graphs, we type:

graph close _all

References

Hamilton, James D.: 2017, "Why You Should Never Use the Hodrick-Prescott Filter", Department of Economics, UC San Diego, Working Paper.

Hamilton, James D.: 2018, "Why You Should Never Use the Hodrick-Prescott Filter", The Review of Economics and Statistics 100(5), 831-843.

Author

Diallo Ibrahima Amadou

CERDI, University of Clermont Auvergne

26 Avenue Leon Blum

63000 Clermont-Ferrand

France

E-Mail: zavren@gmail.com

Also see

Online: help for [TS] tsfilter, tsfilter hp, [TS] tsline