build_matrix {migrate} | R Documentation |
Build a state migration (transition) matrix
Description
build_matrix()
creates a state transition matrix from summarized data (i.e.,
a data frame returned by migrate()
) representing each unique combination
of beginning & ending states and a numeric metric.
Usage
build_matrix(data, state_start = NULL, state_end = NULL, metric = NULL)
Arguments
data |
A data frame or data frame extension (e.g., a tibble or
data.table) containing a minimum of three (3) column variables representing
a starting credit risk state, an ending credit risk state, and a metric
containing values representing the movement (i.e., "transition) in that
metric between the starting credit risk state point in time and the ending
credit risk state point in time. This style of data frame is output by the
|
state_start |
(Optional) A symbol or string, representing the column
variable of the |
state_end |
(Optional) A symbol or string, representing the column
variable of the |
metric |
(Optional) A symbol or string, representing the column variable
of the |
Value
A matrix object, where the first (row) dimension represents the starting
credit risk state, the second (column) dimension represents the ending credit
risk state, and the values within the matrix represent the transitioned
amount based upon the values in the metric
numeric column variable from
the data
data frame.
Note: A matrix object can be coerced to a data frame using as.data.frame()
.
Examples
# Let `build_matrix()` guess which column variables represent `state_start`,
# `state_end` and `metric`
mock_credit |>
migrate(
time = date,
state = risk_rating,
id = customer_id,
metric = principal_balance
) |>
build_matrix()
# Specify which column variables represent `state_start`, `state_end` and
# `metric`
mock_credit |>
migrate(
id = customer_id,
time = date,
state = risk_rating,
percent = FALSE
) |>
build_matrix(
state_start = risk_rating_start,
state_end = risk_rating_end,
metric = count
)