### Example of using add_cow_alliance()
Source: http://svmiller.com/peacesciencer/reference/add_cow_alliance.html
This example demonstrates how `add_cow_alliance` was previously used with a dyad-year data frame. It requires the `tidyverse` and `magrittr` libraries. This code will now stop execution due to deprecation.
```R
if (FALSE) { # \dontrun{
# just call `library(tidyverse)` at the top of the your script
library(magrittr)
cow_ddy %>% add_cow_alliance()
} # }
```
--------------------------------
### Load Terrthreat Data
Source: http://svmiller.com/peacesciencer/reference/terrthreat.html
This code snippet shows how to access the 'terrthreat' dataset. No specific setup is required beyond having the package installed.
```r
terrthreat
```
--------------------------------
### Install R Packages
Source: http://svmiller.com/peacesciencer/ms.html
Installs the peacesciencer and tidyverse packages. Ensure you have R and RStudio set up before running this command.
```r
# Install the packages for use
install.packages(c("tidyverse", "peacesciencer"))
```
--------------------------------
### Add All LEAD Data to Leader-Year Data
Source: http://svmiller.com/peacesciencer/reference/add_lead.html
This example demonstrates how to add all available LEAD data to a leader-year dataframe. Ensure the tidyverse and magrittr libraries are loaded.
```r
library(magrittr)
create_leaderyears() %>% add_lead()
```
--------------------------------
### Load Core R Packages
Source: http://svmiller.com/peacesciencer/articles/coerce-dispute-year-dyad-year.html
Loads the tidyverse, peacesciencer, and kableExtra packages. Ensure these are installed before running.
```r
library(tidyverse)
#> ┴─ Attaching core tidyverse packages ┴─ tidyverse 2.0.0 ┴─
#> ✔ dplyr 1.1.4 ✔ readr 2.1.4
#> ✔ forcats 1.0.0 ✔ stringr 1.5.0
#> ✔ ggplot2 3.5.1 ✔ tibble 3.3.0
#> ✔ lubridate 1.9.4 ✔ tidyr 1.3.0
#> ✔ purrr 1.1.0
#> ┴─ Conflicts ┴─ tidyverse_conflicts() ┴─
#> ✖ dplyr::filter() masks stats::filter()
#> ✖ dplyr::lag() masks stats::lag()
#> ⌂ Use the conflicted package () to force all conflicts to become errors
library(peacesciencer)
#> The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
#> which was just loaded, will retire in October 2023.
#> Please refer to R-spatial evolution reports for details, especially
#> https://r-spatial.org/r/2023/05/15/evolution4.html.
#> It may be desirable to make the sf package available;
#> package maintainers should consider adding sf to Suggests:.
#> The sp package is now running under evolution status 2
#> (status 2 uses the sf package in place of rgdal)
#> {peacesciencer} includes additional remote data for separate download. Please type ?download_extdata() for more information.
#> This message disappears on load when these data are downloaded and in the package's `extdata` directory.
library(kableExtra)
#>
#> Attaching package: 'kableExtra'
#>
#> The following object is masked from 'package:dplyr':
#>
#> group_rows
```
--------------------------------
### Install peacesciencer Development Version
Source: http://svmiller.com/peacesciencer/index.html
Install the latest development version of the peacesciencer package from GitHub using the devtools package. This version may contain more features but might be less stable.
```r
devtools::install_github("svmiller/peacesciencer")
```
--------------------------------
### Load `peacesciencer` and Tidyverse Libraries
Source: http://svmiller.com/peacesciencer/articles/versions.html
Load the necessary libraries for data manipulation and visualization. Ensure these packages are installed before running.
```r
library(tidyverse)
library(peacesciencer)
library(kableExtra)
```
--------------------------------
### Install peacesciencer from CRAN
Source: http://svmiller.com/peacesciencer/index.html
Use this command to install the stable version of the peacesciencer package from the Comprehensive R Archive Network (CRAN).
```r
install.packages("peacesciencer")
```
--------------------------------
### Display All Data Versions with `ps_version()`
Source: http://svmiller.com/peacesciencer/articles/versions.html
Use `ps_version()` to get a data frame of all available data versions in the package. This function can be customized with `kbl()` for formatted output.
```r
ps_version() %>%
kbl(., caption = "Data Versions in `{peacesciencer}`",
align =c("c", "l", "c", "c"),
booktabs = TRUE, longtable = TRUE) %>%
kable_styling(position = "center", full_width = F,
bootstrap_options = "striped") %>%
row_spec(0, bold=TRUE)
```
--------------------------------
### Load R Packages
Source: http://svmiller.com/peacesciencer/ms.html
Loads the installed tidyverse and peacesciencer packages into the current R session. This must be done every time you start a new R session.
```r
# Load the packages for use
library(tidyverse)
library(peacesciencer)
```
--------------------------------
### Load Tidyverse and peacesciencer Packages
Source: http://svmiller.com/peacesciencer/articles/different-data-types.html
Loads essential R packages including tidyverse, peacesciencer, and lubridate. Ensure these packages are installed before running.
```r
library(tidyverse)
library(peacesciencer)
library(lubridate)
packageVersion("peacesciencer")
packageVersion("isard")
Sys.Date()
```
--------------------------------
### Add Minimum Distance Data (use_extdata=FALSE)
Source: http://svmiller.com/peacesciencer/reference/add_minimum_distance.html
This example demonstrates adding minimum distance data when external data is not used. The `slice` argument defaults to 'first' in this scenario.
```R
# just call `library(tidyverse)` at the top of the your script
library(magrittr)
cow_ddy %>% add_minimum_distance(use_extdata = FALSE)
```
--------------------------------
### Add Democracy Data to Stateyears (COW System)
Source: http://svmiller.com/peacesciencer/reference/add_democracy.html
This example demonstrates adding democracy indicators to a state-years dataset created with the "cow" system. The join is performed using `ccode` and `year`.
```R
create_stateyears(system="cow") %>% add_democracy()
```
--------------------------------
### Process COW Mid-Year Displacements with Start Month
Source: http://svmiller.com/peacesciencer/reference/whittle_conflicts_startmonth.html
This snippet shows the application of `whittle_conflicts_onsets` and `whittle_conflicts_startmonth` to the `cow_mid_dirdisps` dataset. It includes a join operation and prepares the data for further analysis.
```R
cow_mid_dirdisps %>% whittle_conflicts_onsets() %>% whittle_conflicts_startmonth()
```
--------------------------------
### Create Base Data and Add GML-MID Conflict Info
Source: http://svmiller.com/peacesciencer/ms.html
Generates base dyad-year data and adds conflict information from the GML-MID dataset. Ensure the peacesciencer package is installed and loaded.
```R
create_dyadyears(directed = FALSE, subset_years = c(1816:2010)) %>%
filter_prd() %>%
add_gml_mids(keep = NULL)
```
--------------------------------
### Anti-Join Example
Source: http://svmiller.com/peacesciencer/articles/joins.html
Demonstrates the use of `anti_join()` to filter out invalid dyads from a combined dataset. This is crucial for removing observations that do not meet the criteria for valid dyads, such as those with no temporal overlap.
```r
valid_dyads <- tibble(ccode1 = c(2, 20, 200),
ccode2 = c(20, 200, 900),
year = c(2016, 2017, 2018))
valid_dyads %>%
bind_rows(., false_cow_dyads %>% select(ccode1:year)) -> valid_and_invalid
valid_and_invalid
#> # A tibble: 63 × 3
#> ccode1 ccode2 year
#>
#> 1 2 20 2016
#> 2 20 200 2017
#> 3 200 900 2018
#> 4 115 817 1975
#> 5 210 255 1945
#> 6 211 255 1945
#> 7 223 678 1990
#> 8 223 680 1990
#> 9 255 210 1945
#> 10 255 211 1945
#> # ℹ 53 more rows
valid_and_invalid %>%
# remove those invalid dyads-years
anti_join(., false_cow_dyads)
#> Joining with `by = join_by(ccode1, ccode2, year)`
#> # A tibble: 3 × 3
#> ccode1 ccode2 year
#>
#> 1 2 20 2016
#> 2 20 200 2017
#> 3 200 900 2018
```
--------------------------------
### Process GML Direct Displacements with Start Month
Source: http://svmiller.com/peacesciencer/reference/whittle_conflicts_startmonth.html
This snippet demonstrates how to apply `whittle_conflicts_onsets` and `whittle_conflicts_startmonth` to the `gml_dirdisp` dataset. Ensure `library(tidyverse)` and `library(magrittr)` are loaded.
```R
library(magrittr)
gml_dirdisp %>% whittle_conflicts_onsets() %>% whittle_conflicts_startmonth()
```
--------------------------------
### Add UCDP Onsets with COW Codes
Source: http://svmiller.com/peacesciencer/reference/add_ucdp_onsets.html
This example shows how to add UCDP onset data to state-year data after converting it to COW codes using `add_gwcode_to_cow()`. Ensure `tidyverse` and `dplyr` are loaded.
```R
create_stateyears() %>%
add_gwcode_to_cow() %>% add_ucdp_onsets()
#> Joining with `by = join_by(year, gwcode)`
#> # A tibble: 17,511 × 10
#> ccode cw_name year gwcode sumnewconf sumonset1 sumonset2 sumonset3 sumonset5
#>
#> 1 2 United… 1816 2 0 0 0 0 0
#> 2 2 United… 1817 2 0 0 0 0 0
#> 3 2 United… 1818 2 0 0 0 0 0
#> 4 2 United… 1819 2 0 0 0 0 0
#> 5 2 United… 1820 2 0 0 0 0 0
#> 6 2 United… 1821 2 0 0 0 0 0
#> 7 2 United… 1822 2 0 0 0 0 0
#> 8 2 United… 1823 2 0 0 0 0 0
#> 9 2 United… 1824 2 0 0 0 0 0
#> 10 2 United… 1825 2 0 0 0 0 0
#> # ℹ 17,501 more rows
#> # ℹ 1 more variable: sumonset10
```
--------------------------------
### Add Democracy Data to Stateyears (GW System)
Source: http://svmiller.com/peacesciencer/reference/add_democracy.html
This example shows how to add democracy indicators to a state-years dataset generated using the "gw" system. The function automatically joins the data by `gwcode` and `year`.
```R
create_stateyears(system="gw") %>% add_democracy()
```
--------------------------------
### Add UCDP Onsets to State-Year Data
Source: http://svmiller.com/peacesciencer/reference/add_ucdp_onsets.html
This example demonstrates how to add UCDP onset data to state-year data created using the `create_stateyears()` function. Ensure `tidyverse` and `dplyr` are loaded.
```R
# just call `library(tidyverse)` at the top of the your script
library(magrittr)
library(dplyr)
create_stateyears(system="gw") %>% add_ucdp_onsets()
#> Joining with `by = join_by(gwcode, year)`
#> # A tibble: 20,652 × 10
#> gwcode gw_name microstate year sumnewconf sumonset1 sumonset2 sumonset3
#>
#> 1 2 United Stat… 0 1816 0 0 0 0
#> 2 2 United Stat… 0 1817 0 0 0 0
#> 3 2 United Stat… 0 1818 0 0 0 0
#> 4 2 United Stat… 0 1819 0 0 0 0
#> 5 2 United Stat… 0 1820 0 0 0 0
#> 6 2 United Stat… 0 1821 0 0 0 0
#> 7 2 United Stat… 0 1822 0 0 0 0
#> 8 2 United Stat… 0 1823 0 0 0 0
#> 9 2 United Stat… 0 1824 0 0 0 0
#> 10 2 United Stat… 0 1825 0 0 0 0
#> # ℹ 20,642 more rows
#> # ℹ 2 more variables: sumonset5 , sumonset10
```
--------------------------------
### Load Tidyverse and Peace Science Packages in R
Source: http://svmiller.com/peacesciencer/articles/democracy.html
Loads core tidyverse packages, the peacesciencer package for peace science data, and auxiliary packages like isard, kableExtra, stevemisc, and stevethemes. Ensure these packages are installed before running.
```r
library(tidyverse)
#> ┼─ Attaching core tidyverse packages ┼─ 2.0.0 ┼─
#> ✔️ dplyr 1.1.4 ✔️ readr 2.1.4
#> ✔️ forcats 1.0.0 ✔️ stringr 1.5.0
#> ✔️ ggplot2 3.5.1 ✔️ tibble 3.3.0
#> ✔️ lubridate 1.9.4 ✔️ tidyr 1.3.0
#> ✔️ purrr 1.1.0
#> ┼─ Conflicts ┼─ tidyverse_conflicts() ┼─
#> ✖️ dplyr::filter() masks stats::filter()
#> ✖️ dplyr::lag() masks stats::lag()
#> ℹ️ Use the conflicted package () to force all conflicts to become errors
library(peacesciencer)
#> The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
#> which was just loaded, will retire in October 2023.
#> Please refer to R-spatial evolution reports for details, especially
#> https://r-spatial.org/r/2023/05/15/evolution4.html.
#> It may be desirable to make the sf package available;
#> package maintainers should consider adding sf to Suggests:.
#> The sp package is now running under evolution status 2
#> (status 2 uses the sf package in place of rgdal)
#> {peacesciencer} includes additional remote data for separate download. Please type ?download_extdata() for more information.
#> This message disappears on load when these data are downloaded and in the package's `extdata` directory.
library(isard)
library(kableExtra)
#>
#> Attaching package: 'kableExtra'
#>
#> The following object is masked from 'package:dplyr':
#>
#> group_rows
library(stevemisc)
#>
#> Attaching package: 'stevemisc'
#>
#> The following object is masked from 'package:lubridate':
#>
#> dst
#>
#> The following object is masked from 'package:dplyr':
#>
#> tbl_df
library(stevethemes)
```
--------------------------------
### Load Directed Leader-Dyadic Dispute-Year Data
Source: http://svmiller.com/peacesciencer/reference/gml_mid_ddlydisps.html
This is the primary usage example for accessing the `gml_mid_ddlydisps` dataset in R. Ensure the package is installed and loaded.
```R
gml_mid_ddlydisps
```
--------------------------------
### View Documentation for create_leaderyears()
Source: http://svmiller.com/peacesciencer/ms.html
Opens the documentation for the `create_leaderyears()` function in R, providing detailed information about its arguments and usage.
```R
?create_leaderyears()
```
--------------------------------
### Load Libraries and Check Versions
Source: http://svmiller.com/peacesciencer/articles/parlor-tricks.html
Loads necessary libraries and checks the versions of peacesciencer and its dependency isard. Also displays the current system date.
```r
library(tidyverse)
library(peacesciencer)
packageVersion("peacesciencer")
#> [1] '1.2.0'
packageVersion("isard") # a dependency, but not formally required.
#> [1] '0.1.0'
Sys.Date()
#> [1] "2025-07-17"
```
--------------------------------
### Access Function Documentation in R
Source: http://svmiller.com/peacesciencer/ms.html
Opens the documentation for the create_stateyears() function in R. This is useful for understanding function arguments and default values.
```r
# See documentation for create_stateyears() function.
?create_stateyears()
```
--------------------------------
### Filter Duplicate Dyad-Years by Start Month
Source: http://svmiller.com/peacesciencer/articles/coerce-dispute-year-dyad-year.html
This R code snippet joins the `hold_this` dataset with `cow_mid_disps` to include the start month, then groups by dyad and year to identify duplicates. It filters to keep only the record with the minimum start month for each dyad-year group, effectively resolving duplicate entries.
```r
hold_this %>%
left_join(., cow_mid_disps %>% select(dispnum, stmon)) %>%
arrange(ccode1, ccode2, year) %>%
group_by(ccode1, ccode2, year) %>%
mutate(duplicated = ifelse(n() > 1, 1, 0)) %>%
group_by(ccode1, ccode2, year, duplicated) %>%
# Keep the reciprocated ones, where non-reciprocated ones exist
filter(stmon == min(stmon)) %>%
arrange(ccode1, ccode2, year) %>%
# practice safe group_by()
ungroup() -> hold_this
```
--------------------------------
### Whittle Duplicate Conflict-Years by Lowest Start Month
Source: http://svmiller.com/peacesciencer/reference/whittle_conflicts_startmonth.html
Use this function to keep observations with the lowest start month when multiple disputes occur in the same dyad and year. It requires a data frame with a declared conflict attribute type.
```R
whittle_conflicts_startmonth(data)
```
--------------------------------
### whittle_conflicts_startmonth
Source: http://svmiller.com/peacesciencer/reference/whittle_conflicts_startmonth.html
Keeps observations with the lowest start month when multiple disputes occur in the same dyad-year.
```APIDOC
## whittle_conflicts_startmonth()
### Description
This function takes a data frame with a declared conflict attribute type and, grouping by dyad and year, returns only those observations that have the lowest start month. This is useful for resolving duplicate dyad-year entries that arise when multiple disputes occur within the same year for the same dyad.
### Arguments
* **data** (data.frame) - A data frame with a declared conflict attribute type.
### Value
A data frame containing unique dyad-year observations, where for any given dyad-year, only the observation with the earliest start month is retained.
### Details
Dyads can have multiple disputes in a given year, leading to duplicate dyad-year entries. This function provides a method to resolve these duplicates by selecting the observation that initiated the earliest. It is recommended to use this function as one of the final steps in data cleaning, as the choice of the lowest start month is primarily a mechanical way to select one observation among otherwise similar duplicates, rather than a substantive criterion.
### Shortcut
`wc_stmon(...)` is a shortcut for `whittle_conflicts_startmonth(...)`.
### References
Miller, Steven V. 2021. "How peacesciencer Coerces Dispute-Year Data into Dyad-Year Data". URL: http://svmiller.com/peacesciencer/articles/coerce-dispute-year-dyad-year.html
```
--------------------------------
### Create Leader-Days (Default)
Source: http://svmiller.com/peacesciencer/reference/create_leaderdays.html
Generates leader-day data using the default settings, which includes all leader-days as presented in Archigos without standardization.
```R
create_leaderdays()
```
--------------------------------
### View All Data Versions
Source: http://svmiller.com/peacesciencer/reference/ps_version.html
Call ps_version() without arguments to display version information for all datasets included in the package. This provides a comprehensive overview of available data and their versions.
```r
ps_version()
```
--------------------------------
### Cite the peacesciencer package
Source: http://svmiller.com/peacesciencer/reference/ps_cite.html
Use `ps_cite` with the package name to get the main citation for the peacesciencer package. This is useful for acknowledging the package in your research.
```r
ps_cite("peacesciencer")
#> @ARTICLE{peacesciencer-package,
#> AUTHOR = {Steven V. Miller},
#> JOURNAL = {Conflict Management and Peace Science},
#> TITLE = {peacesciencer}: An R Package for Quantitative Peace Science Research},
#> YEAR = {2022},
#> KEYWORDS = {peacesciencer, add_capital_distance(), add_ccode_to_gw(), add_gwcode_to_cow(), capitals},
#> URL = {http://svmiller.com/peacesciencer/}}
#>
```
--------------------------------
### Add SDP/GDP to stateyears Data (Default System)
Source: http://svmiller.com/peacesciencer/reference/add_sdp_gdp.html
Adds SDP and GDP data to the output of `create_stateyears()` using the default system. Note that `add_sdp_gdp()` is deprecated.
```R
create_stateyears() %>% add_sdp_gdp()
```
--------------------------------
### Export Data to Stata
Source: http://svmiller.com/peacesciencer/ms.html
This command exports the processed 'Data' object into a Stata-compatible .dta file. Ensure the 'haven' package is installed and loaded before execution.
```R
haven::write_dta(Data, "my-data.dta")
```
--------------------------------
### Access Leader Codes Data
Source: http://svmiller.com/peacesciencer/reference/leader_codes.html
This snippet shows how to access the `leader_codes` dataset in R. No specific setup or imports are required beyond having the dataset available.
```r
leader_codes
```
--------------------------------
### wc_onsets
Source: http://svmiller.com/peacesciencer/reference/whittle_conflicts_onsets.html
A simple, less wordy, shortcut for the `whittle_conflicts_onsets` function.
```APIDOC
## Function: wc_onsets
### Description
A shortcut function for `whittle_conflicts_onsets` that performs the same data coercion to unique conflict onsets.
### Usage
```R
wc_onsets(...)
```
### Arguments
- **...** - Optional arguments, primarily to make the shortcut work seamlessly with piping or other function calls.
```
--------------------------------
### Illustrative Leader Dyad-Year Data Loading
Source: http://svmiller.com/peacesciencer/articles/different-data-types.html
This code demonstrates how to load leader dyad-year data directly from a URL, mimicking the process that `create_leaderdyadyears()` would perform after `download_extdata()` has been used. It's useful for understanding the data structure and content.
```r
# create_leaderdyadyears() is effectively doing this.
# Let's do the G-W leader dyad-year data for illustration's sake.
# `download_extdata()` will download these data into the package directory.
# Thus, it is *not* downloading the data fresh each time.
the_url <- "https://svmiller.com/R/peacesciencer/gw_dir_leader_dyad_years.rds"
readRDS(url(the_url)) %>%
declare_attributes(data_type = "leader_dyad_year", system = "gw")
#> # A tibble: 2,336,990 × 11
#> # year obsid1 obsid2 gwcode1 gwcode2 gender1 gender2 leaderage1 leaderage2
#> #
#> # 1 1870 AFG-1868 AUH-1848 700 300 M M 45 40
#> # 2 1870 AFG-1868 BAV-1864 700 245 M M 45 39
#> # 3 1870 AFG-1868 BRA-1840 700 140 M M 45 45
#> # 4 1870 AFG-1868 CHN-1861 700 710 M M 45 35
#> # 5 1870 AFG-1868 COS-1870 700 94 M M 45 39
#> # 6 1870 AFG-1868 ECU-1869 700 130 M M 45 49
#> # 7 1870 AFG-1868 GMY-1858 700 255 M M 45 73
#> # 8 1870 AFG-1868 GRC-1863 700 350 M M 45 25
#> # 9 1870 AFG-1868 IRN-1848 700 630 M M 45 39
#> # 10 1870 AFG-1868 JPN-1868 700 740 M M 45 18
#> # ℹ 2,336,980 more rows
#> # ℹ 2 more variables: yrinoffice1 , yrinoffice2
# ^ compare with:
# download_extdata()
# create_leaderdyadyears()
```
--------------------------------
### download_extdata Function
Source: http://svmiller.com/peacesciencer/reference/download_extdata.html
Leverages R's `inst` directory flexibility to allow users to download extra data and store it in the package. The function checks if data already exists and can be configured to overwrite it.
```APIDOC
## download_extdata()
### Description
Downloads extra data and stores it in the package's `extdata` directory. Optionally overwrites existing data.
### Arguments
- **overwrite** (logical) - Optional - Defaults to `FALSE`. If `FALSE`, the function checks to see if the data has already been downloaded and does nothing if it has. If `TRUE`, the function redownloads the data.
### Value
Downloads extra data from https://svmiller.com and places it in the `extdata` directory within the package.
### Usage
```R
download_extdata(overwrite = FALSE)
```
### Data Sets Downloaded
#### Correlates of War Dyadic Trade Data Set (v. 4.0)
Directed dyad-year-level data for dyadic trade from the Correlates of War project. Trade values are rounded to three decimal points. The data size is approximately 4.1 megabytes.
| COLUMN | DESCRIPTION |
|-----------|----------------------------------------------------------------|
| `ccode1` | Correlates of War state code for the first state (numeric) |
| `ccode2` | Correlates of War state code for the second state (numeric) |
| `year` | The year (numeric) |
| `flow1` | Imports of `ccode1` from `ccode2`, in current million USD (numeric) |
| `flow2` | Imports of `ccode2` from `ccode1`, in current million USD (numeric) |
| `smoothflow1` | Smoothed `flow1` values (numeric) |
| `smoothflow2` | Smoothed `flow2` values (numeric) |
#### Directed Leader Dyad-Year Data, 1870-2015 (CoW States)
Directed leader dyad-year data from 1870-2015, based on Archigos data (v. 4.1). Standardized to observations where both leaders and states appear in the CoW state system data. The data size is approximately 2 megabytes.
| COLUMN | DESCRIPTION |
|-------------|--------------------------------------------------------------------------------|
| `year` | The year (numeric) |
| `obsid1` | Unique Archigos (v. 4.1) observation ID for the first leader (numeric) |
| `obsid2` | Unique Archigos (v. 4.1) observation ID for the second leader (numeric) |
| `ccode1` | Correlates of War state code for the first state (numeric) |
| `ccode2` | Correlates of War state code for the second state (numeric) |
| `gender1` | Gender of `obsid1` ('M' or 'F') (character) |
| `gender2` | Gender of `obsid2` ('M' or 'F') (character) |
| `leaderage1`| Approximate age of `obsid1` in the year (numeric) |
| `leaderage2`| Approximate age of `obsid2` in the year (numeric) |
| `yrinoffice1`| Running count for the tenure of `obsid1`, starting at 1 (numeric) |
| `yrinoffice2`| Running count for the tenure of `obsid2`, starting at 1 (numeric) |
#### Directed Leader Dyad-Year Data, 1870-2015 (Gleditsch-Ward States)
Directed leader dyad-year data from 1870-2015, based on Archigos data (v. 4.1). Represents all possible dyadic leader-pairings in the Archigos data (denominated in Gleditsch-Ward system), standardized to Gleditsch-Ward state system dates. The data size is approximately 2.2 megabytes.
| COLUMN | DESCRIPTION |
|-------------|--------------------------------------------------------------------------------|
| `year` | The year (numeric) |
| `obsid1` | Unique Archigos (v. 4.1) observation ID for the first leader (numeric) |
| `obsid2` | Unique Archigos (v. 4.1) observation ID for the second leader (numeric) |
| `gwcode1` | Gleditsch-Ward state code for the first state (numeric) |
| `gwcode2` | Gleditsch-Ward state code for the second state (numeric) |
| `gender1` | Gender of `obsid1` ('M' or 'F') (character) |
| `gender2` | Gender of `obsid2` ('M' or 'F') (character) |
| `leaderage1`| Approximate age of `obsid1` in the year (numeric) |
| `leaderage2`| Approximate age of `obsid2` in the year (numeric) |
| `yrinoffice1`| Running count for the tenure of `obsid1`, starting at 1 (numeric) |
| `yrinoffice2`| Running count for the tenure of `obsid2`, starting at 1 (numeric) |
```
--------------------------------
### Load Required Libraries
Source: http://svmiller.com/peacesciencer/articles/joins.html
Loads the `tidyverse`, `lubridate`, and `peacesciencer` libraries. These are essential for data manipulation and analysis within the package.
```r
library(tidyverse)
library(lubridate)
library(peacesciencer)
```
--------------------------------
### Apply Whittle Onsets to COW MID Data
Source: http://svmiller.com/peacesciencer/reference/whittle_conflicts_onsets.html
Shows how to use `whittle_conflicts_onsets` with the `cow_mid_dirdisps` dataset. This is useful for preparing dyadic dispute data for analysis.
```R
cow_mid_dirdisps %>% whittle_conflicts_onsets()
```
--------------------------------
### whittle_conflicts_jds
Source: http://svmiller.com/peacesciencer/reference/whittle_conflicts_jds.html
`whittle_conflicts_jds()` is used to coerce conflict-year data to unique conflict-year data by cross-sectional unit. It drops duplicate observations, keeping only the one with the lowest start month for each dyad-year.
```APIDOC
## whittle_conflicts_jds(data)
### Description
Takes a dyad-year data frame or leader-dyad-year data frame with a declared conflict attribute type and, grouping by the dyad and year, returns just those observations that have the lowest start month.
### Arguments
* **data** (data frame) - A data frame with a declared conflict attribute type.
### Value
A data frame with duplicate conflict-year entries removed, keeping the observation with the earliest start month for each dyad-year.
### Details
This function is a "nuclear option" for resolving duplicate dyad-years, intended to be used after other case exclusion rules have been applied. It assumes remaining duplicates are functionally equivalent and drops all but one observation per dyad-year, prioritizing the one with the earliest start month.
```
--------------------------------
### Create 'New State' Variable
Source: http://svmiller.com/peacesciencer/articles/parlor-tricks.html
Demonstrates how to create a 'new state' variable, indicating states within their first two years of existence. This is done after initial data creation to avoid issues with group_by() modifying data attributes.
```r
# Hypothetical main data
create_stateyears(system = 'gw') %>%
filter(between(year, 1946, 2019)) %>%
add_ucdp_acd(type = "intrastate") %>%
add_peace_years() -> Data
#> Joining with `by = join_by(gwcode, year)`
#> Joining with `by = join_by(gwcode, year)`
# Add in new state variable after the fact
create_stateyears(system = 'gw') %>%
group_by(gwcode) %>%
mutate(newstate = ifelse(row_number() <= 2, 1, 0)) %>%
left_join(Data, .) %>%
select(gwcode:ucdponset, newstate, everything()) -> Data
#> Joining with `by = join_by(gwcode, gw_name, microstate, year)`
# Proof of concept: Here's India
Data %>% filter(gwcode == 750)
#> # A tibble: 73 × 10
#> gwcode gw_name microstate year ucdpongoing ucdponset newstate maxintensity
#>
#> 1 750 India 0 1947 0 0 1 NA
#> 2 750 India 0 1948 1 1 1 2
#> 3 750 India 0 1949 1 0 0 2
#> 4 750 India 0 1950 1 0 0 2
#> 5 750 India 0 1951 1 0 0 2
#> 6 750 India 0 1952 0 0 0 NA
#> 7 750 India 0 1953 0 0 0 NA
#> 8 750 India 0 1954 0 0 0 NA
#> 9 750 India 0 1955 0 0 0 NA
#> 10 750 India 0 1956 1 1 0 1
#> # ℹ 63 more rows
#> # ℹ 2 more variables: conflict_ids , ucdpspell
# And here's Belize
Data %>% filter(gwcode == 80)
#> # A tibble: 39 × 10
#> gwcode gw_name microstate year ucdpongoing ucdponset newstate maxintensity
#>
#> 1 80 Belize 0 1981 0 0 1 NA
#> 2 80 Belize 0 1982 0 0 1 NA
#> 3 80 Belize 0 1983 0 0 0 NA
#> 4 80 Belize 0 1984 0 0 0 NA
#> 5 80 Belize 0 1985 0 0 0 NA
#> 6 80 Belize 0 1986 0 0 0 NA
#> 7 80 Belize 0 1987 0 0 0 NA
#> 8 80 Belize 0 1988 0 0 0 NA
#> 9 80 Belize 0 1989 0 0 0 NA
#> 10 80 Belize 0 1990 0 0 0 NA
#> # ℹ 29 more rows
#> # ℹ 2 more variables: conflict_ids , ucdpspell
```
--------------------------------
### Whittle Duplicate Conflict-Years (JDS)
Source: http://svmiller.com/peacesciencer/reference/whittle_conflicts_jds.html
Use this function to remove duplicate conflict-year observations by keeping only the one with the lowest start month for each dyad-year. This is a last resort for data cleaning.
```R
whittle_conflicts_jds(data)
```
--------------------------------
### Retrieve Citation for a Specific Data Version
Source: http://svmiller.com/peacesciencer/articles/versions.html
Use the `ps_cite()` function with a BibTeX key to get the full citation for a data set. This is useful for properly referencing data sources.
```r
ps_cite("cartersmith2020fml", column = "bibtexkey")
```