Quantitative Economics With R: A Data Science Approach [1st Edition] 9811520348, 9789811520341, 9789811520358

This book provides a contemporary treatment of quantitative economics, with a focus on data science. The book introduces

2,967 568 19MB

English Pages XV, 326 [323] Year 2020

Report DMCA / Copyright

DOWNLOAD FILE

Polecaj historie

Quantitative Economics With R: A Data Science Approach [1st Edition]
 9811520348,  9789811520341,  9789811520358

Table of contents :
Acknowledgements......Page 6
Contents......Page 7
About the Author......Page 13
Part I Introduction to the Book and the Data Software......Page 14
1.1 A Data Science Approach......Page 15
1.2.3 Part 3: Mathematical Preliminaries for Data Analysis......Page 16
1.2.7 Part 7: Introduction to Statistical/Machine Learning from Data......Page 17
1.6 An Overview of Typical R Code......Page 18
1.7 Resources......Page 20
2.2 R and RStudio......Page 21
2.4 Use a Script......Page 22
2.5.1 Vectors......Page 23
2.5.2 Matrices......Page 25
2.5.4 Lists......Page 26
2.6 Toy Example: Net Present Value......Page 27
2.7.1 Data Analysis Workflow......Page 28
2.7.3 Input and Wrangle Synthetic Data......Page 29
2.7.4 Five Data Verbs......Page 32
2.7.5 Graphs......Page 35
2.7.6 Linear Model......Page 36
2.8 Resources......Page 38
Part II Managing and Graphing Data......Page 40
3.2 Data in R or a Package......Page 41
3.3 Data in a csv File......Page 43
3.4 Data in a Stata File......Page 44
3.5 Data from the World Development Indicators......Page 45
3.6 Resources......Page 46
4.2 Example: Anscombe's Synthetic Data......Page 47
4.3 Example: Carbon and Livelihoods Data......Page 50
4.4.1 Getting the Data......Page 56
4.4.2 Graphing the Data......Page 58
4.4.3 Mapping the Data......Page 61
4.5 Resources......Page 64
5.2 Simple Example with Synthetic Data......Page 70
5.3 Example: Medici Network......Page 75
5.4 Example: Bali Terrorist Network......Page 77
5.5 Simulating Network Formation......Page 79
5.6 Example: Electrical Automotive Goods Production Network......Page 82
5.7 Resources......Page 87
Part III Mathematical Preliminaries for Data Analysis......Page 90
6.2 Making Your Own Functions in R......Page 91
6.3 Plotting Functions with the Curve Function......Page 92
6.4 Statistical Loss Functions......Page 93
6.5 Supply and Demand......Page 94
6.6 Cobb–Douglas Production Function......Page 95
6.7 Resources......Page 99
7.2 Simple Toy Example......Page 101
7.3 Example: Global Carbon Stocks......Page 102
7.4.1 Numerical Simulation......Page 104
7.4.2 Example: North Sea Herring......Page 106
7.5.1 Commodity Residual Transformation Function......Page 109
7.5.2 Stock Pollutant......Page 110
7.5.3 Firm's Choice of Commodity Q Given a Tax on Waste S......Page 111
7.5.4 What Is the Optimal Tax?......Page 112
7.6 Resources......Page 113
8.2 Simple Statistics with Vectors......Page 117
8.3 Matrix Operations......Page 119
8.4 Example: Poverty Rate and Relative Income......Page 120
8.5 Resources......Page 123
Part IV Inference from Data......Page 124
9.2.1 Sample......Page 125
9.2.2 Binomial Distribution......Page 127
9.2.3 Function for Binomial Distribution......Page 130
9.3 Sampling Distribution......Page 131
9.3.1 Six-Sided Dice Simulation......Page 132
9.3.2 Function for Sampling Distribution......Page 135
9.3.3 Sampling Distribution for the T-Statistic......Page 136
9.3.4 Inference from One Sample......Page 138
9.3.5 Confidence Intervals......Page 139
9.4 Bootstrap......Page 142
9.4.1 Function to Understand Bootstrap......Page 145
9.5 Permutation Tests......Page 147
9.6 Example: Verizon......Page 151
9.6.1 Permutation Test......Page 153
9.6.2 Bootstrapping Confidence Intervals......Page 154
9.7 Cautionary Example with Synthetic Data......Page 156
9.8 Resources......Page 157
10.2 Causal Graphs and Potential Outcomes......Page 158
10.2.1 Simple Example with Synthetic Data......Page 159
10.2.2 Randomized Assignment of Treatment (Causal Graphs)......Page 161
10.2.3 Randomized Assignment of Treatment (Potential Outcomes)......Page 162
10.2.4 Covariate Adjustment......Page 165
10.2.5 Selecting Regressors by Statistical Significance......Page 168
10.3.1 Example: Anchoring......Page 170
10.3.2 Example: Women as Policymakers......Page 174
10.3.3 Example: Educational Programme......Page 177
10.3.4 Example: Star......Page 180
10.4 Matching......Page 183
10.4.1 Simple Example with Synthetic Data......Page 184
10.4.2 Example: Labour Training Programme......Page 188
10.4.3 Sensitivity Analysis......Page 192
10.4.4 Example: Lead Exposure......Page 195
10.4.5 Example: Compensation for Injury......Page 197
10.5.1 Simple Example with Synthetic Data......Page 202
10.5.2 Example: Minimum Legal Drinking Age (MLDA)......Page 203
10.6.1 Example: Scrap Rate and Training......Page 207
10.6.2 Simulation......Page 208
10.6.3 Example: Banks in Business......Page 211
10.7 Example: Manski Bounds for Crime and Laws......Page 213
10.7.1 Bounds with Maryland as Counterfactual......Page 215
10.7.2 Bounds Based on Difference-in-Difference......Page 217
10.8.1 Simulation......Page 220
10.8.2 Example: Demand for Cigarettes......Page 223
10.9 Resources......Page 227
Part V Accessing, Analysing and Interpreting Growth Data......Page 229
11.2 Example: Growth......Page 230
11.3 Example: Production Model and Crosscountry Data......Page 233
11.4 Solow Model Simulation......Page 235
11.5 Romer Model Simulation......Page 238
11.6 Example: Growth in Recent Decades......Page 239
11.7 Resources......Page 246
12.1 Introduction (Institutions and Growth Example)......Page 248
12.2 Geography and Growth......Page 252
12.3 Exclusion Restriction Simulation......Page 255
12.5 Resources......Page 257
Part VI Time Series Data......Page 259
13.2 Simple Example with Synthetic Data......Page 260
13.3 Example: Air Passengers......Page 261
13.4 Example: Stock Market Volatility......Page 262
13.5 Example: Inflation and Unemployment......Page 264
13.6 Example: Historical Unemployment Data......Page 267
13.7 Resources......Page 270
14.2.1 White Noise......Page 273
14.2.2 Autoregressive Model......Page 277
14.2.3 Random Walk......Page 280
14.2.4 Moving Average......Page 281
14.2.5 Autoregressive Moving Average......Page 282
14.3 Example: Forecasting Inflation......Page 285
14.4.1 Simulating Spurious Regression......Page 292
14.4.2 Simulating Cointegration......Page 293
14.4.3 Example: Federal Funds and Bond Rate......Page 296
14.5 Example: Dynamic Causal Effects of Weather......Page 298
14.6 Resources......Page 299
Part VII Introduction to Statistical/Machine Learning from Data......Page 302
15.2 Simple Example with Synthetic Data......Page 303
15.3 Example: GAMS with Wages Data......Page 305
15.4 Example: Housing in Texas......Page 308
15.5 Resources......Page 311
16.2 Simple Tree Example with Synthetic Data......Page 312
16.3 Example: Arsenic in Wells in Bangladesh......Page 315
16.4 Example: Home Mortgage Disclosure Act......Page 319
16.5 Resources......Page 323

