forked from UCD-pbio-rclub/RethinkingV2_Julin.Maloof
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChapter11.Rmd
840 lines (672 loc) · 23.2 KB
/
Chapter11.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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
---
title: "Chapter 11"
author: "Julin N Maloof"
date: "10/31/2019"
output:
html_document:
keep_md: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, cache = TRUE, autodep = TRUE)
```
```{r}
library(rethinking)
library(tidyverse)
library(GGally)
```
0E1
10E2
10E3
10M1
Problems 1 and 2 at https://github.com/rmcelreath/statrethinking_winter2019/blob/master/homework/week06.pdf
## 10E1
_If an event has probability 0.35, what are the log-odds of this event?_
```{r}
logit(.35)
log(.35/.65)
```
## 10E2
_If an event has log-odds 3.2, what is the probability of this event?_
```{r}
inv_logit(3.2)
exp(3.2) / (1+exp(3.2)) # algebra works!
```
## 10E3
_Suppose that a coefficient in a logistic regression has value 1.7. What does this imply about the proportional change in odds of the outcome?_
The increases the probability of the event by 70%
## 10M1
_Asexplainedinthechapter,binomialdatacanbeorganizedinaggregatedanddisaggregated forms, without any impact on inference. But the likelihood of the data does change when the data are converted between the two formats. Can you explain why?_
extra parameter blah blah
## PDF 1
_The data in data(NWOGrants) are outcomes for scientific funding applications for the Netherlands Organization for Scientific Research (NWO) from 2010–2012 (see van der Lee and Ellemers doi:10.1073/pnas.1510159112). These data have a very similar structure to the UCBAdmit data discussed in Chapter 11. I want you to consider a similar question: What are the total and indirect causal effects of gender on grant awards? Consider a mediation path (a pipe) through dis- cipline. Draw the corresponding DAG and then use one or more binomial GLMs to answer the question. What is your causal interpretation? If NWO’s goal is to equalize rates of funding between the genders, what type of intervention would be most effective?_
```{r}
g <- dagitty("dag{
G -> A;
G -> D;
D -> A
}")
coordinates(g) <- list(x=c(G=0, D=1, A=2),
y=c(G=0, D=1, A=0))
plot(g)
```
```{r}
data("NWOGrants")
NWOGrants
```
plot it
```{r}
NWOGrants %>%
mutate(success=awards/applications) %>%
ggplot(aes(x=discipline, y=success, color=gender, size=applications)) +
geom_point() +
theme(axis.text.x = element_text(angle=90, hjust = 1, vjust=0.5))
```
overall difference, irrespective of field
```{r}
d1 <- with(NWOGrants, list(g=ifelse(gender=="m",1,2),
applications=applications,
awards=awards))
m11.1 <- ulam(
alist(awards ~ dbinom(applications, p),
logit(p) <- a[g],
a[g] ~ dnorm(0,1.5)),
data=d1,
chains = 4,
cores = 4)
```
this is on the logit scale
```{r}
precis(m11.1, depth=2)
```
look at differences in award rate
```{r}
post <- extract.samples(m11.1)
# relative scale
precis(data.frame(rel_dif=exp(post$a[,2]-post$a[,1])))
#absolute scale
precis(data.frame(prob_dif=inv_logit(post$a[,2])-inv_logit(post$a[,1])))
```
Women are 82% as likely to receive an award, translating to a reduced success rate of 3%
## now fit a model that has a separate probability for each discipline
```{r}
d2 <- with(NWOGrants, list(g=ifelse(gender=="m",1,2),
applications=applications,
awards=awards,
discipline=rep(1:9, each=2)))
m11.2 <- ulam(
alist(awards ~ dbinom(applications, p),
logit(p) <- a[g] + b[discipline],
a[g] ~ dnorm(0,1.5),
b[discipline] ~ dnorm(0,1.5)),
data=d2,
iter=2000,
chains = 4,
cores = 4)
```
```{r}
pairs(m11.2)
precis(m11.2, depth=2)
```
so much correlation. Try not indexing gender. I this parameterization each discipline coefficient will be the rate for males in that discipline and then the gender coefficient will be the difference for females.
```{r}
d3 <- with(NWOGrants, list(g=ifelse(gender=="m",0,1),
applications=applications,
awards=awards,
discipline=rep(1:9, each=2)))
m11.3 <- ulam(
alist(awards ~ dbinom(applications, p),
logit(p) <- a[discipline] + b_female*g,
a[discipline] ~ dnorm(0,1.5),
b_female ~dnorm(0,1.5)),
data=d3,
iter=2000,
chains = 4,
cores = 4)
```
```{r}
precis(m11.3, depth=2)
pairs(m11.3)
```
This looks much better.
look at differences in award rate.
On relative scale
```{r}
post <- extract.samples(m11.3, pars="b_female")
# relative and absolute scale
precis(list(rel_female=exp(post$b_female)))
```
So women are 86% as likely to get an award, but the 89% condidence intervals cross 1
For the absolute scale I think it will probably be easier to use link
```{r}
# the difference between men and women will be the same for all disciplines using this model, so just get one of them.
newdat <- data.frame(g=0:1,
discipline=1)
newdat
pred <- link(m11.3, data = newdat)
head(pred)
```
```{r}
precis(list(abs_female=pred[,2] - pred[,1]))
```
Women do 3% worse when accounting for overall differences in award rate between departments, although confidence interval touches 0
Can I do this from posterior directly?
```{r}
post <- extract.samples(m11.3)
str(post)
# again I should just be able to look at one discipline
precis(list(abd_female=inv_logit(post$a[,1]) -inv_logit(post$a[,1]-post$b_female)))
```
Overall I do see a reduction in award rates to women. When we consider discipline than the signficance of this drops, but I wonder if there is still something going on...
interaction?
```{r}
m11.4 <- ulam(
alist(awards ~ dbinom(applications, p),
logit(p) <- a[discipline] + b_female*g + inter[discipline]*g,
a[discipline] ~ dnorm(0,1.5),
b_female ~dnorm(0,1.5),
inter[discipline] ~ dnorm(0,.5)),
data=d3,
iter=2000,
chains = 4,
cores = 4)
```
```{r}
precis(m11.4, depth=2)
```
```{r}
plot(precis(m11.4, depth=2))
```
## 11.2
_2. Suppose that the NWO Grants sample has an unobserved confound that influences both choice of discipline and the probability of an award. One example of such a confound could be the career stage of each applicant. Suppose that in some disciplines, junior scholars apply for most of the grants. In other disciplines, scholars from all career stages compete. As a result, career stage influences discipline as well as the probability of being awarded a grant. Add these influences to your DAG from Problem 1. What happens now when you condition on discipline? Does it provide an un-confounded estimate of the direct path from gender to an award? Why or why not? Justify your answer with the back-door criterion. Hint: This is structurally a lot like the grandparents-parentschildren-neighborhoods example from a previous week. If you have trouble thinking this though, try simulating fake data, assuming your DAG is true. Then analyze it using the model from Problem 1. What do you conclude? Is it possible for gender to have a real direct causal influence but for a regression conditioning on both gender and discipline to suggest zero influence?_
```{r}
g <- dagitty("dag{
G -> A;
G -> D;
D -> A;
C -> A;
C -> D;
}")
coordinates(g) <- list(x=c(G=0, D=1, A=2, C=2),
y=c(G=0, D=1, A=0, C=1))
plot(g)
```
So if this is the DAG, the regression model from 1 closes the D->A, but leaves a back door from D through C to A?
# Chapter 11.2 problems
## 10E4
_Why do Poisson regressions sometimes require the use of an offset? Provide an example._
If the data have been collected (events counted) over different sampling times/ distances (exposures) we need an offset to account for this. For example, we want to compare transposition events across the genome. One group counted number of transposons per 100KB and the other per 1MB.
## 10M2
_If a coefficient in a Poisson regression has value 1.7, what does this imply about the change in the outcome?_
For every unit change in the predictor there will be a 5.47-fold (e^1.7) increase in the number of counts
```{r}
exp(1)
exp(1+1.7)
exp(1+1.7+1.7)
exp(1)^1.7
```
## 10M3
_Explain why the logit link is appropriate for a binomial generalized linear model._
We need to transform the linear model to return a value between 0 and 1 (i.e. the probability scale)
## 10M4
_Explain why the log link is appropriate for a Poisson generalized linear model._
This keeps the outcome variable on a positive scale, required for count data. You cannot have negative counts.
(OK but this is true for so much stuff that we model with linear models and Gaussian distributions...)
## 10H3
_The data contained in library(MASS);data(eagles) are records of salmon pirating at- tempts by Bald Eagles in Washington State. See ?eagles for details. While one eagle feeds, some- times another will swoop in and try to steal the salmon from it. Call the feeding eagle the “victim” and the thief the “pirate.” Use the available data to build a binomial GLM of successful pirating attempts._
```{r}
library(MASS)
data(eagles)
?eagles
eagles
```
```{r}
eagleslist <- with(eagles,
list(y=y,
n=n,
pirate_large=ifelse(P=="L",1,0),
pirate_adult=ifelse(A=="A",1,0),
victim_large=ifelse(V=="L",1,0)))
str(eagleslist)
```
```{r}
m10h3q <- quap(alist(y ~ dbinom(n, p),
logit(p) <- alpha +
b_pirate_large*pirate_large +
b_pirate_adult*pirate_adult +
b_victim_large*victim_large,
alpha ~ dnorm(0,10),
c(b_pirate_large, b_pirate_adult, b_victim_large) ~ dnorm(0,5)),
data=eagleslist)
```
```{r}
precis(m10h3q)
pairs(m10h3q)
```
```{r}
m10h3stan <- ulam(alist(y ~ dbinom(n, p),
logit(p) <- alpha +
b_pirate_large*pirate_large +
b_pirate_adult*pirate_adult +
b_victim_large*victim_large,
alpha ~ dnorm(0,10),
c(b_pirate_large, b_pirate_adult, b_victim_large) ~ dnorm(0,5)),
data=eagleslist,
chains = 4,
cores = 4,
iter = 2000,
log_lik = TRUE)
```
```{r}
precis(m10h3stan)
pairs(m10h3stan)
```
_(b) Now interpret the estimates. If the quadratic approximation turned out okay, then it’s okay to use the map estimates. Otherwise stick to map2stan estimates. Then plot the posterior predictions. Compute and display both (1) the predicted probability of success and its 89% interval for each row (i) in the data, as well as (2) the predicted success count and its 89% interval. What different information does each type of posterior prediction provide?_
posterior are not entirely Gaussian, stick with Stan
Get predictions
```{r}
inv_logit(.65) # this is for when victim small, pirate young, pirate small
inv_logit(.65+ -5.09) # this is probability when victim large, pirate young and small
```
```{r}
pred <- link(m10h3stan)
head(pred)
summary(pred)
```
These are the probability of successful pirate for each of the 8 rows in the table.
```{r}
pred_obs <- as_tibble(cbind(eagles,
mean_prob=colMeans(pred),
low.89=apply(pred, 2, HPDI)[1,],
high.89=apply(pred, 2, HPDI)[2,]
)) %>%
mutate(observed_prob=y/n,
pred_success=mean_prob*n,
pred_low=low.89*n,
pred_high=high.89*n,
label=str_c("P: ", P, ", A: ", A, ", V: ", V)) %>%
dplyr::select(label, everything())
pred_obs
```
```{r}
pred_obs %>%
ggplot(aes(x=label)) +
geom_pointrange(aes(y=mean_prob, ymin=low.89, ymax=high.89), color="blue", fill="blue") +
geom_point(aes(y=observed_prob)) +
theme(axis.text.x = element_text(angle=90))
```
```{r}
pred_obs %>%
ggplot(aes(x=label)) +
geom_pointrange(aes(y=pred_success, ymin=pred_low, ymax=pred_high), color="blue", fill="blue") +
geom_point(aes(y=y)) +
theme(axis.text.x = element_text(angle=90))
```
Overall the fit looks pretty good. Size of pirate and victim are both pretty importnat; age is less important.
Try with interaction
```{r}
m10h3stan_int <- ulam(alist(y ~ dbinom(n, p),
logit(p) <- alpha +
b_pirate_large*pirate_large +
b_pirate_adult*pirate_adult +
b_victim_large*victim_large +
bpsa*pirate_large*pirate_adult,
alpha ~ dnorm(0,10),
c(b_pirate_large, b_pirate_adult, b_victim_large) ~ dnorm(0,5),
bpsa ~ dnorm(0,2.5)),
data=eagleslist,
chains = 4,
cores = 4,
iter = 2000,
log_lik = TRUE)
```
```{r}
precis(m10h3stan_int)
pairs(m10h3stan_int)
```
```{r}
compare(m10h3stan, m10h3stan_int)
```
Interaction model fits better. Lets compare the predictions.
```{r}
pred_int <- link(m10h3stan_int)
head(pred)
summary(pred)
```
These are the probability of successful pirate for each of the 8 rows in the table.
```{r}
pred_obs_int <- as_tibble(cbind(pred_obs,
mean_prob_int=colMeans(pred_int),
low.89_int=apply(pred_int, 2, HPDI)[1,],
high.89_int=apply(pred_int, 2, HPDI)[2,]
)) %>%
mutate(pred_success_int=mean_prob_int*n,
pred_low_int=low.89_int*n,
pred_high_int=high.89_int*n)
pred_obs_int
```
```{r}
pred_obs_int %>%
ggplot(aes(x=label)) +
geom_pointrange(aes(y=mean_prob, ymin=low.89, ymax=high.89), color="blue", position = position_nudge(x=-.1)) +
geom_pointrange(aes(y=mean_prob_int, ymin=low.89_int, ymax=high.89_int), color="red", position = position_nudge(x=.1)) +
geom_point(aes(y=observed_prob)) +
theme(axis.text.x = element_text(angle=90))
```
```{r}
pred_obs_int %>%
ggplot(aes(x=label)) +
geom_pointrange(aes(y=pred_success, ymin=pred_low, ymax=pred_high), color="blue", position = position_nudge(x=-.1)) +
geom_pointrange(aes(y=pred_success_int, ymin=pred_low_int, ymax=pred_high_int), color="red", position = position_nudge(x=.1)) +
geom_point(aes(y=y)) +
theme(axis.text.x = element_text(angle=90))
```
Fits better!
Try it Lin's way (like the chimp problem)
```{r}
eagleslist$inter <- as.numeric(as.factor(str_c(eagles$P,eagles$A)))
m10h3stan_int_lin <- ulam(alist(y ~ dbinom(n, p),
logit(p) <- alpha_int[inter] +
b_victim_large*victim_large,
alpha_int[inter] ~ dnorm(0,10),
c(b_victim_large) ~ dnorm(0,5)),
data=eagleslist,
chains = 4,
cores = 4,
iter = 2000,
log_lik = TRUE)
```
```{r}
precis(m10h3stan_int_lin, depth=2)
```
```{r}
inv_logit(6.54) # adult large pirate, small victim
inv_logit(2.93-5.59) # small adult pirate, large victim
```
```{r}
precis(m10h3stan_int)
```
```{r}
inv_logit(-.56 + 3.02 + 6.14 -2.43) # adult large pirate, small victim
inv_logit(-.56 + 3.02 -5.12)
```
## 10H4
_The data contained in data(salamanders) are counts of salamanders (Plethodon elongatus) from 47 different 49-m2 plots in northern California. The column SALAMAN is the count in each plot, and the columns PCTCOVER and FORESTAGE are percent of ground cover and age of trees in the plot, respectively. You will model SALAMAN as a Poisson variable._
_(a) Model the relationship between density and percent cover, using a log-link (same as the example in the book and lecture). Use weakly informative priors of your choosing. Check the quadratic approximation again, by comparing map to map2stan. Then plot the expected counts and their 89% interval against percent cover. In which ways does the model do a good job? In which ways does it do a bad job?_
```{r}
data("salamanders")
head(salamanders)
```
```{r}
salamanders %>% dplyr::select(SALAMAN, PCTCOVER, FORESTAGE) %>% ggpairs()
```
```{r}
salamanders$pctcover_std <- scale(salamanders$PCTCOVER)
m10h4.1.quap <- quap(
alist(SALAMAN ~ dpois(lambda),
log(lambda) <- alpha + beta_pct*pctcover_std,
alpha ~ dnorm(3, 0.5),
beta_pct ~ dnorm(0, 0.2)),
data=salamanders)
```
```{r}
precis(m10h4.1.quap)
```
```{r}
m10h4.1.stan <- ulam(
alist(SALAMAN ~ dpois(lambda),
log(lambda) <- alpha + beta_pct*pctcover_std,
alpha ~ dnorm(3, 0.5),
beta_pct ~ dnorm(0, 0.5)),
chains=4,
cores=4,
data=salamanders,
log_lik = TRUE)
```
```{r}
precis(m10h4.1.quap)
```
```{r}
precis(m10h4.1.stan)
```
somewhat similar
```{r}
pairs(m10h4.1.stan)
trankplot(m10h4.1.stan)
traceplot(m10h4.1.stan)
```
plot observed and expected
```{r}
pred <- link(m10h4.1.stan)
pred_obs <- as_tibble(cbind(
salamanders,
mu=colMeans(pred),
low.89=apply(pred,2,HPDI)[1,],
high.89=apply(pred, 2, HPDI)[2,]))
head(pred_obs)
```
```{r}
pred_obs %>%
mutate(observed=SALAMAN,
predicted=mu) %>%
ggplot(aes(x=PCTCOVER)) +
geom_point(aes(y=observed), color="black") +
geom_pointrange(aes(y=predicted, ymin=low.89, ymax=high.89), color="blue")
```
Lots of scatter at high PCTCOVER.
```{r}
pred_obs %>%
mutate(observed=SALAMAN,
predicted=mu) %>%
ggplot(aes(observed,predicted)) +
geom_point()
```
_(b) Can you improve the model by using the other predictor, FORESTAGE? Try any models you think useful. Can you explain why FORESTAGE helps or does not help with prediction?_
additive model:
```{r}
salamanders$forestage_std <- scale(salamanders$FORESTAGE)
m10h4.2 <- ulam(
alist(SALAMAN ~ dpois(lambda),
log(lambda) <- alpha + beta_pct*pctcover_std + beta_f*forestage_std,
alpha ~ dnorm(3, 0.5),
c(beta_pct,beta_f) ~ dnorm(0, 0.5)),
chains=4,
cores=4,
data=salamanders,
log_lik = TRUE)
```
```{r}
precis(m10h4.2)
```
```{r}
pairs(m10h4.2)
trankplot(m10h4.2)
traceplot(m10h4.2)
```
plot observed and expected
```{r}
pred <- link(m10h4.2)
pred_obs <- as_tibble(cbind(
salamanders,
mu=colMeans(pred),
low.89=apply(pred,2,HPDI)[1,],
high.89=apply(pred, 2, HPDI)[2,]))
pred_obs
```
```{r}
pred_obs %>%
mutate(observed=SALAMAN,
predicted=mu) %>%
ggplot(aes(x=PCTCOVER)) +
geom_point(aes(y=observed), color="black") +
geom_pointrange(aes(y=predicted, ymin=low.89, ymax=high.89), color="blue")
```
```{r}
compare(m10h4.1.stan, m10h4.2)
```
interaction model:
```{r}
m10h4.3 <- ulam(
alist(SALAMAN ~ dpois(lambda),
log(lambda) <- alpha +
beta_pct*pctcover_std +
beta_f*forestage_std +
beta_pct_f*pctcover_std*forestage_std,
alpha ~ dnorm(3, 0.5),
c(beta_pct,beta_f,beta_pct_f) ~ dnorm(0, 0.5)),
chains=4,
cores=4,
data=salamanders,
log_lik = TRUE)
```
```{r}
precis(m10h4.3)
```
```{r}
pairs(m10h4.3)
trankplot(m10h4.3)
traceplot(m10h4.3)
```
plot observed and expected
```{r}
pred <- link(m10h4.3)
pred_obs <- as_tibble(cbind(
salamanders,
mu=colMeans(pred),
low.89=apply(pred,2,HPDI)[1,],
high.89=apply(pred, 2, HPDI)[2,]))
pred_obs
```
```{r}
pred_obs %>%
mutate(observed=SALAMAN,
predicted=mu) %>%
ggplot(aes(x=PCTCOVER)) +
geom_point(aes(y=observed), color="black") +
geom_pointrange(aes(y=predicted, ymin=low.89, ymax=high.89), color="blue")
```
```{r}
compare(m10h4.1.stan, m10h4.2)
```
## Week6 PDF # 3
_The data in data(Primates301) were first introduced at the end of Chapter 7. In this problem, you will consider how brain size is associated with social learning._
_There are three parts._
_First, model the number of observations of social_learning for each species as a function of the log brain size. Use a Poisson distribution for the social_learning outcome variable. Interpret the resulting posterior._
```{r}
data("Primates301")
p <- Primates301 %>%
dplyr::select(genus, species, social_learning, brain, research_effort) %>%
mutate(l_brain_std = scale(log(brain)),
l_research_effort_std = scale(log(research_effort)),
gen_spec = str_c(genus, "_", species)) %>%
na.omit() %>%
arrange(gen_spec)
table(p$gen_spec) %>% max() #more than one row per species? no
head(p)
```
```{r}
p %>%
dplyr::select(social_learning, l_brain_std, l_research_effort_std) %>%
ggpairs()
```
```{r}
p_small <- list(social_learning=p$social_learning, l_brain_std=as.vector(p$l_brain_std))
m3 <- ulam(
alist(social_learning ~ dpois(lambda),
log(lambda) <- alpha + beta_brain*l_brain_std,
alpha ~ dnorm(3, 0.5),
beta_brain ~ dnorm(0, .5)),
data=p_small,
chains=4,
cores=4,
log_lik=T)
```
```{r}
precis(m3)
```
each std deviation increase in log brain size causes a `r exp(2.7)` fold increase in social learning
```{r}
pairs(m3)
trankplot(m3)
traceplot(m3)
```
```{r}
pred <- link(m3)
pred_obs <- as_tibble(
cbind(p,
predicted=colMeans(pred),
low.89 = apply(pred, 2, HPDI)[1,],
high.89 = apply(pred, 2, HPDI)[2,]))
```
```{r}
pred_obs %>%
ggplot(aes(x=brain)) +
geom_point(aes(y=social_learning)) +
geom_pointrange(aes(y=predicted, ymin=low.89, ymax=high.89), color="blue", alpha=.5) +
scale_x_log10()
```
_Second, some species are studied much more than others. So the number of reported instances of social_learning could be a product of research effort. Use the research_effort variable, specifically its logarithm, as an additional predictor variable. Interpret the coefficient for log research_effort. Does this model disagree with the previous one?_
```{r}
p_small <- list(social_learning=p$social_learning, l_brain_std=as.vector(p$l_brain_std), l_research_effort_std=as.vector(p$l_research_effort_std))
m3.2 <- ulam(
alist(social_learning ~ dpois(lambda),
log(lambda) <- alpha + beta_brain*l_brain_std + beta_r*l_research_effort_std,
alpha ~ dnorm(3, 0.5),
beta_brain ~ dnorm(0, .5),
beta_r ~ dnorm(0, .5)),
data=p_small,
chains=4,
cores=4,
log_lik=T)
```
```{r}
precis(m3.2)
```
The brain size effect is now much smaller, and research effort has a large effect
each std deviation increase in log brain size is associated with a `r exp(0.45)` fold increase in social learning
each std deviation increase in log research effort is associated with a `r exp(1.94)` fold increase in social learning
```{r}
pairs(m3.2)
trankplot(m3.2)
traceplot(m3.2)
```
```{r}
pred <- link(m3.2)
pred_obs <- as_tibble(
cbind(p,
predicted=colMeans(pred),
low.89 = apply(pred, 2, HPDI)[1,],
high.89 = apply(pred, 2, HPDI)[2,]))
```
```{r}
pred_obs %>%
ggplot(aes(x=brain)) +
geom_point(aes(y=social_learning)) +
geom_pointrange(aes(y=predicted, ymin=low.89, ymax=high.89), color="blue", alpha=.5) +
scale_x_log10()
```
```{r}
pred_obs %>%
ggplot(aes(x=research_effort)) +
geom_point(aes(y=social_learning)) +
geom_pointrange(aes(y=predicted, ymin=low.89, ymax=high.89), color="blue", alpha=.5) +
scale_x_log10()
```
```{r}
pred_obs %>%
ggplot(aes(x=social_learning, y=predicted)) +
geom_point() +
scale_x_log10() +
scale_y_log10()
```
_Third, draw a DAG to represent how you think the variables social_learning, brain, and research_effort interact. Justify the DAG with the measured associations in the two models above (and any other models you used)._
There is a backdoor from brain size to research effort
```{r}
g <- dagitty("dag{
brain_size -> learning;
brain_size -> research;
research -> learning
}")
coordinates(g) <- list(x=c(brain_size=1, learning=1, research=2),
y=c(brain_size=0, learning=2, research=1))
plot(g)
```