-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.rmd
2209 lines (1539 loc) · 104 KB
/
index.rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Open Case Studies: Influence of Multicollinearity on Measured Impact of Right-to-Carry Gun Laws"
css: style.css
output:
html_document:
includes:
in_header: GA_Script.Rhtml
self_contained: yes
code_download: yes
highlight: tango
number_sections: no
theme: cosmo
toc: yes
toc_float: yes
pdf_document:
toc: yes
word_document:
toc: yes
---
<style>
#TOC {
background: url("https://opencasestudies.github.io/img/icon-bahi.png");
background-size: contain;
padding-top: 240px !important;
background-repeat: no-repeat;
}
</style>
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
include = TRUE, comment = NA, echo = TRUE,
message = FALSE, warning = FALSE, cache = FALSE, fig.width = 10, fig.height = 7,
fig.align = "center", out.width = "90%"
)
library(here)
library(knitr)
library(magrittr)
remotes::install_github("opencasestudies/OCSdata")
remotes::install_github("benmarwick/wordcountaddin", type = "source", dependencies = TRUE)
remotes::install_github("alistaire47/read.so")
library(wordcountaddin)
library(read.so)
library(OCSdata)
rmarkdown:::perf_timer_reset_all()
rmarkdown:::perf_timer_start("render")
```
<div id="google_translate_element"></div>
<script type="text/javascript" src='//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit'></script>
<script type="text/javascript">
function googleTranslateElementInit() {
new google.translate.TranslateElement({pageLanguage: 'en'}, 'google_translate_element');
}
</script>
#### {.outline}
```{r, echo = FALSE, out.width = "800 px"}
knitr::include_graphics(here::here("img", "mainplot.png"))
```
####
#### {.disclaimer_block}
**Disclaimer**: The purpose of the [Open Case Studies](https://opencasestudies.github.io){target="_blank"} project is **to demonstrate the use of various data science methods, tools, and software in the context of messy, real-world data**. A given case study does not cover all aspects of the research process, is not claiming to be the most appropriate way to analyze a given data set, and should not be used in the context of making policy decisions without external consultation from scientific experts.
####
#### {.license_block}
This work is licensed under the Creative Commons Attribution-NonCommercial 3.0 [(CC BY-NC 3.0)](https://creativecommons.org/licenses/by-nc/3.0/us/){target="_blank"} United States License.
####
#### {.reference_block}
To cite this case study please use:
Wright, Carrie and Ontiveros, Michael and Meng, Qier and Jager, Leah and Taub, Margaret and Hicks, Stephanie. (2020). [https://github.com//opencasestudies/ocs-bp-RTC-analysis](https://github.com//opencasestudies/ocs-bp-RTC-analysis). Influence of Multicollinearity on Measured Impact of Right-to-Carry Gun Laws (Version v1.0.0).
####
To access the GitHub repository for this case study see here: https://github.com//opencasestudies/ocs-bp-RTC-analysis.
You may also access and download the data using our `OCSdata` package. To learn more about this package including examples, see this [link](https://github.com/opencasestudies/OCSdata). Here is how you would install this package:
```{r, eval=FALSE}
install.packages("OCSdata")
```
This case study is part of a series of public health case studies for the [Bloomberg American Health Initiative](https://americanhealth.jhu.edu/open-case-studies).
***
The total reading time for this case study is calculated via [koRpus](https://github.com/unDocUMeantIt/koRpus) and shown below:
```{r, echo=FALSE}
readtable = text_stats("index.Rmd") # producing reading time markdown table
readtime = read.so::read.md(readtable) %>% dplyr::select(Method, koRpus) %>% # reading table into dataframe, selecting relevant factors
dplyr::filter(Method == "Reading time") %>% # dropping unnecessary rows
dplyr::mutate(koRpus = paste(round(as.numeric(stringr::str_split(koRpus, " ")[[1]][1])), "minutes")) %>% # rounding reading time estimate
dplyr::mutate(Method = "koRpus") %>% dplyr::relocate(koRpus, .before = Method) %>% dplyr::rename(`Reading Time` = koRpus) # reorganizing table
knitr::kable(readtime, format="markdown")
```
***
**Readability Score: **
A readability index estimates the reading difficulty level of a particular text. Flesch-Kincaid, FORCAST, and SMOG are three common readability indices that were calculated for this case study via [koRpus](https://github.com/unDocUMeantIt/koRpus). These indices provide an estimation of the minimum reading level required to comprehend this case study by grade and age.
```{r, echo=FALSE}
rt = wordcountaddin::readability("index.Rmd", quiet=TRUE) # producing readability markdown table
df = read.so::read.md(rt) %>% dplyr::select(index, grade, age) %>% # reading table into dataframe, selecting relevant factors
tidyr::drop_na() %>% dplyr::mutate(grade = round(as.numeric(grade)), # dropping rows with missing values, rounding age and grade columns
age = round(as.numeric(age))
)
knitr::kable(df, format="markdown")
```
***
Please help us by filling out our survey.
<div style="display: flex; justify-content: center;"><iframe src="https://docs.google.com/forms/d/e/1FAIpQLSfpN4FN3KELqBNEgf2Atpi7Wy7Nqy2beSkFQINL7Y5sAMV5_w/viewform?embedded=true" width="1200" height="700" frameborder="0" marginheight="0" marginwidth="0">Loading…</iframe></div>
# **Motivation**
***
This case study will introduce the topic of [multicollinearity](https://en.wikipedia.org/wiki/Multicollinearity){target="_blank"}, which occurs in regression when one or more independent variables can be predicted by other independent variables.
We will do so by showcasing a real world example where multicollinearity in part resulted in historically controversial and conflicting findings about the influence of the adoption of right-to-carry (RTC) concealed handgun laws on violent crime rates in the United States.
We will focus on two articles:
1. The first analysis by [Mustard and Lott](https://chicagounbound.uchicago.edu/cgi/viewcontent.cgi?article=1150&context=law_and_economics){target="_blank"} published in 1996 suggests that RTC laws reduce violent crime. Lott authored a book extending these findings in 1998 called [***More Guns, Less Crime***](https://en.wikipedia.org/wiki/More_Guns,_Less_Crime){target="_blank"}.
```{r, echo=FALSE, out.height = '100%', out.width = '100%', fig.align='center'}
knitr::include_graphics(here("img", "Lott.png"))
```
##### [[source]](https://chicagounbound.uchicago.edu/cgi/viewcontent.cgi?article=1150&context=law_and_economics){target="_blank"}
2. The second analysis is a recent article by [Donohue, et al.](https://www.nber.org/papers/w23510.pdf){target="_blank"} published in 2017 that suggests that RTC laws increase violent crime. Donohue has also published previous articles with titles such as [***Shooting down the "More Guns, Less Crime" Hypothesis***](https://www.jstor.org/stable/1229603?seq=1){target="_blank"}.
```{r, echo=FALSE, out.height = '100%', out.width = '100%', fig.align='center'}
knitr::include_graphics(here("img", "Donohue.png"))
```
##### [[source]](https://www.nber.org/papers/w23510.pdf){target="_blank"}
This has been a controversial topic as many other analyses also produced conflicting results. See [here](https://en.wikipedia.org/wiki/More_Guns,_Less_Crime){target="_blank"} for a list of studies.
The [Donohue, et al.](https://www.nber.org/papers/w23510.pdf){target="_blank"} article discusses how there are many other important methodological aspects besides [multicollinearity](https://en.wikipedia.org/wiki/Multicollinearity){target="_blank"} (which occurs when predictor or input variables are highly related in a regression analysis) that could account for the historically conflicting results in these previous manuscripts.
In fact, nearly every aspect of the data analysis process was different between the [Donohue, et al.](https://www.nber.org/papers/w23510.pdf){target="_blank"} and [Mustard and Lott](https://chicagounbound.uchicago.edu/cgi/viewcontent.cgi?article=1150&context=law_and_economics){target="_blank"} analyses.
```{r, echo=FALSE, out.height = '75%', out.width = '75%', fig.align='center'}
knitr::include_graphics(here("img", "Educational_Graphic1.jpg"))
```
However, we will focus particularly on multicollinearity and how it can influence the results we get from linear regression.
Specifically, this analysis will demonstrate how methodological details can be critically influential for our overall conclusions and can result in important policy related consequences. The [Donohue, et al. article]((https://www.nber.org/papers/w23510.pdf){target="_blank"}) will provide the motivation and illustration.
#### {.reference_block}
John J. Donohue et al., Right‐to‐Carry Laws and Violent Crime: A Comprehensive Assessment Using Panel Data and a State‐Level Synthetic Control Analysis. *Journal of Empirical Legal Studies*, 16,2 (2019).
David B. Mustard & John Lott. Crime, Deterrence, and Right-to-Carry Concealed Handguns. *Coase-Sandor Institute for Law & Economics* Working Paper No. 41, (1996).
####
Before we leave this section, we provide a high-level overview of what variables were (or were not) included in the [Donohue, Aneja and Weber](https://www.nber.org/papers/w23510.pdf){target="_blank"} (DAW) paper and the [Mustard and Lott](https://chicagounbound.uchicago.edu/cgi/viewcontent.cgi?article=1150&context=law_and_economics){target="_blank"} (ML) paper:
```{r, echo=FALSE, out.height = '100%', out.width = '100%', fig.align='center'}
knitr::include_graphics(here("img", "Donohue_Table2_edited.png"))
```
##### [[source]](https://www.nber.org/papers/w23510.pdf){target="_blank"}
###### *ML is abbreviated as LM in the source article
**Note**: We are not attempting to re-create the analyses from the original authors. Instead, we aim to use a subset of the listed explanatory variables in this case study to demonstrate multicollinearity. These variables will be consistent for both analyses that we will perform, with the exception that one analysis will have 6 demographic variables as in the analysis in the [Donohue, et al.](https://www.nber.org/papers/w23510.pdf){target="_blank"} article and the other will have 36 demographic variables, grouping individuals into more specific categories, as in the analysis in the [Mustard and Lott](https://chicagounbound.uchicago.edu/cgi/viewcontent.cgi?article=1150&context=law_and_economics){target="_blank"} article.
# **Main Question**
***
#### {.main_question_block}
<b><u> Our main question: </u></b>
What is the effect of multicollinearity on coefficient estimates from linear regression models when analyzing right to carry laws and violence rates?
####
Specifically, we will consider the two ways to define the demographic variables (as described above) and investigate how the inclusion of different numbers of age groups influences the results of an analysis of right to carry laws and violence rates.
# **Learning Objectives**
***
The skills, methods, and concepts that students will be familiar with by the end of this case study are:
<u>**Data Science Learning Objectives:**</u>
1. Create correlation scatterplots and heatmaps (`GGally`, `ggcorrplot`)
2. Create interactive tables (`DT`)
3. Sample subsets of data (`rsample`)
4. Combine multiple plots (`cowplot`)
5. Create data visualizations with equations and text(`ggplot2` and `latex2exp`)
<u>**Statistical Learning Objectives:**</u>
1. Understand what multicollinearity is and how it can influence linear regression coefficients
2. Recognize signs for the presence of multicollinearity and determine its severity
3. Illustrate the difference between multicollinearity and correlation
4. Implement panel regression analysis in R (`plm`)
5. Relate variance inflation factors (VIFs) to multicollinearity and calculate VIFs in R (`car`)
To see another case study about how the original raw data was imported and wrangled please see [here](https://www.opencasestudies.org/ocs-bp-RTC-wrangling/){target="_blank"}.
We will especially focus on using packages and functions from the [`tidyverse`](https://www.tidyverse.org/){target="_blank"}, such as `dplyr` and `ggplot2`. The tidyverse is a library of packages created by RStudio. While some students may be familiar with previous R programming packages, these packages make data science in R especially legible and intuitive.
```{r, out.width = "20%", echo = FALSE, fig.align ="center"}
include_graphics("https://tidyverse.tidyverse.org/logo.png")
```
# **Context**
***
So what exactly is a **right-to-carry law**?
It is a law that specifies _if_ and _how_ citizens are allowed to have a firearm on their person or nearby (for example, in a citizen's car) in public. In this discussion, we will use the [National Rifle Association (NRA)](https://www.nraila.org/gun-laws/){target="_blank"} terminology. Please keep in mind that there are other terms that people use.
The [Second Amendment](https://en.wikipedia.org/wiki/Second_Amendment_to_the_United_States_Constitution){target="_blank"} to the United States Constitution guarantees the right to "keep and bear arms". The amendment was ratified in 1791 as part of the [Bill of Rights](https://en.wikipedia.org/wiki/United_States_Bill_of_Rights){target="_blank"}.
```{r, echo=FALSE, out.height = '50%', out.width = '50%', fig.align='center'}
knitr::include_graphics("https://upload.wikimedia.org/wikipedia/commons/7/79/Bill_of_Rights_Pg1of1_AC.jpg")
```
##### [[source]](https://upload.wikimedia.org/wikipedia/commons/7/79/Bill_of_Rights_Pg1of1_AC.jpg){target="_blank"}
However, there are no federal laws about carrying firearms in public.
These laws are created and enforced at the US state level.
States vary greatly in their laws about the right to carry firearms.
Some require extensive effort to obtain a permit to legally carry a firearm, while other states require very minimal effort to do so. An increasing number of states do not require permits at all.
<details> <summary> Click here for more information on history of right-to-carry policies in the US. </summary>
According to the [Wikipedia entry](https://en.wikipedia.org/wiki/History_of_concealed_carry_in_the_U.S.){target="_blank"} about the history of right-to-carry policies in the United States:
> Public perception on concealed carry vs open carry has largely flipped. In the early days of the United States, open carrying of firearms, long guns and revolvers was a common and well-accepted practice. Seeing guns carried openly was not considered to be any cause for alarm. Therefore, anyone who would carry a firearm but attempt to conceal it was considered to have something to hide, and presumed to be a criminal. For this reason, concealed carry was denounced as a detestable practice in the early days of the United States.
> Concealed weapons bans were passed in Kentucky and Louisiana in 1813. (In those days open carry of weapons for self-defense was considered acceptable; concealed carry was denounced as the practice of criminals.) By 1859, Indiana, Tennessee, Virginia, Alabama, and Ohio had followed suit. By the end of the nineteenth century, similar laws were passed in places such as Texas, Florida, and Oklahoma, which protected some gun rights in their state constitutions. Before the mid 1900s, most U.S. states had passed concealed carry laws rather than banning weapons completely. Until the late 1990s, many Southern states were either "No-Issue" or "Restrictive May-Issue". Since then, these states have largely enacted "Shall-Issue" licensing laws, with numerous states legalizing "Unrestricted concealed carry".
</details>
There are five broad categories of right-to-carry laws according to the NRA:
```{r, echo=FALSE, out.height = '100%', out.width = '100%', fig.align='center'}
knitr::include_graphics(here("img", "RTC.png"))
```
##### [[source]](https://www.nraila.org/gun-laws/){target="_blank"}
```{r, echo=FALSE, out.height = '100%', out.width = '100%', fig.align='center'}
knitr::include_graphics(here("img", "RTC_map.png"))
```
##### [[source]](https://www.nraila.org/gun-laws/){target="_blank"}
You can see that no state in the US currently (this map is from 2020) has a "Rights Infringed/Non-Issue" law (the gray category) -- meaning that all 50 states in the US allow the right to carry firearms at least in some way.
However the level of restrictions is dramatically different from one state to another.
For comparison purposes, many authors use the terms "shall issue", "shall issue with discretion", "no permit required", "may issue", and "non-issue" to describe these categories instead of the NRA terminology.
<details> <summary> Click here for more information about how restrictions vary from one state to another. </summary>
There is variation from state to state even within the same general category:
For example here is an abridged version of the [current carry laws in Idaho](https://www.nraila.org/gun-laws/state-gun-laws/idaho/) which is considered an "Unrestricted - no permit required" state:
> State law ... allows any resident of Idaho or a current member of the armed forces of the United States to carry a concealed handgun without a license to carry, provided the person is over 18 years old and not disqualified from being issued a license to carry concealed weapons under state law. An amendment to state law that takes effect on July 1, 2020 changes the reference in the above law from “a resident of Idaho” to “any citizen of the United States.”
And here are is an abridged version of the [current carry laws in Arizona](https://www.nraila.org/gun-laws/state-gun-laws/arizona/) which is also considered an "Unrestricted - no permit required" state:
> Any person 21 years of age or older, who is not prohibited possessor, may carry a weapon openly or concealed without the need for a license...
Notice that citizens in Idaho only need to be 18 to carry a firearm, whereas they must be 21 in Arizona.
</details>
# **Limitations**
***
There are some important considerations regarding this data analysis to keep in mind:
1. We do not use all of the data used by either the [Mustard and Lott](https://chicagounbound.uchicago.edu/cgi/viewcontent.cgi?article=1150&context=law_and_economics){target="_blank"} or [Donohue, et al.](https://www.nber.org/papers/w23510.pdf){target="_blank"} analyses, nor do we perform the same analysis as in each article. We instead perform a much simpler analysis with fewer variables for the purposes of illustration of the concept of multicollinearity and its influence on regression coefficients, not to reproduce either analysis.
2. Our analysis accounts for either the adoption or lack of adoption of a permissive right-to-carry law in each state, but does not account for differences in the level of permissiveness of the laws.
Recall that these are the categories of right to carry laws:
```{r, echo=FALSE, out.height = '100%', out.width = '100%', fig.align='center'}
knitr::include_graphics(here("img", "RTC.png"))
```
States with laws of the category rights restricted - very limited issue (red) are considered as not having a permissive right-to-carry law. Recall that no states currently have a rights infringed/non-issue law.
States of all other categories (shall issue, discretionary/reasonable issue, and no permit required, all shades of blue) are considered the same in our analysis, as having a permissive right-to-carry law.
3) Because our analysis is an oversimplification, the results presented here should not be used for determining policy changes; instead we suggest that users interested in such a determination consult with a specialist.
4) The inclusion of race as an explanatory variable in an epidemiological study can be useful in certain circumstances. However, there are limitations and issues around defining, determining, and reporting race, as well as in interpreting differences in public health outcomes by race. For more information on this topic, we have included a [link](https://academic.oup.com/epirev/article/22/2/187/456942) to a paper on the use of race as a measure in epidemiology. We include race in this analysis to demonstrate and consider the limitations of what the previous papers have done to analyze the influence of RTC laws on violent crime, with a focus on multicollinearity. Thus in our analysis we have also defined race as was previously done in these papers. Furthermore, we want to point out that reporting analyses about crime with race as a variable can have very unexpected consequences and thus care should be taken. See [here](https://journals.sagepub.com/doi/full/10.1177/0963721418763931) for suggestions. Any association between demographic variables (indicating the proportion of the population from specific race and age groups) and violent crime does not necessarily indicate that the two are linked causally, as aside from the issues presented in the [article]((https://academic.oup.com/epirev/article/22/2/187/456942)), this may instead indicate higher rates of police engagement with certain racial groups due to [racial profling](https://www.aclu.org/other/racial-profiling-definition).
The ACLU defines racial profiling as:
>"Racial Profiling" refers to the discriminatory practice by law enforcement officials of targeting individuals for suspicion of crime based on the individual's race, ethnicity, religion or national origin.
***
We will begin by loading the packages that we will need:
```{r}
library(here)
library(dplyr)
library(magrittr)
library(purrr)
library(tibble)
library(ggplot2)
library(naniar)
library(ggrepel)
library(plm)
library(broom)
library(GGally)
library(ggcorrplot)
library(rsample)
library(DT)
library(car)
library(stringr)
library(cowplot)
library(latex2exp)
library(OCSdata)
```
<u>**Packages used in this case study:** </u>
Package | Use in this case study
---------- |-------------
[`here`](https://github.com/jennybc/here_here){target="_blank"} | to easily load and save data
[`dplyr`](https://dplyr.tidyverse.org/){target="_blank"} | to arrange/filter/select/compare specific subsets of the data
[`magrittr`](https://cran.r-project.org/web/packages/magrittr/vignettes/magrittr.html){target="_blank"} | to use the compound assignment pipe operator `%<>%`
[`purrr`](https://purrr.tidyverse.org/){target="_blank"} | to import the data in all the different excel and csv files efficiently
[`tibble`](https://tibble.tidyverse.org/){target="_blank"} | to create data objects that we can manipulate with `dplyr`/`stringr`/`tidyr`/`purrr`
[`ggplot2`](https://ggplot2.tidyverse.org/){target="_blank"} | to create plots
[`naniar`](https://cran.r-project.org/web/packages/naniar/vignettes/getting-started-w-naniar.html){target="_blank"} | to quickly visualize missing data
[`ggrepel`](https://cran.r-project.org/web/packages/ggrepel/vignettes/ggrepel.html){target="_blank"} | to allow labels in figures not to overlap
[`plm`](https://cran.r-project.org/web/packages/plm/vignettes/plmPackage.html){target="_blank"} | to work with panel data fitting fixed effects and linear regression models
[`broom`](https://cran.r-project.org/web/packages/broom/vignettes/broom.html){target="_blank"} | to create nicely formatted model output
[`GGally`](https://github.com/ggobi/ggally){target="_blank"} | to extend ggplot2 functionality to easily create more complex plots
[`ggcorrplot`](https://www.rdocumentation.org/packages/ggcorrplot/versions/0.1.3){target="_blank"} | to easily visualize a correlation matrix
[`rsample`](https://rsample.tidymodels.org){target="_blank"} | to split our sample for the simulation analysis
[`DT`](https://rstudio.github.io/DT/){target="_blank"} | to create interactive and searchable tables
[`car`](https://cran.r-project.org/web/packages/car/vignettes/embedding.pdf){target="_blank"} | to calculate VIF values on linear model output
[`stringr`](https://stringr.tidyverse.org/articles/stringr.html){target="_blank"} | to manipulate the character strings within the data
[`cowplot`](https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html){target="_blank"} | to allow plots to be combined
[`latex2exp`](https://cran.r-project.org/web/packages/latex2exp/vignettes/using-latex2exp.html){target="_blank"} | to convert latex math formulas to R's plotmath expressions
[`OCSdata`](https://github.com/opencasestudies/OCSdata){target="_blank"} | to access and download OCS data files
The first time we use a function, we will use the `::` to indicate which package we are using. Unless we have overlapping function names, this is not necessary, but we will include it here to be informative about where the functions we will use come from.
# **What are the data?**
***
Below is a table from the [Donohue, et al.](https://www.nber.org/papers/w23510.pdf){target="_blank"} paper that shows the data used in both analyses, where DAW stands for [Donohue, et al.](https://www.nber.org/papers/w23510.pdf){target="_blank"} and LM stands for [Mustard and Lott](https://chicagounbound.uchicago.edu/cgi/viewcontent.cgi?article=1150&context=law_and_economics){target="_blank"}.
```{r, echo=FALSE, out.height = '100%', out.width = '100%', fig.align='center'}
knitr::include_graphics(here("img", "Donohue_AppendixJ.png"))
```
We will be using a subset of these variables, which are highlighted in green:
```{r, echo=FALSE, out.height = '100%', out.width = '100%', fig.align='center'}
knitr::include_graphics(here("img", "ourdata.png"))
```
# **Data Import and Wrangling**
***
See [this case study](https://www.opencasestudies.org/ocs-bp-RTC-wrangling/){target="_blank"} for details about data import and wrangling. We include data sources here for completeness, but will start from processed data in this case study.
## **Demographic and population data**
***
To obtain information about age, sex, race, and overall population size we will use US Census Bureau data, as was done in both of the articles. The census data is available for different time spans. Here are the links for the years used in our analysis. We will use data from 1977 to 2010.
Data | Link
---------- |-------------
**years 1977 to 1979** | [link](https://www2.census.gov/programs-surveys/popest/tables/1900-1980/state/asrh/)
**years 1980 to 1989** | [link](https://www2.census.gov/programs-surveys/popest/tables/1980-1990/counties/asrh/) * county data was used for this decade which also has state information
**years 1990 to 1999** | [link](https://www2.census.gov/programs-surveys/popest/tables/1990-2000/state/asrh/)
**years 2000 to 2010** | [link](https://www.census.gov/data/datasets/time-series/demo/popest/intercensal-2000-2010-state.html) <br> [technical documentation](https://www2.census.gov/programs-surveys/popest/technical-documentation/file-layouts/2000-2010/intercensal/state/st-est00int-alldata.pdf){target="_blank"}
## **State FIPS codes**
***
The data was downloaded from the [US Census Bureau](https://www.census.gov/geographies/reference-files/2014/demo/popest/2014-geocodes-state.html){target="_blank"}.
## **Police staffing data**
***
The following data was downloaded from the [Federal Bureau of Investigation](https://crime-data-explorer.fr.cloud.gov/downloads-and-docs){target="_blank"}.
## **Unemployment data**
***
The following data was downloaded from the [U.S. Bureau of Labor Statistics](https://data.bls.gov/cgi-bin/dsrv?la){target="_blank"}.
## **Poverty data**
***
Extracted from Table 21 from [US Census Bureau Poverty Data ](https://www.census.gov/data/tables/time-series/demo/income-poverty/historical-poverty-people.html){target="_blank"}.
## **Violent crime**
***
Violent crime data was obtained from [here](https://www.ucrdatatool.gov/Search/Crime/State/StatebyState.cfm){target="_blank"}.
## **Right-to-carry data**
***
This data is extracted from table in [Donohue paper](https://www.nber.org/papers/w23510.pdf){target="_blank"}.
***
Here is the table from the [Donohue paper](https://www.nber.org/papers/w23510.pdf){target="_blank"} that compares the data used in the analyses:
```{r, echo=FALSE, out.height = '100%', out.width = '100%', fig.align='center'}
knitr::include_graphics(here("img", "Donohue_Table2_edited.png"))
```
##### [[source]](https://www.nber.org/papers/w23510.pdf){target="_blank"}
###### *ML is abbreviated as LM in the source article
We can see that only the percentage of males that were from age 15-39 of the race groups (black, white, and other) were used in the Donohue analysis, whereas the Mustard and Lott paper, individuals from ages 10 to over 65 were used.
The final products from our data wrangling which is described in [another case study](https://www.opencasestudies.org/ocs-bp-RTC-wrangling/){target="_blank"} are two tibbles of data each with variables selected to be similar to those used in either the Donohue or Mustard and Lott analysis. The overarching idea of this case study is to compare the model results from these two datasets, which differ only in the demographic variables. This is an oversimplification of the actual differences between the datasets and approaches taken by Donohue and Lott/Mustard, but will be useful for illustrating the impact of multicollinearity on our modeling results, which is the main objective of this case study.
If you have trouble accessing the GitHub Repository, the "Wrangled_data.rda" data can be downloaded from [here](https://github.com/opencasestudies/ocs-bp-RTC-analysis/blob/master/data/Wrangled_data.rda).
We will load this data now.
In our case, we downloaded this data and put it within a "wrangled" subdirectory of the "data" directory for our project. If you use an RStudio project, then you can use the `here()` function of the `here` package to make the path for loading this data simpler. The `here` package automatically starts looking for files based on where you have a `.Rproj` file which is created when you start a new RStudio project. We can specify that we want to look for the file within the "wrangled" subdirectory of the "data" directory within a directory where our `.Rproj` file is located by separating the name of the "data" directory and "wrangled" and the file name using commas.
***
<details> <summary> Click here to see more about creating new projects in RStudio. </summary>
You can create a project by going to the File menu of RStudio like so:
```{r, echo = FALSE, out.width="60%"}
knitr::include_graphics(here::here("img", "New_project.png"))
```
You can also do so by clicking the project button:
```{r, echo = FALSE, out.width="60%"}
knitr::include_graphics(here::here("img", "project_button.png"))
```
See [here](https://support.rstudio.com/hc/en-us/articles/200526207-Using-Projects) to learn more about using RStudio projects.
</details>
***
```{r}
load(file = here::here("data", "wrangled", "Wrangled_data.rda"))
```
You may also download the data files with the `OCSdata` package. If you prefer this method, use the following code chunk to download and load the data:
```{r, eval=FALSE}
# install.packages("OCSdata")
library(OCSdata)
wrangled_rda("ocs-bp-RTC-analysis", outpath = getwd())
# This will save the wrangled data files in a "OCSdata/data/wrangled/"
# sub-folder in your current working directory
load(file = here::here("OCSdata", "data", "wrangled", "Wrangled_data.rda"))
# this will load the data into R
```
We will check the dimensions of each tibble using the base `dim()` function:
```{r}
dim(LOTT_DF)
dim(DONOHUE_DF)
```
As expected the `Lott_DF` is 30 columns larger, due to the 30 additional demographic variables. We can check those now as well.
```{r}
LOTT_DF %>%
colnames()
DONOHUE_DF %>%
colnames()
```
Lastly, we will check that the `YEAR` values are the same, i.e., that the two tibbles contain data from the same set of years. We can use the `setequal()` function of the `dplyr` package to see if the values are the same.
```{r}
setequal(
DONOHUE_DF %>% distinct(YEAR),
LOTT_DF %>% distinct(YEAR)
)
```
# **Data Exploration**
***
Let's do some quick visualizations to get a sense of our outcome of interest, the violent crime data.
First we will plot the rate of violent crime over time to get a sense of the general trend.
To do so we need to summarize the data for each year across all of the states.
Thus we will use the `group_by()` function and the `summarize()` functions to calculate an overall number of violent crimes relative to the total population for each year. In fact, we will calculate the log of the number of violent crimes per 100,000 individuals, as this will make it easier to see the trend in the data. In addition, by looking at the rate per 100,000 individuals, we can more directly compare crime statistics across groups like states with different populations, as you will see below.
Then we will use the `ggplot2` package to plot the data. The first step in creating a plot with this package is to use the `ggplot()` function and the `aes()` argument to specify what data should be plotted on the x-axis and what data should be plotted in on the y-axis. Then we select what type of plot we would like to make using one of the `geom_*()` functions. Please see [this case study](https://opencasestudies.github.io/ocs-bp-co2-emissions/){target="_blank"} for more details.
We can use the `scale_x_continuous()` and `scale_y_continuous()` functions to modify the axis tick marks and their labels.
The `labs()` function can be used to add labels to the plot, while the `theme()` function allows for manipulation of the details of the labels, like size and angle.
All of these functions are part of the `ggplot2` package.
```{r}
DONOHUE_DF %>%
group_by(YEAR) %>%
summarize(
Viol_crime_count = sum(Viol_crime_count),
Population = sum(Population),
.groups = "drop"
) %>%
mutate(Viol_crime_rate_100k_log = log((Viol_crime_count * 100000) / Population)) %>%
ggplot(aes(x = YEAR, y = Viol_crime_rate_100k_log)) +
geom_line() +
scale_x_continuous(
breaks = seq(1980, 2010, by = 1),
limits = c(1980, 2010),
labels = c(seq(1980, 2010, by = 1))
) +
scale_y_continuous(
breaks = seq(5.75, 6.75, by = 0.25),
limits = c(5.75, 6.75)
) +
labs(
title = "Crime rates fluctuate over time",
x = "Year",
y = "ln(violent crimes per 100,000 people)"
) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90))
```
Interesting! It appears that there was an overall national peak in violent crime in the early 1990s that has since then declined.
Now let's take a look at each state.
We will use the `ggrepel` package to add text to the plot using the `geom_text_repel()` function. This is especially useful when there is a lot of text, as this function reduces the overlap of text labels. Again see [this case study](https://opencasestudies.github.io/ocs-bp-co2-emissions/){target="_blank"} for more details on how to add labels to elements of plots.
```{r}
DONOHUE_DF %>%
mutate(Viol_crime_rate_100k_log = log((Viol_crime_count * 100000) / Population)) %>%
ggplot(aes(x = YEAR, y = Viol_crime_rate_100k_log, color = STATE)) +
geom_point(size = 0.5) +
geom_line(aes(group = STATE),
size = 0.5,
show.legend = FALSE
) +
geom_text_repel(
data = DONOHUE_DF %>%
mutate(Viol_crime_rate_100k_log = log((Viol_crime_count * 100000) / Population)) %>%
filter(YEAR == last(YEAR)),
aes(
label = STATE,
x = YEAR,
y = Viol_crime_rate_100k_log
),
size = 3,
alpha = 1,
nudge_x = 10,
direction = "y",
hjust = 1,
vjust = 1,
segment.size = 0.25,
segment.alpha = 0.25,
force = 1,
max.iter = 9999
) +
guides(color = FALSE) +
scale_x_continuous(
breaks = seq(1980, 2015, by = 1),
limits = c(1980, 2015),
labels = c(seq(1980, 2010, by = 1), rep("", 5))
) +
scale_y_continuous(
breaks = seq(3.5, 8.5, by = 0.5),
limits = c(3.5, 8.5)
) +
labs(
title = "States have different levels of crime",
x = "Year",
y = "ln(violent crimes per 100,000 people)"
) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90))
```
It looks like the crime rates vary quite a bit from one state to another. Some states show increased crime over time while others show decreased crime.
Now let's take a closer look at some of our other variables.
As we do this, we may start to run into issues with missing data for some of our variables, and it is always good to be aware of missing data in any data analysis. We can use the `vis_miss()` function of the `naniar` package to confirm that there are no missing values.
```{r}
DONOHUE_DF %>%
naniar::vis_miss()
```
Looks like no missing data!
```{r}
LOTT_DF %>%
naniar::vis_miss()
```
Same for the `LOTT_DF`.
We can use the `skim()` of the `skimr` package to get a better sense of the data. This also shows missingness, as well as standard deviations, spread, and means for our data. Also notice that there is a small histogram of each variable in the final column.
```{r, eval=FALSE}
skimr::skim(DONOHUE_DF)
```
```{r, echo=FALSE, out.height = '100%', out.width = '100%', fig.align='center'}
knitr::include_graphics(here("img", "skim1.png"))
knitr::include_graphics(here("img", "skim2.png"))
```
```{r, eval=FALSE}
skimr::skim(LOTT_DF)
```
```{r, echo=FALSE, out.height = '100%', out.width = '100%', fig.align='center'}
knitr::include_graphics(here("img", "skim3.png"))
knitr::include_graphics(here("img", "skim4.png"))
knitr::include_graphics(here("img", "skim5.png"))
```
We can see from this function that we have the number of variables of the class types that we expect for each tibble. We can also see that the means of the variables that should be the same for each tibble are in fact the same.
We can also tell that the values for the variables are in general what we would expect.
# **Data Analysis**
***
## **Panel Analysis**
***
In these datasets, we have what is called [panel data](https://en.wikipedia.org/wiki/Panel_data){target="_blank"}, a special type of longitudinal data. Longitudinal data are data measurements taken over time. Panel data are data repeatedly measured for multiple panel members or individuals over time. This is in contrast with time series data, which measures one individual over time and cross sectional data, which measures multiple individuals at one point in time. In other words, panel data is a combination of both, with measurements for multiple individuals/units of observation over multiple time periods. In our case, we have measurements of violent crime and other variables for each state over many years. Therefore we are using measurements about the same states over time.
In a panel analysis there are $N$ individual panel members and $T$ time points.
There are two types of panels:
1. **Balanced** - At each time point ($T$), there are data points for each individual($N$).
Time Points ($T$) | Individuals ($N$)
---------- |-------------
1977 | Nevada
1977 | Alabama
1977 | Kansas
1978 | Nevada
1978 | Alabama
1978 | Kansas
1979 | Nevada
1979 | Alabama
1979 | Kansas
2. **Unbalanced** - There may be data points missing for some individuals ($N$) at some time points ($T$).
Time Points ($T$) | Individuals ($N$)
---------- |-------------
1977 | Nevada
1977 | Alabama
1978 | Nevada
1978 | Alabama
1979 | Nevada
1979 | Alabama
1979 | Kansas
Overall in a balanced panel, we have $n$ observations, where $n = N*T$.
In an unbalanced panel, the number of observations is less than $N*T$.
In our case we have:
$N$ = 45 states (in the data wrangling process we removed those who had adopted an RTC law before 1980)
$T$ = 31 years (1980 - 2010)
In every year we have measurements for each state (as we just saw above), thus our panel is balanced.
So, our total observations $n = 45*31$, thus $n$ = `r 45*31`.
We will be performing a **panel linear regression model analysis**.
In such an analysis we will model our data according to this generic model:
$$Y_{it}=β_{0}+β_{1}X_{1it}+...+β_{K}X_{Kit}+e_{it}$$
Where $i$ is the individual dimension (in our case individual states) and $t$ is the time dimension.
Some explanatory/independent variables or regressors $X_{it}$ will vary across individuals and time, while others will be fixed across the time of the study (or don't change over time), while others still will be fixed across individuals but vary across time periods.
There are three general sub-types of [panel regression analysis](https://en.wikipedia.org/wiki/Panel_analysis){target="_blank"}.
Overall, they assume that the different individuals are independent, however the same data for the same individual may be correlated across time.
The main difference between the three sub-types are the assumptions about unobserved differences between individuals.
If you are familiar with fixed and random effects in the context in the statistical literature the following econometric definitions will sound a bit different.
From the `plm` package [vignette](https://cran.r-project.org/web/packages/plm/vignettes/plmPackage.html):
> In the mixed models literature,...fixed effect indicates a parameter that is assumed constant, while random effects are parameters that vary randomly around zero according to a joint multivariate normal distribution.
> ...Having fixed effects in an **econometric model** has the meaning of allowing the intercept to vary with group, or time, or both, while the other parameters are generally still assumed to be homogeneous. Having random effects means having a group– (or time–, or both) specific component in the error term.
OK, so now that we know not to expect the typical mixed model definitions of fixed and random effects, let's get back to the three sub-types of panel regression analysis:
1) **independently pooled panels** - assumes that there are no individual effects that are independent of time and also no effect of time on all the individuals. In other words, the independent variables are not correlated with any error term. This is essentially an ordinary least squares linear regression. In our setting, it would mean treating each year-state observation as unrelated to the others, which probably does not make sense, since we expect observations within a state to be related to one another. This type of panel regression makes the most assumptions and is therefore typically not used for panel data. In this case the model formulation is:
$$Y_{it}=\beta_{0}+\beta_{1}x_{1it}+...+\beta_{K}X_{Kit} + e_{it}$$
where the intercept $\beta_{0it}=\beta_0$for all $i,t$ and slope $\beta_{kit}=\beta_k$ for all $i,t$.
2) **fixed effects** - assumes that there are unknown or unobserved unique aspects about the individuals or heterogeneity among individuals $a_i$ that are not explained by the independent variables but influence the outcome variable of interest. They do not vary with time or in other words are fixed over time but may be **correlated** with independent variables $X_{it}$.
In this case the intercept can be different for each individual $\beta_{0i}$, but the other coefficients are assumed to be the same across all the individuals.
These individual $a_i$ effects can be correlated with the independent variables $X$. This model can be expressed as
$$Y_{it}=\beta_{0}+\beta_{1}X_{1it}+...\beta_{K}X_{Kit}+ a_i +e_{it}$$
or alternatively the individual effects can be absorbed into an individual-specific intercept term $\beta_{0i}$:
$$Y_{it}=\beta_{0i}+\beta_{1}X_{1it}+...\beta_{k}X_{kit} +e_{it}$$
This type of panel regression makes the fewest assumptions.
3) **random effects** - assumes that there are unknown or unobserved unique qualities about the individuals that influence the outcome variable of interest that are **not correlated** with the independent variables. Thus, the random effects model actually makes **more assumptions** than the fixed effect model. We will not consider this model here.
There is quite a lot of discussion in econometrics around which model is appropriate to use and when, but we are not going to dwell on this topic here, as our main focus in this case study is on multicollinearity. However, here are some additional references on these topics for further reading:
See [here](https://www.bauer.uh.edu/rsusmel/phd/ec1-15.pdf) and [here](https://sites.google.com/site/econometricsacademy/econometrics-models/panel-data-models) and [here](https://cran.r-project.org/web/packages/plm/vignettes/plmPackage.html) for more information about these different models.
## **Donohue, et al.**
***
OK! We are now ready to start analyzing our data!
In our case, we will be performing a fixed effect panel regression analysis, as we do in fact think that some of the unobserved qualities about the different states may be correlated with some of our independent variables. For example, the level of economic opportunity might be an unobserved feature about the states that influences violent crime rate and would be possibly correlated with poverty rate and unemployment. There are statistical tests for evaluating which model is the most appropriate, which are implemented in the `plm` package that we will be using.
To perform our analysis we will be using the `plm` package. This stands for Panel Linear Model.
We need to use a special type of data to use this package, called a `pdata.frame` which is short for panel data frame. This allows us to specify that we are using panel data and what the panel structure looks like, i.e., how the different observations are meant to be grouped together, both by state and by time.
We need to indicate which variable should be used to identify the individuals in our panel, and what variable should be used to identify the time periods in our panel. In our case the `STATE` variable identifies the individuals and the `YEAR` variable identifies the time periods.
We can specify this structure using the `pdata.frame()` function of the `plm` package, by using the `index` argument, where the individual variable is specified first followed by the time variable, like so: `index=c("Individual_Variable_NAME", "Time_Period_Variable_NAME")`.
```{r}
d_panel_DONOHUE <- pdata.frame(DONOHUE_DF, index = c("STATE", "YEAR"))
class(d_panel_DONOHUE)
slice_head(d_panel_DONOHUE, n = 3)
```
Indeed we have now created a pdata.frame object and we can see that the row names show the individual states and time period years.
OK, now we are ready to run our panel linear model on our panel data frame.
To do so we will use the `plm()` function and we will specify the formula for our model, where the dependent variable `Viol_crime_rate_1k_log` will be on the left of our `~` sign and all of the independent variables will be listed on the right with `+` signs in between each.
As discussed above, there are different types of panel data analysis, and which type of model is fit is controlled by the `effect` and `model` arguments to `plm()`. So we need to specify what type of `effect` we would like to model and what type of `model` we would like to use.
There are three main options for the `effect` argument:
1) individual - model for the effect of individual identity
2) time - model for the effect of time
3) twoways - meaning modeling for the effect of both individual identity and time
There are four main options for the `model` argument:
1) pooling - standard pooled ordinary least squares regression model
2) within - fixed effects model (variation between individuals is ignored, model compares individuals to themselves at different periods of time)
3) between - fixed effects model (variation within individuals from one time point to another is ignored, model compares different individuals at each point of time)
4) random - random effects (each state has a different intercept but force it to follow a normal distribution - requires more assumptions)
Typically it is best to think about what you are trying to evaluate with your data in trying to choose how to model your data. However, there are also some tests that can help to assess this which we will briefly cover.
We are interested in how violence in each state varied over time, thus we are interested in within `STATE`variation, so we will perform our PLM analysis with the `model = within` argument to perform this particular type of fixed effects model.
We also speculate that there is an effect of individual `STATE` identity and time on violent crime rate. In other words, we expect some states to have high rates of crime, and others to have low rates of crime. We also expect crime to change over time. This means we want to use the `effect = "twoways"` argument to `plm()`.
Here is how we would implement this for the Donohue data:
```{r}
DONOHUE_OUTPUT <- plm(Viol_crime_rate_1k_log ~
RTC_LAW +
White_Male_15_to_19_years +
White_Male_20_to_39_years +
Black_Male_15_to_19_years +
Black_Male_20_to_39_years +
Other_Male_15_to_19_years +
Other_Male_20_to_39_years +
Unemployment_rate +
Poverty_rate +
Population_log +
police_per_100k_lag,
effect = "twoways",
model = "within",
data = d_panel_DONOHUE
)
```
To see the results we can use the base `summary()` function. We can view this output in tidy format using the `tidy()` function of the `broom` package.
We will add an `analysis` variable as a label for plots.
```{r}
summary(DONOHUE_OUTPUT)
DONOHUE_OUTPUT_TIDY <- tidy(DONOHUE_OUTPUT, conf.int = 0.95)
DONOHUE_OUTPUT_TIDY
DONOHUE_OUTPUT_TIDY$Analysis <- "Analysis 1"
```
As discussed above, we are choosing to use a fixed effects model because we believe it is most appropriate for our data. However, there are statistical tests implemented in the `plm` package that would allow us to test whether one of the other models would be as appropriate for our data. For example, we could perform a test to determine if we could have simply used a pooled model, i.e., the test evaluates whether the coefficients (including the intercepts) are equal across individuals. This test is implemented in the `pooltest()` function of the `plm` package and performs a comparison between the pooled model to the fixed effect within model. To test if using a random effect model would be more appropriate compared to the fixed effect model, one could use the [Hausman test](https://en.wikipedia.org/wiki/Durbin%E2%80%93Wu%E2%80%93Hausman_test){target="_blank"} (also called the Durbin-Wu-Hausman test). This test is implemented using the `phtest()` function of the `plm` package.
Finally, we could also perform a test to evaluate if there is indeed an individual (state) effect and a time effect in our model using the `plmtest()` function of the `plm` package.
For more information on these tests and this package, see [here](https://cran.r-project.org/web/packages/plm/vignettes/plmPackage.html){target="_blank"} and [here](http://www.princeton.edu/~otorres/Panel101R.pdf){target="_blank"}.
## **Mustard and Lott**
***
OK, now we will do the same for the Mustard and Lott analysis. In this case we would have a very large formula to write. So instead, we can use the `as.formula()` function of the `stats` package and the base `paste()` function to combine all of our explanatory variables into one formula without making a mistake. First we will create an object where we select only the explanatory variables.
```{r}
LOTT_variables <- LOTT_DF %>%
dplyr::select(
RTC_LAW,
contains(c("White", "Black", "Other")),
Unemployment_rate,
Poverty_rate,
Population_log,
police_per_100k_lag
) %>%
colnames()
LOTT_fmla <- as.formula(paste(
"Viol_crime_rate_1k_log ~",
paste(LOTT_variables, collapse = " + ")
))
LOTT_fmla
```
That is quite the formula!
OK, now again we will make a panel data frame and we will fit a fixed effect two-way model for time and individuals (`STATE`) with this data as well.
```{r}
d_panel_LOTT <- pdata.frame(LOTT_DF, index = c("STATE", "YEAR"))
LOTT_OUTPUT <- plm(LOTT_fmla,
model = "within",
effect = "twoways",
data = d_panel_LOTT
)
summary(LOTT_OUTPUT)
LOTT_OUTPUT_TIDY <- tidy(LOTT_OUTPUT, conf.int = 0.95)
LOTT_OUTPUT_TIDY$Analysis <- "Analysis 2"
```
## **RTC coefficient comparison**
***
Now let's make a plot to compare the coefficient estimate for the Right-to-carry law adoption variable in each model.
First we will combine model fit information for this coefficient for each model.
```{r}
comparing_analyses <- DONOHUE_OUTPUT_TIDY %>%
bind_rows(LOTT_OUTPUT_TIDY) %>%
filter(term == "RTC_LAWTRUE")
comparing_analyses
```
We can see that for the first analysis (similar to the [Donohue et al.](https://www.nber.org/papers/w23510.pdf){target="_blank"} study) the coefficient estimate for the presence of a permissive right-to-carry law is positive, while for the second analysis (similar to the [Mustard and Lott](https://chicagounbound.uchicago.edu/cgi/viewcontent.cgi?article=1150&context=law_and_economics){target="_blank"} study) the coefficient estimate is negative. Thus in the first analysis we could conclude that the effect of adopting permissive right-to-carry laws may be associated with increases in violent crime (although this was not a significant result (in contrast with the real [Donohue et al.](https://www.nber.org/papers/w23510.pdf){target="_blank"} study )); while in the other analysis we could conclude that the laws may be associated with decreases in violent crime.
Let's make a plot of this finding. We will show error bars for the coefficient estimates for both analyses using the `geom_errorbar()` function of the `ggplot2` package. This requires specifying the minimum and maximum for our error bar, which in our case will be the low and high values of our confidence intervals for the coefficient estimates. We will also add a horizontal line at y = 0 using the `geom_hline()` function of the `ggplot2` package.
Finally we will add arrows to emphasize the difference in the direction of the findings using the `geom_segment()` function of the `ggplot2` package. Using the `arrow()` function, we can specify details about the arrow we would like to add.
```{r}
comparing_analyses_plot <- ggplot(comparing_analyses) +
geom_point(aes(x = Analysis, y = estimate)) +
geom_errorbar(aes(x = Analysis, ymin = conf.low, ymax = conf.high), width = 0.25) +
geom_hline(yintercept = 0, color = "red") +
scale_y_continuous(
breaks = seq(-0.2, 0.2, by = 0.05),
labels = seq(-0.2, 0.2, by = 0.05),
limits = c(-0.2, 0.2)
) +
geom_segment(aes(x = 1, y = 0.125, xend = 1, yend = 0.175),
arrow = arrow(angle = 45, ends = "last", type = "open"),
size = 2,
color = "green",
lineend = "butt",
linejoin = "mitre"
) +
geom_segment(aes(x = 2, y = -0.125, xend = 2, yend = -0.175),
arrow = arrow(angle = 45, ends = "last", type = "open"),
size = 2,
color = "red",
lineend = "butt",
linejoin = "mitre"
) +
theme_minimal() +
theme(
axis.title.x = element_blank(),
axis.text = element_text(size = 8, color = "black")
) +
labs(
title = "Effect estimate on ln(violent crimes per 100,000 people)",
y = " Effect estimate (95% CI)"
)
comparing_analyses_plot
```
We can see that the confidence interval from analysis 1 is mostly covering positive values, while the entire confidence interval is negative for analysis 2.
# **Multicollinearity analysis**