Citation preview

Vikram Dayal

Quantitative Economics with R A Data Science Approach

Quantitative Economics with R

Vikram Dayal

Quantitative Economics with R A Data Science Approach

123

Vikram Dayal Indian Economic Service Section Institute of Economic Growth Delhi, India

ISBN 978-981-15-2034-1 ISBN 978-981-15-2035-8 https://doi.org/10.1007/978-981-15-2035-8

(eBook)

© Springer Nature Singapore Pte Ltd. 2020 This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use. The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, expressed or implied, with respect to the material contained herein or for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and institutional affiliations. This Springer imprint is published by the registered company Springer Nature Singapore Pte Ltd. The registered company address is: 152 Beach Road, #21-01/04 Gateway East, Singapore 189721, Singapore

For my old and wonderful friends Ranu, Deepu and Chinna

Acknowledgements

The Institute of Economic Growth gave me a most conducive environment to work on the book. My colleagues have been supportive, two especially so: Prof. Bhavani and Purnamita. Suresh introduced me to R, and over the years shared my enthusiasm for R talk. Rahul provided inspiration with his painting about a sense of wonder, and specific advice on the text. Ranu edited most chapters and followed up on progress. This book presents the work of a global community of scholars, coders and data scientists; not just economists like Acemoglu and Duflo, but statisticians like Paul Rosenbaum, the master of the tidyverse, Hadley Wickham, the deep philosopher of causal inference, Judea Pearl, the guru of networks, Barabasi, the experts in statistical learning, Hastie and Tibshirani. Writing this book led to a greater appreciation of their work and ideas. Nupoor Singh at Springer and anonymous reviewers helped me with initial ideas for the book, and others at Springer facilitated the book very professionally. My parents were encouraging. Varsha was unstinting in her support. Vikram Dayal

