migrate
An R package for building state transition matrices
Science Score: 13.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
○.zenodo.json file
-
○DOI references
-
○Academic publication links
-
○Academic email domains
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (18.7%) to scientific vocabulary
Keywords
credit-risk
finance
risk-management
rstats-package
Last synced: 6 months ago
·
JSON representation
Repository
An R package for building state transition matrices
Basic Info
- Host: GitHub
- Owner: ketchbrookanalytics
- License: other
- Language: R
- Default Branch: master
- Homepage: https://ketchbrookanalytics.github.io/migrate/
- Size: 3.96 MB
Statistics
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 0
- Releases: 5
Topics
credit-risk
finance
risk-management
rstats-package
Created over 5 years ago
· Last pushed over 1 year ago
Metadata Files
Readme
Changelog
License
README.Rmd
---
output: github_document
---
```{r, include = FALSE}
options(width = 999)
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
devtools::load_all()
```
# migrate
[](https://lifecycle.r-lib.org/articles/stages.html#stable)
[](https://CRAN.R-project.org/package=migrate)
[](https://cran.r-project.org/package=migrate)
[](https://github.com/ketchbrookanalytics/migrate/actions/workflows/R-CMD-check.yaml)
The goal of {migrate} is to provide users with an easy set of tools for building *state transition matrices*.

## Methodology
{migrate} provides an easy way to calculate absolute or percentage migration within a credit portfolio. The above image shows a typical credit migration matrix using the *absolute* approach; each cell in the grid represents the total balance in the portfolio at 2020-06-30 that started at the Risk Rating represented on the left-hand vertical axis and ended (at 2020-09-30) at the Risk Rating represented on the upper horizontal axis of the matrix. For example, $6.58M moved from a Risk Rating **AAA** at 2020-06-30 to a Risk Rating **AA** at 2020-09-30.
While the above, *absolute*, migration example is typically more of a reporting function, the *percentage* (or probabilistic) methodology is often more of a statistical modeling exercise, often used in credit portfolio risk management. Currently, this package only supports the simple "cohort" methodology. This estimates the probability of moving from state *i* to state *j* in a single time step, echoing a Markov process. We can visualize this in a matrix, for a credit portfolio with *N* unique, ordinal states:

### Future Plans for {migrate}
Future development plans for this package include building functionality for the more complex **duration**/**hazard** methodology, including both the *time-homogeneous* and *non-homogeneous* implementations.
## Installation
You can install the released version of {migrate} from [CRAN](https://CRAN.R-project.org) with:
``` {r, eval = FALSE}
install.packages("migrate")
```
And the development version from [GitHub](https://github.com/) with:
``` {r, eval = FALSE}
# install.packages("devtools")
devtools::install_github("ketchbrookanalytics/migrate")
```
## Practical Usage
{migrate} currently only handles transitions between exactly two (2) timepoints. Under the hood, `migrate()` finds the earliest & latest dates in the given *time* variable, and filters out any observations where the *time* value does not match those two dates.
If you are writing a SQL query to get data to be used with `migrate()`, the query would likely look something like this:
```{r, eval = FALSE}
# -- Get the *State* risk status and *Balance* dollar amount for each ID, at two distinct dates
# SELECT ID, Date, State, Balance
# FROM my_database
# WHERE Date IN ('2020-12-31', '2021-06-30')
```
By default, `migrate()` drops observations that belong to IDs found at a single timepoint. However, users can define a *filler state* so that IDs with a single timepoint are not removed but rather migrated from or to this *filler state*. This allows for more flexible handling of such data, ensuring that no information is lost during the migration process. Check [Handle IDs with observations at a single timepoint](https://ketchbrookanalytics.github.io/migrate/articles/migrate.html#handle-ids-with-observations-at-a-single-timepoint) for more information.
## Example
First, load the package using `library()`
```{r load, eval = FALSE}
library(migrate)
```
The package has a built-in mock dataset, which can be loaded into the environment like so:
```{r data, eval = FALSE}
data("mock_credit")
head(mock_credit[order(mock_credit$customer_id), ]) # sort by 'customer_id'
```
```{r data_tbl, echo = FALSE}
head(mock_credit[order(mock_credit$customer_id), ]) |>
knitr::kable(row.names = FALSE)
```
Note that an important feature of the `mock_credit` dataset is that there are exactly two (2) unique values in the `date` column variable; if the `time` argument passed to `migrate()` has more than two (2) unique values, the function will throw an error.
```{r dates}
unique(mock_credit$date)
```
To summarize the migration within the data, use the `migrate()` function
```{r migrate}
migrated_df <- migrate(
data = mock_credit,
id = customer_id,
time = date,
state = risk_rating,
)
head(migrated_df)
```
To create the state transition matrix, use the `build_matrix()` function
```{r matrix}
build_matrix(migrated_df)
```
Or, to do it all in one shot, use the `|>`
```{r pipe}
mock_credit |>
migrate(
id = customer_id,
time = date,
state = risk_rating,
metric = principal_balance,
percent = FALSE,
verbose = FALSE
) |>
build_matrix(
state_start = risk_rating_start,
state_end = risk_rating_end,
metric = principal_balance
)
```
Owner
- Name: Ketchbrook Analytics
- Login: ketchbrookanalytics
- Kind: organization
- Email: info@ketchbrookanalytics.com
- Website: www.ketchbrookanalytics.com
- Repositories: 6
- Profile: https://github.com/ketchbrookanalytics
Ketchbrook Analytics is a data science consultancy specializing in full-stack, end-to-end, development of data science solutions.
GitHub Events
Total
Last Year
Packages
- Total packages: 1
-
Total downloads:
- cran 317 last-month
- Total dependent packages: 0
- Total dependent repositories: 0
- Total versions: 5
- Total maintainers: 1
cran.r-project.org: migrate
Create Credit State Migration (Transition) Matrices
- Homepage: https://github.com/ketchbrookanalytics/migrate
- Documentation: http://cran.r-project.org/web/packages/migrate/migrate.pdf
- License: MIT + file LICENSE
-
Latest release: 0.5.0
published over 1 year ago
Rankings
Forks count: 21.9%
Stargazers count: 22.5%
Dependent packages count: 29.8%
Average: 33.9%
Dependent repos count: 35.5%
Downloads: 59.7%
Maintainers (1)
Last synced:
6 months ago
Dependencies
DESCRIPTION
cran
- R >= 3.1 depends
- dplyr >= 1.0.7 imports
- magrittr * imports
- rlang * imports
- tibble >= 3.0.1 imports
- tidyr >= 1.1.0 imports
- utils * imports
- knitr * suggests
- rmarkdown * suggests
- testthat >= 2.1.0 suggests
.github/workflows/R-CMD-check.yaml
actions
- actions/checkout v2 composite
- actions/upload-artifact main composite
- r-lib/actions/check-r-package v1 composite
- r-lib/actions/setup-pandoc v1 composite
- r-lib/actions/setup-r v1 composite
- r-lib/actions/setup-r-dependencies v1 composite