# mages' blog

## ChainLadder 0.1.5-6 released on CRAN

Last week we released version 0.1.5-6 of the ChainLadder package on CRAN. The ChainLadder package provides statistical models, which are typically used for the estimation of outstanding claims reserves in general insurance. The package vignette gives an overview of the package functionality.

 Output of plot(MackChainLadder(GenIns))

Since the last CRAN release Dan Murphy added new features to the MackChainLadder function and we fixed a bug in BootChainLadder. Here are he details:

### New Features

• The list output of the MackChainLadder function now includes the parameter risk and process risk breakdowns of the total risk estimate for the sum of projected losses across all origin years by development age.
• The Mack Method's recursive parameter risk calculation now enables Mack's original two-term formula (the default) and optionally the three-term formula found in Murphy's 1994 paper and in the 2006 paper by Buchwalder, Bühlmann, Merz, and Wüthrich.
• A few more Mack Method examples.

### Bug Fixes

• The phi-scaling factor in BootChainLadder was incorrect. Instead of calculating the number of data items in the upper left triangle as n*(n+1)/2, n*(n-1)/2 was used. Thanks to Thomas Girodot for reporting this bug.

Please get in touch if you would like to collaborate or find any issues or bugs.

Join me at the first R in Insurance conference at Cass Business School in London, 15 July 2013.

## Submit a talk for the first R in Insurance conference

The registration for the first R in Insurance is open and there is still time to submit a talk / lightning talk.

The conference will take place at Cass Business School in London on Monday, 15 July 2013. This is the Monday following the useR! 2013 conference in Spain. Thus, if you come from overseas to Spain, why not stop in London on your way back?

All further information and registration details are available on the Cass Business School conference site.

## googleVis 0.4.2 with support for shiny released on CRAN

The new version of googleVis 0.4.2 is now available via CRAN. Many thanks to all who provided feedback on version 0.4.0 and particularly to Sebastian Campbell, John Maindonald and Aonan Zhang. As usual, if you find any issues or bugs, please send us an email or add a line to our online issues log.

With version 0.4.0 we introduced support for googleVis on shiny. See my previous post for more details and examples.

The CRAN release of googleVis 0.4.2 has some further improvements:
• New shiny and FAQ sections in package vignette
• Core charts (e.g. line, area, scatter, bar, column and combo charts) accept now also date variables for the x-axis
• Typos in the Stock and Andrew data sets have been fixed
• The WorldBank demo uses now the WDI package to download data from the World Bank, see the R code below

## How to use optim in R

A friend of mine asked me the other day how she could use the function optim in R to fit data. Of course there are built-in functions for fitting data in R and I wrote about this earlier. However, she wanted to understand how to do this from scratch using optim.

The function optim provides algorithms for general-purpose optimisations and the documentation is perfectly reasonable, but I remember that it took me a little while to get my head around how to pass data and parameters to optim. Thus, here are two simple examples.

I start with a linear regression by minimising the residual sum of square and discuss how to carry out a maximum likelihood estimation in the second example.

### Minimise residual sum of squares

I start with an x-y data set, which I believe has a linear relationship and therefore I'd like to fit y against x by minimising the residual sum of squares.
dat=data.frame(x=c(1,2,3,4,5,6),
y=c(1,3,5,6,8,12))

Next, I create a function that calculates the residual sum of square of my data against a linear model with two parameter. Think of y = par[1] + par[2] * x.
min.RSS <- function(data, par) {
with(data, sum((par[1] + par[2] * x - y)^2))
}
Optim minimises a function by varying its parameters. The first argument of optim are the parameters I'd like to vary, par in this case; the second argument is the function to be minimised, min.RSS. The tricky bit is to understand how to apply optim to your data. The solution is the ... argument in optim, which allows me to pass other arguments through to min.RSS, here my data. Therefore I can use the following statement:
result <- optim(par = c(0, 1), min.RSS, data = dat)
# I find the optimised parameters in result$par # the minimised RSS is stored in result$value
result
## $par ## [1] -1.267 2.029 ## ##$value
## [1] 2.819
##
## $counts ## function gradient ## 89 NA ## ##$convergence
## [1] 0
##
## $message ## NULL Let me plot the result: plot(y ~ x, data = dat, main="Least square regression") abline(a = result$par[1], b = result\$par[2], col = "red")

## Create an R package from a single R file with roxyPackage

Documenting code can be a bit of a pain. Yet, the older (and wiser?) I get, the more I realise how important it is. When I was younger I said 'documentation is for people without talent'. Well, I am clearly loosing my talent, as I sometimes struggle to understand what I programmed years ago. Thus, anything that soothes the pain of writing and maintaining documentation must be good and should help me to better understand my 'old me' in the future.

Ideally I want my R code and documentation in as few files as possible. A good start to achieve this is using roxygen2, an R package which has been around for some time. It allows me to tie R code together with the documentation in the same file and helps considerably in maintaining R packages.

The roxyPackage by Meik Michalke goes a step further, building on roxygen2. Meik presented his package at the Cologne R user group meeting a few weeks ago and I was intrigued by it. As I said in my notes to the meeting, with roxyPackage I can create a package from a single file of R code and documentation.

Here is an example of one R file to build a package using roxyPackage. For my toy example I wrote a doughnut plot function in R, something which is clearly missing ;)

I took the basic pie chart function and amended it to plot another white disc in the middle. On top of the function code I wrote the help file documentation using the roxygen2 syntax.