Next Kölner R User Meeting: 18 Oktober 2013
Quick reminder: The next Cologne R user group meeting is scheduled for this Friday, 18 October 2013. We will discuss and hear about the apply family of functions and the XLConnect package. Further details and the agenda are available on our KölnRUG Meetup site. Please sign up if you would like to come along. Notes from past meetings are available here.
Thanks to Revolution Analytics, who sponsors the Cologne R user group as part of their vector programme.
15 Oct 2013
06:17
Kölner R Users
,
KölnR
,
News
,
R
Why models need a certain culture to flourish
About half a year ago Ian Branagan, Chief Risk Officer of Renaissance Re - a Bermudian reinsurance company with a focus on property catastrophe insurance, gave a talk about the usage of models in risk management and how they evolved over the last twenty years. Ian's presentation, titled with the famous quote of George E.P. Box: "All models are wrong, but some are useful", was part of the lunch time lecture series of talks at Lloyd's, organised by the Insurance Institute of London.
I re-discovered the talk online over the weekend and found it most enlightening again.
So, what makes models useful? And here I mean models that estimate extreme outcomes / percentiles. Three factors are critical, according to Ian, to embed models successfully in risk management and decision making processes.
Where in the past senior management may have relied on advisors' expert judgement to guide them in their decision makings, they have to use models in a similar way now as well. I suppose, in the same way as it takes time and effort to build effective relationships with people, it is true for models as well. And equally, decisions should never rely purely on either other people's opinion or indeed model output. As Ian put it, outsourcing all modelling/thinking, and with that the decision making to vendors of models, such as catastrophe modelling companies or rating agencies, who both aim to provide probabilities for extreme events (catastrophes and companies failures) may be sufficient to tick a risk management box, but can ultimately put the company at risk, if model assumptions and limitations are not well understood.
Perhaps we are at the dawn of another enlightenment? Recall Kant's first sentence of his essay What is enlightenment?: "Enlightenment is man's emergence from his self-incurred immaturity." Indeed, it doesn't matter if we use experts' opinions or the output of models, relying blindly on them is dangerous and foolish. Don't stop thinking for yourself. Be critical! Remember, all models are wrong, but some are useful.
I re-discovered the talk online over the weekend and found it most enlightening again.
So, what makes models useful? And here I mean models that estimate extreme outcomes / percentiles. Three factors are critical, according to Ian, to embed models successfully in risk management and decision making processes.
- Need - A clear defined need for the model.
- Capabilities - The skills and resources to build and maintain the model.
- Culture - An organisational culture that embraces, understands and challenges the model.
Where in the past senior management may have relied on advisors' expert judgement to guide them in their decision makings, they have to use models in a similar way now as well. I suppose, in the same way as it takes time and effort to build effective relationships with people, it is true for models as well. And equally, decisions should never rely purely on either other people's opinion or indeed model output. As Ian put it, outsourcing all modelling/thinking, and with that the decision making to vendors of models, such as catastrophe modelling companies or rating agencies, who both aim to provide probabilities for extreme events (catastrophes and companies failures) may be sufficient to tick a risk management box, but can ultimately put the company at risk, if model assumptions and limitations are not well understood.
Perhaps we are at the dawn of another enlightenment? Recall Kant's first sentence of his essay What is enlightenment?: "Enlightenment is man's emergence from his self-incurred immaturity." Indeed, it doesn't matter if we use experts' opinions or the output of models, relying blindly on them is dangerous and foolish. Don't stop thinking for yourself. Be critical! Remember, all models are wrong, but some are useful.
Creating a matrix from a long data.frame
There can never be too many examples for transforming data with R. So, here is another example of reshaping a
Here I have a data frame that shows incremental claim payments over time for different loss occurrence (origin) years.
The format of the data frame above is how this kind of data is usually stored in a data base. However, I would like to see the payments of the different origin years in rows of a matrix.
The first idea might be to use the
An elegant alternative to
data.frame into a matrix.Here I have a data frame that shows incremental claim payments over time for different loss occurrence (origin) years.
The format of the data frame above is how this kind of data is usually stored in a data base. However, I would like to see the payments of the different origin years in rows of a matrix.
The first idea might be to use the
reshape function, but that would return a data.frame. Yet, it is actually much easier with the matrix function itself. Most of the code below is about formatting the dimension names of the matrix. Note that I use the with function to save me a bit of typing.An elegant alternative to
matrix provides the acast function of the reshape2 package. It has a nice formula argument and allows me not only to specify the aggregation function, but also to add the margin totals.Changing the width of bars and columns in googleVis
Changing the plotting width in bar-, column- and combo-charts of googleVis works identical and is defined by the
bar.groupWidth argument. The dot in the argument means that it has to be split in R into bar="{groupWidth:'10%'}". Example
library(googleVis)
cc <- gvisColumnChart(head(Population,10),
xvar="Country", yvar="Population",
options=list(seriesType="bars", legend="top",
bar="{groupWidth:'10%'}",
width=500, height=450),
chartid="thincolumns")
plot(cc)Session Info
R version 3.0.1 (2013-05-16)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
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 base
other attached packages:
[1] googleVis_0.4.5
loaded via a namespace (and not attached):
[1] RJSONIO_1.0-3 tools_3.0.1
24 Sept 2013
07:16
googleVis
,
gvisColumnChart
,
R
,
width
Using planel.groups in lattice
Last Tuesday I attended the LondonR user group meeting, where Rich and Andy from Mango argued about the better package for multivariate graphics with R: lattice vs. ggplot2.
As part of their talk they had a little competition in visualising London Underground performance data, see their slides. Both made heavy use of the respective panelling / faceting capabilities. Additionally Rich used the
As part of their talk they had a little competition in visualising London Underground performance data, see their slides. Both made heavy use of the respective panelling / faceting capabilities. Additionally Rich used the
panel.groups argument of xyplot to fine control the content of each panel. Brilliant! I had never used this argument before. So, here is a silly example with the iris data set to remind myself of panel.groups in the future.
17 Sept 2013
07:02
lattice
,
LondonR
,
panel.groups
,
R
ave and the "[" function in R
The
The top of
Group Averages Over Level Combinations of Factors
Subsets of x[] are averaged, where each subset consist of those observations with the same factor levels.
As an example I look at revenue data by product and shop.
I think it is the following sentence in the help file of
So there we are. I feel less inclined to use
ave function in R is one of those little helper function I feel I should be using more. Investigating its source code showed me another twist about R and the "[" function. But first let's look at ave.The top of
ave's help page reads:Group Averages Over Level Combinations of Factors
Subsets of x[] are averaged, where each subset consist of those observations with the same factor levels.
As an example I look at revenue data by product and shop.
revenue <- c(30,20, 23, 17)
product <- factor(c("bread", "cake", "bread", "cake"))
shop <- gl(2,2, labels=c("shop_1", "shop_2"))
To answer the question "Which shop sells proportionally more bread?" I need to divide the revenue vector by the sum of revenue per shop, which can be calculated easily by ave:(shop_revenue <- ave(revenue, shop, FUN=sum))
# [1] 50 50 40 40
(revenue_split_in_shop <- revenue/shop_revenue)
# [1] 0.600 0.400 0.575 0.425 # Shop 1 sells more bread than cake
In other words, ave has to split the revenue vector by shop and apply the sum function to it. Well that's exactly what it does. Here is the source code of ave:# Copyright (C) 1995-2012 The R Core Team
ave <- function (x, ..., FUN = mean)
{
if(missing(...))
x[] <- FUN(x)
else {
g <- interaction(...)
split(x,g) <- lapply(split(x, g), FUN)
}
x
}However, and this is what intrigued me, if I don't provide a grouping variable (missing(...)) it will apply the function FUN on x itself and write its output to x[]. That's actually what the help file to ave mentioned in its description. So what does it do? Here is an example again:ave(revenue, FUN=sum)
# [1] 90 90 90 90I get the sum of revenue repeated as many time as the vector has elements, not just once, as with sum(revenue). The trick is that the output of FUN(x) is written into x[], which of course is output of a function call itself "["(x). I think it is the following sentence in the help file of
"[" (see ?"["), which explains it: Subsetting (except by an empty index) will drop all attributes except names, dim and dimnames.So there we are. I feel less inclined to use
ave more, as it is just short for the usual split, lapply routine, but I learned something new about the subtleties of R.Doughnut chart in R with googleVis
The guys at Google continue to update and enhance the Chart Tools API. One new recent feature is a pie chart with a hole, or as some call them: donut charts.
Thankfully the new functionality is being achieved through new options for the existing pie chart, which means that those new features are available in R via googleVis as well, without the need of writing new code.
Thankfully the new functionality is being achieved through new options for the existing pie chart, which means that those new features are available in R via googleVis as well, without the need of writing new code.
Doughnut chart example
With the German election coming up soon, here is the composition of the current parliament.Session Info
R version 3.0.1 (2013-05-16)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
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] googleVis_0.4.5
loaded via a namespace (and not attached):
[1] RJSONIO_1.0-3 tools_3.0.1
3 Sept 2013
07:02
doughnut
,
googleVis
,
gvisPieChart
,
R

