Repository navigation
Expand file tree
/
Copy pathgretaExamples.R
More file actions
191 lines (178 loc) · 5.87 KB
/
Copy pathgretaExamples.R
File metadata and controls
191 lines (178 loc) · 5.87 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
### greta example 0
graph = dag_create() %>%
dag_node("Sepal Length","y",
data = iris$Petal.Length,
rhs = normal) %>%
dag_node("Exp Sepal Length (Obs.)","mean",
rhs = int + coef * x,
child = "y") %>%
dag_node("Exp Sepal Length (Pop.)","int",
rhs = normal(0,5),
child = "mean") %>%
dag_node("Sepal / Petal Slope","coef",
rhs = normal(0,3),
child = "mean") %>%
dag_node("Std Dev of Obs. Sepal Length","sd",
rhs = lognormal(0,3),
child = "y") %>%
dag_node("Petal Length","x",
child = "mean",
data = iris$Petal.Length) %>%
dag_plate("Observation","i",
nodeLabels = c("y","mean","x"))
graph %>% dag_render()
graph %>% dag_render(shortLabel = TRUE)
graph %>% dag_greta()
graph %>% dag_greta(mcmc = TRUE)
tidyDrawsDF %>% dagp_plot()
### greta example 1
graph = dag_create() %>%
dag_node("Rating","y",
data = attitude$rating,
rhs = normal(mu,sd)) %>%
dag_node("Exp. Rating","mu",
rhs = int + coef * x,
child = "y") %>%
dag_node("Average Population Rating", "int",
rhs = normal(0,10),
child = "mu") %>%
dag_node("Rating Change Per Complaint","coef",
rhs = normal(0,10),
child = "mu") %>%
dag_node("Complaints","x",
data = attitude$complaints,
child = "mu") %>%
dag_node("Std Dev of Ratings","sd",
rhs = cauchy(0, 3, truncation = c(0,Inf)),
child = "y") %>%
dag_plate("Observation", "i",
nodeLabels = c("y","mu","x"))
graph %>% dag_render()
graph %>% dag_render(shortLabel = TRUE)
graph %>% dag_greta()
graph %>% dag_greta(mcmc = TRUE)
tidyDrawsDF %>% dagp_plot()
### greta example #2
graph = dag_create() %>%
dag_node("Rating","y",
data = attitude$rating,
rhs = normal(mu,sd)) %>%
dag_node("Exp. Rating","mu",
rhs = int + design %*% coefs,
child = "y") %>%
dag_node("Std Dev of Ratings","sd",
rhs = cauchy(0,3, truncation = c(0,Inf)),
child = "y") %>%
dag_node("Predictor Coefficients","coefs",
rhs = normal(0,10),
child = "mu",
extract = FALSE) %>%
dag_node("Predictors","design",
data = attitude[, 2:7],
keepAsDF = TRUE,
child = "mu") %>%
dag_plate(descr = "Predictors",label = "j",
nodeLabels = c("coefs"),
data = names(attitude[, 2:7])) %>%
dag_plate(descr = "Observation", label = "i",
nodeLabels = c("mu","y"))
graph %>% dag_render()
graph %>% dag_render(shortLabel = TRUE)
graph %>% dag_greta()
graph %>% dag_greta(mcmc = TRUE)
tidyDrawsDF %>% dagp_plot()
### greta example #3
graph = dag_create() %>%
dag_node("Rating","y",
data = attitude$rating,
rhs = normal(mu,sd)) %>%
dag_node("Exp. Rating","mu",
rhs = int + design %*% coefs,
child = "y") %>%
dag_node("Std Dev of Ratings","sd",
rhs = cauchy(0,3, truncation = c(0,Inf)),
child = "y") %>%
dag_node("Predictor Coefficients","coefs",
rhs = normal(0,10),
child = "mu",
extract = FALSE) %>%
dag_node("Predictors","design",
data = attitude[, 2:7],
keepAsDF = TRUE,
child = "mu") %>%
dag_plate(descr = "Predictors",label = "j",
nodeLabels = c("coefs"),
data = names(attitude[, 2:7])) %>%
dag_plate(descr = "Observation", label = "i",
nodeLabels = c("mu","y"))
graph %>% dag_render()
graph %>% dag_render(shortLabel = TRUE)
graph %>% dag_greta()
graph %>% dag_greta(mcmc = TRUE)
tidyDrawsDF %>% dagp_plot()
### greta example #4
df = as.data.frame(model.matrix(breaks ~ wool + tension, warpbreaks))
graph = dag_create() %>%
dag_node("# of Breaks Per Loom","y",
rhs = poisson(exp(eta)),
data = warpbreaks$breaks) %>%
dag_node("Linear Predictor","eta",
rhs = X %*% beta,
child = "y") %>%
dag_node(descr = "Wool + Tension Data", label = "X",
data = df,
keepAsDF = TRUE,
child = "eta") %>%
dag_node("Model Coef. Vector","beta",
rhs = c(int, coefs),
child = "eta") %>%
dag_node("Intercept","int",
rhs = variable(-Inf,Inf),
child = "beta") %>%
dag_node("Slope Coefficients","coefs",
rhs = normal(0,5),
child = "beta",
extract = FALSE) %>%
dag_plate("Predictors","j",
data = names(df[ ,-1]),
nodeLabels = "coefs") %>%
dag_plate("Observations","i",
nodeLabels = c("y","eta"))
graph %>% dag_render()
graph %>% dag_render(shortLabel = TRUE)
graph %>% dag_greta()
graph %>% dag_greta(mcmc = TRUE)
tidyDrawsDF %>% dagp_plot()
### greta example #5: Multiple Categorical Regression
designDF = as.data.frame(model.matrix(~ Species -1, iris))
speciesCoding = colnames(designDF) %>% head(-1)
graph = dag_create() %>%
dag_node("Species","y",
data = designDF,
keepAsDF = TRUE,
rhs = categorical(prob)) %>%
dag_node("Category Probabilities","prob",
child = "y",
rhs = imultilogit(eta)) %>%
dag_node("Linear Predictor","eta",
rhs = X %*% beta,
child = "prob") %>%
dag_node("Predictors","X",
data = cbind(1,iris[, 1:4]),
keepAsDF = TRUE,
child = "eta") %>%
dag_node("Predictor Coefficients","beta",
rhs = normal(0,5),
child = "eta",
extract = FALSE) %>%
dag_plate(descr = "Predictors",label = "J",
nodeLabels = "beta",
data = c("Intercept",colnames(iris[,1:4]))) %>%
dag_plate("Category Codes","K",
nodeLabels = "beta",
data = speciesCoding)
graph %>% dag_render()
graph %>% dag_render(shortLabel = TRUE)
graph %>% dag_greta()
graph %>% dag_greta(mcmc = TRUE)
tidyDrawsDF %>% dagp_plot()