ChainLadder Markus 20090724 PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 53

R you ready?

One-Day Workshop
24 July, Staple Inn, London

Claims reserving in R
The ChainLadder package

Markus Gesmann

Contact: [email protected] 1
Content

Introduction
ChainLadder package
R and databases
R and MS Office interfaces

Double click the paperclip symbol in the bottom of this slide to access all the
R code of this presentation.

Markus Gesmann
The ChainLadder package R code from ChainLadder slides.R
2
Claims reserving in insurance

Insurers sell the promise to pay for future


claims occurring over an agreed period for an
upfront received premium
Unlike other industries insurers dont know the
production cost of their product
The estimated future claims have to be held
in the reserves, one of the biggest liability
items on an insurers balance sheet

Markus Gesmann
The ChainLadder package
3
Reserving in insurance

Reserves cover IBNR (Incurred But Not


Reported) claims
Reserves are usually estimated based on
historical claims payment/reporting patterns
In the past a point estimator for the reserves
was sufficient
New regulatory requirements ( Solvency II)
foster stochastic methods

Markus Gesmann
The ChainLadder package
4
Typical scenario

Usually an insurance Development Years

portfolio is split into

C
al
"homogeneous" classes of

en
da
rY
Origin Years
business, e.g. motor, marine,

ea
s r
property, etc.
Policies are aggregated by
class and looked at in a
triangle view of reported Reported claims

claims to forecast future Future claims developments

claims developments
Markus Gesmann
The ChainLadder package
5
Stochastic reserving

Over recent years stochastic methods have


been developed and published
Excel is often the standard tool, but is not an
ideal environment for implementing those
stochastic methods
Idea: Use R to implement stochastic reserving
methods, and CRAN to share them
Use the RExcel Add-in as a front end for Excel
to use R functions
Markus Gesmann
The ChainLadder package
6
The ChainLadder package

Started out of presentations given at the


Institute of Actuaries on stochastic reserving
Mack-, Munich- and Bootstrap-chain-ladder
implemented
Example spreadsheet shows how to use the
functions with Excel using the RExcel Add-in
Available from CRAN - sources and binaries
Contribution most welcome!

Markus Gesmann
The ChainLadder package
7
The ChainLadder package
Agenda:
Getting started
ChainLadder package
philosophy
Examples for
MackChainLadder
MunichChainLadder
BootChainLadder
Project web page: http://code.google.com/p/chainladder/
Current version: 0.1.2-11

Markus Gesmann
The ChainLadder package
8
Getting started

Start R and type for


Installation:
install.packages("ChainLadder")
Loading the package:
library(ChainLadder)
Help:
?ChainLadder
Examples:
example(ChainLadder)
Markus Gesmann
The ChainLadder package
9
Example data sets
The ChainLadder package comes with some
example data sets, e.g.
> library(ChainLadder)
> RAA
dev
origin 1 2 3 4 5 6 7 8 9 10
1981 5012 8269 10907 11805 13539 16181 18009 18608 18662 18834
1982 106 4285 5396 10666 13782 15599 15496 16169 16704 NA
1983 3410 8992 13873 16141 18735 22214 22863 23466 NA NA
1984 5655 11555 15766 21266 23425 26083 27067 NA NA NA
1985 1092 9565 15836 22169 25955 26180 NA NA NA NA
1986 1513 6445 11702 12935 15852 NA NA NA NA NA
1987 557 4020 10946 12314 NA NA NA NA NA NA
1988 1351 6947 13112 NA NA NA NA NA NA NA
1989 3133 5395 NA NA NA NA NA NA NA NA
1990 2063 NA NA NA NA NA NA NA NA NA

Markus Gesmann
The ChainLadder package
10
Triangle plot
Incurred claims development by origin year

4
5 5
4
4 3 3
5 3
4
20000

3 1 1 1
1
Incurred Claims

3 1 2 2
5
4 6 2 2
3 2
1
8 6
6 7
1
4
5000 10000

7
1 2
5
3
1
8
6
4
1 9 2
2
7
3
9
0
6
8
5
7
2
0

2 4 6 8 10

Development Year

> matplot(t(RAA), t="b")

Markus Gesmann
The ChainLadder package
11
Working with triangles

Transform from cumulative to incremental


