Applied Econometrics Using MATLAB
Total Page:16
File Type:pdf, Size:1020Kb
Applied Econometrics using MATLAB James P. LeSage Department of Economics University of Toledo CIRCULATED FOR REVIEW October, 1998 2 Preface This text describes a set of MATLAB functions that implement a host of econometric estimation methods. Toolboxes are the name given by the MathWorks to related sets of MATLAB functions aimed at solving a par- ticular class of problems. Toolboxes of functions useful in signal processing, optimization, statistics, nance and a host of other areas are available from the MathWorks as add-ons to the standard MATLAB software distribution. I use the termEconometrics Toolbox to refer to the collection of function libraries described in this book. The intended audience is faculty and students using statistical methods, whether they are engaged in econometric analysis or more general regression modeling. The MATLAB functions described in this book have been used in my own research as well as teaching both undergraduate and graduate econometrics courses. Researchers currently using Gauss, RATS, TSP, or SAS/IML for econometric programming might nd switching to MATLAB advantageous. MATLAB software has always had excellent numerical algo- rithms, and has recently been extended to include: sparse matrix algorithms, very good graphical capabilities, and a complete set of object oriented and graphical user-interface programming tools. MATLAB software is available on a wide variety of computing platforms including mainframe, Intel, Apple, and Linux or Unix workstations. When contemplating a change in software, there is always the initial investment in developing a set of basic routines and functions to support econometric analysis. It is my hope that the routines in the Econometrics Toolbox provide a relatively complete set of basic econometric analysis tools. The toolbox also includes a number of functions to mimic those available in Gauss, which should make converting existing Gauss functions and ap- plications easier. For those involved in vector autoregressive modeling, a complete set of estimation and forecasting routines is available that imple- ment a wider variety of these estimation methods than RATS software. For example, Bayesian Markov Chain Monte Carlo (MCMC) estimation of VAR i ii models that robustify against outliers and accommodate heteroscedastic dis- turbances have been implemented. In addition, the estimation functions for error correction models (ECM) carry out Johansen’s tests to determine the number of cointegrating relations, which are automatically incorporated in the model. In the area of vector autoregressive forecasting, routines are available for VAR and ECM methods that automatically handle data trans- formations (e.g. di erencing, seasonal di erences, growth rates). This allows users to work with variables in raw levels form. The forecasting functions carry out needed transformations for estimation and return forecasted values in level form. Comparison of forecast accuracy from a wide variety of vector autoregressive, error correction and other methods is quite simple. Users can avoid the dicult task of unraveling transformed forecasted values from alternative estimation methods and proceed directly to forecast accuracy comparisons. The collection of around 300 functions and demonstration programs are organized into libraries that are described in each chapter of the book. Many faculty use MATLAB or Gauss software for research in econometric analysis, but the functions written to support research are often suitable for only a single problem. This is because time and energy (both of which are in short supply) are involved in writing more generally applicable functions. The functions described in this book are intended to be re-usable in any number of applications. Some of the functions implement relatively new Markov Chain Monte Carlo (MCMC) estimation methods, making these accessible to undergraduate and graduate students with absolutely no programming involved on the students part. Many of the automated features available in the vector autoregressive, error correction, and forecasting functions arose from my own experience in dealing with students using these functions. It seemed a shame to waste valuable class time on implementation details when these can be handled by well-written functions that take care of the details. A consistent design was implemented that provides documentation, ex- ample programs, and functions to produce printed as well as graphical pre- sentation of estimation results for all of the econometric functions. This was accomplished using the “structure variables” introduced in MATLAB Version 5. Information from econometric estimation is encapsulated into a single variable that contains “ elds” for individual parameters and statistics related to the econometric results. A thoughtful design by the MathWorks allows these structure variables to contain scalar, vector, matrix, string, and even multi-dimensional matrices as elds. This allows the econometric functions to return a single structure that contains all estimation results. These structures can be passed to other functions that can intelligently de- iii cipher the information and provide a printed or graphical presentation of the results. TheEconometrics Toolbox should allow faculty to use MATLAB in un- dergraduate and graduate level econometrics courses with absolutely no pro- gramming on the part of students or faculty. An added bene t to using MATLAB and theEconometrics Toolbox is that faculty have the option of implementing methods that best re ect the material in their courses as well as their own research interests. It should be easy to implement a host of ideas and methods by: drawing on existing functions in the toolbox, extending these functions, or operating on the results from these functions. As there is an expectation that users are likely to extend the toolbox, examples of how to accomplish this are provided at the outset in the rst chapter. Another way to extend the toolbox is to download MATLAB functions that are avail- able on Internet sites. (In fact, some of the routines in the toolbox originally came from the Internet.) I would urge you to re-write the documentation for these functions in a format consistent with the other functions in the toolbox and return the results from the function in a “structure variable”. A detailed example of how to do this is provided in the rst chapter. In addition to providing a set of econometric estimation routines and doc- umentation, the book has another goal. Programming approaches as well as design decisions are discussed in the book. This discussion should make it easier to use the toolbox functions intelligently, and facilitate creating new functions that t into the overall design, and work well with existing toolbox routines. This text can be read as a manual for simply using the existing functions in the toolbox, which is how students tend to approach the book. It can also be seen as providing programming and design approaches that will help implement extensions for research and teaching of econometrics. This is how I would think faculty would approach the text. Some faculty in Ph.D. programs expect their graduate students to engage in econometric problem solving that requires programming, and certainly this text would eliminate the burden of spending valuable course time on computer pro- gramming and implementation details. Students in Ph.D. programs receive the added bene t that functions implemented for dissertation work can be easily transported to another institution, since MATLAB is available for almost any conceivable hardware/operating system environment. Finally, there are obviously omissions, bugs and perhaps programming errors in theEconometrics Toolbox . This would likely be the case with any such endeavor. I would be grateful if users would notify me when they en- counter problems. It would also be helpful if users who produce generally useful functions that extend the toolbox would submit them for inclusion. iv Much of the econometric code I encounter on the internet is simply too speci c to a single research problem to be generally useful in other appli- cations. If econometric researchers are serious about their newly proposed estimation methods, they should take the time to craft a generally useful MATLAB function that others could use in applied research. Inclusion in theEconometrics Toolbox would also have the bene t of introducing the method to faculty teaching econometrics and their students. The latest version of theEconometrics Toolbox functions can be found on the Internet at: http://www.econ.utoledo.edu under the MATLAB gallery icon. Instructions for installing these functions are in an Appendix to this text along with a listing of the functions in the library and a brief description of each. Contents 1 Introduction 1 2 Regression using MATLAB 5 2.1 Design of the regression library . 6 2.2 The ols function . 8 2.3 Selecting a least-squares algorithm . 12 2.4 Using the results structure . 17 2.5 Performance pro ling the regression toolbox . 28 2.6 Using the regression library . 30 2.6.1 A Monte Carlo experiment . 31 2.6.2 Dealing with serial correlation . 32 2.6.3 Implementing statistical tests . 38 2.7 Chapter summary . 41 Chapter 2 Appendix 43 3 Utility Functions 47 3.1 Calendar function utilities . 47 3.2 Printing and plotting matrices . 51 3.3 Data transformation utilities . 67 3.4 Gauss functions . 71 3.5 Wrapper functions . 75 3.6 Chapter summary . 76 Chapter 3 Appendix 79 4 Regression Diagnostics 83 4.1 Collinearity diagnostics and procedures . 83 4.2 Outlier diagnostics and procedures . 97 4.3 Chapter summary . 103 v vi CONTENTS Chapter 4 Appendix 105 5 VAR and Error Correction Models 107 5.1 VAR models . 107 5.2 Error correction models . 116 5.3 Bayesian variants . 128 5.3.1 Theil-Goldberger estimation of these models . 141 5.4 Forecasting the models . 142 5.5 Chapter summary . 148 Chapter 5 Appendix 151 6 Markov Chain Monte Carlo Models 155 6.1 The Bayesian Regression Model . 158 6.2 The Gibbs Sampler . 160 6.2.1 Monitoring convergence of the sampler .