lab9: panel data estimation in r · zhaopeng qu (nanjing university) lab9: panel data estimation in...

35
Lab9: Panel Data Estimation in R Introduction to Econometrics,Fall 2019 Zhaopeng Qu Nanjing University 12/24/2020 Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 1 / 35

Upload: others

Post on 23-Jan-2021

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Lab9: Panel Data Estimation in RIntroduction to Econometrics,Fall 2019

Zhaopeng Qu

Nanjing University

12/24/2020

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 1 / 35

Page 2: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Section 1

Panel Data

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 2 / 35

Page 3: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

introduction

# load the data set and get an overviewlibrary(AER)library(stargazer)library(plm)data("Fatalities")

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 3 / 35

Page 4: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Panel Data# obtain the dimension and inspect the structuredim(Fatalities)

## [1] 336 34str(Fatalities)

## 'data.frame': 336 obs. of 34 variables:## $ state : Factor w/ 48 levels "al","az","ar",..: 1 1 1 1 1 1 1 2 2 2 ...## $ year : Factor w/ 7 levels "1982","1983",..: 1 2 3 4 5 6 7 1 2 3 ...## $ spirits : num 1.37 1.36 1.32 1.28 1.23 ...## $ unemp : num 14.4 13.7 11.1 8.9 9.8 ...## $ income : num 10544 10733 11109 11333 11662 ...## $ emppop : num 50.7 52.1 54.2 55.3 56.5 ...## $ beertax : num 1.54 1.79 1.71 1.65 1.61 ...## $ baptist : num 30.4 30.3 30.3 30.3 30.3 ...## $ mormon : num 0.328 0.343 0.359 0.376 0.393 ...## $ drinkage : num 19 19 19 19.7 21 ...## $ dry : num 25 23 24 23.6 23.5 ...## $ youngdrivers: num 0.212 0.211 0.211 0.211 0.213 ...## $ miles : num 7234 7836 8263 8727 8953 ...## $ breath : Factor w/ 2 levels "no","yes": 1 1 1 1 1 1 1 1 1 1 ...## $ jail : Factor w/ 2 levels "no","yes": 1 1 1 1 1 1 1 2 2 2 ...## $ service : Factor w/ 2 levels "no","yes": 1 1 1 1 1 1 1 2 2 2 ...## $ fatal : int 839 930 932 882 1081 1110 1023 724 675 869 ...## $ nfatal : int 146 154 165 146 172 181 139 131 112 149 ...## $ sfatal : int 99 98 94 98 119 114 89 76 60 81 ...## $ fatal1517 : int 53 71 49 66 82 94 66 40 40 51 ...## $ nfatal1517 : int 9 8 7 9 10 11 8 7 7 8 ...## $ fatal1820 : int 99 108 103 100 120 127 105 81 83 118 ...## $ nfatal1820 : int 34 26 25 23 23 31 24 16 19 34 ...## $ fatal2124 : int 120 124 118 114 119 138 123 96 80 123 ...## $ nfatal2124 : int 32 35 34 45 29 30 25 36 17 33 ...## $ afatal : num 309 342 305 277 361 ...## $ pop : num 3942002 3960008 3988992 4021008 4049994 ...## $ pop1517 : num 209000 202000 197000 195000 204000 ...## $ pop1820 : num 221553 219125 216724 214349 212000 ...## $ pop2124 : num 290000 290000 288000 284000 263000 ...## $ milestot : num 28516 31032 32961 35091 36259 ...## $ unempus : num 9.7 9.6 7.5 7.2 7 ...## $ emppopus : num 57.8 57.9 59.5 60.1 60.7 ...## $ gsp : num -0.0221 0.0466 0.0628 0.0275 0.0321 ...

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 4 / 35

Page 5: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Panel Data# basci summaryhead(Fatalities)

