GithubHelp home page GithubHelp logo

jimbrig / lossrx Goto Github PK

View Code? Open in Web Editor NEW
10.0 4.0 1.0 39.35 MB

An R package, plumber API, database, and Shiny App for Actuarial Loss Development and Reserving Workflows.

Home Page: https://docs.jimbrig.com/lossrx/

License: Other

R 59.00% Dockerfile 0.63% PowerShell 4.99% Shell 4.83% TSQL 28.58% TeX 1.87% Rez 0.09%
workflow actuarial-science modelling reserving data-science claims-reserving claims-data property-casualty insurance rstats

lossrx's Introduction

lossrx

R-CMD-check Lifecycle:experimental Project Status:WIP

View the Changelog for detailed progression on development of the package.

View the GITHUB.md document for details on the various CI/CD workflows implemented with this repository on the R Package (~14 total workflows).

View the Latest Database Documentation for a detailed look into its structure.

Contents

Table of Contents

Overview

The desired outcome of the lossrx R package is to provide actuaries and data scientists valuable tools and frameworks for performing Property Casualty related workflows.

Specifically, the package will contain:

  • A suite of utility and workflow oriented functions bundled as an R package
  • A plumber API that serves various endpoints related to a backend database and models
  • A comprehensive, yet simple migration-friendly relational database representing the various entities, attributes, and relationships involved with actuarial reserving.1
  • A demo Shiny App for Actuarial Loss Development and Reserving including triangles, loss development factor selection, preliminary ultimates, etc. and a backend database/API to store results and selections.
  • Comprehensive documentation and tests

Installation

You can install the development version of lossrx with pak: 2

pak::pak("jimbrig/lossrx")

Data

Data Preparation

This package utilizes a lot of data and in turn has a lot of code inside the data-raw folder. I recommend taking a look at its README to gain an understanding of how the data was prepared for both use in the package as well as included and uploaded to the database.

Data is prepared in the following ways:

  • Simulated transactional claims data using the claims_transactional.R Simulation R Script
  • Downloaded and extracted using ropensci’s piggyback package - see the dataprep.R Script.
  • Supplemental mocked data generated using the Mockaroo API for randomizing certain data attribute’s.
  • Uploaded and merged into the relational database using SQL, R, CSVs, and YAML files (see below about database details).
  • Certain datasets are budled and included with the package as native .rda packaged data files.

Database

View Detailed Database Documentation created via dbdocs

The core database files are housed in the inst/database directory and are included on package installation.

Checks

Latest R CMD CHECK:

-- R CMD check results -------------------------------------- lossrx 0.0.2 ----
Duration: 1m 19.3s

0 errors v | 0 warnings v | 0 notes v

R CMD check succeeded

Jimmy Briggs | 2022

Footnotes

  1. See the database container package for details on how to run the database as a container image.

  2. Similarly, you can install the package using the more common devtools::install_github() and remotes::install_github()

lossrx's People

Contributors

dependabot[bot] avatar elgohr avatar github-actions[bot] avatar jimbrig avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

lossrx's Issues

Bugfix: Fix R CMD CHECK Issues

Tasks:

  • Attempt to reduce dependencies down from 24
  • Remove .env file in spawnctl inst dir
  • Reduce size of data and doc directories
  • Fix globals:
    • view_claim_history: no visible binding for global variable 'claims_transactional'
    • view_claim_history: no visible global function definition for 'case_when'
    • view_claim_history: no visible binding for global variable 'Transaction ID'
    • view_claim_history: no visible binding for global variable 'Paid Change'
    • view_claim_history: no visible binding for global variable 'Case Reserve Change'
    • view_claim_history: no visible binding for global variable 'Reported Change'
    • view_claim_history: no visible binding for global variable 'Status Details'
    • view_claim_history: no visible binding for global variable 'Status'
    • view_claim_history: no visible binding for global variable 'Transaction Details'
    • view_claim_history: no visible binding for global variable 'Transaction Date'

Output:

-- R CMD check results ----------------------------------------------- lossrx 0.0.2 ----
Duration: 1m 38.2s

> checking package dependencies ... NOTE
  Imports includes 24 non-default packages.
  Importing from so many packages makes the package vulnerable to any of
  them becoming unavailable.  Move as many as possible to Suggests and
  use conditionally.

> checking for hidden files and directories ... NOTE
  Found the following hidden files and directories:
    inst/spawnctl/.env
  These were most likely included in error. See section 'Package
  structure' in the 'Writing R Extensions' manual.

