
Add conflict spell (peace-years) durations to a state panel
Source:R/add_spell_duration.R
add_spell_duration.RdComputes a BTSCS-style spell/duration counter from a binary conflict
column already present in the panel (typically added by
add_conflict()): the number of years since the last event for that
state, restarting after each new event. This is the same construct as
peacesciencer::add_spells(), generalized to work with any binary
column rather than a fixed set of bundled column names. Named
add_spell_duration(), not add_spells(), to avoid colliding with
peacesciencer::add_spells() when both packages are attached.
Arguments
- panel
A data frame produced by
build_states_panel(), optionally already joined with conflict data viaadd_conflict().- event
Character. Name of the binary (0/1) column to compute spells from. If
NULL(the default),add_spell_duration()looks for exactly one column inpanelwhose name ends in_onset,_incidence, or_ongoing; if zero or more than one match, it errors and asks for an explicitevent.- ongoing
Logical. If
TRUE,eventis treated as an "ongoing" series where a run of consecutive1s is one continuous event (only the first year of the run counts as a new failure; the remaining years of that run getNAin the output, matchingpeacesciencer::add_spells()'s convention of excluding within-event years from a spell that measures time since the last event). IfFALSE, every1ineventis treated as its own event, appropriate for an already onset-coded column. IfNULL(the default), this is inferred from the column name:TRUEfor_incidence/_ongoingsuffixes,FALSEfor_onset.
Value
The input panel with one additional integer column, named after
event with its onset/incidence/ongoing suffix (if any) replaced by
_spell. In a fresh spell, the first observed year is 0 and each
subsequent year without a new event increments by one; the event year
itself carries the final count of the spell it closes.
Details
event must not contain NA. add_conflict()'s output frequently has
NA for country-years unmatched by the source (documented behavior:
absence from a source is not evidence of zero events), and a spell count
cannot be computed across a gap of unknown conflict status. Filter or
otherwise resolve these NAs before calling add_spell_duration().
Examples
panel <- build_states_panel(1990, 2010, coding_system = "gw") |>
add_conflict(dataset = "ucdp_prio") |>
tidyr::drop_na(ucdp_prio_onset) |>
add_spell_duration(event = "ucdp_prio_onset")