## state year spirits unemp income emppop beertax baptist mormon drinkage## 1 al 1982 1.37 14.4 10544.15 50.69204 1.539379 30.3557 0.32829 19.00## 2 al 1983 1.36 13.7 10732.80 52.14703 1.788991 30.3336 0.34341 19.00## 3 al 1984 1.32 11.1 11108.79 54.16809 1.714286 30.3115 0.35924 19.00## 4 al 1985 1.28 8.9 11332.63 55.27114 1.652542 30.2895 0.37579 19.67## 5 al 1986 1.23 9.8 11661.51 56.51450 1.609907 30.2674 0.39311 21.00## 6 al 1987 1.18 7.8 11944.00 57.50988 1.560000 30.2453 0.41123 21.00## dry youngdrivers miles breath jail service fatal nfatal sfatal## 1 25.0063 0.211572 7233.887 no no no 839 146 99## 2 22.9942 0.210768 7836.348 no no no 930 154 98## 3 24.0426 0.211484 8262.990 no no no 932 165 94## 4 23.6339 0.211140 8726.917 no no no 882 146 98## 5 23.4647 0.213400 8952.854 no no no 1081 172 119## 6 23.7924 0.215527 9166.302 no no no 1110 181 114## fatal1517 nfatal1517 fatal1820 nfatal1820 fatal2124 nfatal2124 afatal## 1 53 9 99 34 120 32 309.438## 2 71 8 108 26 124 35 341.834## 3 49 7 103 25 118 34 304.872## 4 66 9 100 23 114 45 276.742## 5 82 10 120 23 119 29 360.716## 6 94 11 127 31 138 30 368.421## pop pop1517 pop1820 pop2124 milestot unempus emppopus gsp## 1 3942002 208999.6 221553.4 290000.1 28516 9.7 57.8 -0.02212476## 2 3960008 202000.1 219125.5 290000.2 31032 9.6 57.9 0.04655825## 3 3988992 197000.0 216724.1 288000.2 32961 7.5 59.5 0.06279784## 4 4021008 194999.7 214349.0 284000.3 35091 7.2 60.1 0.02748997## 5 4049994 203999.9 212000.0 263000.3 36259 7.0 60.7 0.03214295## 6 4082999 204999.8 208998.5 258999.8 37426 6.2 61.5 0.04897637

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 5 / 35

Page 6: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Panel Data# basic summarysummary(Fatalities)

## state year spirits unemp income## al : 7 1982:48 Min. :0.790 Min. : 2.400 Min. : 9514## az : 7 1983:48 1st Qu.:1.300 1st Qu.: 5.475 1st Qu.:12086## ar : 7 1984:48 Median :1.670 Median : 7.000 Median :13763## ca : 7 1985:48 Mean :1.754 Mean : 7.347 Mean :13880## co : 7 1986:48 3rd Qu.:2.013 3rd Qu.: 8.900 3rd Qu.:15175## ct : 7 1987:48 Max. :4.900 Max. :18.000 Max. :22193## (Other):294 1988:48## emppop beertax baptist mormon## Min. :42.99 Min. :0.04331 Min. : 0.0000 Min. : 0.1000## 1st Qu.:57.69 1st Qu.:0.20885 1st Qu.: 0.6268 1st Qu.: 0.2722## Median :61.36 Median :0.35259 Median : 1.7492 Median : 0.3931## Mean :60.81 Mean :0.51326 Mean : 7.1569 Mean : 2.8019## 3rd Qu.:64.41 3rd Qu.:0.65157 3rd Qu.:13.1271 3rd Qu.: 0.6293## Max. :71.27 Max. :2.72076 Max. :30.3557 Max. :65.9165#### drinkage dry youngdrivers miles breath## Min. :18.00 Min. : 0.00000 Min. :0.07314 Min. : 4576 no :181## 1st Qu.:20.00 1st Qu.: 0.00000 1st Qu.:0.17037 1st Qu.: 7183 yes:155## Median :21.00 Median : 0.08681 Median :0.18539 Median : 7796## Mean :20.46 Mean : 4.26707 Mean :0.18593 Mean : 7891## 3rd Qu.:21.00 3rd Qu.: 2.42481 3rd Qu.:0.20219 3rd Qu.: 8504## Max. :21.00 Max. :45.79210 Max. :0.28163 Max. :26148#### jail service fatal nfatal sfatal## no :241 no :273 Min. : 79.0 Min. : 13.00 Min. : 8.0## yes : 94 yes : 62 1st Qu.: 293.8 1st Qu.: 53.75 1st Qu.: 35.0## NA's: 1 NA's: 1 Median : 701.0 Median : 135.00 Median : 81.0## Mean : 928.7 Mean : 182.58 Mean :109.9## 3rd Qu.:1063.5 3rd Qu.: 212.00 3rd Qu.:131.0## Max. :5504.0 Max. :1049.00 Max. :603.0#### fatal1517 nfatal1517 fatal1820 nfatal1820## Min. : 3.00 Min. : 0.00 Min. : 7.0 Min. : 0.00## 1st Qu.: 25.75 1st Qu.: 4.00 1st Qu.: 38.0 1st Qu.: 11.00## Median : 49.00 Median :10.00 Median : 82.0 Median : 24.00## Mean : 62.61 Mean :12.26 Mean :106.7 Mean : 33.53## 3rd Qu.: 77.00 3rd Qu.:15.25 3rd Qu.:130.2 3rd Qu.: 44.00## Max. :318.00 Max. :76.00 Max. :601.0 Max. :196.00#### fatal2124 nfatal2124 afatal pop## Min. : 12.0 Min. : 1.00 Min. : 24.6 Min. : 479000## 1st Qu.: 42.0 1st Qu.: 13.00 1st Qu.: 90.5 1st Qu.: 1545251## Median : 97.5 Median : 30.00 Median : 211.6 Median : 3310503## Mean :126.9 Mean : 41.38 Mean : 293.3 Mean : 4930272## 3rd Qu.:150.5 3rd Qu.: 49.00 3rd Qu.: 364.0 3rd Qu.: 5751735## Max. :770.0 Max. :249.00 Max. :2094.9 Max. :28314028#### pop1517 pop1820 pop2124 milestot## Min. : 21000 Min. : 21000 Min. : 30000 Min. : 3993## 1st Qu.: 71750 1st Qu.: 76962 1st Qu.: 103500 1st Qu.: 11692## Median : 163000 Median : 170982 Median : 241000 Median : 28484## Mean : 230816 Mean : 249090 Mean : 336390 Mean : 37101## 3rd Qu.: 270500 3rd Qu.: 308311 3rd Qu.: 413000 3rd Qu.: 44140## Max. :1172000 Max. :1321004 Max. :1892998 Max. :241575#### unempus emppopus gsp## Min. :5.500 Min. :57.80 Min. :-0.123641## 1st Qu.:6.200 1st Qu.:57.90 1st Qu.: 0.001182## Median :7.200 Median :60.10 Median : 0.032413## Mean :7.529 Mean :59.97 Mean : 0.025313## 3rd Qu.:9.600 3rd Qu.:61.50 3rd Qu.: 0.056501## Max. :9.700 Max. :62.30 Max. : 0.142361##

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 6 / 35

Page 7: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Example: Traffic Deaths and Alcohol Taxes

# define the fatality rateFatalities$fatal_rate <- Fatalities$fatal /

Fatalities$pop * 10000

# subset the dataFatalities1982 <- subset(Fatalities, year == "1982")Fatalities1988 <- subset(Fatalities, year == "1988")

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 7 / 35

Page 8: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Example: Traffic Deaths and Alcohol Taxes

# estimate simple regression models using 1982 and 1988 datafatal1982_mod <- lm(fatal_rate ~ beertax, data=Fatalities1982)fatal1988_mod <- lm(fatal_rate ~ beertax, data=Fatalities1988)

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 8 / 35

Page 9: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Example: Traffic Deaths and Alcohol Taxes

# estimate simple regression models using 1982 and 1988 datacoeftest(fatal1982_mod, vcov. = vcovHC, type = "HC1")

