Polynomial weight model model
using DynamicHMCModels
ProjDir = rel_path_d("..", "scripts", "04")
cd(ProjDir)
Import the dataset.
howell1 = CSV.read(rel_path("..", "data", "Howell1.csv"), delim=';')
df = convert(DataFrame, howell1);
height | weight | age | male | |
---|---|---|---|---|
Float64 | Float64 | Float64 | Int64 | |
1 | 151.765 | 47.8256 | 63.0 | 1 |
2 | 139.7 | 36.4858 | 63.0 | 0 |
3 | 136.525 | 31.8648 | 65.0 | 0 |
4 | 156.845 | 53.0419 | 41.0 | 1 |
5 | 145.415 | 41.2769 | 51.0 | 0 |
6 | 163.83 | 62.9926 | 35.0 | 1 |
7 | 149.225 | 38.2435 | 32.0 | 0 |
8 | 168.91 | 55.48 | 27.0 | 1 |
9 | 147.955 | 34.8699 | 19.0 | 0 |
10 | 165.1 | 54.4877 | 54.0 | 1 |
11 | 154.305 | 49.8951 | 47.0 | 0 |
12 | 151.13 | 41.2202 | 66.0 | 1 |
13 | 144.78 | 36.0322 | 73.0 | 0 |
14 | 149.9 | 47.7 | 20.0 | 0 |
15 | 150.495 | 33.8493 | 65.3 | 0 |
16 | 163.195 | 48.5627 | 36.0 | 1 |
17 | 157.48 | 42.3258 | 44.0 | 1 |
18 | 143.942 | 38.3569 | 31.0 | 0 |
19 | 121.92 | 19.6179 | 12.0 | 1 |
20 | 105.41 | 13.948 | 8.0 | 0 |
21 | 86.36 | 10.4893 | 6.5 | 0 |
22 | 161.29 | 48.9879 | 39.0 | 1 |
23 | 156.21 | 42.7227 | 29.0 | 0 |
24 | 129.54 | 23.5868 | 13.0 | 1 |
25 | 109.22 | 15.9891 | 7.0 | 0 |
26 | 146.4 | 35.4936 | 56.0 | 1 |
27 | 148.59 | 37.9033 | 45.0 | 0 |
28 | 147.32 | 35.4652 | 19.0 | 0 |
29 | 137.16 | 27.3289 | 17.0 | 1 |
30 | 125.73 | 22.6796 | 16.0 | 0 |
31 | 114.3 | 17.8602 | 11.0 | 1 |
32 | 147.955 | 40.313 | 29.0 | 1 |
33 | 161.925 | 55.1114 | 30.0 | 1 |
34 | 146.05 | 37.5064 | 24.0 | 0 |
35 | 146.05 | 38.4986 | 35.0 | 0 |
36 | 152.705 | 46.6066 | 33.0 | 0 |
37 | 142.875 | 38.8388 | 27.0 | 0 |
38 | 142.875 | 35.5786 | 32.0 | 0 |
39 | 147.955 | 47.4004 | 36.0 | 0 |
40 | 160.655 | 47.8823 | 24.0 | 1 |
41 | 151.765 | 49.4132 | 30.0 | 1 |
42 | 162.865 | 49.3848 | 24.0 | 1 |
43 | 171.45 | 56.5573 | 52.0 | 1 |
44 | 147.32 | 39.1223 | 42.0 | 0 |
45 | 147.955 | 49.8951 | 19.0 | 0 |
46 | 144.78 | 28.8031 | 17.0 | 0 |
47 | 121.92 | 20.4116 | 8.0 | 1 |
48 | 128.905 | 23.36 | 12.0 | 0 |
49 | 97.79 | 13.2676 | 5.0 | 0 |
50 | 154.305 | 41.2485 | 55.0 | 1 |
51 | 143.51 | 38.5553 | 43.0 | 0 |
52 | 146.7 | 42.4 | 20.0 | 1 |
53 | 157.48 | 44.6505 | 18.0 | 1 |
54 | 127.0 | 22.0106 | 13.0 | 1 |
55 | 110.49 | 15.4221 | 9.0 | 0 |
56 | 97.79 | 12.7573 | 5.0 | 0 |
57 | 165.735 | 58.5984 | 42.0 | 1 |
58 | 152.4 | 46.72 | 44.0 | 0 |
59 | 141.605 | 44.2252 | 60.0 | 0 |
60 | 158.8 | 50.9 | 20.0 | 0 |
61 | 155.575 | 54.3176 | 37.0 | 0 |
62 | 164.465 | 45.8978 | 50.0 | 1 |
63 | 151.765 | 48.0241 | 50.0 | 0 |
64 | 161.29 | 52.2198 | 31.0 | 1 |
65 | 154.305 | 47.6272 | 25.0 | 0 |
66 | 145.415 | 45.6427 | 23.0 | 0 |
67 | 145.415 | 42.4109 | 52.0 | 0 |
68 | 152.4 | 36.4858 | 79.3 | 1 |
69 | 163.83 | 55.9336 | 35.0 | 1 |
70 | 144.145 | 37.1945 | 27.0 | 0 |
71 | 129.54 | 24.5507 | 13.0 | 1 |
72 | 129.54 | 25.6279 | 14.0 | 0 |
73 | 153.67 | 48.3075 | 38.0 | 1 |
74 | 142.875 | 37.3363 | 39.0 | 0 |
75 | 146.05 | 29.5969 | 12.0 | 0 |
76 | 167.005 | 47.1736 | 30.0 | 1 |
77 | 158.42 | 47.287 | 24.0 | 0 |
78 | 91.44 | 12.9274 | 0.6 | 1 |
79 | 165.735 | 57.5495 | 51.0 | 1 |
80 | 149.86 | 37.9316 | 46.0 | 0 |
81 | 147.955 | 41.9006 | 17.0 | 0 |
82 | 137.795 | 27.5841 | 12.0 | 0 |
83 | 154.94 | 47.2019 | 22.0 | 0 |
84 | 160.96 | 43.2046 | 29.0 | 1 |
85 | 161.925 | 50.2637 | 38.0 | 1 |
86 | 147.955 | 39.3775 | 30.0 | 0 |
87 | 113.665 | 17.4633 | 6.0 | 1 |
88 | 159.385 | 50.689 | 45.0 | 1 |
89 | 148.59 | 39.4342 | 47.0 | 0 |
90 | 136.525 | 36.2874 | 79.0 | 0 |
91 | 158.115 | 46.2664 | 45.0 | 1 |
92 | 144.78 | 42.2691 | 54.0 | 0 |
93 | 156.845 | 47.6272 | 31.0 | 1 |
94 | 179.07 | 55.7068 | 23.0 | 1 |
95 | 118.745 | 18.8241 | 9.0 | 0 |
96 | 170.18 | 48.5627 | 41.0 | 1 |
97 | 146.05 | 42.8077 | 23.0 | 0 |
98 | 147.32 | 35.0683 | 36.0 | 0 |
99 | 113.03 | 17.8885 | 5.0 | 1 |
100 | 162.56 | 56.7557 | 30.0 | 0 |
101 | 133.985 | 27.4423 | 12.0 | 1 |
102 | 152.4 | 51.2559 | 34.0 | 0 |
103 | 160.02 | 47.2303 | 44.0 | 1 |
104 | 149.86 | 40.9367 | 43.0 | 0 |
105 | 142.875 | 32.7153 | 73.3 | 0 |
106 | 167.005 | 57.0675 | 38.0 | 1 |
107 | 159.385 | 42.9778 | 43.0 | 1 |
108 | 154.94 | 39.9444 | 33.0 | 0 |
109 | 148.59 | 32.4602 | 16.0 | 0 |
110 | 111.125 | 17.1231 | 11.0 | 1 |
111 | 111.76 | 16.4994 | 6.0 | 1 |
112 | 162.56 | 45.9545 | 35.0 | 1 |
113 | 152.4 | 41.1068 | 29.0 | 0 |
114 | 124.46 | 18.2571 | 12.0 | 0 |
115 | 111.76 | 15.0819 | 9.0 | 1 |
116 | 86.36 | 11.4815 | 7.6 | 1 |
117 | 170.18 | 47.5988 | 58.0 | 1 |
118 | 146.05 | 37.5064 | 53.0 | 0 |
119 | 159.385 | 45.019 | 51.0 | 1 |
120 | 151.13 | 42.2691 | 48.0 | 0 |
121 | 160.655 | 54.8563 | 29.0 | 1 |
122 | 169.545 | 53.5239 | 41.0 | 1 |
123 | 158.75 | 52.1914 | 81.75 | 1 |
124 | 74.295 | 9.75223 | 1.0 | 1 |
125 | 149.86 | 42.4109 | 35.0 | 0 |
126 | 153.035 | 49.5833 | 46.0 | 0 |
127 | 96.52 | 13.0975 | 5.0 | 1 |
128 | 161.925 | 41.7305 | 29.0 | 1 |
129 | 162.56 | 56.0186 | 42.0 | 1 |
130 | 149.225 | 42.1557 | 27.0 | 0 |
131 | 116.84 | 19.3911 | 8.0 | 0 |
132 | 100.076 | 15.0819 | 6.0 | 1 |
133 | 163.195 | 53.0986 | 22.0 | 1 |
134 | 161.925 | 50.2353 | 43.0 | 1 |
135 | 145.415 | 42.5243 | 53.0 | 0 |
136 | 163.195 | 49.1013 | 43.0 | 1 |
137 | 151.13 | 38.4986 | 41.0 | 0 |
138 | 150.495 | 49.8101 | 50.0 | 0 |
139 | 141.605 | 29.3134 | 15.0 | 1 |
140 | 170.815 | 59.7607 | 33.0 | 1 |
141 | 91.44 | 11.7083 | 3.0 | 0 |
142 | 157.48 | 47.939 | 62.0 | 1 |
143 | 152.4 | 39.2924 | 49.0 | 0 |
144 | 149.225 | 38.1301 | 17.0 | 1 |
145 | 129.54 | 21.9992 | 12.0 | 0 |
146 | 147.32 | 36.8827 | 22.0 | 0 |
147 | 145.415 | 42.1274 | 29.0 | 0 |
148 | 121.92 | 19.788 | 8.0 | 0 |
149 | 113.665 | 16.7829 | 5.0 | 1 |
150 | 157.48 | 44.5654 | 33.0 | 1 |
151 | 154.305 | 47.854 | 34.0 | 0 |
152 | 120.65 | 21.1771 | 12.0 | 0 |
153 | 115.6 | 18.9 | 7.0 | 1 |
154 | 167.005 | 55.1965 | 42.0 | 1 |
155 | 142.875 | 32.9988 | 40.0 | 0 |
156 | 152.4 | 40.88 | 27.0 | 0 |
157 | 96.52 | 13.2676 | 3.0 | 0 |
158 | 160.0 | 51.2 | 25.0 | 1 |
159 | 159.385 | 49.0446 | 29.0 | 1 |
160 | 149.86 | 53.4388 | 45.0 | 0 |
161 | 160.655 | 54.0908 | 26.0 | 1 |
162 | 160.655 | 55.3666 | 45.0 | 1 |
163 | 149.225 | 42.2408 | 45.0 | 0 |
164 | 125.095 | 22.3678 | 11.0 | 0 |
165 | 140.97 | 40.9367 | 85.6 | 0 |
166 | 154.94 | 49.6967 | 26.0 | 1 |
167 | 141.605 | 44.3386 | 24.0 | 0 |
168 | 160.02 | 45.9545 | 57.0 | 1 |
169 | 150.165 | 41.9573 | 22.0 | 0 |
170 | 155.575 | 51.4827 | 24.0 | 0 |
171 | 103.505 | 12.7573 | 6.0 | 0 |
172 | 94.615 | 13.0124 | 4.0 | 0 |
173 | 156.21 | 44.1118 | 21.0 | 0 |
174 | 153.035 | 32.205 | 79.0 | 0 |
175 | 167.005 | 56.7557 | 50.0 | 1 |
176 | 149.86 | 52.6734 | 40.0 | 0 |
177 | 147.955 | 36.4858 | 64.0 | 0 |
178 | 159.385 | 48.8462 | 32.0 | 1 |
179 | 161.925 | 56.9541 | 38.7 | 1 |
180 | 155.575 | 42.099 | 26.0 | 0 |
181 | 159.385 | 50.1786 | 63.0 | 1 |
182 | 146.685 | 46.5499 | 62.0 | 0 |
183 | 172.72 | 61.8019 | 22.0 | 1 |
184 | 166.37 | 48.9879 | 41.0 | 1 |
185 | 141.605 | 31.5246 | 19.0 | 1 |
186 | 142.875 | 32.205 | 17.0 | 0 |
187 | 133.35 | 23.7569 | 14.0 | 0 |
188 | 127.635 | 24.4089 | 9.0 | 1 |
189 | 119.38 | 21.5173 | 7.0 | 1 |
190 | 151.765 | 35.2951 | 74.0 | 0 |
191 | 156.845 | 45.6427 | 41.0 | 1 |
192 | 148.59 | 43.885 | 33.0 | 0 |
193 | 157.48 | 45.5576 | 53.0 | 0 |
194 | 149.86 | 39.0089 | 18.0 | 0 |
195 | 147.955 | 41.1635 | 37.0 | 0 |
196 | 102.235 | 13.1258 | 6.0 | 0 |
197 | 153.035 | 45.2458 | 61.0 | 0 |
198 | 160.655 | 53.6373 | 44.0 | 1 |
199 | 149.225 | 52.3048 | 35.0 | 0 |
200 | 114.3 | 18.3421 | 7.0 | 1 |
201 | 100.965 | 13.7495 | 4.0 | 1 |
202 | 138.43 | 39.094 | 23.0 | 0 |
203 | 91.44 | 12.5305 | 4.0 | 1 |
204 | 162.56 | 45.6994 | 55.0 | 1 |
205 | 149.225 | 40.398 | 53.0 | 0 |
206 | 158.75 | 51.4827 | 59.0 | 1 |
207 | 149.86 | 38.6687 | 57.0 | 0 |
208 | 158.115 | 39.2357 | 35.0 | 1 |
209 | 156.21 | 44.3386 | 29.0 | 0 |
210 | 148.59 | 39.5192 | 62.0 | 1 |
211 | 143.51 | 31.0711 | 18.0 | 0 |
212 | 154.305 | 46.7767 | 51.0 | 0 |
213 | 131.445 | 22.5095 | 14.0 | 0 |
214 | 157.48 | 40.6248 | 19.0 | 1 |
215 | 157.48 | 50.1786 | 42.0 | 1 |
216 | 154.305 | 41.2769 | 25.0 | 0 |
217 | 107.95 | 17.5767 | 6.0 | 1 |
218 | 168.275 | 54.6 | 41.0 | 1 |
219 | 145.415 | 44.9907 | 37.0 | 0 |
220 | 147.955 | 44.7355 | 16.0 | 0 |
221 | 100.965 | 14.4015 | 5.0 | 1 |
222 | 113.03 | 19.0509 | 9.0 | 1 |
223 | 149.225 | 35.8054 | 82.0 | 1 |
224 | 154.94 | 45.2175 | 28.0 | 1 |
225 | 162.56 | 48.1091 | 50.0 | 1 |
226 | 156.845 | 45.671 | 43.0 | 0 |
227 | 123.19 | 20.8085 | 8.0 | 1 |
228 | 161.011 | 48.4209 | 31.0 | 1 |
229 | 144.78 | 41.1918 | 67.0 | 0 |
230 | 143.51 | 38.4136 | 39.0 | 0 |
231 | 149.225 | 42.1274 | 18.0 | 0 |
232 | 110.49 | 17.6617 | 11.0 | 0 |
233 | 149.86 | 38.2435 | 48.0 | 0 |
234 | 165.735 | 48.3359 | 30.0 | 1 |
235 | 144.145 | 38.9239 | 64.0 | 0 |
236 | 157.48 | 40.0295 | 72.0 | 1 |
237 | 154.305 | 50.207 | 68.0 | 0 |
238 | 163.83 | 54.2893 | 44.0 | 1 |
239 | 156.21 | 45.6 | 43.0 | 0 |
240 | 153.67 | 40.7666 | 16.0 | 0 |
241 | 134.62 | 27.1305 | 13.0 | 0 |
242 | 144.145 | 39.4342 | 34.0 | 0 |
243 | 114.3 | 20.4967 | 10.0 | 0 |
244 | 162.56 | 43.2046 | 62.0 | 1 |
245 | 146.05 | 31.8648 | 44.0 | 0 |
246 | 120.65 | 20.8936 | 11.0 | 1 |
247 | 154.94 | 45.4442 | 31.0 | 1 |
248 | 144.78 | 38.045 | 29.0 | 0 |
249 | 106.68 | 15.9891 | 8.0 | 0 |
250 | 146.685 | 36.0889 | 62.0 | 0 |
251 | 152.4 | 40.88 | 67.0 | 0 |
252 | 163.83 | 47.9107 | 57.0 | 1 |
253 | 165.735 | 47.7122 | 32.0 | 1 |
254 | 156.21 | 46.3798 | 24.0 | 0 |
255 | 152.4 | 41.1635 | 77.0 | 1 |
256 | 140.335 | 36.5992 | 62.0 | 0 |
257 | 158.115 | 43.0912 | 17.0 | 1 |
258 | 163.195 | 48.1375 | 67.0 | 1 |
259 | 151.13 | 36.7126 | 70.0 | 0 |
260 | 171.12 | 56.5573 | 37.0 | 1 |
261 | 149.86 | 38.6971 | 58.0 | 0 |
262 | 163.83 | 47.4854 | 35.0 | 1 |
263 | 141.605 | 36.2023 | 30.0 | 0 |
264 | 93.98 | 14.2881 | 5.0 | 0 |
265 | 149.225 | 41.2769 | 26.0 | 0 |
266 | 105.41 | 15.2237 | 5.0 | 0 |
267 | 146.05 | 44.7639 | 21.0 | 0 |
268 | 161.29 | 50.4338 | 41.0 | 1 |
269 | 162.56 | 55.2815 | 46.0 | 1 |
270 | 145.415 | 37.9316 | 49.0 | 0 |
271 | 145.415 | 35.4936 | 15.0 | 1 |
272 | 170.815 | 58.4567 | 28.0 | 1 |
273 | 127.0 | 21.4889 | 12.0 | 0 |
274 | 159.385 | 44.4237 | 83.0 | 0 |
275 | 159.4 | 44.4 | 54.0 | 1 |
276 | 153.67 | 44.5654 | 54.0 | 0 |
277 | 160.02 | 44.6221 | 68.0 | 1 |
278 | 150.495 | 40.4831 | 68.0 | 0 |
279 | 149.225 | 44.0835 | 56.0 | 0 |
280 | 127.0 | 24.4089 | 15.0 | 0 |
281 | 142.875 | 34.4163 | 57.0 | 0 |
282 | 142.113 | 32.772 | 22.0 | 0 |
283 | 147.32 | 35.9472 | 40.0 | 0 |
284 | 162.56 | 49.5549 | 19.0 | 1 |
285 | 164.465 | 53.1837 | 41.0 | 1 |
286 | 160.02 | 37.0811 | 75.9 | 1 |
287 | 153.67 | 40.5114 | 73.9 | 0 |
288 | 167.005 | 50.6039 | 49.0 | 1 |
289 | 151.13 | 43.9701 | 26.0 | 1 |
290 | 147.955 | 33.7926 | 17.0 | 0 |
291 | 125.4 | 21.3755 | 13.0 | 0 |
292 | 111.125 | 16.6695 | 8.0 | 0 |
293 | 153.035 | 49.89 | 88.0 | 1 |
294 | 139.065 | 33.5942 | 68.0 | 0 |
295 | 152.4 | 43.8567 | 33.0 | 1 |
296 | 154.94 | 48.1375 | 26.0 | 0 |
297 | 147.955 | 42.751 | 56.0 | 0 |
298 | 143.51 | 34.8415 | 16.0 | 1 |
299 | 117.983 | 24.0971 | 13.0 | 0 |
300 | 144.145 | 33.906 | 34.0 | 0 |
301 | 92.71 | 12.0769 | 5.0 | 0 |
302 | 147.955 | 41.2769 | 17.0 | 0 |
303 | 155.575 | 39.7176 | 74.0 | 1 |
304 | 150.495 | 35.9472 | 69.0 | 0 |
305 | 155.575 | 50.9157 | 50.0 | 1 |
306 | 154.305 | 45.7561 | 44.0 | 0 |
307 | 130.607 | 25.2594 | 15.0 | 0 |
308 | 101.6 | 15.3371 | 5.0 | 0 |
309 | 157.48 | 49.2147 | 18.0 | 0 |
310 | 168.91 | 58.8252 | 41.0 | 1 |
311 | 150.495 | 43.4598 | 27.0 | 0 |
312 | 111.76 | 17.8318 | 8.9 | 1 |
313 | 160.02 | 51.9646 | 38.0 | 1 |
314 | 167.64 | 50.6889 | 57.0 | 1 |
315 | 144.145 | 34.2462 | 64.5 | 0 |
316 | 145.415 | 39.3775 | 42.0 | 0 |
317 | 160.02 | 59.5623 | 24.0 | 1 |
318 | 147.32 | 40.313 | 16.0 | 1 |
319 | 164.465 | 52.1631 | 71.0 | 1 |
320 | 153.035 | 39.9728 | 49.5 | 0 |
321 | 149.225 | 43.9417 | 33.0 | 1 |
322 | 160.02 | 54.6011 | 28.0 | 0 |
323 | 149.225 | 45.0757 | 47.0 | 0 |
324 | 85.09 | 11.4532 | 3.0 | 1 |
325 | 84.455 | 11.765 | 1.0 | 1 |
326 | 59.6138 | 5.8967 | 1.0 | 0 |
327 | 92.71 | 12.1052 | 3.0 | 1 |
328 | 111.125 | 18.3138 | 6.0 | 0 |
329 | 90.805 | 11.3681 | 5.0 | 0 |
330 | 153.67 | 41.3336 | 27.0 | 0 |
331 | 99.695 | 16.2443 | 5.0 | 0 |
332 | 62.484 | 6.80388 | 1.0 | 0 |
333 | 81.915 | 11.8784 | 2.0 | 1 |
334 | 96.52 | 14.9685 | 2.0 | 0 |
335 | 80.01 | 9.86563 | 1.0 | 1 |
336 | 150.495 | 41.9006 | 55.0 | 0 |
337 | 151.765 | 42.524 | 83.4 | 1 |
338 | 140.64 | 28.8598 | 12.0 | 1 |
339 | 88.265 | 12.7856 | 2.0 | 0 |
340 | 158.115 | 43.1479 | 63.0 | 1 |
341 | 149.225 | 40.8233 | 52.0 | 0 |
342 | 151.765 | 42.8644 | 49.0 | 1 |
343 | 154.94 | 46.2097 | 31.0 | 0 |
344 | 123.825 | 20.5817 | 9.0 | 0 |
345 | 104.14 | 15.8757 | 6.0 | 0 |
346 | 161.29 | 47.854 | 35.0 | 1 |
347 | 148.59 | 42.5243 | 35.0 | 0 |
348 | 97.155 | 17.0664 | 7.0 | 0 |
349 | 93.345 | 13.1825 | 5.0 | 1 |
350 | 160.655 | 48.506 | 24.0 | 1 |
351 | 157.48 | 45.8695 | 41.0 | 1 |
352 | 167.005 | 52.9002 | 32.0 | 1 |
353 | 157.48 | 47.5705 | 43.0 | 1 |
354 | 91.44 | 12.9274 | 6.0 | 0 |
355 | 60.452 | 5.6699 | 1.0 | 1 |
356 | 137.16 | 28.9165 | 15.0 | 1 |
357 | 152.4 | 43.5448 | 63.0 | 0 |
358 | 152.4 | 43.4314 | 21.0 | 0 |
359 | 81.28 | 11.5099 | 1.0 | 1 |
360 | 109.22 | 11.7083 | 2.0 | 0 |
361 | 71.12 | 7.54097 | 1.0 | 1 |
362 | 89.2048 | 12.7006 | 3.0 | 0 |
363 | 67.31 | 7.20077 | 1.0 | 0 |
364 | 85.09 | 12.3604 | 1.0 | 1 |
365 | 69.85 | 7.79611 | 1.0 | 0 |
366 | 161.925 | 53.212 | 55.0 | 0 |
367 | 152.4 | 44.6788 | 38.0 | 0 |
368 | 88.9 | 12.5588 | 3.0 | 1 |
369 | 90.17 | 12.7006 | 3.0 | 1 |
370 | 71.755 | 7.37087 | 1.0 | 0 |
371 | 83.82 | 9.21359 | 1.0 | 0 |
372 | 159.385 | 47.2019 | 28.0 | 1 |
373 | 142.24 | 28.633 | 16.0 | 0 |
374 | 142.24 | 31.6664 | 36.0 | 0 |
375 | 168.91 | 56.4439 | 38.0 | 1 |
376 | 123.19 | 20.0147 | 12.0 | 1 |
377 | 74.93 | 8.50485 | 1.0 | 1 |
378 | 74.295 | 8.3064 | 1.0 | 0 |
379 | 90.805 | 11.6233 | 3.0 | 0 |
380 | 160.02 | 55.7918 | 48.0 | 1 |
381 | 67.945 | 7.96621 | 1.0 | 0 |
382 | 135.89 | 27.2155 | 15.0 | 0 |
383 | 158.115 | 47.4854 | 45.0 | 1 |
384 | 85.09 | 10.8012 | 3.0 | 1 |
385 | 93.345 | 14.0047 | 3.0 | 0 |
386 | 152.4 | 45.1608 | 38.0 | 0 |
387 | 155.575 | 45.5293 | 21.0 | 0 |
388 | 154.305 | 48.8745 | 50.0 | 0 |
389 | 156.845 | 46.5782 | 41.0 | 1 |
390 | 120.015 | 20.1281 | 13.0 | 0 |
391 | 114.3 | 18.1437 | 8.0 | 1 |
392 | 83.82 | 10.9146 | 3.0 | 1 |
393 | 156.21 | 43.885 | 30.0 | 0 |
394 | 137.16 | 27.1588 | 12.0 | 1 |
395 | 114.3 | 19.0509 | 7.0 | 1 |
396 | 93.98 | 13.8346 | 4.0 | 0 |
397 | 168.275 | 56.047 | 21.0 | 1 |
398 | 147.955 | 40.0862 | 38.0 | 0 |
399 | 139.7 | 26.5635 | 15.0 | 1 |
400 | 157.48 | 50.8023 | 19.0 | 0 |
401 | 76.2 | 9.21359 | 1.0 | 1 |
402 | 66.04 | 7.56932 | 1.0 | 1 |
403 | 160.7 | 46.3 | 31.0 | 1 |
404 | 114.3 | 19.4194 | 8.0 | 0 |
405 | 146.05 | 37.9033 | 16.0 | 1 |
406 | 161.29 | 49.3565 | 21.0 | 1 |
407 | 69.85 | 7.31417 | 0.0 | 0 |
408 | 133.985 | 28.1511 | 13.0 | 1 |
409 | 67.945 | 7.82446 | 0.0 | 1 |
410 | 150.495 | 44.1118 | 50.0 | 0 |
411 | 163.195 | 51.0291 | 39.0 | 1 |
412 | 148.59 | 40.7666 | 44.0 | 1 |
413 | 148.59 | 37.5631 | 36.0 | 0 |
414 | 161.925 | 51.5961 | 36.0 | 1 |
415 | 153.67 | 44.8206 | 18.0 | 0 |
416 | 68.58 | 8.02291 | 0.0 | 0 |
417 | 151.13 | 43.4031 | 58.0 | 0 |
418 | 163.83 | 46.72 | 58.0 | 1 |
419 | 153.035 | 39.5476 | 33.0 | 0 |
420 | 151.765 | 34.7848 | 21.5 | 0 |
421 | 132.08 | 22.793 | 11.0 | 1 |
422 | 156.21 | 39.2924 | 26.0 | 1 |
423 | 140.335 | 37.4497 | 22.0 | 0 |
424 | 158.75 | 48.6761 | 28.0 | 1 |
425 | 142.875 | 35.607 | 42.0 | 0 |
426 | 84.455 | 9.38368 | 2.0 | 1 |
427 | 151.943 | 43.7149 | 21.0 | 1 |
428 | 161.29 | 48.1942 | 19.0 | 1 |
429 | 127.991 | 29.852 | 13.0 | 1 |
430 | 160.985 | 50.9724 | 48.0 | 1 |
431 | 144.78 | 43.9984 | 46.0 | 0 |
432 | 132.08 | 28.2928 | 11.0 | 1 |
433 | 117.983 | 20.3549 | 8.0 | 1 |
434 | 160.02 | 48.1942 | 25.0 | 1 |
435 | 154.94 | 39.179 | 16.0 | 1 |
436 | 160.985 | 46.6916 | 51.0 | 1 |
437 | 165.989 | 56.4155 | 25.0 | 1 |
438 | 157.988 | 48.591 | 28.0 | 1 |
439 | 154.94 | 48.2225 | 26.0 | 0 |
440 | 97.9932 | 13.2959 | 5.0 | 1 |
441 | 64.135 | 6.66213 | 1.0 | 0 |
442 | 160.655 | 47.4854 | 54.0 | 1 |
443 | 147.32 | 35.5503 | 66.0 | 0 |
444 | 146.7 | 36.6 | 20.0 | 0 |
445 | 147.32 | 48.9596 | 25.0 | 0 |
446 | 172.999 | 51.2559 | 38.0 | 1 |
447 | 158.115 | 46.5215 | 51.0 | 1 |
448 | 147.32 | 36.9677 | 48.0 | 0 |
449 | 124.993 | 25.1177 | 13.0 | 1 |
450 | 106.045 | 16.2726 | 6.0 | 1 |
451 | 165.989 | 48.6477 | 27.0 | 1 |
452 | 149.86 | 38.045 | 22.0 | 0 |
453 | 76.2 | 8.50485 | 1.0 | 0 |
454 | 161.925 | 47.287 | 60.0 | 1 |
455 | 140.005 | 28.3495 | 15.0 | 0 |
456 | 66.675 | 8.13631 | 0.0 | 0 |
457 | 62.865 | 7.20077 | 0.0 | 1 |
458 | 163.83 | 55.3949 | 43.0 | 1 |
459 | 147.955 | 32.4885 | 12.0 | 1 |
460 | 160.02 | 54.2042 | 27.0 | 1 |
461 | 154.94 | 48.4776 | 30.0 | 1 |
462 | 152.4 | 43.0629 | 29.0 | 0 |
463 | 62.23 | 7.25747 | 0.0 | 0 |
464 | 146.05 | 34.1895 | 23.0 | 0 |
465 | 151.994 | 49.9518 | 30.0 | 0 |
466 | 157.48 | 41.3052 | 17.0 | 1 |
467 | 55.88 | 4.84776 | 0.0 | 0 |
468 | 60.96 | 6.23689 | 0.0 | 1 |
469 | 151.765 | 44.3386 | 41.0 | 0 |
470 | 144.78 | 33.4524 | 42.0 | 0 |
471 | 118.11 | 16.8963 | 7.0 | 0 |
472 | 78.105 | 8.22136 | 3.0 | 0 |
473 | 160.655 | 47.287 | 43.0 | 1 |
474 | 151.13 | 46.1246 | 35.0 | 0 |
475 | 121.92 | 20.1848 | 10.0 | 0 |
476 | 92.71 | 12.7573 | 3.0 | 1 |
477 | 153.67 | 47.4004 | 75.5 | 1 |
478 | 147.32 | 40.8516 | 64.0 | 0 |
479 | 139.7 | 50.3487 | 38.0 | 1 |
480 | 157.48 | 45.1324 | 24.2 | 0 |
481 | 91.44 | 11.6233 | 4.0 | 0 |
482 | 154.94 | 42.2408 | 26.0 | 1 |
483 | 143.51 | 41.6454 | 19.0 | 0 |
484 | 83.185 | 9.15689 | 2.0 | 1 |
485 | 158.115 | 45.2175 | 43.0 | 1 |
486 | 147.32 | 51.2559 | 38.0 | 0 |
487 | 123.825 | 21.2054 | 10.0 | 1 |
488 | 88.9 | 11.5949 | 3.0 | 1 |
489 | 160.02 | 49.2714 | 23.0 | 1 |
490 | 137.16 | 27.9526 | 16.0 | 0 |
491 | 165.1 | 51.1992 | 49.0 | 1 |
492 | 154.94 | 43.8567 | 41.0 | 0 |
493 | 111.125 | 17.6901 | 6.0 | 1 |
494 | 153.67 | 35.5219 | 23.0 | 0 |
495 | 145.415 | 34.2462 | 14.0 | 0 |
496 | 141.605 | 42.8854 | 43.0 | 0 |
497 | 144.78 | 32.5452 | 15.0 | 0 |
498 | 163.83 | 46.7767 | 21.0 | 1 |
499 | 161.29 | 41.8722 | 24.0 | 1 |
500 | 154.9 | 38.2 | 20.0 | 1 |
501 | 161.3 | 43.3 | 20.0 | 1 |
502 | 170.18 | 53.6373 | 34.0 | 1 |
503 | 149.86 | 42.9778 | 29.0 | 0 |
504 | 123.825 | 21.5456 | 11.0 | 1 |
505 | 85.09 | 11.4248 | 3.0 | 0 |
506 | 160.655 | 39.7743 | 65.0 | 1 |
507 | 154.94 | 43.3464 | 46.0 | 0 |
508 | 106.045 | 15.4788 | 8.0 | 0 |
509 | 126.365 | 21.9142 | 15.0 | 1 |
510 | 166.37 | 52.6734 | 43.0 | 1 |
511 | 148.285 | 38.4419 | 39.0 | 0 |
512 | 124.46 | 19.2777 | 12.0 | 0 |
513 | 89.535 | 11.113 | 3.0 | 1 |
514 | 101.6 | 13.4944 | 4.0 | 0 |
515 | 151.765 | 42.8077 | 43.0 | 0 |
516 | 148.59 | 35.8905 | 70.0 | 0 |
517 | 153.67 | 44.2252 | 26.0 | 0 |
518 | 53.975 | 4.25242 | 0.0 | 0 |
519 | 146.685 | 38.0734 | 48.0 | 0 |
520 | 56.515 | 5.15961 | 0.0 | 0 |
521 | 100.965 | 14.3165 | 5.0 | 1 |
522 | 121.92 | 23.2182 | 8.0 | 1 |
523 | 81.5848 | 10.6594 | 3.0 | 0 |
524 | 154.94 | 44.1118 | 44.0 | 1 |
525 | 156.21 | 44.0268 | 33.0 | 0 |
526 | 132.715 | 24.9759 | 15.0 | 1 |
527 | 125.095 | 22.5946 | 12.0 | 0 |
528 | 101.6 | 14.3448 | 5.0 | 0 |
529 | 160.655 | 47.8823 | 41.0 | 1 |
530 | 146.05 | 39.4058 | 37.4 | 0 |
531 | 132.715 | 24.7775 | 13.0 | 0 |
532 | 87.63 | 10.6594 | 6.0 | 0 |
533 | 156.21 | 41.0501 | 53.0 | 1 |
534 | 152.4 | 40.8233 | 49.0 | 0 |
535 | 162.56 | 47.0318 | 27.0 | 0 |
536 | 114.935 | 17.52 | 7.0 | 1 |
537 | 67.945 | 7.22912 | 1.0 | 0 |
538 | 142.875 | 34.2462 | 31.0 | 0 |
539 | 76.835 | 8.02291 | 1.0 | 1 |
540 | 145.415 | 31.1278 | 17.0 | 1 |
541 | 162.56 | 52.1631 | 31.0 | 1 |
542 | 156.21 | 54.0625 | 21.0 | 0 |
543 | 71.12 | 8.05126 | 0.0 | 1 |
544 | 158.75 | 52.5316 | 68.0 | 1 |
Use only adults and standardize
df2 = filter(row -> row[:age] >= 18, df);
df2[:weight] = convert(Vector{Float64}, df2[:weight]);
df2[:weight_s] = (df2[:weight] .- mean(df2[:weight])) / std(df2[:weight]);
df2[:weight_s2] = df2[:weight_s] .^ 2;
352-element Array{Float64,1}:
0.19280615097192907
1.7349763044783346
4.1325600023146265
1.5549757699350777
0.3308042660851645
7.7736359966100865
1.0919440933750901
2.6392838898982456
2.45691571770544
2.163583954566629
⋮
0.2005950450601446
0.748125332136797
0.37244351134337544
0.416550377673148
0.0999553970190397
2.7690646673659187
1.2340428738476732
1.9741712709264059
1.3641165169515888
Show the first six rows of the dataset.
first(df2, 6)
height | weight | age | male | weight_s | weight_s2 | |
---|---|---|---|---|---|---|
Float64 | Float64 | Float64 | Int64 | Float64 | Float64 | |
1 | 151.765 | 47.8256 | 63.0 | 1 | 0.439097 | 0.192806 |
2 | 139.7 | 36.4858 | 63.0 | 0 | -1.31718 | 1.73498 |
3 | 136.525 | 31.8648 | 65.0 | 0 | -2.03287 | 4.13256 |
4 | 156.845 | 53.0419 | 41.0 | 1 | 1.24699 | 1.55498 |
5 | 145.415 | 41.2769 | 51.0 | 0 | -0.575156 | 0.330804 |
6 | 163.83 | 62.9926 | 35.0 | 1 | 2.78812 | 7.77364 |
Then define a structure to hold the data: observables, covariates, and the degrees of freedom for the prior.
Linear regression model $y ∼ Xβ + ϵ$, where $ϵ ∼ N(0, σ²)$ IID.
struct ConstraintHeightProblem{TY <: AbstractVector, TX <: AbstractMatrix}
"Observations."
y::TY
"Covariates"
X::TX
end;
Then make the type callable with the parameters as a single argument.
function (problem::ConstraintHeightProblem)(θ)
@unpack y, X, = problem # extract the data
@unpack β, σ = θ # works on the named tuple too
ll = 0.0
ll += logpdf(Normal(178, 100), X[1]) # a = X[1]
ll += logpdf(Normal(0, 10), X[2]) # b1 = X[2]
ll += logpdf(Normal(0, 10), X[3]) # b2 = X[3]
ll += logpdf(TDist(1.0), σ)
ll += loglikelihood(Normal(0, σ), y .- X*β)
ll
end
Setup data and inits.
N = size(df2, 1)
X = hcat(ones(N), hcat(df2[:weight_s], df2[:weight_s2]));
y = convert(Vector{Float64}, df2[:height])
p = ConstraintHeightProblem(y, X);
p((β = [1.0, 2.0, 3.0], σ = 1.0))
-4.0013242576346947e6
Use a function to return the transformation (as it varies with the number of covariates).
problem_transformation(p::ConstraintHeightProblem) =
as((β = as(Array, size(p.X, 2)), σ = asℝ₊))
problem_transformation (generic function with 1 method)
Wrap the problem with a transformation, then use Flux for the gradient.
P = TransformedLogDensity(problem_transformation(p), p)
∇P = LogDensityRejectErrors(ADgradient(:ForwardDiff, P));
LogDensityRejectErrors{InvalidLogDensityException,LogDensityProblems.ForwardDiffLogDensity{TransformedLogDensity{TransformVariables.TransformTuple{NamedTuple{(:β, :σ),Tuple{TransformVariables.ArrayTransform{TransformVariables.Identity,1},TransformVariables.ShiftedExp{true,Float64}}}},Main.ex-m4.5d.ConstraintHeightProblem{Array{Float64,1},Array{Float64,2}}},ForwardDiff.GradientConfig{ForwardDiff.Tag{getfield(LogDensityProblems, Symbol("##1#2")){TransformedLogDensity{TransformVariables.TransformTuple{NamedTuple{(:β, :σ),Tuple{TransformVariables.ArrayTransform{TransformVariables.Identity,1},TransformVariables.ShiftedExp{true,Float64}}}},Main.ex-m4.5d.ConstraintHeightProblem{Array{Float64,1},Array{Float64,2}}}},Float64},Float64,4,Array{ForwardDiff.Dual{ForwardDiff.Tag{getfield(LogDensityProblems, Symbol("##1#2")){TransformedLogDensity{TransformVariables.TransformTuple{NamedTuple{(:β, :σ),Tuple{TransformVariables.ArrayTransform{TransformVariables.Identity,1},TransformVariables.ShiftedExp{true,Float64}}}},Main.ex-m4.5d.ConstraintHeightProblem{Array{Float64,1},Array{Float64,2}}}},Float64},Float64,4},1}}}}(ForwardDiff AD wrapper for TransformedLogDensity of dimension 4, w/ chunk size 4)
Draw samples.
chain, NUTS_tuned = NUTS_init_tune_mcmc(∇P, 1000);
(NUTS_Transition{Array{Float64,1},Float64}[NUTS_Transition{Array{Float64,1},Float64}([154.68144259166908, 6.1879685245233675, -0.0335400815421743, 1.5747045382544886], -1089.3699060031731, 3, DynamicHMC.DoubledTurn, 0.9956208592189933, 7), NUTS_Transition{Array{Float64,1},Float64}([155.06336625059575, 5.775236246745802, -0.34737977466365993, 1.6503933809162654], -1091.3701813334249, 3, DynamicHMC.DoubledTurn, 0.9116578504808491, 7), NUTS_Transition{Array{Float64,1},Float64}([154.98838747908349, 5.723563791147264, -0.2581881761383544, 1.6703005616874123], -1089.4414592986238, 2, DynamicHMC.AdjacentTurn, 0.9761295123241143, 7), NUTS_Transition{Array{Float64,1},Float64}([154.93329241870524, 5.7449871662394925, -0.4475666981488615, 1.7056799868587578], -1092.763196570722, 2, DynamicHMC.DoubledTurn, 0.842098026483578, 3), NUTS_Transition{Array{Float64,1},Float64}([154.6396883506302, 5.877063049463691, -0.20218484166364487, 1.696825291936204], -1092.458008487654, 2, DynamicHMC.DoubledTurn, 1.0, 3), NUTS_Transition{Array{Float64,1},Float64}([154.68698458652275, 5.820397962714695, -0.14203863661287164, 1.670289818170209], -1090.2776641427254, 3, DynamicHMC.DoubledTurn, 0.9598060436145998, 7), NUTS_Transition{Array{Float64,1},Float64}([154.54599069921792, 5.927105166725718, 0.07084436300542657, 1.6119598390156338], -1090.1222029166204, 1, DynamicHMC.AdjacentTurn, 0.8721741083525206, 3), NUTS_Transition{Array{Float64,1},Float64}([154.49080237294976, 6.024977120821876, 0.2507069066886138, 1.6552703534406723], -1090.2285415096237, 1, DynamicHMC.AdjacentTurn, 0.7930806699665314, 3), NUTS_Transition{Array{Float64,1},Float64}([154.58691853768957, 6.021386444496842, 0.2582109349202482, 1.6794485936658554], -1090.3725067097685, 2, DynamicHMC.DoubledTurn, 0.9130508431518797, 3), NUTS_Transition{Array{Float64,1},Float64}([154.80209435140958, 5.876279235940016, 0.17656167759237656, 1.600652497329514], -1090.568775831649, 2, DynamicHMC.AdjacentTurn, 0.99120793876878, 7) … NUTS_Transition{Array{Float64,1},Float64}([154.10451580574897, 5.930719893213091, 0.06996837892880195, 1.6585981256332705], -1093.2631000552976, 3, DynamicHMC.DoubledTurn, 0.798666202954589, 7), NUTS_Transition{Array{Float64,1},Float64}([154.19851587297907, 5.483320976521171, 0.10093012795253782, 1.6874211971123527], -1091.312750407877, 2, DynamicHMC.DoubledTurn, 0.8875220769555429, 3), NUTS_Transition{Array{Float64,1},Float64}([154.0916716222212, 5.401279683267812, 0.3587295050422174, 1.6842727823891492], -1092.7074067688022, 2, DynamicHMC.DoubledTurn, 0.91022937237642, 3), NUTS_Transition{Array{Float64,1},Float64}([155.51843012587662, 6.110425031342413, -0.46760489158069707, 1.6566851502073183], -1092.1864449251714, 3, DynamicHMC.DoubledTurn, 1.0, 7), NUTS_Transition{Array{Float64,1},Float64}([154.94430243281627, 6.186019942953062, -0.3223800435858167, 1.5902439678760907], -1092.2564857320162, 2, DynamicHMC.AdjacentTurn, 0.9902381179551946, 7), NUTS_Transition{Array{Float64,1},Float64}([154.17786873206566, 5.571160067758131, 0.3483212006691731, 1.625645830095357], -1091.6069020360947, 3, DynamicHMC.DoubledTurn, 0.9840680824130674, 7), NUTS_Transition{Array{Float64,1},Float64}([155.54115372552806, 6.487786270211303, -0.3377394907775441, 1.6679493754321295], -1094.6726163307173, 2, DynamicHMC.AdjacentTurn, 0.740801550868251, 7), NUTS_Transition{Array{Float64,1},Float64}([154.70965096034342, 6.34620215303118, -0.1760675766977283, 1.6848082635970425], -1094.8748243981952, 2, DynamicHMC.AdjacentTurn, 0.9826149831996952, 7), NUTS_Transition{Array{Float64,1},Float64}([154.60712042398876, 5.327718597513535, -0.0031178985921916647, 1.6117986067265087], -1093.9363211096552, 3, DynamicHMC.AdjacentTurn, 0.8532649285464331, 11), NUTS_Transition{Array{Float64,1},Float64}([154.45006897794553, 6.1877664061505575, 0.5333578863359139, 1.6387100224581679], -1094.407617590381, 3, DynamicHMC.DoubledTurn, 0.8047057764971309, 7)], NUTS sampler in 4 dimensions
stepsize (ϵ) ≈ 0.648
maximum depth = 10
Gaussian kinetic energy, √diag(M⁻¹): [0.357463491409066, 0.28007702228690057, 0.22060922821385556, 0.047842895509854544]
)
We use the transformation to obtain the posterior from the chain.
posterior = TransformVariables.transform.(Ref(problem_transformation(p)), get_position.(chain));
posterior[1:5]
5-element Array{NamedTuple{(:β, :σ),Tuple{Array{Float64,1},Float64}},1}:
(β = [154.68144259166908, 6.1879685245233675, -0.0335400815421743], σ = 4.829314529595091)
(β = [155.06336625059575, 5.775236246745802, -0.34737977466365993], σ = 5.209028556614351)
(β = [154.98838747908349, 5.723563791147264, -0.2581881761383544], σ = 5.313764671265668)
(β = [154.93329241870524, 5.7449871662394925, -0.4475666981488615], σ = 5.505127809329699)
(β = [154.6396883506302, 5.877063049463691, -0.20218484166364487], σ = 5.456596762965159)
Extract the parameter posterior means: β
,
posterior_β = mean(first, posterior)
3-element Array{Float64,1}:
154.61847426754244
5.8299075628152215
-0.016146952236143312
then σ
:
posterior_σ = mean(last, posterior)
5.095700613912254
Effective sample sizes (of untransformed draws)
ess = mapslices(effective_sample_size,
get_position_matrix(chain); dims = 1)
1×4 Array{Float64,2}:
1000.0 1000.0 682.411 1000.0
NUTS-specific statistics
NUTS_statistics(chain)
cmdstan_result = "
Iterations = 1:1000
Thinning interval = 1
Chains = 1,2,3,4
Samples per chain = 1000
Empirical Posterior Estimates:
Mean SD Naive SE MCSE ESS
a 154.609019750 0.36158389 0.0057171433 0.0071845548 1000
b1 5.838431778 0.27920926 0.0044146860 0.0048693502 1000
b2 -0.009985954 0.22897191 0.0036203637 0.0047224478 1000
sigma 5.110136300 0.19096315 0.0030193925 0.0030728192 1000
Quantiles:
2.5% 25.0% 50.0% 75.0% 97.5%
a 153.92392500 154.3567500 154.60700000 154.8502500 155.32100000
b1 5.27846200 5.6493250 5.83991000 6.0276275 6.39728200
b2 -0.45954687 -0.1668285 -0.01382935 0.1423620 0.43600905
sigma 4.76114350 4.9816850 5.10326000 5.2300450 5.51500975
";
"\nIterations = 1:1000\nThinning interval = 1\nChains = 1,2,3,4\nSamples per chain = 1000\n\nEmpirical Posterior Estimates:\n Mean SD Naive SE MCSE ESS\n a 154.609019750 0.36158389 0.0057171433 0.0071845548 1000\n b1 5.838431778 0.27920926 0.0044146860 0.0048693502 1000\n b2 -0.009985954 0.22897191 0.0036203637 0.0047224478 1000\nsigma 5.110136300 0.19096315 0.0030193925 0.0030728192 1000\n\nQuantiles:\n 2.5% 25.0% 50.0% 75.0% 97.5%\n a 153.92392500 154.3567500 154.60700000 154.8502500 155.32100000\n b1 5.27846200 5.6493250 5.83991000 6.0276275 6.39728200\n b2 -0.45954687 -0.1668285 -0.01382935 0.1423620 0.43600905\nsigma 4.76114350 4.9816850 5.10326000 5.2300450 5.51500975\n"
Extract the parameter posterior means: β
,
[posterior_β, posterior_σ]
2-element Array{Any,1}:
[154.61847426754244, 5.8299075628152215, -0.016146952236143312]
5.095700613912254
end of m4.5d.jl#- This page was generated using Literate.jl.