-
Notifications
You must be signed in to change notification settings - Fork 18
/
ch05_hw.R
433 lines (380 loc) · 11.1 KB
/
ch05_hw.R
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
library(rethinking)
library(MASS)
library(ggplot2)
library(GGally)
library(dplyr)
library(htmltab)
library(stringr)
library(tidyr)
#source("plot_bindings.R")
## Easy ####
# 5E3 (related)
# I am curious about generating dataset with similar properties as described in task
# I'm looking for dependent variable z that
# z ~ a*x+b*y -> a>>0 and b>>0 (significantly greater then zero)
# but
# z ~ a*x --> a ~ 0 (a is close to zero)
# z ~ b*y --> b ~ 0
N <- 1000
rho <- 0.8
set.seed(11)
x <- rnorm(N, 10)
y <- rnorm(N, -rho*x , sqrt(1-rho^2) )
d <- data.frame(x=x, y=y, z=rnorm(1000, x + y, 0.5))
ggpairs(d)
summary(lm(z~y, d))
summary(lm(z~x, d))
summary(lm(z~x+y, d))
m5e3x <- map(
alist(
z ~ dnorm(mu, sigma),
mu <- a + bx*x,
a ~ dnorm(0,10),
bx ~ dnorm(0, 1),
sigma ~ dunif(0, 10)
),
data = d
)
summary(m5e3x)
plot(z~x,d, col = col.alpha("blue",0.5))
x.seq <- seq(7, 14, by=0.1)
mu.5e3x <- link(m5e3x, data=list(x=x.seq), n=1000)
mu.5e3x.mean <- apply(mu.5e3x, 2, mean)
mu.5e3x.pi <- apply(mu.5e3x, 2, PI)
lines(x.seq, mu.5e3x.mean, col='red')
shade(mu.5e3x.pi, x.seq, col = col.alpha("red",0.15))
m5e3y <- map(
alist(
z ~ dnorm(mu, sigma),
mu <- a + by*y,
a ~ dnorm(0,10),
by ~ dnorm(0, 1),
sigma ~ dunif(0, 10)
),
data = d
)
summary(m5e3y)
plot(z~y,d, col = col.alpha("blue",0.5))
y.seq <- seq(-14, -4, by=0.1)
mu.5e3y <- link(m5e3y, data=list(y=y.seq), n=1000)
mu.5e3y.mean <- apply(mu.5e3y, 2, mean)
mu.5e3y.pi <- apply(mu.5e3y, 2, PI)
lines(y.seq, mu.5e3y.mean, col='red')
shade(mu.5e3y.pi, y.seq, col = col.alpha("red",0.15))
m5e3xy <- map(
alist(
z ~ dnorm(mu, sigma),
mu <- a + bx*x + by*y,
a ~ dnorm(0,10),
bx ~ dnorm(0, 1),
by ~ dnorm(0, 1),
sigma ~ dunif(0, 10)
),
data = d
)
summary(m5e3xy)
# add to the previous plot of z~y mean of relation between z and y in the new model(5e3xy)
mu.5e3xy.y <- link(m5e3xy, data=data.frame(y=y.seq, x=mean(d$x)), n=1000)
mu.5e3xy.y.mean <- apply(mu.5e3xy.y, 2, mean)
mu.5e3xy.y.pi <- apply(mu.5e3xy.y, 2, PI)
lines(y.seq, mu.5e3xy.y.mean, col='green')
shade(mu.5e3xy.y.pi, y.seq, col = col.alpha("green",0.5))
# 5E4
# lets explore inferential equivalency of m1 and m3 using milk data
data(milk)
d <- milk
d$catvar <- as.factor(d$clade)
levels(d$catvar) <- c('A','B','C','D')
(d$catvar.A <- as.integer(d$catvar=='A'))
(d$catvar.B <- as.integer(d$catvar=='B'))
(d$catvar.C <- as.integer(d$catvar=='C'))
(d$catvar.D <- as.integer(d$catvar=='D'))
ggplot(d, aes(y=kcal.per.g, x=catvar, fill=catvar)) + geom_boxplot()
m5e4.m1 <- map(
alist(
kcal.per.g ~ dnorm( mu , sigma ) ,
mu <- a + b.a*catvar.A + b.b*catvar.B + b.d*catvar.D, #a is mean val for level C
a ~ dnorm( 0.6 , 10 ) ,
b.a ~ dnorm( 0 , 1 ) ,
b.b ~ dnorm( 0 , 1 ) ,
b.d ~ dnorm( 0 , 1 ) ,
sigma ~ dunif( 0 , 10 )
) ,
data=d )
precis(m5e4.m1)
post.5e4.m1 <- extract.samples(m5e4.m1, n=1e3)
head(post.5e4.m1)
m5e4.m3 <- map(
alist(
kcal.per.g ~ dnorm( mu , sigma ) ,
mu <- a + b.b*catvar.B + b.c*catvar.C + b.d*catvar.D, #a is mean val for level A
a ~ dnorm( 0.6 , 10 ) ,
b.b ~ dnorm( 0 , 1 ) ,
b.c ~ dnorm( 0 , 1 ) ,
b.d ~ dnorm( 0 , 1 ) ,
sigma ~ dunif( 0 , 10 )
) ,
data=d )
precis(m5e4.m3)
post.5e4.m3 <- extract.samples(m5e4.m3, n=1e3)
head(post.5e4.m3)
# transform m1 into m3 and compare histograms of params
post.m3from1 <- with(post.5e4.m1,
data.frame(
a = a + b.a,
b.b = b.b - b.a,
b.c = -b.a,
b.d = b.d - b.a,
sigma=sigma,
source='3from1'
)
)
post.5e4.m3$source='m3'
dd <- bind_rows(post.m3from1 , post.5e4.m3)
ggplot(dd, aes(x=a, fill=source)) +
geom_density(alpha=0.5)
# geom_histogram(alpha=0.5, position="identity", bins=20)
ggplot(dd, aes(x=b.b, fill=source)) +
geom_density(alpha=0.5)
# geom_histogram(alpha=0.5, position="identity", bins=20)
ggplot(dd, aes(x=sigma, fill=source)) +
geom_density(alpha=0.5)
# let's check what happens if we add all indicators and intercept to the model
# intercept now is correspondent to unexistent examples with no categories
m5e4.m2 <- map(
alist(
kcal.per.g ~ dnorm( mu , sigma ) ,
mu <- a + b.a*catvar.A + b.b*catvar.B + b.c*catvar.C + b.d*catvar.D,
a ~ dnorm( 0 , 10 ) ,
b.a ~ dnorm( 0 , 1 ) ,
b.b ~ dnorm( 0 , 1 ) ,
b.c ~ dnorm( 0 , 1 ) ,
b.d ~ dnorm( 0 , 1 ) ,
sigma ~ dunif( 0 , 10 )
) ,
data=d)
precis(m5e4.m2)
post.5e4.m2 <- extract.samples(m5e4.m2, n=1e3)
head(post.5e4.m2)
# still possible to convert to m3
post.m3from2 <- with(post.5e4.m2,
data.frame(
a = a + b.a,
b.b = b.b - b.a,
b.c = b.c - b.a,
b.d = b.d - b.a,
sigma=sigma,
source='3from2'
))
dd <- bind_rows(post.m3from2 , post.5e4.m3)
ggplot(dd, aes(x=a, fill=source)) +
geom_density(alpha=0.5)
# geom_histogram(alpha=0.5, position="identity", bins=20)
ggplot(dd, aes(x=b.b, fill=source)) +
geom_density(alpha=0.5)
# geom_histogram(alpha=0.5, position="identity", bins=20)
ggplot(dd, aes(x=sigma, fill=source)) +
geom_density(alpha=0.5)
#### Medium ####
# 5M1
# example give in the book looks like
# x -> x_sp (x is used to generate x_sp as correlated)
# x -> y (x is used to generate y as correlated)
# then
# x_sp is correlated to y
# but in presense of x, dependency between y and x_sp is almost degraded
# Another example that can be adopted to solve the task is masked treatment
# (t)reatment -> (c)onsequences -> (r)esult
# t ~ c
# c ~ r
# ==> t ~ r, but modelling r ~ t+c should make beta_t close to zero (degrade order of dependency)
N = 1000
r1 = 0.7
r2 = 0.9
set.seed(112)
x <- rnorm(N, 5)
y <- rnorm(N, r1*x , sqrt(1-r1^2) )
cor(x,y)
z <- rnorm(N, r2*y , sqrt(1-r2^2) )
cor(x,z)
cor(y,z)
d <- data.frame(x=x, y=y, z=z)
pairs(d)
confint(lm(z~x, d))
confint(lm(z~y, d))
confint(lm(z~y+x, d))
# 5M2
# obvious solution is same as in book
# x - any random
# y - correlated with x
# z = x-y
N <- 1000
rho <- 0.8
set.seed(11)
x <- rnorm(N, 1)
y <- rnorm(N, rho*x , sqrt(1-rho^2) )
z <- rnorm(N, x - y, 0.5)
d <- data.frame(x=x, y=y, z=z)
ggpairs(d)
summary(lm(z~y, d))
summary(lm(z~x, d))
summary(lm(z~x+y, d))
# 5M4
data(WaffleDivorce)
d <- WaffleDivorce
# data about LDS per state took from https://www.worldatlas.com/articles/mormon-population-by-state.html
q <- htmltab('https://www.worldatlas.com/articles/mormon-population-by-state.html',
which="//div[@id='artReg-table']/table")
stopifnot("Percentage of Mormon Residents" %in% names(q))
stopifnot(nrow(q) == 51)
names(q)[2] <- 'Location'
lcd <- q %>% select(Location, 'Percentage of Mormon Residents') %>%
rename(ratio_str='Percentage of Mormon Residents') %>%
mutate(lcd_ratio=as.double(str_replace(ratio_str, "[%]", "")))
d2 <- left_join(d, select(lcd, Location, lcd_ratio), by='Location')
normalise <- function(x){
(x-mean(x))/sd(x)
}
d2 <- d2 %>% mutate(
marriage.s=normalise(Marriage),
age.marriage.s=normalise(MedianAgeMarriage),
lcd.ratio.s=normalise(lcd_ratio)
)
m1 <- lm(Divorce ~ marriage.s + age.marriage.s, data=d2)
summary(m1)
confint(m1)
m2 <- lm(Divorce ~ marriage.s + age.marriage.s + lcd.ratio.s, data=d2)
summary(m2)
confint(m2)
predict_with_conf <- function(model, data, name){
mu <- link( model )
# summarize samples across cases
mu.mean <- apply( mu , 2 , mean )
mu.PI <- apply( mu , 2 , PI )
data.frame(divorce=data$Divorce,
prediction=mu.mean,
pred.low = mu.PI[1,],
pred.high = mu.PI[2,],
model=name
)
}
res <- bind_rows(
predict_with_conf(m1, d2, 'm1'),
predict_with_conf(m2, d2, 'm2.w.lcd')
)
pd <- position_dodge(0.1)
ggplot(res, aes(x=divorce, y=prediction, color=model, fill=model)) +
geom_point(alpha=0.7, size=2, shape=21, color='black', position=pd) +
geom_abline(slope=1,intercept = 0) +
geom_errorbar(aes(ymin=pred.low, ymax=pred.high), width=.5, position=pd) +
ylim(4, 16) + xlim(4,16) + xlab('Observed divorce')
### Hard ####
data(foxes)
d <- foxes
str(d)
unique(d$group)
gd <- d %>% group_by(group) %>% summarise(
avgfood=first(avgfood),
groupsize=first(groupsize),
area=first(area),
weight.min = min(weight),
weight.avg = mean(weight),
weight.max = max(weight)
)
ggpairs(gd)
ggpairs(d)
### 5H1
ggplot(d, aes(area, weight)) + geom_point(size=2) + geom_smooth(method='lm')
ggplot(d, aes(groupsize, weight)) + geom_point(size=2) + geom_smooth(method='lm')
### area
m5h1.a <- map(alist(
weight ~ dnorm( mu , sigma ),
mu <- Intercept + b_area*area,
Intercept ~ dnorm(0,10),
b_area ~ dnorm(0,10),
sigma ~ dunif(0,10)
),
data=d
)
summary(m5h1.a)
area.seq <- seq(1,6,0.1)
mu5h1.a <- link(m5h1.a, data=list(area=area.seq))
mu5h1.a.mean <- apply(mu5h1.a, 2, mean)
mu5h1.a.PI <- apply(mu5h1.a, 2, PI, .89)
plot(weight~area, d, col='blue')
lines(area.seq, mu5h1.a.mean, col='red')
shade(mu5h1.a.PI, area.seq)
### groupsize
m5h1.g <- map(alist(
weight ~ dnorm( mu , sigma ),
mu <- Intercept + b_gs*groupsize,
Intercept ~ dnorm(0,10),
b_gs ~ dnorm(0,10),
sigma ~ dunif(0,10)
),
data=d
)
summary(m5h1.g)
gs.seq <- seq(1,9,0.1)
mu5h1.g <- link(m5h1.g, data=list(groupsize=gs.seq))
mu5h1.g.mean <- apply(mu5h1.g, 2, mean)
mu5h1.g.PI <- apply(mu5h1.g, 2, PI, .89)
plot(weight~groupsize, d, col='blue')
lines(gs.seq, mu5h1.g.mean, col='red')
shade(mu5h1.g.PI, gs.seq)
# 5H2
m5h2 <- map(alist(
weight ~ dnorm( mu , sigma ),
mu <- Intercept + b_gs*groupsize + b_area*area,
Intercept ~ dnorm(0,10),
b_gs ~ dnorm(0,10),
b_area ~ dnorm(0,10),
sigma ~ dunif(0,10)
),
data=d
)
summary(m5h2)
# 2 area
mu5h2.a <- link(m5h2, data=data.frame(groupsize=mean(d$groupsize), area=area.seq))
mu5h2.a.mean <- apply(mu5h2.a, 2, mean)
mu5h2.a.PI <- apply(mu5h2.a, 2, PI, .89)
plot(weight~area, d, col='blue')
lines(area.seq, mu5h1.a.mean, col='red')
shade(mu5h1.a.PI, area.seq, col = col.alpha("goldenrod",0.5))
lines(area.seq, mu5h2.a.mean, col='cyan3')
shade(mu5h2.a.PI, area.seq, col = col.alpha("cyan3",0.5))
# 2 groupsize
mu5h2.g <- link(m5h2, data=data.frame(area=mean(d$area), groupsize=gs.seq))
mu5h2.g.mean <- apply(mu5h2.g, 2, mean)
mu5h2.g.PI <- apply(mu5h2.g, 2, PI, .89)
plot(weight~groupsize, d, col='blue')
lines(gs.seq, mu5h1.g.mean, col='red')
shade(mu5h1.g.PI, gs.seq, col = col.alpha("goldenrod",0.5))
lines(gs.seq, mu5h2.g.mean, col='cyan3')
shade(mu5h2.g.PI, gs.seq, col = col.alpha("cyan3",0.5))
# 5H3
# quick check
d <- mutate(d,
avgfood.s = normalise(avgfood),
area.s = normalise(area),
groupsize.s=normalise(groupsize))
m.fd.gs <- lm(weight ~ avgfood.s + groupsize.s, d)
summary(m.fd.gs)
precis(m.fd.gs, digits=3)
m.fd.gs.a <- lm(weight ~ avgfood.s + groupsize.s + area.s, d)
summary(m.fd.gs.a)
precis(m.fd.gs.a, digits=3)
m.gs.a <- lm(weight ~ groupsize.s + area.s, d)
summary(m.gs.a)
precis(m.gs.a, digits=3)
# group size from area and food
m.f.a <- lm(groupsize ~ avgfood.s + area.s, d)
summary(m.f.a)
precis(m.f.a, digits=3)
gd <- mutate(gd,
avgfood.s = normalise(avgfood),
area.s = normalise(area),
groupsize.s=normalise(groupsize))
m.f.a2 <- lm(groupsize ~ avgfood.s + area.s, gd)
summary(m.f.a2)
precis(m.f.a2, digits=3)