Skip to content

Instantly share code, notes, and snippets.

@mages
Last active January 30, 2018 19:00
Show Gist options
  • Save mages/9db27042b96934fedb2111bcac660919 to your computer and use it in GitHub Desktop.
Save mages/9db27042b96934fedb2111bcac660919 to your computer and use it in GitHub Desktop.
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