-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw_week02.R
147 lines (104 loc) · 2.74 KB
/
hw_week02.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
###statistical rethinking course 2023###
###WEEK 02####
##########################
####Homework Questions####
##########################
packages <- c('rstan','rethinking','ggplot2','ggdag', 'data.table', 'tidyr' )
lapply(packages, require, character.only=TRUE)
####Question 1#####
##Howell Data
data(Howell1)
##younger than 13
d<-Howell1[Howell1$age < 13,]
##Estimate causal associations between height and weight
##first age influences height, height influences weight
##A->H, H->W
##second age influences weight through age related changes
## A -> W
##draw a DAG
coords <- data.frame(
name = c('A', 'H', 'W'),
x = c(1, 1, 2),
y = c(1, 2, 2)
)
dag <- dagify(
H ~ A,
W ~ H + A,
coords = coords
)
ggdag(dag) +
theme_dag()
##write a generative simulation
sim_wheight<-function(H,A,b_ah,b_hw,b_aw) {
N <- length(A)
H <- rnorm(N, b_ah*A,3)
W<-rnorm(N,b_hw*H + b_aw*A,2)
return(data.table(age = A, height = H, weight = W))
}
A <- runif(1e3, 0, 12)
H<- 100 + rnorm(n,0,5)
summary(H)
dat <-sim_wheight(H,A,b_ah = 10, b_hw = 1, b_aw = 5)
ggplot(dat) + geom_point(aes(age, weight))
ggplot(dat) + geom_point(aes(age, height))
ggplot(dat) + geom_point(aes(height, weight))
####Question 2#####
##linear regression : total causal effect of year year of growth on weight
n <- 100
ggplot() +
geom_abline(aes(intercept = rnorm(n, 5, 1),
slope = rnorm(n, 3, 1)),
# slope = runif(n, 0, 10)), ##why uniform versus normal
alpha = 0.1, size = 2) +
labs(x = 'age', y = 'weight (kg)') +
xlim(0, 12) +
ylim(0, 50)
m2 <- quap(
alist(
W ~ dnorm(mu, sigma),
mu <- alpha + b_aw * A,
alpha ~ dnorm(5, 1),
b_aw ~ dnorm(3,1),
#b_aw ~ dunif(0,10),
sigma ~ dexp(1)
),
data = list(W=d$weight,A=d$age)
)
precis(m2)
####Question 3####
coords <- data.frame(
name = c('A', 'H', 'W', 'S'),
x = c(1, 1, 2, 2),
y = c(1, 2, 1, 2)
)
dag <- dagify(
H ~ A + S,
W ~ H + A + S,
coords = coords
)
ggdag(dag) +
theme_dag()
d3 <-data.table(W=d$weight, A = d$age, S=d$male+1)
m3 <- quap(
alist(
W ~ dnorm(mu, sigma),
mu <- alpha[S] + b_aw[S]* A,
alpha[S] ~ dnorm(5, 1),
b_aw[S] ~ dnorm(3,1),
sigma ~ dexp(1)
),
data = d3
)
precis(m3, depth=2)
precis(m3)
####contrast
###taken right from the solutions but not working
Aseq <- 0:12
mu1 <- sim(m3, data = list(A = Aseq, S = rep(1,13)))
mu2 <- sim(m3, data = list(A = Aseq, S = rep(2,13)))
mu_contrast <- mu1
for ( i in 1:13) mu_contrast[,1] <- mu2[,i] - mu1[,i]
plot(NULL, xlim=c(0,13), ylim = c(-15,15), xlab = "age", ylab = "weighted difference (boys-girls)")
for (p in c (0.5, 0.67, 0.89, 0.99))
shade(apply (mu_contrast,2,PI, prob = p), Aseq)
abline(h=0,lty = 2, lwd = 2)