incRAA <- cbind(RAA[,1], t(apply(RAA,1,diff)))
Transform from incremental to cumulative
cumRAA <- t(apply(incRAA,1, cumsum))
Triangles to long format
lRAA <- expand.grid(origin=as.numeric(dimnames(RAA)
$origin), dev=as.numeric(dimnames(RAA)$dev))
lRAA$value <- as.vector(RAA)
Long format to triangle (see later for as.ArrayTriangle function, works much better with ChainLadder)
reshape(lRAA, timevar="dev", idvar="origin",
v.names="value", direction="wide")

Markus Gesmann
The ChainLadder package
12
ChainLadder package philosophy

Use the linear regression function "lm" as


much as possible and utilise its output
The chain-ladder model for volume weighted
average link ratios is expressed as a formula:
y ~ x + 0, weights=1/x
and can easily be changed
Provide tests for the model assumptions

Markus Gesmann
The ChainLadder package
13
Chain-ladder as linear regression
Chain-ladder can be regarded as weighted linear
regression through the origin:
x <- RAA[,1] # dev. period 1 Force it through
the origin
y <- RAA[,2] # dev. period 2
model <- lm(y ~ x + 0, weights=1/x)
Volume weighted
Call:
lm(formula = y ~ x + 0, weights = 1/x)
Coefficients:
x
2.999 chain-ladder link-ratio

Markus Gesmann
The ChainLadder package
14
Full regression output
The output shows:
model formula
> summary(model)
Call:
chain-ladder link
ratio
lm(formula = y ~ x + 0, weights = 1/x)
std. error of the link
Residuals: ratio
Min 1Q Median 3Q Max P-value
-95.54 -71.50 49.03 99.55 385.32
Residual std. error
Coefficients:
Estimate Std. Error t value Pr(>|t|)
x 2.999 1.130 2.654 0.0291 *
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Residual standard error: 167 on 8 degrees of freedom
Multiple R-squared: 0.4682, Adjusted R-squared: 0.4017
F-statistic: 7.043 on 1 and 8 DF, p-value: 0.02908

Markus Gesmann
The ChainLadder package
15
Chain-ladder using the lm function

Idea: Create linear model for each development period

ChainLadder <- function(tri, weights=1/tri){


n <- ncol(tri)
myModel <- vector("list", (n-1))
for(i in c(1:(n-1))){
myModel[[i]] <- lm(y~x+0,
data.frame(x=tri[,i], y=tri[,i+1]),
weights=weights[,i])
}
return(myModel)
}

Markus Gesmann
The ChainLadder package
16
Accessing regression statistics

CL <- ChainLadder(RAA)
# Get chain-ladder link-ratios
sapply(CL, coef)
# 2.999 1.62 1.27 1.17 1.11 1.04 1.03 1.016 1.0
# Get residual standard errors
sapply(lapply(CL, summary), "[[", "sigma")
# 166.98 33.29 26.295 7.82 10.9 6.389 1.159 2.8 NaN
# Get R squared values
sapply(lapply(ChainLadder(RAA), summary), "[[",
"r.squared")
# 0.468 0.95 0.97 0.997 0.995 0.998 0.999 0.999 1.00

Markus Gesmann
The ChainLadder package
17
Mack-chain-ladder
Macks chain-ladder method calculates the
standard error for the reserves estimates.
The method works for a cumulative triangle Cik if
the following assumptions are hold:

All accident years are independent


Markus Gesmann
The ChainLadder package
18
MackChainLadder
Usage:
MackChainLadder(Triangle,
weights = 1/Triangle,
est.sigma="log-linear",
tail=FALSE, tail.se=NULL,
tail.sigma=NULL)
Triangle: cumulative claims triangle
weights: default (1/Triangle) volume weighted CL
est.sigma: Estimator for sigman-1
tail, tail.se, tail.sigma: estimators for the tail
Markus Gesmann
The ChainLadder package
19
MackChainLadder example
library(ChainLadder)
M <- MackChainLadder(Triangle = RAA, est.sigma = "Mack")
M
Latest Dev.To.Date Ultimate IBNR Mack.S.E CV(IBNR)
1981 18,834 1.000 18,834 0 0 NaN
1982 16,704 0.991 16,858 154 206 1.339
1983 23,466 0.974 24,083 617 623 1.010
1984 27,067 0.943 28,703 1,636 747 0.457
1985 26,180 0.905 28,927 2,747 1,469 0.535
1986 15,852 0.813 19,501 3,649 2,002 0.549
1987 12,314 0.694 17,749 5,435 2,209 0.406
1988 13,112 0.546 24,019 10,907 5,358 0.491
1989 5,395 0.336 16,045 10,650 6,333 0.595
1990 2,063 0.112 18,402 16,339 24,566 1.503

