-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathREADME.Rmd
186 lines (140 loc) · 9.39 KB
/
README.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
---
output: github_document
always_allow_html: TRUE
---
# `peacesciencer`: Tools and Data for Quantitative Peace Science
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
```{r loadstuff, echo=FALSE, results="hide", message=FALSE}
library("badger")
library("tidyverse")
library(kableExtra)
library(peacesciencer)
library(stevemisc)
```
```{r, echo = FALSE, results='asis'}
cat(
badge_cran_release("peacesciencer", "green"),
badge_cran_download("peacesciencer", "grand-total", "green"),
badge_cran_download("peacesciencer", "last-month", "green"),
badge_cran_download("peacesciencer", "last-week", "green")
)
```
<img src="http://svmiller.com/images/peacesciencer-hexlogo.png" alt="peacesciencer hexlogo" align="right" width="200" style="padding: 0 15px; float: right;"/>
`peacesciencer` is an R package including various functions and data sets to allow easier analyses in the field of quantitative peace science. The goal is to provide an R package that reasonably approximates what made EUGene so attractive to scholars working in the field of quantitative peace science in the early 2000s. EUGene shined because it encouraged replications of conflict models while having the user also generate data from scratch. Likewise, this R package will offer tools to approximate what EUGene did within the R environment (i.e. not requiring Windows for installation).
<!-- [EUGene](https://journals.sagepub.com/doi/10.1177/0738894211413055) -->
# Installation
You can install this on CRAN, as follows:
```r
install.packages("peacesciencer")
```
You can install the development version of this package through the `devtools` package. The development version of the package invariably has more goodies, but may or may not be at various levels of stress-testing.
```r
devtools::install_github("svmiller/peacesciencer")
```
# How to Use `{peacesciencer}`
New users should read two things to get started. [The package's website](http://svmiller.com/peacesciencer/index.html) has an exhaustive list and description of [all the functions and data included](http://svmiller.com/peacesciencer/reference/index.html) in the package. [`{peacesciencer}` has a user's guide](http://svmiller.com/peacesciencer/ms.pdf) that is worth reading. The user's guide points to its potential uses and benefits while also offering some encouragement for those completely new to the R programming language. The package is designed to be accessible to those with no prior experience with R, though completely new users who feel lost or overwhelmed should learn about [the "tidy" approach to R](https://www.tidyverse.org/) to help them get started.
The workflow is going to look something like this. First, start with one of two processes to create either dyad-year or state-year data. The dyad-year data are created with the `create_dyadyears()` function. It has a few optional parameters with hidden defaults. The user can specify what kind of state system (`system`) data they want to use---either Correlates of War (`"cow"`) or Gleditsch-Ward (`"gw"`), whether they want to extend the data to the most recently concluded calendar year (`mry`) (i.e. Correlates of War state system membership data are current as of Dec. 31, 2016 and the script can extend that to the end of the most recently concluded calendar year), and whether the user wants directed or non-directed dyad-year data (`directed`).
The `create_stateyears()` works much the same way, though "directed" and "non-directed" make no sense in the state-year context. Both functions default to Correlates of War state system membership data to the most recently concluded calendar year.
Thereafter, the user can specify what additional variables they want added to these dyad-year or state-year data. Do note: the additional functions lean primarily on Correlates of War state code identifiers. Indeed, the bulk of the quantitative peace science data ecosystem is built around the Correlates of War project. The variables the user wants are added in [a "pipe"](https://r4ds.had.co.nz/pipes.html) in a process like this. Do note that the user may want to break up the data-generating process into a few manageable "chunks" (e.g. first generating dyad-year data and saving to an object, adding to it piece by piece).
Here's what this will look like in operation. Assume you want to create some data for something analogous to a "dangerous dyads" design for all non-directed dyad-years. Here's how you'd do it in `{peacesciencer}`, which is going to be lifted from the source R scripts for the user's guide. The first part of this code chunk will lean on core `{peacesciencer}` functionality whereas the other stuff is some post-processing and, as a bonus, some modeling.
```{r, cache=F, message=F, eval=T}
# library(tidyverse) # load this first for most/all things
# library(peacesciencer) # the package of interest
# library(stevemisc) # a dependency, but also used for standardizing variables for better interpretation
library(tictoc)
tic()
create_dyadyears(directed = FALSE, mry = FALSE) %>%
filter_prd() %>%
add_gml_mids(keep = NULL) %>%
add_peace_years() %>%
add_nmc() %>%
add_democracy() %>%
add_cow_alliance() %>%
add_sdp_gdp() -> Data
Data %>%
mutate(landcontig = ifelse(conttype == 1, 1, 0)) %>%
mutate(cowmajdyad = ifelse(cowmaj1 == 1 | cowmaj2 == 1, 1, 0)) %>%
# Create estimate of militarization as milper/tpop
# Then make a weak-link
mutate(milit1 = milper1/tpop1,
milit2 = milper2/tpop2,
minmilit = ifelse(milit1 > milit2,
milit2, milit1)) %>%
# create CINC proportion (lower over higher)
mutate(cincprop = ifelse(cinc1 > cinc2,
cinc2/cinc1, cinc1/cinc2)) %>%
# create weak-link specification using Quick UDS data
mutate(mindemest = ifelse(xm_qudsest1 > xm_qudsest2,
xm_qudsest2, xm_qudsest1)) %>%
# Create "weak-link" measure of jointly advanced economies
mutate(minwbgdppc = ifelse(wbgdppc2011est1 > wbgdppc2011est2,
wbgdppc2011est2, wbgdppc2011est1)) -> Data
# r2sd() is in {stevemisc}, a {peacesciencer} dependency.
# This is just for a more readable regression output.
Data %>%
mutate_at(vars("cincprop", "mindemest", "minwbgdppc", "minmilit"),
~r2sd(.)) -> Data
broom::tidy(modDD <- glm(gmlmidonset ~ landcontig + cincprop + cowmajdyad + cow_defense +
mindemest + minwbgdppc + minmilit +
gmlmidspell + I(gmlmidspell^2) + I(gmlmidspell^3), data= Data,
family=binomial(link="logit")))
toc()
```
Here is how you might do a standard civil conflict analysis using Gleditsch-Ward states and UCDP conflict data.
```{r, cache=F, message=F, eval=T}
tic()
create_stateyears(system = 'gw') %>%
filter(year %in% c(1946:2019)) %>%
add_ucdp_acd(type=c("intrastate"), only_wars = FALSE) %>%
add_peace_years() %>%
add_democracy() %>%
add_creg_fractionalization() %>%
add_sdp_gdp() %>%
add_rugged_terrain() -> Data
create_stateyears(system = 'gw') %>%
filter(year %in% c(1946:2019)) %>%
add_ucdp_acd(type=c("intrastate"), only_wars = TRUE) %>%
add_peace_years() %>%
rename_at(vars(ucdpongoing:ucdpspell), ~paste0("war_", .)) %>%
left_join(Data, .) -> Data
Data %>%
arrange(gwcode, year) %>%
group_by(gwcode) %>%
mutate_at(vars("xm_qudsest", "wbgdppc2011est",
"wbpopest"), list(l1 = ~lag(., 1))) %>%
rename_at(vars(contains("_l1")),
~paste("l1", gsub("_l1", "", .), sep = "_") ) -> Data
modCW <- list()
broom::tidy(modCW$"All UCDP Conflicts" <- glm(ucdponset ~ l1_wbgdppc2011est + l1_wbpopest +
l1_xm_qudsest + I(l1_xm_qudsest^2) +
newlmtnest + ethfrac + relfrac +
ucdpspell + I(ucdpspell^2) + I(ucdpspell^3), data=subset(Data),
family = binomial(link="logit")))
broom::tidy(modCW$"Wars Only" <- glm(war_ucdponset ~ l1_wbgdppc2011est + l1_wbpopest +
l1_xm_qudsest + I(l1_xm_qudsest^2) +
newlmtnest + ethfrac + relfrac +
war_ucdpspell + I(war_ucdpspell^2) + I(war_ucdpspell^3), data=subset(Data),
family = binomial(link="logit")))
toc()
```
# Citing What You Do in `{peacesciencer}`
You can (and should) cite what you do in `{peacesciencer}`. The package includes a data frame of a `BibTeX` file (`ps_bib`) and a function for finding and returning `BibTeX` entries that you can include in your projects. This is the `ps_cite()` function. The `ps_cite()` function takes a string and does a partial match for relevant keywords (as `KEYWORDS`) associated with entries in the `ps_bib` file. For example, you can (and should) cite the package itself.
```{r}
ps_cite("peacesciencer")
```
You can see what are the relevant citations to consider using for the data returned by `add_democracy()`
```{r}
ps_cite("add_democracy()")
```
You can also return partial matches to see what citations are associated with, say, alliance data in this package.
```{r}
ps_cite("alliance")
```
This function might expand in complexity in future releases, but you can use it right now for finding appropriate citations. You an also scan the `ps_bib` data to see what is in there.
# Issues/Requests
`{peacesciencer}` is already more than capable to meet a wide variety of needs in the peace science community. Users are free to raise an issue on the project's Github if some feature is not performing as they think it should or if there are additions they would like to see.