public
Created

MRMR Triangle constructor

  • Download Gist
MRMR Triangle constructor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#==================================================================================================
# 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 ======================================================================

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.