Totals
Latest: 160,987.00
Ultimate: 213,122.23
IBNR: 52,135.23
Mack S.E.: 26,909.01
CV(IBNR): 0.52

Markus Gesmann
The ChainLadder package
20
plot.MackChainLadder
The residual plots show
Mack Chain Ladder Results Chain ladder developments by origin period
4

25000
IBNR 5 5
4
Latest

the standardised
4 3 3
30000

5
4 3
3 1 1 1 1
Amounts

Amounts

15000
5
4 3 6 1 2 2

residuals against fitted


2 2
3
8 6 2
1
4 6 7
1
7
1 2
5
3
1
10000

values, origin period,


8
6

5000
4
1 9 2
3 2
7
9
0
6
8
5
7
2

0
calendar period and
0

1981 1983 1985 1987 1989 2 4 6 8 10

development period.
Origin period Development period
2

2
Standardised residuals

Standardised residuals
1

All residual plots should


0

show no pattern or
-1

-1

0 5000 10000 15000 20000 25000 30000 1982 1984 1986 1988
direction for Mack's
Fitted Origin period method to be
applicable.
2

2
Standardised residuals

Standardised residuals
1

Pattern in any direction


0

can be the result of


-1

-1

1982 1984

Calendar period
1986 1988 1 2 3 4 5

Development period
6 7 8
trends and require
plot(M) further investigations.

Markus Gesmann
The ChainLadder package
21
Munich-chain-ladder

Munich-chain-ladder (MCL) is an extension of Macks


method that reduces the gap between IBNR projections
based on paid (P) and incurred (I) losses
Mack has to be applicable to both triangles
MCL adjusts the chain-ladder link-ratios depending if the
momentary (P/I) ratio is above or below average
MCL uses the correlation of residuals between P vs. (I/P)
and I vs. (P/I) chain-ladder link-ratio to estimate the
correction factor

Markus Gesmann
The ChainLadder package
22
Munich-chain-ladder example
P/I triangle Full P/I triangle using chain ladder
1.0

3 1 1 6 6
5
4 3
4
1 1 2
1 6 6 5 5
6 2 3
0.9

3 6 4

1.0
4 2 5 5 3
4
3 2 5 3 3
4 1
2 1
2
1 4 4
1 1
6 1
3 2 2
0.8

4 2
Paid/Incurred

Paid/Incurred
5 3
1
2

0.8
5
0.7

2
5
4 7 7
7 7
5
4 7
0.6

0.6
1 7
6
6
0.5

3
2 3
2
7 7

0.4
0.4

1 2 3 4 5 6 7 1 2 3 4 5 6 7

Development year Development year


MCLpaid MCLincurred
dev dev
origin 1 2 3 4 5 6 7 origin 1 2 3 4 5 6 7
1 576 1804 1970 2024 2074 2102 2131 1 978 2104 2134 2144 2174 2182 2174
2 866 1948 2162 2232 2284 2348 NA 2 1844 2552 2466 2480 2508 2454 NA
3 1412 3758 4252 4416 4494 NA NA 3 2904 4354 4698 4600 4644 NA NA
4 2286 5292 5724 5850 NA NA NA 4 3502 5958 6070 6142 NA NA NA
5 1868 3778 4648 NA NA NA NA 5 2812 4882 4852 NA NA NA NA
6 1442 4010 NA NA NA NA NA 6 2642 4406 NA NA NA NA NA
7 2044 NA NA NA NA NA NA 7 5022 NA NA NA NA NA N

Markus Gesmann
The ChainLadder package
23
MunichChainLadder

Usage:
MunichChainLadder(Paid, Incurred,
est.sigmaP = "log-linear",
est.sigmaI = "log-linear",
tailP=FALSE, tailI=FALSE)

Paid: cumulative paid claims triangle


Incurred: cumulative incurred claims triangle
est.sigmaP, est.sigmaI: Estimator for sigman-1
tailP, tailI: estimator for the tail