#### t test of coefficients:#### Estimate Std. Error t value Pr(>|t|)## (Intercept) 2.01038 0.14957 13.4408 <2e-16 ***## beertax 0.14846 0.13261 1.1196 0.2687## ---## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 9 / 35

Page 10: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Example: Traffic Deaths and Alcohol Taxes

# estimate simple regression models using 1982 and 1988 datacoeftest(fatal1988_mod, vcov. = vcovHC, type = "HC1")

#### t test of coefficients:#### Estimate Std. Error t value Pr(>|t|)## (Intercept) 1.85907 0.11461 16.2205 < 2.2e-16 ***## beertax 0.43875 0.12786 3.4314 0.001279 **## ---## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 10 / 35

Page 11: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Example: Traffic Deaths and Alcohol Taxes(1982)

# plot the observations and add the estimated regression line for 1982 dataplot(x = Fatalities1982$beertax,

y = Fatalities1982$fatal_rate,xlab = "Beer tax (in 1988 dollars)",ylab = "Fatality rate (fatalities per 10000)",main = "Traffic Fatality Rates and Beer Taxes in 1982",ylim = c(0, 4.5),pch = 20,col = "steelblue")

abline(fatal1982_mod, lwd = 1.5)

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11 / 35

Page 12: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Example: Traffic Deaths and Alcohol Taxes(1982)

0.0 0.5 1.0 1.5 2.0 2.5

01

23

4Traffic Fatality Rates and Beer Taxes in 1982

Beer tax (in 1988 dollars)

Fata

lity

rate

(fa

talit

ies

per

1000

0)

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 12 / 35

Page 13: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Example: Traffic Deaths and Alcohol Taxes(1988)

# plot observations and add estimated regression line for 1988 dataplot(x = Fatalities1988$beertax,

y = Fatalities1988$fatal_rate,xlab = "Beer tax (in 1988 dollars)",ylab = "Fatality rate (fatalities per 10000)",main = "Traffic Fatality Rates and Beer Taxes in 1988",ylim = c(0, 4.5),pch = 20,col = "steelblue")

abline(fatal1988_mod, lwd = 1.5)

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 13 / 35

Page 14: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Example: Traffic Deaths and Alcohol Taxes(1988)

0.0 0.5 1.0 1.5 2.0

01

23

4Traffic Fatality Rates and Beer Taxes in 1988

Beer tax (in 1988 dollars)

Fata

lity

rate

(fa

talit

ies

per

1000

0)

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 14 / 35

Page 15: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Panel Data with Two Time Periods: “Before andAfter” Comparisons

𝐹𝑎𝑡𝑎𝑙𝑖𝑡𝑦𝑅𝑎𝑡𝑒𝑖1988−𝐹𝑎𝑡𝑎𝑙𝑖𝑡𝑦𝑅𝑎𝑡𝑒𝑖1982 = 𝛽1(𝐵𝑒𝑒𝑟𝑇 𝑎𝑥𝑖1988−𝐵𝑒𝑒𝑟𝑇 𝑎𝑥𝑖1982)+𝑢𝑖1988−𝑢𝑖1982

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 15 / 35

Page 16: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Before and After# compute the differencesdiff_fatal_rate <- Fatalities1988$fatal_rate

- Fatalities1982$fatal_rate

## [1] -2.12836 -2.49914 -2.38405 -1.86194 -2.17448 -1.64695 -2.03333 -2.53197## [9] -2.17484 -2.61759 -1.43840 -1.75269 -1.65119 -2.06811 -2.22523 -2.48916## [17] -1.46127 -1.49778 -1.14669 -1.52682 -1.38156 -2.84379 -1.80089 -3.15528## [25] -1.64151 -3.18907 -1.82489 -1.42799 -4.21784 -1.22932 -2.16589 -2.20238## [33] -1.49155 -3.26215 -1.94080 -1.53127 -1.10063 -2.26286 -2.13256 -2.26152## [41] -2.74034 -1.89345 -2.05769 -1.60503 -1.74848 -2.29475 -1.62242 -3.94118diff_beertax <- Fatalities1988$beertax

