Skip to content

Instantly share code, notes, and snippets.

@PirateGrunt
Created January 24, 2013 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PirateGrunt/4622610 to your computer and use it in GitHub Desktop.
Save PirateGrunt/4622610 to your computer and use it in GitHub Desktop.
MRMR Triangle constructor
#==================================================================================================
# User-friendly constructor
# This is a giant pile of code which basically does the following:
# * Ensure that we have a proper date for the loss period start
# * Ensure that we have a column for the development lag
# * Once those have been established, create columns for loss period start and end, create a column
# for development period (based on lubridate Period class), compute the evaluation date.
Triangle = function(TriangleData
, TriangleName
, LossPeriodType = "accident"
, LossPeriodInterval = years(1)
, DevelopmentInterval = years(1)
, LossPeriodColumn
, DevelopmentColumn)
{
# confirm that the loss period column exists. If not, we throw an error.
if (!ColumnExists(TriangleData, LossPeriodColumn))
{
stop ("The specified column for the loss period does not exist. Unable to create the triangle.")
}
require(lubridate)
# if the loss period column is not a date, attempt to convert it.
if (!is.POSIXt(TriangleData[,LossPeriodColumn])){
attemptConvert = ymd(TriangleData[,LossPeriodColumn])
if (sum(is.Date(attemptConvert)) ==0 ){
attemptConvert = ydm(TriangleData[,LossPeriodColumn])
if (sum(is.Date(attemptConvert)) ==0 ){
attemptConvert = mdy(TriangleData[,LossPeriodColumn])
if (sum(is.Date(attemptConvert)) ==0 ){
attemptConvert = myd(TriangleData[,LossPeriodColumn])
if (sum(is.Date(attemptConvert)) ==0 ){
attemptConvert = dmy(TriangleData[,LossPeriodColumn])
if (sum(is.Date(attemptConvert)) ==0 ){
attemptConvert = dym(TriangleData[,LossPeriodColumn])
if (sum(is.Date(attemptConvert)) ==0 ){
stop ("Loss period column is not in date form and all values cannot be converted to a date. Unable to create the triangle")
}
}
}
}
}
}
}
# If we've made it this far, we can safely convert the loss period start date
if (!is.POSIXt(TriangleData[, LossPeriodColumn])){
TriangleData$LossPeriodStart = attemptConvert
} else {
TriangleData$LossPeriodStart = TriangleData[, LossPeriodColumn]
}
# Now add the period to create the end date
TriangleData$LossPeriodEnd = TriangleData$LossPeriodStart + LossPeriodInterval - days(1)
# it's possible that the user has fed data with overlap.
# Annual data with two start dates in the same year and annual development period, zB.
# I might decide to check for this and throw a warning, but for now, I'll just blame the user.
# Now confirm that the development lag column exists. If not, we throw an error.
if (!ColumnExists(TriangleData, DevelopmentColumn))
{
stop ("The specified column for the development does not exist. Unable to create the triangle.")
}
# For now, development periods must be integer.
if (!is.integer(TriangleData[,DevelopmentColumn])){
TriangleData[,DevelopmentColumn] = as.integer(TriangleData[,DevelopmentColumn])
}
TriangleData$Development = TriangleData[,DevelopmentColumn] * DevelopmentInterval
# if we can, construct an evaluation date based on loss period start and development interval
TriangleData$EvaluationDate = TriangleData$LossPeriodStart + TriangleData$Development - days(1)
tri = new("Triangle", TriangleData = TriangleData
, TriangleName = TriangleName
, LossPeriodType = LossPeriodType
, LossPeriodInterval = LossPeriodInterval
, DevelopmentInterval = DevelopmentInterval)
tri@TriangleData = CalculateIncrementals(tri)
return (tri)
}
#== End Triangle constructor ======================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment