Skip to content

Instantly share code, notes, and snippets.

@mages
mages / ClusterAnalysis.R
Last active July 24, 2018 01:44
k-means cluster analysis in R
library(jpeg)
library(RCurl)
url <-"https://raw.githubusercontent.com/mages/diesunddas/master/Blog/LloydsBuilding.jpg"
readImage <- readJPEG(getURLContent(url, binary=TRUE))
dm <- dim(readImage)
rgbImage <- data.frame(
x=rep(1:dm[2], each=dm[1]),
y=rep(dm[1]:1, dm[2]),
r.value=as.vector(readImage[,,1]),
g.value=as.vector(readImage[,,2]),

Stan in Insurance Workshop 17 July 2018

[8:30-9:00] Registration

[9:00 - 10:30] Eric Novik

  • Intro to Stan, including:
    • Coding linear regression to assess wine quality
    • Demonstrating important parts of the Stan program
  • Doing some basic posterior predicting checking
@mages
mages / Using_R_in_Insurance_GIRO_2012.Rmd
Created September 9, 2012 22:33
Using R in Insurance GIRO 2012
Using R in Insurance
==============================
```{r wordcloud, echo=FALSE, message=FALSE, fig.cap='Markus Gesmann, GIRO Brussels, 19 September 2012'}
library(ChainLadder)
library(googleVis)
library(wordcloud)
library(tm)
## About GIRO, sourced from:
## http://www.actuaries.org.uk/events/residential/giro-conference-and-exhibition-2012/about
@mages
mages / gist:1694442
Created January 28, 2012 14:15
Say it in R with by, apply and friends
## Markus Gesmann, January 2012
## Please install the following R packages first:
## data.table, doBy, plyr, reshape, sqldf, e.g. via
## install.packages(c("data.table", "doBy", "plyr", "reshape", "sqldf"))
f <- function(x) x^2
sapply(1:10, f)
do.call("rbind", as.list(
by(iris, list(Species=iris$Species), function(x){
stanmodel <- "
data {
int<lower=0> N;
real x[N];
real Y[N];
}
parameters {
real alpha;
real beta;
real<lower=.5,upper= 1> lambda; // original gamma in the JAGS example
# Create some sample data
CV_1 <- 0.2
CV_2 <- 0.3
Mean <- 65
sigma_1 <- sqrt(log(1 + CV_1^2))
mu_1 <- log(Mean) - sigma_1^2 / 2
sigma_2 <- sqrt(log(1 + CV_2^2))
mu_2 <- log(Mean) - sigma_2^2 / 2
q <- c(0.25, 0.5, 0.75, 0.9, 0.95)
SummaryTable <- data.frame(
checkModelPlot <- function(x, data, delta, company_code, col=c(1,2),
main="Reported incurred loss development by accident year",
ylab="Loss ratio (%)", xlab="Development year",...){
library(latticeExtra)
my.settings <- list(
strip.background=list(col="#CBDDE6"),
par.main.text = list(font = 2, # make it bold
just = "left",
x = grid::unit(5, "mm")),
par.sub.text = list(font = 1,
## Copyright Markus Gesmann, January 2013
## See: http://lamages.blogspot.co.uk/2013/01/reserving-based-on-log-incremental.html
## Also: www.actuaries.org.uk/system/files/documents/pdf/crm2-D5.pdf
##
## Incremental claims triangle
tri <- t(matrix(
c(11073, 6427, 1839, 766,
14799, 9357, 2344, NA,
15636, 10523, NA, NA,
16913, NA, NA, NA),
n <- 50
m <- 50
set.seed(1)
mu <- -0.4
sig <- 0.12
x <- matrix(data=rlnorm(n*m, mu, sig), nrow=m)
library(fitdistrplus)
## Fit a log-normal distribution to the 50 random data set
f <- apply(x, 2, fitdist, "lnorm")