# mages' blog

## R in Insurance 2015 Conference Programme

The programme for the 3rd R in Insurance conference is on-line. The event will take place on 29 June 2015 at the University of Amsterdam. Time to register now.

Special thanks to our sponsors, without whom the conference wouldn't be possible:
CYBAEA, RStudio, APPLIED AI, Milliman, QBE Re, AEGON, Delta Lloyd Amsterdam , Deloitte.

You find impressions from the previous events on www.rininsurance.com.

We hope to see you in Amsterdam!

## Combining several lattice charts into one

Last week I mentioned the grid.arrange function of the gridExtra package that allows me to combine graphical grid objects onto one page. The latticeExtra package provides another elegant solution for trellis (lattice) plots: the function c.trellis() or just c() combines the panels of multiple trellis objects into one.

Here is minimal example from the help file of c.trellis:

library(latticeExtra)
## Combine different types of plots.
c(wireframe(volcano), contourplot(volcano))


In my next example I am using data from Eurostat, the statistical office of the European Union, showing the use of public transport in four countries. The data can be accessed directly in R via the eurostat package; see also the package vignette.

Here I have two xyplot objects that I combine into one chart using a named vector. I know this is not the best way to present the data, but that is not the point here. Naming the elements in c() adds those names also into the panel strip. Very handy indeed!

### Session Info

R version 3.1.3 (2015-03-09)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.3 (Yosemite)

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods
[7] base

other attached packages:
[1] latticeExtra_0.6-26 lattice_0.20-31     RColorBrewer_1.1-2
[4] eurostat_1.0.16

loaded via a namespace (and not attached):
[1] grid_3.1.3     plyr_1.8.1     Rcpp_0.11.5    reshape2_1.4.1
[5] stringi_0.4-1  stringr_0.6.2  tidyr_0.2.0    tools_3.1.3

## Plotting tables alsongside charts in R

Occasionally I'd like to plot a table alongside a chart in R, e.g. to present summary statistics of the graph itself. Thanks to the gridExtra package this is quite straightforward. The function tableGrob creates a table like plot of a data frame, while arrangeGrob allows me to arrange ggplot2, lattice and grid graphical objects (short 'grobs', such as tableGrob) on a page.

Here is a little example:

### Session Info

R version 3.2.1 (2015-06-18)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.4 (Yosemite)

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods
[7] base

other attached packages:
[1] gridExtra_2.0.0 ggplot2_1.0.1

loaded via a namespace (and not attached):
[1] Rcpp_0.11.6      digest_0.6.8     MASS_7.3-42
[4] grid_3.2.1       plyr_1.8.3       gtable_0.1.2
[7] magrittr_1.5     scales_0.2.5     stringi_0.5-5
[10] reshape2_1.4.1   proto_0.3-10     labeling_0.3
[13] tools_3.2.1      stringr_1.0.0    munsell_0.4.2
[16] colorspace_1.2-6

## Test Driven Analysis

I mused over Test Driven Analysis on this blog before, but it was Richard Pugh's talk on SAS to R Migration at LondonR last week that brought the topic back into my mind and clarified a few things.

Rich's presentation focused on the challenge of how to ensure that the new system (R) would provide the same answers as the legacy system (SAS).

This is when it clicked with me: My brain is just another system as well. Suppose you have an idea for an analysis in your head. Taking that idea and transforming it into code is basically just the same as migrating code from one system to another system. Or, isn't it?

Rich showed us how he does it: Start with the old code, write unit tests in the legacy system to confirm your understanding, re-write the unit tests in the new system and then start building the new analysis code in the new system.

Once he achieved that, he said, he would go backwards in forwards between the different pieces until he has enough confidence that the new system does what it supposed to do.

Test Driven Analysis is just that as well.

I start with an idea in my head, think about reasonable checks and following that I (should) write down unit tests and only then start writing the analysis code. Finally I go backwards and forwards until I have gained enough evidence and confidence to present my output and be able to defend it.