- Fatalities1982$beertax

## [1] -1.53937948 -0.21479714 -0.65035802 -0.10739857 -0.21479714 -0.22434367## [7] -0.17303102 -1.07398570 -2.72076368 -0.40274465 -0.18854415 -0.30913246## [13] -0.37589499 -0.48400956 -0.21599045 -0.86634845 -0.80548930 -0.24105012## [19] -0.28639618 -0.54565394 -0.34606203 -1.14594281 -0.34661219 -0.34644747## [25] -0.37589499 -0.16109785 -0.48329356 -0.08949881 -0.24164678 -0.11933175## [31] -1.43198097 -0.42959428 -0.42959428 -0.86634845 -0.22519094 -0.28639618## [37] -0.17422435 -2.06205249 -0.71837711 -0.33809426 -0.43317422 -0.35684010## [43] -0.71151549 -0.75894988 -0.23175895 -0.47636396 -0.17303102 -0.05369928# estimate a regression using differenced datafatal_diff_mod <- lm(diff_fatal_rate ~ diff_beertax)coeftest(fatal_diff_mod, vcov = vcovHC, type = "HC1")

#### t test of coefficients:#### Estimate Std. Error t value Pr(>|t|)## (Intercept) 1.85907 0.11461 16.2205 < 2.2e-16 ***## diff_beertax 0.43875 0.12786 3.4314 0.001279 **## ---## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 16 / 35

Page 17: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Before and After

# plot the differenced dataplot(x = diff_beertax,

y = diff_fatal_rate,xlab = "Change in beer tax (in 1988 dollars)",ylab = "Change in fatality rate (fatalities per 10000)",main = "Changes in Traffic Fatality Rates and Beer Taxes in 1982-1988",xlim = c(-0.6, 0.6),ylim = c(-1.5, 1),pch = 20,col = "steelblue")

# add the regression line to plotabline(fatal_diff_mod, lwd = 1.5)

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 17 / 35

Page 18: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Before and After

0.0 0.5 1.0 1.5 2.0

1.5

2.0

2.5

3.0

Changes in Traffic Fatality Rates and Beer Taxes in 1982−1988

Change in beer tax (in 1988 dollars)

Cha

nge

in fa

talit

y ra

te (

fata

litie

s pe

r 10

000)

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 18 / 35

Page 19: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Fixed Effects Regression

Consider the panel regression model

𝑌𝑖𝑡 = 𝛽0 + 𝛽1𝑋𝑖𝑡 + 𝛽2𝑍𝑖 + 𝑢𝑖𝑡

traffic fatality rates and the beer taxes

𝐹𝑎𝑡𝑎𝑙𝑖𝑡𝑦𝑅𝑎𝑡𝑒𝑖𝑡 = 𝛽1𝐵𝑒𝑒𝑟𝑇 𝑎𝑥𝑖𝑡 + 𝑆𝑡𝑎𝑡𝑒𝐹𝑖𝑥𝑒𝑑𝐸𝑓𝑓𝑒𝑐𝑡𝑠 + 𝑢𝑖𝑡

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 19 / 35

Page 20: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Fixed Effects Regression: Dummy Variablesfatal_fe_lm_mod <- lm(fatal_rate ~ beertax + state - 1,

data = Fatalities)fatal_fe_lm_mod

#### Call:## lm(formula = fatal_rate ~ beertax + state - 1, data = Fatalities)#### Coefficients:## beertax stateal stateaz statear stateca stateco statect statede## -0.6559 3.4776 2.9099 2.8227 1.9682 1.9933 1.6154 2.1700## statefl statega stateid stateil statein stateia stateks stateky## 3.2095 4.0022 2.8086 1.5160 2.0161 1.9337 2.2544 2.2601## statela stateme statemd statema statemi statemn statems statemo## 2.6305 2.3697 1.7712 1.3679 1.9931 1.5804 3.4486 2.1814## statemt statene statenv statenh statenj statenm stateny statenc## 3.1172 1.9555 2.8769 2.2232 1.3719 3.9040 1.2910 3.1872## statend stateoh stateok stateor statepa stateri statesc statesd## 1.8542 1.8032 2.9326 2.3096 1.7102 1.2126 4.0348 2.4739## statetn statetx stateut statevt stateva statewa statewv statewi## 2.6020 2.5602 2.3137 2.5116 2.1874 1.8181 2.5809 1.7184## statewy## 3.2491

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 20 / 35

