-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path07_2_mcelreath_ch4_code.R
More file actions
237 lines (191 loc) · 5.61 KB
/
Copy path07_2_mcelreath_ch4_code.R
File metadata and controls
237 lines (191 loc) · 5.61 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# Code for McElreath Ch 4, from section 4.3.5
# You may encounter an error from time to time that says "Start values for
# parameters may be too far from MAP". This is a familiar problem in training a
# model. Here the algorithm is randomly initializing start values that are too
# far from the high-probability region. We just need to try again until we get
# better start values (we don't need to change any values; simply run the block
# of code again until it works). The error may cause R studio to enter debug
# mode. Hit Esc to get out of debug mode then run the block of code again.
# R code 4.24
library(rethinking)
data(Howell1)
d <- Howell1
d2 <- d[d$age >= 18,]
# Add the new function sampost
# To sample posterior
sampost <- function(flist, data, n=10000) {
quadapprox <- map(flist, data)
posterior_sample <- extract.samples(quadapprox, n)
return(posterior_sample)
}
# R code 4.25
flist <- alist(
height ~ dnorm(mu, sigma),
mu ~ dnorm(178, 20),
sigma ~ dunif(0, 50)
)
# R code 4.26
m4.1 <- sampost(flist, data=d2)
# R code 4.27
precis(m4.1)
head(m4.1)
hist(m4.1$mu, breaks=100, freq=FALSE, col="lightblue")
hist(m4.1$sigma, breaks=100, freq=FALSE, col="lightblue")
# R code 4.29
m4.2 <- sampost(
alist(
height ~ dnorm(mu, sigma),
mu ~ dnorm(178, 0.1),
sigma ~ dunif(0, 50)
),
data=d2 )
precis(m4.2)
hist(m4.2$mu, breaks=100, freq=FALSE, col="lightblue")
hist(m4.2$sigma, breaks=100, freq=FALSE, col="lightblue")
# R code 4.32
head(m4.1)
# R code 4.33
precis(m4.1)
# R code 4.37
plot(d2$height ~ d2$weight)
# R code 4.38
# Fit model
m4.3 <- sampost(
alist(
height ~ dnorm(mu, sigma),
mu <- a + b * weight,
a ~ dnorm(156, 100),
b ~ dnorm(0, 10),
sigma ~ dunif(0, 50)
),
data=d2 )
# R code 4.40
precis(m4.3)
hist(m4.3$a, breaks=100, freq=FALSE, col="lightblue")
hist(m4.3$b, breaks=100, freq=FALSE, col="lightblue")
hist(m4.3$sigma, breaks=100, freq=FALSE, col="lightblue")
# R code 4.41
round(cor(m4.3), 2)
# R code 4.42
d2$weight.c <- d2$weight - mean(d2$weight)
# R code 4.43
m4.4 <- sampost(
alist(
height ~ dnorm(mu, sigma),
mu <- a + b * weight.c,
a ~ dnorm(178, 100),
b ~ dnorm(0, 10),
sigma ~ dunif(0, 50)
),
data=d2 )
# R code 4.44
precis(m4.4)
round(cor(m4.4), 2)
# R code 4.45
plot(height ~ weight, data=d2)
abline(a=mean(m4.3[,"a"]), b=mean(m4.3[,"b"]))
# R code 4.47
m4.3[1:5,]
# R code 4.48
N <- 10 #N <- 352 is the full dataset
dN <- d2[1:N,]
mN <- sampost(
alist(
height ~ dnorm(mu, sigma),
mu <- a + b * weight,
a ~ dnorm(178, 100),
b ~ dnorm(0, 10),
sigma ~ dunif(0, 50)
),
data=dN )
# R code 4.49
# display raw data and sample size
plot(dN$weight, dN$height, xlim=range(d2$weight), ylim=range(d2$height),
col=rangi2, xlab="weight", ylab="height")
mtext(concat("N = ", N))
# plot the lines, with transparency
for ( i in 1:20 ) {
abline(a=mN$a[i], b=mN$b[i], col=col.alpha("black", 0.3))
}
# R code 4.50
mu_at_50 <- m4.3$a + m4.3$b * 50
# R code 4.51
hist(mu_at_50, breaks=100, freq=FALSE, col="lightblue")
# R code 4.52
HPDI(mu_at_50, prob=0.89)
# Replacement code for credible intervals and plotting
w <- seq(from=25, to=70, by=1) #or 25:70
n <- length(w)
hpdi_m <- matrix(NA, nrow=n, ncol=2) #matrix to store hpdi values
colnames(hpdi_m) <- c("low89","high89") #optional but nice
for ( i in 1:n ) {
mu <- m4.3$a + m4.3$b * w[i] #the posterior sample of mu at weight w
hpdi_m[i,] <- HPDI(mu, prob=0.89) #hpdi of the sample
}
hpdi_m
plot(height ~ weight, data=d2, col = "blue")
abline(a=mean(m4.3[,"a"]), b=mean(m4.3[,"b"]))
lines(w, hpdi_m[,"low89"], col="grey")
lines(w, hpdi_m[,"high89"], col="grey")
# Replacement code for prediction intervals and plotting
w <- seq(from=25, to=70, by=1) #or 25:70
n <- length(w)
pred_hpdi_m <- matrix(NA, nrow=n, ncol=2) #matrix to store hpdi values
colnames(pred_hpdi_m) <- c("low89","high89")
for ( i in 1:n ) {
mu <- m4.3$a + m4.3$b * w[i] #the posterior sample of mu at weight w
#Here is the new bit: the normal distribution sample
newdat <- rnorm(n=length(mu), mu, sd=m4.3$sigma)
pred_hpdi_m[i,] <- HPDI(newdat, prob=0.89) #hpdi of the sample
}
pred_hpdi_m
plot(height ~ weight, data=d2, col = "blue")
abline(a=mean(m4.3[,"a"]), b=mean(m4.3[,"b"]))
lines(w, hpdi_m[,"low89"], col="grey")
lines(w, hpdi_m[,"high89"], col="grey")
lines(w, pred_hpdi_m[,"low89"], col="grey")
lines(w, pred_hpdi_m[,"high89"], col="grey")
# R code 4.64
library(rethinking)
data(Howell1)
d <- Howell1
str(d)
# R code 4.65
d$weight.s <- ( d$weight - mean(d$weight) ) / sd(d$weight)
# R code 4.66
d$weight.s2 <- d$weight.s ^ 2
m4.5 <- sampost(
alist(
height ~ dnorm(mu, sigma),
mu <- a + b1 * weight.s + b2 * weight.s2,
a ~ dnorm(178, 100),
b1 ~ dnorm(0, 10),
b2 ~ dnorm(0, 10),
sigma ~ dunif(0, 50)
),
data=d )
# R code 4.67
precis(m4.5)
# Modify code above for HPDI of credible intervals and prediction intervals
# R code 4.70
d$weight.s3 <- d$weight.s ^ 3
m4.6 <- sampost(
alist(
height ~ dnorm(mu, sigma),
mu <- a + b1 * weight.s + b2 * weight.s2 + b3 * weight.s3,
a ~ dnorm(178, 100),
b1 ~ dnorm(0, 10),
b2 ~ dnorm(0, 10),
b3 ~ dnorm(0, 10),
sigma ~ dunif(0, 50)
),
data=d )
precis(m4.6)
# R code 4.71
plot(height ~ weight.s, data=d, col=col.alpha(rangi2, 0.5), xaxt="n")
# R code 4.72
at <- c(-2,-1,0,1,2)
labels <- at * sd(d$weight) + mean(d$weight)
axis(side=1, at=at, labels=round(labels, 1))
# R code 4.73
plot(height ~ weight, data=Howell1, col=col.alpha(rangi2, 0.4))