Title: | Create Credit State Migration (Transition) Matrices |
---|---|
Description: | Tools to help convert credit risk data at two timepoints into traditional credit state migration (aka, "transition") matrices. At a higher level, 'migrate' is intended to help an analyst understand how risk moved in their credit portfolio over a time interval. References to this methodology include: 1. Schuermann, T. (2008) <doi:10.1002/9780470061596.risk0409>. 2. Perederiy, V. (2017) <doi:10.48550/arXiv.1708.00062>. |
Authors: | Michael Thomas [aut, cre], Brad Lindblad [ctb], Ivan Millanes [ctb] |
Maintainer: | Michael Thomas <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.5.0 |
Built: | 2024-11-07 06:13:49 UTC |
Source: | https://github.com/ketchbrookanalytics/migrate |
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.
build_matrix(data, state_start = NULL, state_end = NULL, metric = NULL)
build_matrix(data, state_start = NULL, state_end = NULL, metric = NULL)
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 |
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()
.
# 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 )
# 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 )
migrate()
summarizes the transition amount (or percentage) of a numeric
variable from each beginning credit risk state category to each ending credit
risk state, given a data frame input.
migrate( data, id, time, state, metric = NULL, percent = TRUE, fill_state = NULL, verbose = TRUE )
migrate( data, id, time, state, metric = NULL, percent = TRUE, fill_state = NULL, verbose = TRUE )
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 time, a credit risk state, and an ID identifying the credit facility (we
would expect to see most unique values in this column variable appear twice
in the dataset; once at the first unique |
id |
The column variable of the |
time |
The column variable of in the |
state |
The column variable of the |
metric |
(Optional) The column variable of type "numeric" in the |
percent |
If |
fill_state |
(Optional) A value (e.g., a character string such as "No
Rating" or "NR") to be used as the filler |
verbose |
If |
A data frame containing three (3) column variables representing the unique combinations of starting & ending credit risk states and the calculated migration observed during the period.
# Return the percent migration of the number of credit facilities migrate( data = mock_credit, id = customer_id, time = date, state = risk_rating ) # Return the absolute migration in `principal_balance` migrate( data = mock_credit, id = customer_id, time = date, state = risk_rating, metric = principal_balance, percent = FALSE ) # Provide a filler `state` value when a unique `id` is missing a timepoint migrate( data = head(mock_credit, n = 995), # drop the last 5 observations id = customer_id, time = date, state = risk_rating, fill_state = "NR", percent = FALSE )
# Return the percent migration of the number of credit facilities migrate( data = mock_credit, id = customer_id, time = date, state = risk_rating ) # Return the absolute migration in `principal_balance` migrate( data = mock_credit, id = customer_id, time = date, state = risk_rating, metric = principal_balance, percent = FALSE ) # Provide a filler `state` value when a unique `id` is missing a timepoint migrate( data = head(mock_credit, n = 995), # drop the last 5 observations id = customer_id, time = date, state = risk_rating, fill_state = "NR", percent = FALSE )
Mock dataset containing credit statistics by customer at two time intervals. Some customers only exist in one time interval (they either became a customer after the first time interval, or discontinued being a customer before the second time interval).
mock_credit
mock_credit
A data frame with columns:
Customer ID for 497 unique customers.
Date of the observation; there are two unique dates in the dataset.
Factor representing risk level on a 1-14 scale.
Principal balance outstanding on the loan.
Developed by Ketchbrook Analytics
## Not run: mock_credit ## End(Not run)
## Not run: mock_credit ## End(Not run)