vii

Contents

Part I 1

2

Introduction to the Book and the Data Software

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.1 A Data Science Approach . . . . . . . . . . . . . . . . . . . . . . . . . 1.2 Quick Tour of the Book . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2.1 Part 1: Introduction to the Book and the Data Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2.2 Part 2: Managing and Graphing Data . . . . . . . . . . 1.2.3 Part 3: Mathematical Preliminaries for Data Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2.4 Part 4: Inference from Data . . . . . . . . . . . . . . . . . . 1.2.5 Part 5: Accessing, Analysing and Interpreting Growth Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2.6 Part 6: Basic Time Series Data . . . . . . . . . . . . . . . 1.2.7 Part 7: Introduction to Statistical/Machine Learning from Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.3 How to Use the Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.4 Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.5 R Code and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.6 An Overview of Typical R Code . . . . . . . . . . . . . . . . . . . . 1.7 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . RStudio and R . . . . . . . . . . 2.1 Introduction . . . . . . . . 2.2 R and RStudio . . . . . . 2.3 Use Projects . . . . . . . . 2.4 Use a Script . . . . . . . . 2.5 Different Objects in R . 2.5.1 Vectors . . . . . 2.5.2 Matrices . . . . . 2.5.3 Data Frames . . 2.5.4 Lists . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

... ... ...

3 3 4

... ...

4 4

... ...

4 5

... ...

5 5

. . . . . . .

. . . . . . .

. . . . . . .

5 6 6 6 6 8 8

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

9 9 9 10 10 11 11 13 14 14

ix

x

Contents

2.6 2.7

Toy Example: Net Present Value . . . . . . . . The Tidyverse Approach . . . . . . . . . . . . . . 2.7.1 Data Analysis Workflow . . . . . . . . 2.7.2 The Tidyverse Package . . . . . . . . . 2.7.3 Input and Wrangle Synthetic Data . 2.7.4 Five Data Verbs . . . . . . . . . . . . . . 2.7.5 Graphs . . . . . . . . . . . . . . . . . . . . . 2.7.6 Linear Model . . . . . . . . . . . . . . . . 2.8 Resources . . . . . . . . . . . . . . . . . . . . . . . . . References . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

15 16 16 17 17 20 23 24 26 27

3

Getting Data into R . . . . . . . . . . . . . . . . . . . . . . . 3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . 3.2 Data in R or a Package . . . . . . . . . . . . . . . . . 3.3 Data in a csv File . . . . . . . . . . . . . . . . . . . . . 3.4 Data in a Stata File . . . . . . . . . . . . . . . . . . . . 3.5 Data from the World Development Indicators . 3.6 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

31 31 31 33 34 35 36 36

4

Wrangling and Graphing Data . . . . . . . . . . . . . . . . . . . . 4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.2 Example: Anscombe’s Synthetic Data . . . . . . . . . . . 4.3 Example: Carbon and Livelihoods Data . . . . . . . . . . 4.4 Example: WDI Data on CO2 and Per Capita Income 4.4.1 Getting the Data . . . . . . . . . . . . . . . . . . . . . 4.4.2 Graphing the Data . . . . . . . . . . . . . . . . . . . 4.4.3 Mapping the Data . . . . . . . . . . . . . . . . . . . . 4.5 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

37 37 37 40 46 46 48 51 54 59

