Marginal posterior predictive checking
Tail area probabilities of marginal predictive distributions, aka probability integral transformation (PIT).
Normal model for light speed data.
ggplot2 is used for plotting, tidyr for manipulating data frames
library(ggplot2)
library(bayesplot)
theme_set(bayesplot::theme_default(base_family = "sans"))
library(tidyr)
library(latex2exp)
library(rprojroot)
root<-has_file(".BDA_R_demos_root")$make_fix_file()
Data
y <- read.table(root("demos_ch6","light.txt"))$V1
Sufficient statistics
n <- length(y)
s <- sd(y)
my <- mean(y)
Tail area probabilities of marginal predictive distributions, aka probability integral transformation (PIT)
Ty <- data.frame(x = pt((y - my)/(sqrt(1+1/n)*s), n-1))
#* Plot histogram of PIT values. Ideally histogram should be close to uniform.
title1 <- 'Light speed example
distribution of predictive distribution tail-values'
ggplot(data = Ty) +
geom_histogram(aes(x = x), fill = 'steelblue',
color = 'black', binwidth = 0.05) +
coord_cartesian(xlim = c(0, 1)) +
labs(x = TeX(r"($\textit{p}(\textit{y}^{\textrm{rep}}_{\textit{i}} < \textit{y_i} | \textit{y})$)"),
y = '', title = title1) +
scale_y_continuous(breaks=NULL)
#* Plot ECDF of PIT values. Ideally ECDF should be close to diagonal line
ggplot(data=data.frame(x=Ty), aes(x)) +
stat_ecdf(geom = "step", color=4) +
xlim(c(0,1))+
labs(x="Observed PIT values", y="ECDF")+
annotate(geom="segment",x=0,y=0,xend=1,yend=1)
Repeat the PIT checking after removing two “outliers”
y <- y[y>0]
Sufficient statistics
n <- length(y)
s <- sd(y)
my <- mean(y)
Tail area probabilities of marginal predictive distributions, aka probability integral transformation (PIT)
Ty <- data.frame(x = pt((y - my)/(sqrt(1+1/n)*s), n-1))
Plot histogram of PIT values. Ideally histogram should be close to uniform.
title1 <- 'Light speed example
distribution of predictive distribution tail-values'
ggplot(data = Ty) +
geom_histogram(aes(x = x), fill = 'steelblue',
color = 'black', binwidth = 0.05) +
coord_cartesian(xlim = c(0, 1)) +
labs(x = TeX(r"($\textit{p}(\textit{y}^{\textrm{rep}}_{\textit{i}} < \textit{y_i} | \textit{y})$)"),
y = '', title = title1) +
scale_y_continuous(breaks=NULL)
#* Plot ECDF of PIT values. Ideally ECDF should be close to diagonal line
ggplot(data=data.frame(x=Ty), aes(x)) +
stat_ecdf(geom = "step", color=4) +
xlim(c(0,1))+
labs(x="Observed PIT values", y="ECDF")+
annotate(geom="segment",x=0,y=0,xend=1,yend=1)
LS0tCnRpdGxlOiAiQmF5ZXNpYW4gZGF0YSBhbmFseXNpcyBkZW1vIDYuNCIKYXV0aG9yOiAiQWtpIFZlaHRhcmksIE1hcmt1cyBQYWFzaW5pZW1pIgpkYXRlOiAiYHIgZm9ybWF0KFN5cy5EYXRlKCkpYCIKb3V0cHV0OgogIGh0bWxfZG9jdW1lbnQ6CiAgICB0aGVtZTogcmVhZGFibGUKICAgIGNvZGVfZG93bmxvYWQ6IHRydWUKLS0tCiMjIE1hcmdpbmFsIHBvc3RlcmlvciBwcmVkaWN0aXZlIGNoZWNraW5nCgpUYWlsIGFyZWEgcHJvYmFiaWxpdGllcyBvZiBtYXJnaW5hbCBwcmVkaWN0aXZlIGRpc3RyaWJ1dGlvbnMsCmFrYSBwcm9iYWJpbGl0eSBpbnRlZ3JhbCB0cmFuc2Zvcm1hdGlvbiAoUElUKS4KCk5vcm1hbCBtb2RlbCBmb3IgbGlnaHQgc3BlZWQgZGF0YS4KCmdncGxvdDIgaXMgdXNlZCBmb3IgcGxvdHRpbmcsIHRpZHlyIGZvciBtYW5pcHVsYXRpbmcgZGF0YSBmcmFtZXMKCmBgYHtyIHNldHVwLCBtZXNzYWdlPUZBTFNFLCBlcnJvcj1GQUxTRSwgd2FybmluZz1GQUxTRX0KbGlicmFyeShnZ3Bsb3QyKQpsaWJyYXJ5KGJheWVzcGxvdCkKdGhlbWVfc2V0KGJheWVzcGxvdDo6dGhlbWVfZGVmYXVsdChiYXNlX2ZhbWlseSA9ICJzYW5zIikpCmxpYnJhcnkodGlkeXIpCmxpYnJhcnkobGF0ZXgyZXhwKQpsaWJyYXJ5KHJwcm9qcm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