Page 21: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Fixed Effects Regression: demeaned

traffic fatality rates and the beer taxes∼

𝐹𝑎𝑡𝑎𝑙𝑖𝑡𝑦𝑅𝑎𝑡𝑒 = 𝛽1∼

𝐵𝑒𝑒𝑟𝑇 𝑎𝑥𝑖𝑡 + 𝑢𝑖𝑡.

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 21 / 35

Page 22: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Fixed Effects Regression: demeaned

# obtain demeaned dataFatalities_demeaned <- with(Fatalities,

data.frame(fatal_rate = fatal_rate- ave(fatal_rate, state),

beertax = beertax - ave(beertax, state)))

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 22 / 35

Page 23: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Fixed Effects Regression: demeanedsummary(lm(fatal_rate ~ beertax - 1,

data = Fatalities_demeaned))

#### Call:## lm(formula = fatal_rate ~ beertax - 1, data = Fatalities_demeaned)#### Residuals:## Min 1Q Median 3Q Max## -0.58696 -0.08284 -0.00127 0.07955 0.89780#### Coefficients:## Estimate Std. Error t value Pr(>|t|)## beertax -0.6559 0.1739 -3.772 0.000191 ***## ---## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1#### Residual standard error: 0.1757 on 335 degrees of freedom## Multiple R-squared: 0.04074, Adjusted R-squared: 0.03788## F-statistic: 14.23 on 1 and 335 DF, p-value: 0.0001913

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 23 / 35

Page 24: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Fixed Effects Regression: plm package

# install and load the 'plm' package## # install.packages("plm")library(plm)

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 24 / 35

Page 25: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Fixed Effects Regression: plm package# estimate the fixed effects regression with plm()fatal_fe_mod <- plm(fatal_rate ~ beertax,

data = Fatalities,index = c("state", "year"),model = "within")

# print summary using robust standard errorscoeftest(fatal_fe_mod, vcov. = vcovHC, type = "HC1")

#### t test of coefficients:#### Estimate Std. Error t value Pr(>|t|)## beertax -0.65587 0.28880 -2.271 0.02388 *## ---## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 25 / 35

Page 26: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Regression with Time Fixed Effectsthe relation between fatalities and beer tax,𝐹𝑎𝑡𝑎𝑙𝑖𝑡𝑦𝑅𝑎𝑡𝑒𝑖𝑡 = 𝛽1𝐵𝑒𝑒𝑟𝑇 𝑎𝑥𝑖𝑡+𝑆𝑡𝑎𝑡𝑒𝐸𝑓𝑓𝑒𝑐𝑡𝑠+𝑇 𝑖𝑚𝑒𝐹𝑖𝑥𝑒𝑑𝐸𝑓𝑓𝑒𝑐𝑡𝑠+𝑢𝑖𝑡

# via lm()fatal_tefe_lm_mod <- lm(fatal_rate ~ beertax + state

+ year - 1, data = Fatalities)fatal_tefe_lm_mod