> checking installed package size ... NOTE
    installed size is  5.4Mb
    sub-directories of 1Mb or more:
      data   2.0Mb
      doc    2.5Mb

> checking R code for possible problems ... NOTE
  view_claim_history: no visible binding for global variable
    'claims_transactional'
  view_claim_history: no visible global function definition for
    'case_when'
  view_claim_history: no visible binding for global variable 'Transaction
    ID'
  view_claim_history: no visible binding for global variable 'Paid
    Change'
  view_claim_history: no visible binding for global variable 'Case
    Reserve Change'
  view_claim_history: no visible binding for global variable 'Reported
    Change'
  view_claim_history: no visible binding for global variable 'Status
    Details'
  view_claim_history: no visible binding for global variable 'Status'
  view_claim_history: no visible binding for global variable 'Transaction
    Details'
  view_claim_history: no visible binding for global variable 'Transaction
    Date'
  Undefined global functions or variables:
    Case Reserve Change Paid Change Reported Change Status Status Details
    Transaction Date Transaction Details Transaction ID case_when
    claims_transactional

Enhancement: Potential Enhancement for Interpolation Functions

From mages/ChainLadder#26 (comment):

interpolate_ldfs <- function(observed_ldf_df, interp_age){
  # observed_ldf_df <- sel_data
  # interp_age <- 9

  ## At some age ('ldf_2_one') all selected 'ldfs' = 1 for all 'ages' >= ldf_2_one 
  ## Hence our 'pct_ibnr' -> inf for all 'ages' >= 'ldf_2_one', 
  ## and recieve error when fit linear model
  ## Test if 'interp_age' >= 'ldf_2_one' then return 1. Else proceed to interpolation 

  ldf_2_one <- min(observed_ldf_df$age[observed_ldf_df$ldf == 1]) 
  #the first age which the ldf is 1

  if (interp_age >= ldf_2_one) {
    return(1)
  } else {

    ## Exclude rows from 'observed_ldf_df' where ldf == 1  
    observed_ldf_df <- observed_ldf_df[observed_ldf_df$ldf != 1,]

    observed_ldf_df <- observed_ldf_df %>% 
      dplyr::mutate(pct_ibnr = 1 - (1 / ldf)) 

    ## Fit weibull model
    weibul_model <- lm(log(-log(observed_ldf_df$pct_ibnr)) ~ 
        log(observed_ldf_df$age)) # Boor Eq (8)

    ## Define the age of the ldfs above and below the interpulated age  
    age_below <- interp_age - (interp_age %% 12) 
    age_above <- interp_age + (12 - (interp_age %% 12))


    fit_below <- exp(-exp(weibul_model$coefficients[1] + 
        weibul_model$coefficients[2] * log(age_below))) 
    fit_above <- exp(-exp(weibul_model$coefficients[1] + 
        weibul_model$coefficients[2] * log(age_above)))
    fit_at <- exp(-exp(weibul_model$coefficients[1] + 
        weibul_model$coefficients[2] * log(interp_age))) 

    ## Selected ldfs at age_below and age_above
    observed_below <- observed_ldf_df$pct_ibnr[observed_ldf_df$age == age_below]
    observed_above <- observed_ldf_df$pct_ibnr[observed_ldf_df$age == age_above]

    ## observed_below is na when age_below < 12. Set equal to 1
    if(interp_age < 12) observed_below = 1

    ## variables to make extrapolation easier
    max_obs_age <- max(observed_ldf_df$age)

    if(interp_age < max_obs_age){   # interpolate
      interp_along_curve <- observed_below + (((fit_at - fit_below) / 
          (fit_above - fit_below)) * 
          (observed_above - observed_below))
    }  else{                           # extrapolate

      fit_at_max_age <- exp(-exp(weibul_model$coefficients[1] + 
          weibul_model$coefficients[2] * 
          log(max_obs_age)))

      obs_at_max_age <- observed_ldf_df$pct_ibnr[observed_ldf_df$age == 
          max_obs_age]

      interp_along_curve <- fit_at * obs_at_max_age / fit_at_max_age
    }
    ## Calculate ldf
    implied_ldf <- 1 / (1 - interp_along_curve)
    ## Adjust for age < 12 months 
    implied_full_ay_ldf <- ifelse(interp_age >= 12, implied_ldf, 
      implied_ldf * 12 / interp_age) 

    return(implied_full_ay_ldf)

  }}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.