5

Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2 Simple Example with Synthetic Data . . . . . . . . . . . . . . . . . 5.3 Example: Medici Network . . . . . . . . . . . . . . . . . . . . . . . . . 5.4 Example: Bali Terrorist Network . . . . . . . . . . . . . . . . . . . . 5.5 Simulating Network Formation . . . . . . . . . . . . . . . . . . . . . 5.6 Example: Electrical Automotive Goods Production Network 5.7 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

61 61 62 66 68 70 73 78 80

Part II

. . . . . . . . . .

Managing and Graphing Data

Contents

Part III

xi

Mathematical Preliminaries for Data Analysis 83 83 83 84 85 86 87 91 92

6

Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . 6.2 Making Your Own Functions in R . . . . . . . 6.3 Plotting Functions with the Curve Function 6.4 Statistical Loss Functions . . . . . . . . . . . . . 6.5 Supply and Demand . . . . . . . . . . . . . . . . . 6.6 Cobb–Douglas Production Function . . . . . . 6.7 Resources . . . . . . . . . . . . . . . . . . . . . . . . . References . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

7

Difference Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.2 Simple Toy Example . . . . . . . . . . . . . . . . . . . . . . . . . 7.3 Example: Global Carbon Stocks . . . . . . . . . . . . . . . . . 7.4 Fish . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.4.1 Numerical Simulation . . . . . . . . . . . . . . . . . . . 7.4.2 Example: North Sea Herring . . . . . . . . . . . . . . 7.5 Example: Conrad’s Model of a Stock Pollutant . . . . . . . 7.5.1 Commodity Residual Transformation Function . 7.5.2 Stock Pollutant . . . . . . . . . . . . . . . . . . . . . . . . 7.5.3 Firm’s Choice of Commodity Q Given a Tax on Waste S . . . . . . . . . . . . . . . . . . . . . . . . . . 7.5.4 What Is the Optimal Tax? . . . . . . . . . . . . . . . . 7.6 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. . . . . . . . . .

. 93 . 93 . 93 . 94 . 96 . 96 . 98 . 101 . 101 . 102

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

103 104 105 108

Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . 8.2 Simple Statistics with Vectors . . . . . . . . . . . 8.3 Matrix Operations . . . . . . . . . . . . . . . . . . . . 8.4 Example: Poverty Rate and Relative Income 8.5 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

8

Part IV 9

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

109 109 109 111 112 115 115

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

119 119 119 119 121 124

Inference from Data

Statistical Inference . . . . . . . . . . . . . . . . . . . . . . 9.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . 9.2 Box Models . . . . . . . . . . . . . . . . . . . . . . . 9.2.1 Sample . . . . . . . . . . . . . . . . . . . . 9.2.2 Binomial Distribution . . . . . . . . . . 9.2.3 Function for Binomial Distribution

. . . . . .

xii

Contents

9.3

Sampling Distribution . . . . . . . . . . . . . . . . . . . . 9.3.1 Six-Sided Dice Simulation . . . . . . . . . . 9.3.2 Function for Sampling Distribution . . . . 9.3.3 Sampling Distribution for the T-Statistic 9.3.4 Inference from One Sample . . . . . . . . . 9.3.5 Confidence Intervals . . . . . . . . . . . . . . . 9.4 Bootstrap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.4.1 Function to Understand Bootstrap . . . . . 9.5 Permutation Tests . . . . . . . . . . . . . . . . . . . . . . . 9.6 Example: Verizon . . . . . . . . . . . . . . . . . . . . . . . 9.6.1 Permutation Test . . . . . . . . . . . . . . . . . 9.6.2 Bootstrapping Confidence Intervals . . . . 9.7 Cautionary Example with Synthetic Data . . . . . . 9.8 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

125 126 129 130 132 133 136 139 141 145 147 148 150 151 151

