forked from avehtari/BDA_R_demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo6_4.Rmd
94 lines (74 loc) · 2.24 KB
/
demo6_4.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
---
title: "Bayesian data analysis demo 6.4"
author: "Aki Vehtari, Markus Paasiniemi"
date: "`r format(Sys.Date())`"
output:
html_document:
theme: readable
code_download: true
---
## 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
```{r setup, message=FALSE, error=FALSE, warning=FALSE}
library(ggplot2)
theme_set(theme_minimal())
library(tidyr)
library(latex2exp)
library(rprojroot)
root<-has_file(".BDA_R_demos_root")$make_fix_file()
```
Data
```{r }
y <- read.table(root("demos_ch6","light.txt"))$V1
```
Sufficient statistics
```{r }
n <- length(y)
s <- sd(y)
my <- mean(y)
```
Tail area probabilities of marginal predictive distributions,
aka probability integral transformation (PIT)
```{r }
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 marginal posterior 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('\\mathit{p}(\\mathit{y}^{\\mathrm{rep}}_{\\mathit{i}} < \\mathit{y_i} | \\mathit{y})'),
y = '', title = title1) +
scale_y_continuous(breaks=NULL)
```
Repeat the PIT checking after removing two "outliers"
```{r }
y <- y[y>0]
```
Sufficient statistics
```{r }
n <- length(y)
s <- sd(y)
my <- mean(y)
```
Tail area probabilities of marginal predictive distributions,
aka probability integral transformation (PIT)
```{r }
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.
```{r }
title1 <- 'Light speed example
distribution of marginal posterior 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('\\mathit{p}(\\mathit{y}^{\\mathrm{rep}}_{\\mathit{i}} < \\mathit{y_i} | \\mathit{y})'),
y = '', title = title1) +
scale_y_continuous(breaks=NULL)
```