Builds a duration version of a data frame representing panel data.
add_duration(data, y, unitID, tID, freq = "month", sort = FALSE, ongoing = TRUE, slice.last = FALSE)
data | Data frame representing panel data. |
---|---|
y | A binary indicator of the incidence of some event, e.g. a coup. |
unitID | Name of the variable in the data frame identifying the
cross-sectional units, e.g. |
tID | Name of the variable in the data frame identifying the time unit,
preferably as class |
freq | Frequency at which units are measured in |
sort | Sort data by unit and time? Default is |
ongoing | If |
slice.last | Set to |
Returns the original data frame with 8 duration-specific additional variables:
Binary indicator of an event.
Binary indicator for ongoing events, not counting the initial failure time.
Binary indicator for the last observation in a spell, either due to censoring or failure.
Binary indicator for spells that are coded as cured, or immune
from failure. Equal to 1 - atrisk
.
Binary indicator for spells that are coded as at risk for
failure. Equal to 1 - cured
.
Binary indicator for right-censored spells.
t
, counter for how long a spell has survived without
failure.
Starting time for period observed during t
, by default
equals duration
- 1.
This function processes a panel data frame by creating a failure
variable from y
and corresponding duration counter, as well as
risk/immunity indicators. Supported time resolutions are year, month, and
day, and input data should be (dis-)aggregated to one of these levels.
The returned data frame should have the same number of rows at the original.
If y
is an indicator of the incidence of some event, rather than an
onset indicator, then ongoing spells of failure beyond the initial event are
coded as NA (e.g. 000111 becomes a spell of 0001 NA NA). This is to preserve
compatibility with the base dataset. Note that the order of rows may be
different though.
There cannot be missing values ("NA
") in any of the key variables
y
, unitID
, or tID
; they will stop the function.
Furthermore, series that start with an event, e.g. (100), are treated as
experiencing failure in the first time period. If those events are in fact
ongoing, e.g. the last year of a war that started before the start time of
the dataset, they should be dropped manually before using
buildDuration()
.
t.0
is the starting time of the period of observation at tID
.
It is by default set as duration - 1
and currently only serves as a
placeholder to allow future expansion for varying observation times.
panel_lag
for lagging variables in a panel data frame
before building duration data.
# Yearly data data <- data.frame(y=c(0,0,0,1,0), unitID=c(1,1,1,1,1), tID=c(2000, 2001, 2002, 2003, 2004)) dur.data <- add_duration(data, "y", "unitID", "tID", freq="year")#> Warning: Converting to 'Date' class with yyyy-06-30dur.data#> y unitID tID failure ongoing end.spell cured atrisk censor duration t.0 #> 2 0 1 2000 0 0 0 0 1 0 1 0 #> 3 0 1 2001 0 0 0 0 1 0 2 1 #> 4 0 1 2002 0 0 0 0 1 0 3 2 #> 5 1 1 2003 1 0 1 0 1 0 4 3 #> 1 0 1 2004 0 0 1 1 0 1 1 0