10 Causal Inference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.2 Causal Graphs and Potential Outcomes . . . . . . . . . . . . . . . 10.2.1 Simple Example with Synthetic Data . . . . . . . . . . . 10.2.2 Randomized Assignment of Treatment (Causal Graphs) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.2.3 Randomized Assignment of Treatment (Potential Outcomes) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.2.4 Covariate Adjustment . . . . . . . . . . . . . . . . . . . . . . 10.2.5 Selecting Regressors by Statistical Significance . . . 10.3 Experiments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.3.1 Example: Anchoring . . . . . . . . . . . . . . . . . . . . . . . 10.3.2 Example: Women as Policymakers . . . . . . . . . . . . 10.3.3 Example: Educational Programme . . . . . . . . . . . . . 10.3.4 Example: Star . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.4 Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.4.1 Simple Example with Synthetic Data . . . . . . . . . . . 10.4.2 Example: Labour Training Programme . . . . . . . . . 10.4.3 Sensitivity Analysis . . . . . . . . . . . . . . . . . . . . . . . 10.4.4 Example: Lead Exposure . . . . . . . . . . . . . . . . . . . 10.4.5 Example: Compensation for Injury . . . . . . . . . . . . 10.5 Regression Discontinuity . . . . . . . . . . . . . . . . . . . . . . . . . . 10.5.1 Simple Example with Synthetic Data . . . . . . . . . . . 10.5.2 Example: Minimum Legal Drinking Age (MLDA) .

. . . .

. . . .

. . . .

153 153 154 154

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . . . . . . . . . . . . .

. . . 156 . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . .

157 160 163 165 165 169 172 175 178 179 183 187 190 192 197 197 198

Contents

xiii

10.6 Difference-in-Difference . . . . . . . . . . . . . . . . . . . . 10.6.1 Example: Scrap Rate and Training . . . . . . 10.6.2 Simulation . . . . . . . . . . . . . . . . . . . . . . . . 10.6.3 Example: Banks in Business . . . . . . . . . . . 10.7 Example:Manski Bounds for Crime and Laws . . . . 10.7.1 Bounds with Maryland as Counterfactual . . 10.7.2 Bounds Based on Difference-in-Difference . 10.8 Instrumental Variables . . . . . . . . . . . . . . . . . . . . . . 10.8.1 Simulation . . . . . . . . . . . . . . . . . . . . . . . . 10.8.2 Example: Demand for Cigarettes . . . . . . . . 10.9 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Part V

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

. . . . . . . . . . . .

202 202 203 206 208 210 212 215 215 218 222 222

Accessing, Analysing and Interpreting Growth Data

11 Growth Data and Models . . . . . . . . . . . . . . . . . . . . . . . 11.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 Example: Growth . . . . . . . . . . . . . . . . . . . . . . . . . 11.3 Example: Production Model and Crosscountry Data 11.4 Solow Model Simulation . . . . . . . . . . . . . . . . . . . . 11.5 Romer Model Simulation . . . . . . . . . . . . . . . . . . . 11.6 Example: Growth in Recent Decades . . . . . . . . . . . 11.7 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

227 227 227 230 232 235 237 243 244

12 Growth Causes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12.1 Introduction (Institutions and Growth Example) 12.2 Geography and Growth . . . . . . . . . . . . . . . . . . 12.3 Exclusion Restriction Simulation . . . . . . . . . . . 12.4 Other Support for AJR . . . . . . . . . . . . . . . . . . 12.5 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Part VI

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

245 245 249 252 254 254 254

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

259 259 259 260 262 263 266 269 271

Time Series Data

13 Graphs for Time Series . . . . . . . . . . . . . . . . . . 13.1 Introduction . . . . . . . . . . . . . . . . . . . . . . 13.2 Simple Example with Synthetic Data . . . . 13.3 Example: Air Passengers . . . . . . . . . . . . . 13.4 Example: Stock Market Volatility . . . . . . 13.5 Example: Inflation and Unemployment . . . 13.6 Example: Historical Unemployment Data . 13.7 Resources . . . . . . . . . . . . . . . . . . . . . . . . References . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

xiv

Contents

14 Basic Time Series Models . . . . . . . . . . . . . . . . . . . . . 14.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . 14.2 Simulations . . . . . . . . . . . . . . . . . . . . . . . . . . . 14.2.1 White Noise . . . . . . . . . . . . . . . . . . . . . 14.2.2 Autoregressive Model . . . . . . . . . . . . . . 14.2.3 Random Walk . . . . . . . . . . . . . . . . . . . 14.2.4 Moving Average . . . . . . . . . . . . . . . . . 14.2.5 Autoregressive Moving Average . . . . . . 14.3 Example: Forecasting Inflation . . . . . . . . . . . . . . 14.4 Cointegration . . . . . . . . . . . . . . . . . . . . . . . . . . 14.4.1 Simulating Spurious Regression . . . . . . 14.4.2 Simulating Cointegration . . . . . . . . . . . 14.4.3 Example: Federal Funds and Bond Rate 14.5 Example: Dynamic Causal Effects of Weather . . 14.6 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Part VII

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . .

