Skip to content

Instantly share code, notes, and snippets.

@mages mages/plotBananas.R
Last active Jan 30, 2018

Embed
What would you like to do?
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,
just = "left",
x = grid::unit(5, "mm")))
key <- list(rep=FALSE, columns=2,
lines=list(col=col,
type=c("p", "p", "l"),
pch=c(19, 1, NA)),
text=list(lab=c("Observation", "Hold out observation",
"Prediction")))
xyplot(x, data, t=c("b", "b", "l"), pch=c(19, 1, NA),
col=col,
layout=c(5,2),
par.settings = my.settings ,
par.strip.text = list(font = 2),
key = key,
as.table=TRUE,
scales=list(alternating=1),
xlab=xlab, ylab=ylab, main=main,
sub=paste("Data source: CAS Loss Reserving Database, ",
company_code), ...)
}
plotDevBananas <- function(x, data,
xlab="Development year",
ylab="$k",
main="Model output",
...){
library(latticeExtra)
key <- list(
rep=FALSE,
lines=list(col=c("#00526D", "#00526D", "purple"),
type=c("p", "p", "l"),
pch=c(19, 1, NA)),
text=list(lab=c("Observation", "Hold out observation", "Mean estimate")),
rectangles = list(col=adjustcolor("yellow", alpha.f=0.5), border="grey"),
text=list(lab="95% Prediction credible interval"))
xyplot(x, data=data,as.table=TRUE,xlab=xlab, ylab=ylab, main=main,
scales=list(alternating=1), layout=c(5,2), key=key,
par.settings = list(strip.background=list(col="#CBDDE6")),
par.strip.text = list(font = 2),
panel=function(x, y){
panel.abline(h=0, col="grey")
n <- length(x)
divisor <- 5
cn <- c(1:(n/divisor))
upper <- y[cn+n/divisor*0]
lower <- y[cn+n/divisor*1]
x <- x[cn]
panel.polygon(c(x, rev(x)), c(upper, rev(lower)),
col = adjustcolor("yellow", alpha.f = 0.5),
border = "grey")
panel.lines(x, y[cn+n/divisor*2], col="purple")
panel.points(x, y[cn+n/divisor*4], lwd=1, col="#00526D")
panel.points(x, y[cn+n/divisor*3], lwd=1, pch=19, col="#00526D")
}, ...)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.