#### Call:## lm(formula = fatal_rate ~ beertax + state + year - 1, data = Fatalities)#### Coefficients:## beertax stateal stateaz statear stateca stateco statect statede## -0.63998 3.51137 2.96451 2.87284 2.02618 2.04984 1.67125 2.22711## statefl statega stateid stateil statein stateia stateks stateky## 3.25132 4.02300 2.86242 1.57287 2.07123 1.98709 2.30707 2.31659## statela stateme statemd statema statemi statemn statems statemo## 2.67772 2.41713 1.82731 1.42335 2.04488 1.63488 3.49146 2.23598## statemt statene statenv statenh statenj statenm stateny statenc## 3.17160 2.00846 2.93322 2.27245 1.43016 3.95748 1.34849 3.22630## statend stateoh stateok stateor statepa stateri statesc statesd## 1.90762 1.85664 2.97776 2.36597 1.76563 1.26964 4.06496 2.52317## statetn statetx stateut statevt stateva statewa statewv statewi## 2.65670 2.61282 2.36165 2.56100 2.23618 1.87424 2.63364 1.77545## statewy year1983 year1984 year1985 year1986 year1987 year1988## 3.30791 -0.07990 -0.07242 -0.12398 -0.03786 -0.05090 -0.05180

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 26 / 35

Page 27: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Regression with Time Fixed Effects# via plm()fatal_tefe_mod <- plm(fatal_rate ~ beertax,

data = Fatalities,index = c("state", "year"),model = "within",effect = "twoways")

coeftest(fatal_tefe_mod, vcov = vcovHC, type = "HC1")

#### t test of coefficients:#### Estimate Std. Error t value Pr(>|t|)## beertax -0.63998 0.35015 -1.8277 0.06865 .## ---## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 27 / 35

Page 28: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Drunk Driving Laws and Traffic DeathsThese covariates are defined as follows:

unemp: a numeric variable stating the state specific unemploymentrate.log(income): the logarithm of real per capita income (in prices of1988).miles: the state average miles per driver.drinkage: the state specify minimum legal drinking age.drinkagc: a discretized version of drinkage that classifies statesinto four categories of minimal drinking age; 18, 19, 20, 21 and older.It denotes this as [18, 19), [19,20), [20,21) and [21,22]. Thesecategories are included as dummy regressors where [21,22] is chosenas the reference category."punish": a dummy variable with levels "yes" and "no" thatmeasures if drunk driving is severely punished by mandatory jail timeor mandatory community service (first conviction).

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 28 / 35

Page 29: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Drunk Driving Laws and Traffic Deaths# discretize the minimum legal drinking ageFatalities$drinkagec <- cut(Fatalities$drinkage,

breaks = 18:22,include.lowest = TRUE,right = FALSE)

# set minimum drinking age [21, 22] to be the baseline levelFatalities$drinkagec <-relevel(Fatalities$drinkagec,"[21,22]")

# mandadory jail or community service?Fatalities$punish <- with(Fatalities,

factor(jail == "yes" |service == "yes",labels = c("no", "yes")))

# the set of observations on all variables for 1982 and 1988Fatalities_1982_1988 <- Fatalities[with(Fatalities,

year == 1982 | year == 1988), ]Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 29 / 35

Page 30: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Drunk Driving Laws and Traffic Deaths# estimate all seven modelsfatalities_mod1 <- lm(fatal_rate ~ beertax,

data = Fatalities)

fatalities_mod2 <- plm(fatal_rate ~ beertax +state, data = Fatalities)

fatalities_mod3 <- plm(fatal_rate ~ beertax +year, data = Fatalities)

fatalities_mod4 <- plm(fatal_rate ~ beertax+ state + year,index = c("state","year"),model = "within",effect = "twoways",data = Fatalities)

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 30 / 35

Page 31: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Drunk Driving Laws and Traffic Deaths# estimate all eight models

fatalities_mod5 <- plm(fatal_rate ~ beertax+ state + year + drinkagec+ punish + miles + unemp + log(income),index = c("state", "year"),model = "within",effect = "twoways",data = Fatalities)

fatalities_mod6 <- plm(fatal_rate ~ beertax+ state + year + drinkagec+ punish + miles,index = c("state", "year"),model = "within",effect = "twoways",data = Fatalities)

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 31 / 35

Page 32: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Drunk Driving Laws and Traffic Deaths# estimate all seven modelsfatalities_mod7 <- plm(fatal_rate ~ beertax

