Learning Econometrics with GAUSS
Total Page:16
File Type:pdf, Size:1020Kb
Learning Econometrics with GAUSS by Ching-Fan Chung Institute of Economics, Academia Sinica ♠ · ♥ · ♦ · ♣ ii Contents 1 Introduction 1 1.1 Getting Started with GAUSS . 2 1.1.1 Executing DOS Commands in GAUSS . 3 1.1.2 Some GAUSS Keystrokes . 3 1.1.3 A Note on Computer Memory . 4 1.2 The GAUSS Editor . 4 1.3 GAUSS Statements . 5 1.3.1 Some Syntax Rules . 6 1.3.2 Two Types of Errors . 6 2 Data Input and Output 9 2.1 ASCII Files . 9 2.1.1 ASCII Data Files . 10 2.1.2 ASCII Output Files . 11 2.1.3 Other Commands Related to ASCII Output Files . 12 2.1.4 An Example . 13 2.2 Matrix Files . 15 3 Basic Algebraic Operations 17 3.1 Arithmetic Operators . 17 3.2 Element-by-Element Operations . 17 3.3 Other Arithmetic Operators . 18 3.4 Priority of the Arithmetic Operators . 19 3.5 Matrix Concatenation and Indexing Matrices . 20 4 GAUSS Commands 23 4.1 Special Matrices . 23 4.2 Simple Statistical Commands . 23 4.3 Simple Mathematical Commands . 24 4.4 Matrix Manipulation . 24 4.5 Basic Control Commands . 25 4.6 Some Examples . 26 4.7 Character Matrices and Strings . 34 4.7.1 Character Matrices . 34 4.7.2 Strings . 35 4.7.3 The Data Type . 36 4.7.4 Three Useful GAUSS Commands . 36 5 GAUSS Program for Linear Regression 41 5.1 A Brief Review . 41 5.1.1 The Ordinary Least Squares Estimation . 41 5.1.2 Analysis of Variance . 42 5.1.3 Durbin-Watson Test Statistic . 43 5.2 The Program . 44 5.3 The ‘ols’ Command . 48 iii iv CONTENTS 5.4 Linear Restrictions . 53 5.5 Chow Test for Structural Changes . 55 6 Relational Operators and Logic Operators 59 6.1 Relational Operators . 59 6.2 Logic Operators . 60 6.3 Conditional Statements . 60 6.4 Row-Selectors: the ‘selif’ and ‘delif’ Commands . 62 6.5 Dummy Variables in Linear Regression Models . 62 6.5.1 Binary Dummy Variables . 62 6.5.2 The Polychotomous Case . 67 6.5.3 The Piecewise Linear Regression Model . 71 7 Iteration with Do-Loops 75 7.1 Do-loops . 75 7.2 Some Statistics Related to the OLS Estimation . 80 7.2.1 The Heteroscedasticity Problem . 80 7.2.2 The Autocorrelation Problem . 82 7.2.3 Structural Stability . 85 8 GAUSS Procedures: Basics 87 8.1 Structural Programming . 92 8.2 Accessing Global Variables Directly . 93 8.3 Calling Other Procedures in a Procedure . 94 8.4 String Inputs . 96 8.5 Functions: Simplified Procedures . 98 8.6 Keywords: Specialized Procedures? .............................. 99 9 GAUSS Procedures: The Library System and Compiling 101 9.1 Autoloading and the Library Files . 101 9.2 The ‘GAUSS.LCG’ Library File for Extrinsic GAUSS Commands . 102 9.3 The ‘USER.LCG’ Library File for User-Defined Procedures . 102 9.4 Other Library Files . 102 9.5 On-Line Help: Seeking Information as the Autoloader . 103 9.6 Compiling? ........................................... 103 9.7 The External and Declare Commands . 104 10 Nonlinear Optimization 107 10.1 Newton’s Method . 107 10.1.1 The Computation of Gradients . 108 10.1.2 The Computation of Hessian . 109 10.1.3 Quasi-Newton Method . 109 10.1.4 Newton’s Method for Maximum Likelihood Estimation . 110 10.1.5 The Computation of the Step Length . 111 10.2 A GAUSS Program for Nonlinear Minimization: NLOPT . 111 10.2.1 Changing Options . 115 CONTENTS v 10.2.2 Analytic Gradients and Analytic Hessian . 116 10.2.3 Imposing Restrictions . 120 10.2.4 Additional Options . 123 10.2.5 Run-Time Option Switching . 123 10.2.6 Global Variable List . 125 A Drawing Graphs for the Simple Linear Regression Model 300 B GAUSS Data Set (GDS) Files 311 B.1 Writing Data to a New GAUSS Data Set . 311 B.2 Reading the GAUSS Data Set . 314 B.2.1 Using Variable Names . 315 B.3 Reading and Processing Data with Do-Loops . 315 B.3.1 The ‘readr’ and the ‘writer’ Commands and Do-Loop . 317 B.3.2 The ‘seekr’ Command . 318 B.4 GAUSS Commands That Are Related to GDS Files . 318 B.4.1 Sorting the GDS File . 319 B.4.2 The ‘ols’ Command and the GDS File . 319 B.5 Revising GDS Files . 320 B.6 Reading and Writing Small GDS Files . 320 B.7 The ATOG Program? ...................................... 321 B.7.1 The Structure of the Source ASCII file . 322 vi CONTENTS Chapter 1 Introduction GAUSS is a computer programming language. We use it to write programs, i.e., collections of commands. Each command in a GAUSS program directs a computer to do one mathematical or statistical operation. The main difference between the GAUSS language and the other statistics packages is that using GAUSS requires us to know more about the mathematical derivation underlying each statistical procedure because we have to translate it into GAUSS language. The advantage of using a programming language is the maximum flexibility it allows for. There is virtually no limit in what we can do with GAUSS. The main difference between GAUSS and other programming languages, such as Fortran, C, Pascal, etc. is that the basic elements for mathematical operations in GAUSS are matrices. This is a powerful feature that can save us from many programming details and allow us to concentrate more on statistics and econometrics. Let’s use an example to explain this feature. Consider the following equation: 1 y = X*b + e; /* 1.1 */ If we see equation /* 1.1*/ in an econometrics class, then it is quite natural to associate it with the follow- ing “handwritten” equation: y = Xβ + εε. (1.1) It is almost like our instinct to think.