Hands-On Intermediate Econometrics Using R
Hands-On Intermediate Econometrics Using R
ECONOMETRICS USING R
Templates for Extending Dozens
of Practical Examples
This page intentionally left blank
HANDS-ON INTERMEDIATE
ECONOMETRICS USING R
Templates for Extending Dozens
of Practical Examples
Hrishikesh D Vinod
Fordham University in New York, USA
World Scientific
NEW JERSEY • LONDON • SINGAPORE • BEIJING • SHANGHAI • HONG KONG • TA I P E I • CHENNAI
Published by
World Scientific Publishing Co. Pte. Ltd.
5 Toh Tuck Link, Singapore 596224
USA office: 27 Warren Street, Suite 401-402, Hackensack, NJ 07601
UK office: 57 Shelton Street, Covent Garden, London WC2H 9HE
For photocopying of material in this volume, please pay a copying fee through the Copyright
Clearance Center, Inc., 222 Rosewood Drive, Danvers, MA 01923, USA. In this case permission to
photocopy is not required from the publisher.
ISBN-13 978-981-281-885-0
ISBN-10 981-281-885-5
Printed in Singapore.
To the memory of
and
v
September 15, 2008 14:20 9in x 6in B-645 fm FA
Preface
vii
September 15, 2008 14:20 9in x 6in B-645 fm FA
In the new century, fast linking of all scientists through the Internet
is revolutionizing the exchange of scientific ideas including data and
software tools. This book is intended to highlight and welcome these
exciting changes. Since open source free software is particularly powerful
for exchange of software tools, the book embraces R (distributed under
Free Software Foundation’s GNU GENERAL PUBLIC LICENSE Version
2, June 1991).
Why R?
All empirical scientists should learn more than one programming language,
not some point-and-click software restricted by the imagination of the pro-
grammer. R is based on an earlier language called S developed in Bell Labs
around 1979 (by John Chambers and others with whom I often used to
have lunch). It is an object oriented Unix type language, where all inputs,
data and outputs from R are ‘objects’ inside the computer. Also, R is
an “interpreted programming language” not “compiled” language similar
to FORTRAN or GAUSS (reviewed in Vinod, J2000c) or many older lan-
guages. Hence, all R commands (or command sets enclosed in curly braces)
are implemented as they are typed (or ‘sourced’).
An advantage of Unix type languages is that numerical results are iden-
tical (platform-independent), and yet R is available for a wide variety of
UNIX platforms, Windows and Mac systems. This provides one kind of
flexibility in using R. SPlus is the commercial version of S, and R is the
free version. Since Splus programs work in R, this allows second kind of
flexibility for R. Third kind of flexibility of R arises from the fact that it
is open source, meaning that every line of the code is available for any
researcher anywhere to see, modify, criticize, etc. A black box of hidden
code is unavoidable for any proprietary commercial software. Not so for R.
September 15, 2008 14:20 9in x 6in B-645 fm FA
Preface ix
Political Economy and many top journals are following the lead of AER and
require authors to submit both data and software. I hope that this book
facilitates the movement toward replicable econometrics by making R more
easy and fun. Replication with free R will be much simpler and fast for
international students, eliminating possible delays in paying for software in
different currencies.
Packages within R
R comes with the “base” package and additional contributed “contrib”
packages have to be explicitly requested from the R website. The con-
tributed packages are written by statisticians, computer scientists and
econometricians from around the world and contain several functions for
doing operations plus many illustrative data sets. They are freely available
on demand for noncommercial purposes. All R packages are required to
follow a nice readable format describing all their functions. Each function
is described with details of inputs and outputs, references to the literature
and generally does have examples. If one is curious to know exactly how the
function was implemented, usually all one has to do is write the name of the
function and the entire program can be seen. The user then has an option
to modify the program, except that beginners will find that many programs
are hard to understand, since their expert authors use their ingenuity in
writing efficient (fast, not necessarily easy to read) code. By contrast, the
code in the snippets in this book should be easy to read.
September 15, 2008 14:20 9in x 6in B-645 fm FA
Preface xi
Preface xiii
Foreword
xv
September 15, 2008 14:20 9in x 6in B-645 fm FA
Foreword xvii
Contents
Preface vii
Foreword xv
xix
September 15, 2008 14:20 9in x 6in B-645 fm FA
Contents xxi
Contents xxiii
Contents xxv
Contents xxvii
Appendix 479
References 485
Index 505