+ year + drinkage+ punish + miles+ unemp + log(income),index = c("state", "year"),model = "within",effect = "twoways",data = Fatalities)

fatalities_mod8 <- plm(fatal_rate ~ beertax+ state + year + drinkagec+ punish + miles+ unemp + log(income),index = c("state", "year"),model = "within",effect = "twoways",data = Fatalities_1982_1988)

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 32 / 35

Page 33: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Drunk Driving Laws and Traffic Deaths# gather clustered standard errors in a listrob_se <- list(sqrt(diag(vcovHC(fatalities_mod1,

type = "HC1"))),sqrt(diag(vcovHC(fatalities_mod2,

type = "HC1"))),sqrt(diag(vcovHC(fatalities_mod3,

type = "HC1"))),sqrt(diag(vcovHC(fatalities_mod4,

type = "HC1"))),sqrt(diag(vcovHC(fatalities_mod5,

type = "HC1"))),sqrt(diag(vcovHC(fatalities_mod6,

type = "HC1"))),sqrt(diag(vcovHC(fatalities_mod7,

type = "HC1"))),sqrt(diag(vcovHC(fatalities_mod8,

type = "HC1"))))Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 33 / 35

Page 34: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Drunk Driving Laws and Traffic Deaths

# generate the tablestargazer(fatalities_mod1, fatalities_mod2, fatalities_mod3,

fatalities_mod4, fatalities_mod5,fatalities_mod6,fatalities_mod7, fatalities_mod8,digits = 3,header = FALSE,type = "latex",se = rob_se,title = "Linear Panel Regression Modelsof Traffic Fatalities due to Drunk Driving",model.numbers = FALSE,column.labels = c("(1)", "(2)", "(3)", "(4)",

"(5)", "(6)", "(7)","(8)")),add.lines=list(c('State Fixed effects', 'No','Yes',"No",'Yes','Yes',"Yes",'Yes','Yes'),

c('Year Fixed effects', 'No','No',"Yes",'Yes','Yes',"Yes",'Yes','Yes')))

Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 34 / 35

Page 35: Lab9: Panel Data Estimation in R · Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 11/35. Example: Traffic Deaths and Alcohol Taxes(1982) 0.0 0.5 1.0

Drunk Driving Laws and Traffic Deaths

Dependent variable:OLS panel

linear(1) (2) (3) (4) (5) (6) (7) (8)

beertax 0.365∗∗∗ −0.656∗∗ −0.640∗ −0.640∗ −0.445 −0.690∗∗ −0.456 −0.926∗∗∗(0.053) (0.289) (0.353) (0.350) (0.291) (0.345) (0.301) (0.337)

drinkagec[18,19) 0.028 −0.010 0.037(0.068) (0.081) (0.101)

drinkagec[19,20) −0.018 −0.076 −0.065(0.049) (0.066) (0.097)

drinkagec[20,21) 0.032 −0.100∗ −0.113(0.050) (0.055) (0.123)

drinkage −0.002(0.021)

punishyes 0.038 0.085 0.039 0.089(0.101) (0.109) (0.101) (0.161)

miles 0.00001 0.00002∗ 0.00001 0.0001∗∗∗(0.00001) (0.00001) (0.00001) (0.00005)

unemp −0.063∗∗∗ −0.063∗∗∗ −0.091∗∗∗(0.013) (0.013) (0.021)

log(income) 1.816∗∗∗ 1.786∗∗∗ 0.996(0.624) (0.631) (0.666)

Constant 1.853∗∗∗(0.047)

State Fixed effects No Yes No Yes Yes Yes Yes YesYear Fixed effects No No Yes Yes Yes Yes Yes YesObservations 336 336 336 336 335 335 335 95Adjusted R2 0.091 −0.120 −0.096 −0.149 0.217 −0.134 0.219 0.157

Note: ∗p<0.1; ∗∗p<0.05; ∗∗∗p<0.01Zhaopeng Qu (Nanjing University) Lab9: Panel Data Estimation in R 12/24/2020 35 / 35