Markus Gesmann
The ChainLadder package
24
MunichChainLadder example

MCL <- MunichChainLadder(Paid = MCLpaid, Incurred = MCLincurred, est.sigmaP = 0.1,


est.sigmaI = 0.1)
MCL

Latest Paid Latest Incurred Latest P/I Ratio Ult. Paid Ult. Incurred Ult. P/I Ratio
1 2,131 2,174 0.980 2,131 2,174 0.980
2 2,348 2,454 0.957 2,383 2,444 0.975
3 4,494 4,644 0.968 4,597 4,629 0.993
4 5,850 6,142 0.952 6,119 6,176 0.991
5 4,648 4,852 0.958 4,937 4,950 0.997
6 4,010 4,406 0.910 4,656 4,665 0.998
7 2,044 5,022 0.407 7,549 7,650 0.987

Totals
Paid Incurred P/I Ratio
Latest: 25,525 29,694 0.86
Munich-chain-ladder
Ultimate: 32,371 32,688 0.99 forecasts based on paid and
incurred losses

Markus Gesmann
The ChainLadder package
25
plot.MunichChainLadder
1. 2.
Munich Chain Ladder Results Munich Chain Ladder vs. Standard Chain Ladder
1. MCL forecasts
on P and I
MCL Paid Mack P/I
2000 4000 6000

2. Comparison of
MCL Incurred MCL P/I

80
Amounts

60
Ultimate P/I

40
ratios of MCL

20
and Mack
0

0
1 2 3 4 5 6 7 1 2 3 4 5 6 7

origin period origin period


3. I/P link-ratio
3. Paid residual plot 4. Incurred residual plot residuals against
P link-ratio
2

2
Incurred/Paid residuals

Paid/Incurred residuals

residuals
1

1
0

4. P/I link-ratio
-1

-1

residuals against
-2

-2

-2 -1 0 1 2 -2 -1 0 1 2 I link-ratios
Paid residuals Incurred residuals
residuals
plot(MCL)

Markus Gesmann
The ChainLadder package
26
Bootstrap-chain-ladder

BootChainLadder uses a two-stage approach.


1. Calculate the scaled Pearson residuals and bootstrap
R times to forecast future incremental claims
payments via the standard chain-ladder method.
2. Simulate the process error with the bootstrap value as
the mean and using an assumed process distribution.
The set of reserves obtained in this way forms the
predictive distribution, from which summary statistics
such as mean, prediction error or quantiles can be
derived.

Markus Gesmann
The ChainLadder package
27
BootChainLadder

Usage:
BootChainLadder(Triangle, R = 999,
process.distr=c("gamma",
"od.pois"))

Triangle: cumulative claims triangle


R: Number of resampled bootstraps
process.distr: Assumed process distribution

Markus Gesmann
The ChainLadder package
28
BootChainLadder example
set.seed(1)
BootChainLadder(Triangle = RAA, R = 999, process.distr = "od.pois")

Latest Mean Ultimate Mean IBNR SD IBNR IBNR 75% IBNR 95%
1981 18,834 18,834 0 0 0 0
1982 16,704 16,921 217 710 253 1,597
1983 23,466 24,108 642 1,340 1,074 3,205
1984 27,067 28,739 1,672 1,949 2,679 4,980
1985 26,180 29,077 2,897 2,467 4,149 7,298
1986 15,852 19,611 3,759 2,447 4,976 8,645
1987 12,314 17,724 5,410 3,157 7,214 11,232
1988 13,112 24,219 11,107 5,072 14,140 20,651
1989 5,395 16,119 10,724 6,052 14,094 21,817
1990 2,063 18,714 16,651 13,426 24,459 42,339

Totals
Latest: 160,987
Mean Ultimate: 214,066
Mean IBNR: 53,079
SD IBNR: 18,884
Total IBNR 75%: 64,788
Total IBNR 95%: 88,037

Markus Gesmann
The ChainLadder package
29
plot.BootChainLadder
1. 2.
1.Histogram of simulated
total IBNR
2.Empirical distribution of
total IBNR
3.Box-whisker plot of
simulated ultimate
claims cost by origin
period
3. 4.
4.Test if latest actual
incremental loss could
come from simulated
distribution of claims
cost