273 273 273 273 277 280 281 282 285 292 292 293 296 298 299 301

Introduction to Statistical/Machine Learning from Data . . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

305 305 305 307 310 313 313

16 From Trees to Random Forests . . . . . . . . . . . 16.1 Introduction . . . . . . . . . . . . . . . . . . . . . . 16.2 Simple Tree Example with Synthetic Data 16.3 Example: Arsenic in Wells in Bangladesh 16.4 Example: Home Mortgage Disclosure Act 16.5 Resources . . . . . . . . . . . . . . . . . . . . . . . . References . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

315 315 316 318 322 326 326

15 Smoothers and Generalized Additive Models 15.1 Introduction . . . . . . . . . . . . . . . . . . . . . 15.2 Simple Example with Synthetic Data . . . 15.3 Example: GAMS with Wages Data . . . . 15.4 Example: Housing in Texas . . . . . . . . . . 15.5 Resources . . . . . . . . . . . . . . . . . . . . . . . References . . . . . . . . . . . . . . . . . . . . . . . . . . .

About the Author

Vikram Dayal is a Professor at the Institute of Economic Growth, Delhi. He has been using the R software in teaching quantitative economics to diverse audiences, and is the author of the hugely popular SpringerBrief titled An Introduction to R for Quantitative Economics: Graphing, Simulating and Computing. Since its publication in 2015, each of its fourteen chapters has been downloaded at least 4000 times (and still counting) from SpringerLink. One can read the book at https://link.springer.com/ book/10.1007%2F978-81-322-2340-5. He has published research on a range of environmental and developmental issues, from outdoor and indoor air pollution in Goa, India, to tigers and Prosopis juliflora in Ranthambore National Park. He studied economics in India and the USA, and received his doctoral degree from the Delhi School of Economics, University of Delhi.

xv

Part I Introduction to the Book and the Data Software

1

Introduction

1.1

A Data Science Approach

Why a data science approach to economics? Data science involves the intersection of computer science, statistics, which is a discipline that is about learning from data, and a knowledge domain—here, economics. In 2009, Hal Varian, then Google’s chief economist, had provided the reasons for such an approach (McKinsey Quarterly 2009): The ability to take data—to be able to understand it, to process it, to extract value from it, to visualize it, to communicate it—that’s going to be a hugely important skill in the next decades, not only at the professional level but even at the educational level for elementary school kids, for high school kids, for college kids. Because now we really do have essentially free and ubiquitous data. So the complementary scarce factor is the ability to understand that data and extract value from it. I think statisticians are part of it, but it’s just a part. You also want to be able to visualize the data, communicate the data, and utilize it effectively. But I do think those skills—of being able to access, understand, and communicate the insights you get from data analysis—are going to be extremely important.

Note that Varian emphasizes that we need more than statistics or econometrics; we need to visualize and communicate. In this book, we use the free software R (R Core Team 2019) to take a data science approach to quantitative economics. R was always amazing, Hadley Wickham has taken R to new levels by creating a set of packages, the tidyverse. Wickham (Grolemund and Wickham 2017) has thought carefully about the data analysis workflow, and he starts with the need to get the data (as does Varian). Then we spend a great deal of time ‘wrangling’ the data. We then graph the data to understand it, and fit models. We may also use graphs to communicate our analysis.

© Springer Nature Singapore Pte Ltd. 2020 V. Dayal, Quantitative Economics with R, https://doi.org/10.1007/978-981-15-2035-8_1

3

4

1

1.2

Introduction

Quick Tour of the Book

We take a quick tour of the book.

1.2.1

Part 1: Introduction to the Book and the Data Software

Part 1 of this book aims to provide just enough of an introduction to R. Though Wickham’s work is well recognized, I think his contribution is crucial in helping the person who is new to R and has not written code. The use of what Wickham calls data verbs makes necessary wrangling of the data less of a chore; with time, it can be enjoyable. This part should convey some of the essential skills for getting the data in, working with the data and then making graphs with the data. There are ‘Your turn’ activities through the book that will build up skills.

1.2.2

Part 2: Managing and Graphing Data