Markus Gesmann
The ChainLadder package
30
Generic Methods
Mack-, Munich-, BootChainLadder
names: gives the individual elements back
summary: summary by origin and totals
print: nice formatted output
plot: plot overview of the results
MackChainLadder
residuals: chain-ladder residuals
BootChainLadder
mean: mean IBNR by origin and totals
quantile: gives quantiles of the simulation back

Markus Gesmann
The ChainLadder package
31
R and databases
DB
ODBC
R
sqlQuery
Agenda: as.ArrayTriangle

Create test data


ODBC - SQL

sqlSave
Query database
Tables to triangles
Apply functions
Write to database R: ChainLadder

Markus Gesmann
The ChainLadder package
32
Working with databases

Triangles are usually stored in databases


Triangles are stored in long tables
Use ODBC to connect to databases
Use SQL to interact with databases
Use R to transform tables into triangles
Apply ChainLadder function across many
triangles in one statement
Write results back into database
Markus Gesmann
The ChainLadder package
33
Create sample data in a table format
Use example data sets to create a sample data table
tri=list(RAA=RAA, Mortgage=Mortgage, GenIns=GenIns, ABC=ABC)
# create function to transform triangle into long format
longTriangle <- function(triangle){
long <- expand.grid(origin=as.numeric(dimnames(triangle)$origin),
dev=as.numeric(dimnames(triangle)$dev))
long$value <- as.vector(triangle)
return(na.omit(long))
}
# apply the new function to our list
ltri <- lapply(tri, longTriangle)
# add the names of the triangles to the list
ltri <- lapply(names(ltri), function(x) data.frame(LOB=x, ltri[[x]]))
# transform list into data.frame
triangleTable <- do.call("rbind", ltri)

Markus Gesmann
The ChainLadder package
34
Write test data into database

Example with MS Access 2003


See also documentation for RODBC

library(RODBC)
# Create a test database in c:/Temp (here MS Access 2003)
channel <- odbcConnectAccess(
"C:/Temp/ChainLadderTestData.mdb")
sqlSave(channel, triangleTable, "tblTestTriangles",
rownames=FALSE)
odbcClose(channel)

Markus Gesmann
The ChainLadder package
35
Read tables from database

Access data via ODBC and SQL-statements


ChainLadderTestData.mdb attached at the bottom of this page (double click on paper clip) and save in C:/Temp

# From database
channel <- odbcConnectAccess(
"C:/Temp/ChainLadderTestData.mdb")
myData <- sqlQuery(channel,
"SELECT * FROM tblTestTriangles;")
odbcClose(channel)

Markus Gesmann
The ChainLadder package ChainLadderTestData.mdb
36
As an aside: Plot tables with lattice
GenIns ABC

1.2
Triangles stored in

1.0
long tables are much

0.8
easier to plot than

0.6
triangles in cross-tab

0.4
formats

0.2
value/1e+06
2 4 6 8 10 2 4 6 8 10
# Plot long triangles RAA Mortgage

library(lattice) 0.000 0.005 0.010 0.015 0.020 0.025

6
xyplot(
value/1e6 ~ dev | LOB,

4
groups=origin, t="l",

2
data=myData,
scales="free"

0
) 2 4 6 8 10

dev
2 4 6 8

Markus Gesmann
The ChainLadder package
37
Transform tables into triangles
We use the array function rather than reshape,
as its output is ready to be used by ChainLadder

as.ArrayTriangle <- function(x){


# x has to be a data.frame with columns: origin, dev and value
.names <- apply(x[,c("origin", "dev", "value")], 2, unique)
.namesOD <- .names[c("origin", "dev")]
# Expand to include entire array, in case don't have complete data
.id <- paste(x$origin, x$dev, sep='.')
.grid <- expand.grid(.namesOD)
.grid$id <- paste(.grid$origin, .grid$dev, sep='.')
.grid$data <- x$value[match(.grid$id, .id)]
# Create data array
.data <- array(.grid$data, dim=unlist(lapply(.namesOD, length)),
dimnames=.namesOD)
return(.data)
}

Markus Gesmann
The ChainLadder package
38
Use by to apply ChainLadder functions

by function applies functions on sub sets of data


convert table for each LOB into a triangle
apply MackChainLadder for each triangle
Output is stored in a list
myResults <- by(myData, list(LOB=myData$LOB),
function(x){
triangle <- as.ArrayTriangle(x)
M <- MackChainLadder(triangle, est.sigma="Mack")
return(M)
})
myResults

Markus Gesmann
The ChainLadder package
39
Combine results in tables

Use lapply to access MackChainLadder output


Access origin year and total results separately
OriginResults <- lapply(lapply(myResults, summary), "[[", "ByOrigin")
# add the names of the triangles to the list
OriginResults <- lapply(names(OriginResults ),
function(x) data.frame(LOB=x, OriginResults[[x]]))
# transform list into data.frame
OriginResultsTable <- do.call("rbind", OriginResults)

TotalResults <- lapply(lapply(lapply(myResults, summary),


"[[", "Totals"),t)
# add the names of the triangles to the list
TotalResults <- lapply(names(TotalResults ),
function(x) data.frame(LOB=x, TotalResults[[x]]))
# transform list into data.frame
TotalResultsTable <- do.call("rbind", TotalResults)

Markus Gesmann
The ChainLadder package
40
Write results into database

Write results back into new tables of the database via


QDBC and sqlSave

channel <- odbcConnectAccess("C:/Temp/


ChainLadderTestData.mdb")
sqlSave(channel, OriginResultsTable, "myOriginResults",
rownames=FALSE)
sqlSave(channel, TotalResultsTable, "myTotalResults",
rownames=FALSE)
odbcClose(channel)

Markus Gesmann
The ChainLadder package
41
Database summary

Use R to query DB
Transform table to
triangles
Apply ChainLadder
function across all
triangles
Summaries results
Save results in DB

Markus Gesmann
The ChainLadder package
42
R and MS Office interfaces

Agenda:
win.metafile
Clipboard
RExcel
COM-server
rcom

Markus Gesmann
The ChainLadder package
43
Windows meta-file

Windows meta-file (WMF, or EMF (Enhanced


meta-file) is a vector graphic format
High quality, but editable format for MS Office
Create WMF-files in R with win.metafile()

win.metafile(file="C:/Temp/Testplot.wmf")
plot(sin(seq(0,round(2*pi,2),0.01)))
dev.off()

Markus Gesmann
The ChainLadder package
44
Clipboard to exchange data

Copy and paste from R to and from Excel


R -> Excel
mydf=data.frame(x=1:10, y=letters[1:10])
write.table(mydf, file="clipboard",
sep="\t", row.names=FALSE)

Excel -> R
read.table(file= "clipboard", sep="\t")

Markus Gesmann
The ChainLadder package
45
RExcel - Using R from within Excel
RExcel Add-in allows to use R functions from Excel, see:
http://sunsite.univie.ac.at/rcom/
There are at least three different ways of using R from within Excel

Scratchpad mode
Writing R Code directly in an Excel worksheet and
transferring scalar, vector, and matrix variables between R
and Excel
Macro mode
Writing macros using VBA and the macros supplied by
RExcel, attaching the macros to menu items or toolbar items
Worksheet functions
R can be called directly in functions in worksheet cells
Source: http://sunsite.univie.ac.at/rcom/server/doc/RExcel.html

Markus Gesmann
The ChainLadder package
46
ChainLadder_in_Excel.xls
RExcel allows to
use R functions
within Excel
Package comes
with example file
R function can be
embedded and
are interactive
Use R graphics

Markus Gesmann
The ChainLadder package
47
Using the COM server (VBA Example)

StatConnector allows to use R within MS Office VBA


Add reference to StatConnectorSrv 1.1 Type Library

Sub FirstR()
Dim nrandom As Integer, x As Double
nrandom = 100
Set StaR = New StatConnector
StaR.Init ("R")
With StaR
.SetSymbol "n", nrandom
.EvaluateNoReturn ("x <- rnorm(n)")
.EvaluateNoReturn ("pdf(file='c:/Temp/Testplot.pdf')")
.EvaluateNoReturn ("hist(x)")
.EvaluateNoReturn ("dev.off()")
x = .Evaluate("mean(x)")
End With
Debug.Print x
End Sub

Markus Gesmann
The ChainLadder package
48
rcom: Control MS Office from R

Using the rcom R-package you can write output from R


into MS Office application
Example: Create PowerPoint slide with MackChainLadder output
library(ChainLadder)
R <- MackChainLadder(RAA)
myfile=tempfile()
win.metafile(file=myfile)
plot(R)
dev.off()
#
library(rcom)
ppt<-comCreateObject("Powerpoint.Application")
comSetProperty(ppt,"Visible",TRUE)
myPresColl<-comGetProperty(ppt,"Presentations")
myPres<-comInvoke(myPresColl,"Add")
mySlides<-comGetProperty(myPres,"Slides")
mySlide<-comInvoke(mySlides,"Add",1,12)
myShapes<-comGetProperty(mySlide,"Shapes")
myPicture<-comInvoke(myShapes,"AddPicture",myfile,0,1,100,10)

Markus Gesmann
The ChainLadder package
49
More help ...
See examples on project web page
Read documentation on CRAN: http://cran.r-project.org/
web/packages/ChainLadder/ChainLadder.pdf
Read help pages in R:
?MackChainLadder
?MunichChainLadder
?BootChainLadder
Follow examples in R:
example(MackChainLadder)
example(MunichChainLadder)
example(BootChainLadder)

Markus Gesmann
The ChainLadder package
50
Conclusions

R is ideal for reserving


Built-in functions for statistical modelling
Powerful language for data manipulations
Fantastic graphical capabilities for analysis and
presentation
Easy to set-up connections to databases (ODBC)
RExcel add-in allows to share R functions with
colleagues without R knowledge
rcom allows to control MS Office from R
Effective knowledge transfer - plain text files

Markus Gesmann
The ChainLadder package
51
For a laugh - fancy 3d plot

library(rgl) #provides interactive 3d plotting functions


MCL=MackChainLadder(GenIns/1e6)
FT <- MCL$FullTriangle
FTpSE <- FT+MCL$Mack.S.E
FTpSE[which(MCL$Mack.S.E==0, arr.ind=TRUE)] <- NA
FTmSE <- FT-MCL$Mack.S.E
FTmSE[which(MCL$Mack.S.E==0, arr.ind=TRUE)] <- NA
zr <- round(FT/FT[1,10]*100)
zlim <- range(zr, na.rm=TRUE)
zlen <- zlim[2] - zlim[1] + 1
colorlut <- terrain.colors(zlen) # height color lookup table
cols <- colorlut[ zr -zlim[1]+1 ] # assign colors to heights for each point
x <- as.numeric(dimnames(FT)$origin)
y <- as.numeric(dimnames(FT)$dev)
persp3d(x, y=y,
z=(FT), col=cols, xlab="origin", ylab="dev", zlab="loss",back="lines")
mSE <- data.frame(as.table(FTmSE))
points3d(xyz.coords(x=as.numeric(as.character(mSE$origin)),
y=as.numeric(as.character(mSE$dev)),z=mSE$Freq), size=2)
pSE <- data.frame(as.table(FTpSE))
points3d(xyz.coords(x=as.numeric(as.character(pSE$origin)),
y=as.numeric(as.character(pSE$dev)),z=pSE$Freq), size=2)

Markus Gesmann
The ChainLadder package
52
References
Thomas Mack. Distribution-free calculation of the standard error of chain
ladder reserve estimates. Astin Bulletin. Vol. 23. No 2. 1993. pp 213-225.
Thomas Mack. The standard error of chain ladder reserve estimates:
Recursive calculation and inclusion of a tail factor. Astin Bulletin. Vol. 29. No
2. 1999. pp 361-366.
Murphy, Daniel M. Unbiased Loss Development Factors. Proceedings of the
Casualty Actuarial Society Casualty Actuarial Society - Arlington, Virginia
1994: LXXXI 154-222.
Zehnwirth and Barnett. Best estimates for reserves. Proceedings of the CAS,
LXXXVI I(167), November 2000.
P.D.England and R.J.Verrall, Stochastic Claims Reserving in General
Insurance, British Actuarial Journal, Vol. 8, pp.443-544, 2002.
Gerhard Quarg and Thomas Mack. Munich Chain Ladder. Bltter DGVFM 26,
Munich, 2004.
Nigel De Silva. An Introduction to R: Examples for Actuaries. Actuarial Toolkit
Working Party, version 0.1 edition, 2006. http://toolkit.pbwiki.com/RToolkit.

Markus Gesmann
The ChainLadder package
53

You might also like