One feature of data science that Wickham has greatly facilitated is wrangling and graphing. What Wickham grasped is that good visualization more often than not requires having suitably wrangled the data. When we graph data, we learn from data. Deaton (1997, pp. 3–4) explains his approach to analysing data: Rather than starting with the theory, I more often begin with the data and then try to find elementary procedures for describing them in a way that illuminates some aspect of theory or policy. Rather than use the theory to summarize the data through a set of structural parameters, it is sometimes more useful to present features of the data, often through simple descriptive statistics, or through graphical presentations of densities or regression functions, and then to think about whether these features tell us anything useful about the process whereby they were generated.

A few years back David Robinson suggested that we could teach R by taking newcomers through a direct tour of the tidyverse. We start with what has to be done: getting our data into R, before we enjoy the sheer pleasure of the bestselling ggplot2 package, Wickham’s crowning achievement. Modern data science deals with a rich variety of data, and an exciting type of data relates to networks. We start from a small toy network and contemplate the breathtaking complexity of global auto component trade networks.

1.2.3

Part 3: Mathematical Preliminaries for Data Analysis

We can use R for numerical mathematics, and we present some simple mathematics with R in Part 3. We can especially do a lot in R with difference equations.

1.2 Quick Tour of the Book

1.2.4

5

Part 4: Inference from Data

R has for long been the lingua franca of statistics. Statistical and econometric teachers such as Kennedy (2003) have advocated the use of simulation, and it plays a central role in part 4 of the book, and in the first chapter of that part we use simulation to illuminate the central limit theorem. Two simulation-based inferential methods are presented: the bootstrap and randomization inference. Simulation is also used to illuminate causal inference. We begin with a short look at causal graphs and potential outcomes, two frameworks that have greatly clarified issues in causal inference. We aim to understand and see examples of what Angrist and Pischke (2015) call the Furious Five—experiments, regression adjustment, regression discontinuity, difference-in-difference and instrumental variables. In addition, some applications of matching and a brief view of sensitivity analysis and Manski bounds are provided. As a result, the causal inference chapter is the longest in the book.

1.2.5

Part 5: Accessing, Analysing and Interpreting Growth Data

The next Part is devoted to economic growth. Data is visualized to examine the stylized facts of growth, and simple growth theory talks to the data. The second chapter in this Part looks at a key, famous paper by Acemoglu et al. (2001), their bold attempt at quantifying the effect of institutions on growth, and the mini-literature that developed around this paper.

1.2.6

Part 6: Basic Time Series Data

Part 6 deals with time series data. Time series graphs figure prominently in economics in the news. The first chapter deals with graphing time series. The next chapter is a view of basic time series analysis. A section of this chapter uses simulation to illustrate key building blocks of time series models. As in the rest of the book, in this part we consider it important to distinguish between description, prediction and causal inference.

1.2.7

Part 7: Introduction to Statistical/Machine Learning from Data

Varian (2014) wrote an engaging essay titled, Big Data: New Tricks for Econometrics. By now, the paper is old, and so are the tricks. We consider two key tricks of statistical learning: GAMS and random forests. Learning and using these and similar tricks is a big motivation to learn R, which is the software often used to implement such methods.

6

1

1.3

Introduction

How to Use the Book

We can learn R in the same way we would learn a language. We should follow the book with RStudio open, typing in the R code and running it. We should experiment with the code, and see what happens. It is a good idea to use Google when we have doubts. Several Your turn activities are available through the book. Answers to some Your turn activities are at the end of the chapters. In this book, we usually start a topic with a small, easy example, so we can see what is going on. Selected resources for learning are mentioned at the end of chapters; these include online courses and videos. If we are new to R or the tidyverse, we need to spend more time on the initial chapters.

1.4

Help

We can get help on a function in R by typing help followed by the function enclosed in parentheses; for example, help(mean)

opens a help page on that function in RStudio. Typing help.start() and running the command will open a page with hyperlinked manuals and package references in RStudio.

1.5

R Code and Output

In this book, the R code is in typewriter font. The resulting output is also indicated (with double hash) in typewriter font.

1.6

An Overview of Typical R Code

We can get lost in R code because there are so many commands and options; so we take a brief tour to get an overview. Typically, R code takes the form: new object ← function ( object or formula , object information , options ) Not all the above elements come into a given line of code; what we have above is a generalization. An example helps illustrate this more specifically: • Price