-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulations.html
473 lines (464 loc) · 53.3 KB
/
simulations.html
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
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>6 Simulations | Creating the flowcut R package</title>
<meta name="description" content="6 Simulations | Creating the flowcut R package">
<meta name="generator" content="bookdown 0.40 and GitBook 2.6.7">
<meta property="og:title" content="6 Simulations | Creating the flowcut R package">
<meta property="og:type" content="book">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="6 Simulations | Creating the flowcut R package">
<meta name="author" content="Sheng Jiang, Sangwon Hyun">
<meta name="date" content="2025-01-18">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="prev" href="gibbs-sampler.html">
<link rel="next" href="real-data-example.html">
<script src="libs/header-attrs-2.28/header-attrs.js"></script><script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/fuse.min.js"></script><link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet">
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet">
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet">
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet">
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet">
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet">
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet">
<link href="libs/anchor-sections-1.1.0/anchor-sections.css" rel="stylesheet">
<link href="libs/anchor-sections-1.1.0/anchor-sections-hash.css" rel="stylesheet">
<script src="libs/anchor-sections-1.1.0/anchor-sections.js"></script><style type="text/css">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { display: inline-block; text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { color: #008000; } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { color: #008000; font-weight: bold; } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<style type="text/css">
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
</style>
<style type="text/css">fieldset.chunkfield {border:1px dotted black;padding-bottom: 0px;padding-top: 0px;margin:0 2px;padding:.35em .625em .75em}
legend.chunklegend {padding:0;width:auto;border:0; border-bottom: none; margin-bottom:0}
</style>
</head>
<body>
<div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">
<div class="book-summary">
<nav role="navigation"><ul class="summary">
<li class="chapter" data-level="1" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i><b>1</b> Introduction</a></li>
<li class="chapter" data-level="2" data-path="package-setup.html"><a href="package-setup.html"><i class="fa fa-check"></i><b>2</b> Package setup</a></li>
<li class="chapter" data-level="3" data-path="helpers-functions.html">
<a href="helpers-functions.html"><i class="fa fa-check"></i><b>3</b> Helpers functions</a>
<ul>
<li class="chapter" data-level="3.1" data-path="helpers-functions.html"><a href="helpers-functions.html#ball-constraint-on-xt-beta_k-retire-soon"><i class="fa fa-check"></i><b>3.1</b> Ball constraint on <span class="math inline">\(X^{(t)} \beta_k\)</span> (retire soon)</a></li>
<li class="chapter" data-level="3.2" data-path="helpers-functions.html"><a href="helpers-functions.html#closed-form-g-value-in-betas-prior-d1"><i class="fa fa-check"></i><b>3.2</b> Closed-form <span class="math inline">\(g\)</span> value in <span class="math inline">\(\beta\)</span>’s prior (<span class="math inline">\(d=1\)</span>)</a></li>
<li class="chapter" data-level="3.3" data-path="helpers-functions.html"><a href="helpers-functions.html#closed-form-g-value-in-betas-prior-d1-1"><i class="fa fa-check"></i><b>3.3</b> Closed-form <span class="math inline">\(g\)</span> value in <span class="math inline">\(\beta\)</span>’s prior (<span class="math inline">\(d=1\)</span>)</a></li>
<li class="chapter" data-level="3.4" data-path="helpers-functions.html"><a href="helpers-functions.html#other-helpers"><i class="fa fa-check"></i><b>3.4</b> Other helpers</a></li>
</ul>
</li>
<li class="chapter" data-level="4" data-path="syntheticdata.html"><a href="syntheticdata.html"><i class="fa fa-check"></i><b>4</b> Generating synthetic data</a></li>
<li class="chapter" data-level="5" data-path="gibbs-sampler.html">
<a href="gibbs-sampler.html"><i class="fa fa-check"></i><b>5</b> Gibbs sampler</a>
<ul>
<li class="chapter" data-level="5.1" data-path="gibbs-sampler.html"><a href="gibbs-sampler.html#the-main-gibbs-sampler"><i class="fa fa-check"></i><b>5.1</b> The main Gibbs sampler</a></li>
</ul>
</li>
<li class="chapter" data-level="6" data-path="simulations.html">
<a href="simulations.html"><i class="fa fa-check"></i><b>6</b> Simulations</a>
<ul>
<li class="chapter" data-level="6.1" data-path="simulations.html"><a href="simulations.html#simulation-helpers"><i class="fa fa-check"></i><b>6.1</b> Simulation helpers</a></li>
</ul>
</li>
<li class="chapter" data-level="7" data-path="real-data-example.html"><a href="real-data-example.html"><i class="fa fa-check"></i><b>7</b> Real data example</a></li>
<li class="chapter" data-level="8" data-path="documenting-the-package-and-building.html"><a href="documenting-the-package-and-building.html"><i class="fa fa-check"></i><b>8</b> Documenting the package and building</a></li>
</ul></nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i><a href="./">Creating the <code>flowcut</code> R package</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-"><div id="simulations" class="section level1 hasAnchor" number="6">
<h1>
<span class="header-section-number">6</span> Simulations<a href="simulations.html#simulations" class="anchor-section" aria-label="Anchor link to header"></a>
</h1>
<p>Refer back to the data generation in @ref{syntheticdata}. We will be
top-censoring the data at 0.5. Before censoring, this is what the data looks
like.</p>
<div class="sourceCode" id="cb33"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb33-1"><a href="simulations.html#cb33-1" tabindex="-1"></a><span class="do">## Load the "original" model</span></span>
<span id="cb33-2"><a href="simulations.html#cb33-2" tabindex="-1"></a>orig_model <span class="ot">=</span> <span class="fu">readRDS</span>(<span class="at">file=</span><span class="fu">file.path</span>(<span class="st">"~/repos/flowcut/inst/output"</span>, <span class="st">"orig_model.RDS"</span>))</span>
<span id="cb33-3"><a href="simulations.html#cb33-3" tabindex="-1"></a></span>
<span id="cb33-4"><a href="simulations.html#cb33-4" tabindex="-1"></a><span class="do">## Generate data</span></span>
<span id="cb33-5"><a href="simulations.html#cb33-5" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">100</span>)</span>
<span id="cb33-6"><a href="simulations.html#cb33-6" tabindex="-1"></a>isignal <span class="ot">=</span> <span class="dv">10</span></span>
<span id="cb33-7"><a href="simulations.html#cb33-7" tabindex="-1"></a>new_model <span class="ot">=</span> flowcut<span class="sc">::</span><span class="fu">make_model</span>(orig_model, isignal)</span>
<span id="cb33-8"><a href="simulations.html#cb33-8" tabindex="-1"></a><span class="do">## new_model = flowcut::make_model(orig_sim_model, isignal)</span></span>
<span id="cb33-9"><a href="simulations.html#cb33-9" tabindex="-1"></a>ylist <span class="ot">=</span> flowcut<span class="sc">::</span><a href="syntheticdata.html#gen_1d">gen_1d</a>(new_model, <span class="at">nt =</span> <span class="dv">100</span>)</span>
<span id="cb33-10"><a href="simulations.html#cb33-10" tabindex="-1"></a>flowtrend<span class="sc">::</span><span class="fu">plot_1d</span>(ylist, <span class="at">obj =</span> new_model)</span></code></pre></div>
<p>After censoring, this is it.</p>
<div class="sourceCode" id="cb34"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb34-1"><a href="simulations.html#cb34-1" tabindex="-1"></a><span class="do">## Censor it</span></span>
<span id="cb34-2"><a href="simulations.html#cb34-2" tabindex="-1"></a>ylist <span class="ot">=</span> <span class="fu">lapply</span>(ylist, <span class="cf">function</span>(y){</span>
<span id="cb34-3"><a href="simulations.html#cb34-3" tabindex="-1"></a> y <span class="ot">=</span> <span class="fu">pmin</span>(y, <span class="fl">0.5</span>)</span>
<span id="cb34-4"><a href="simulations.html#cb34-4" tabindex="-1"></a>})</span>
<span id="cb34-5"><a href="simulations.html#cb34-5" tabindex="-1"></a>flowtrend<span class="sc">::</span><span class="fu">plot_1d</span>(ylist, <span class="at">obj =</span> new_model)</span></code></pre></div>
<p>We need to specify a few things (1) like the censoring limits <code>Cbox</code> and (2)
<code>countslist</code> before running the MCMC.</p>
<div class="sourceCode" id="cb35"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb35-1"><a href="simulations.html#cb35-1" tabindex="-1"></a><span class="do">## Form the censoring "box"</span></span>
<span id="cb35-2"><a href="simulations.html#cb35-2" tabindex="-1"></a>Cbox <span class="ot">=</span> <span class="fu">rbind</span>(<span class="fu">c</span>(<span class="sc">-</span><span class="cn">Inf</span>, <span class="fl">0.5</span>)) </span>
<span id="cb35-3"><a href="simulations.html#cb35-3" tabindex="-1"></a></span>
<span id="cb35-4"><a href="simulations.html#cb35-4" tabindex="-1"></a><span class="do">## Counts are all equal for now</span></span>
<span id="cb35-5"><a href="simulations.html#cb35-5" tabindex="-1"></a>countslist <span class="ot">=</span> <span class="fu">lapply</span>(ylist, <span class="cf">function</span>(y){ <span class="fu">rep</span>(<span class="dv">1</span>, <span class="fu">nrow</span>(y)) })</span>
<span id="cb35-6"><a href="simulations.html#cb35-6" tabindex="-1"></a></span>
<span id="cb35-7"><a href="simulations.html#cb35-7" tabindex="-1"></a><span class="do">## Save the metadata</span></span>
<span id="cb35-8"><a href="simulations.html#cb35-8" tabindex="-1"></a>datobj <span class="ot">=</span> <span class="fu">list</span>(<span class="at">ylist=</span>ylist, <span class="at">countslist=</span>countslist, <span class="at">Cbox=</span>Cbox, <span class="at">X=</span>orig_model<span class="sc">$</span>X)</span>
<span id="cb35-9"><a href="simulations.html#cb35-9" tabindex="-1"></a><span class="do">## datobj = list(ylist=ylist, countslist=countslist, Cbox=Cbox, X=orig_sim_model$X)</span></span>
<span id="cb35-10"><a href="simulations.html#cb35-10" tabindex="-1"></a><span class="fu">saveRDS</span>(datobj,</span>
<span id="cb35-11"><a href="simulations.html#cb35-11" tabindex="-1"></a> <span class="fu">file.path</span>(<span class="st">"~/repos/flowcut/inst/output"</span>,</span>
<span id="cb35-12"><a href="simulations.html#cb35-12" tabindex="-1"></a> <span class="fu">paste0</span>(<span class="st">"isignal-"</span>, isignal, <span class="st">"-datobj.RDS"</span>)))</span></code></pre></div>
<p>We also need some <em>prior elicitation</em> to prevent the cluster means from changing
too much across time.</p>
<p>(code copy-pasted as-is for now, not meant to be run)</p>
<div class="sourceCode" id="cb36"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb36-1"><a href="simulations.html#cb36-1" tabindex="-1"></a>gg <span class="ot"><-</span> <a href="helpers-functions.html#maxdev_to_gg">maxdev_to_gg</a>(datobj<span class="sc">$</span>X, </span>
<span id="cb36-2"><a href="simulations.html#cb36-2" tabindex="-1"></a> <span class="at">dimdat =</span> <span class="dv">3</span>,</span>
<span id="cb36-3"><a href="simulations.html#cb36-3" tabindex="-1"></a> <span class="at">maxdev =</span> <span class="fl">0.5</span>,</span>
<span id="cb36-4"><a href="simulations.html#cb36-4" tabindex="-1"></a> <span class="at">numclust =</span> <span class="dv">10</span>,</span>
<span id="cb36-5"><a href="simulations.html#cb36-5" tabindex="-1"></a> <span class="at">ggvec =</span> (<span class="dv">1</span><span class="sc">:</span><span class="dv">40</span>)<span class="sc">/</span><span class="dv">200</span>, </span>
<span id="cb36-6"><a href="simulations.html#cb36-6" tabindex="-1"></a> <span class="at">n.cores =</span> <span class="fu">detectCores</span>(),</span>
<span id="cb36-7"><a href="simulations.html#cb36-7" tabindex="-1"></a> <span class="at">Nmc =</span> <span class="fl">1e4</span>, <span class="at">viz=</span><span class="cn">FALSE</span>)</span>
<span id="cb36-8"><a href="simulations.html#cb36-8" tabindex="-1"></a>gg <span class="ot"><-</span> <span class="fl">0.01287879</span></span>
<span id="cb36-9"><a href="simulations.html#cb36-9" tabindex="-1"></a><span class="do">## hist(<a href="helpers-functions.html#ball.deviance">ball.deviance</a>(0.01,0.5,t(X))$msd, breaks = "FD")</span></span>
<span id="cb36-10"><a href="simulations.html#cb36-10" tabindex="-1"></a><span class="do">## hist(<a href="helpers-functions.html#ball.deviance">ball.deviance</a>(0.1,0.5,X)$msd, breaks = "FD")</span></span>
<span id="cb36-11"><a href="simulations.html#cb36-11" tabindex="-1"></a><span class="do">## hist(<a href="helpers-functions.html#ball.deviance">ball.deviance</a>(0.2,0.5,X)$msd, breaks = "FD")</span></span>
<span id="cb36-12"><a href="simulations.html#cb36-12" tabindex="-1"></a><span class="do">## hist(<a href="helpers-functions.html#ball.deviance">ball.deviance</a>(0.5,0.5,X)$msd, breaks = "FD")</span></span>
<span id="cb36-13"><a href="simulations.html#cb36-13" tabindex="-1"></a><span class="do">## hist(<a href="helpers-functions.html#ball.deviance">ball.deviance</a>(1,0.5,X)$msd, breaks = "FD")</span></span>
<span id="cb36-14"><a href="simulations.html#cb36-14" tabindex="-1"></a><span class="do">## hist(<a href="helpers-functions.html#ball.deviance">ball.deviance</a>(2,0.5,X)$msd, breaks = "FD")</span></span></code></pre></div>
<p>Next, we run the MCMC.</p>
<div class="sourceCode" id="cb37"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb37-1"><a href="simulations.html#cb37-1" tabindex="-1"></a><span class="do">## Run the MCMC</span></span>
<span id="cb37-2"><a href="simulations.html#cb37-2" tabindex="-1"></a>Nmc <span class="ot"><-</span> <span class="fl">1e3</span> <span class="sc">*</span> <span class="dv">5</span></span>
<span id="cb37-3"><a href="simulations.html#cb37-3" tabindex="-1"></a>Nburn <span class="ot"><-</span> <span class="dv">500</span></span>
<span id="cb37-4"><a href="simulations.html#cb37-4" tabindex="-1"></a></span>
<span id="cb37-5"><a href="simulations.html#cb37-5" tabindex="-1"></a>Nmc <span class="ot"><-</span> <span class="dv">20</span></span>
<span id="cb37-6"><a href="simulations.html#cb37-6" tabindex="-1"></a>Nburn <span class="ot"><-</span> <span class="dv">10</span></span>
<span id="cb37-7"><a href="simulations.html#cb37-7" tabindex="-1"></a></span>
<span id="cb37-8"><a href="simulations.html#cb37-8" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">123</span>)</span>
<span id="cb37-9"><a href="simulations.html#cb37-9" tabindex="-1"></a>Gibbs.res <span class="ot"><-</span> <a href="gibbs-sampler.html#run.Gibbs.fast">run.Gibbs.fast</a>(<span class="at">ylist =</span> datobj<span class="sc">$</span>ylist,</span>
<span id="cb37-10"><a href="simulations.html#cb37-10" tabindex="-1"></a> <span class="at">countslist =</span> datobj<span class="sc">$</span>countslist,</span>
<span id="cb37-11"><a href="simulations.html#cb37-11" tabindex="-1"></a> <span class="at">numclust =</span> <span class="dv">2</span>,</span>
<span id="cb37-12"><a href="simulations.html#cb37-12" tabindex="-1"></a> <span class="at">Nmc =</span> Nmc, <span class="at">Nburn =</span> Nburn,</span>
<span id="cb37-13"><a href="simulations.html#cb37-13" tabindex="-1"></a> <span class="at">gg =</span> <span class="fl">0.1</span>, </span>
<span id="cb37-14"><a href="simulations.html#cb37-14" tabindex="-1"></a> <span class="at">X =</span> <span class="fu">t</span>(datobj<span class="sc">$</span>X),</span>
<span id="cb37-15"><a href="simulations.html#cb37-15" tabindex="-1"></a> <span class="at">Cbox =</span> datobj<span class="sc">$</span>Cbox, <span class="at">verbose =</span> <span class="cn">TRUE</span>)</span>
<span id="cb37-16"><a href="simulations.html#cb37-16" tabindex="-1"></a></span>
<span id="cb37-17"><a href="simulations.html#cb37-17" tabindex="-1"></a><span class="do">## Save the results</span></span>
<span id="cb37-18"><a href="simulations.html#cb37-18" tabindex="-1"></a><span class="fu">saveRDS</span>(Gibbs.res,</span>
<span id="cb37-19"><a href="simulations.html#cb37-19" tabindex="-1"></a> <span class="fu">file.path</span>(<span class="st">"~/repos/flowcut/inst/output"</span>,</span>
<span id="cb37-20"><a href="simulations.html#cb37-20" tabindex="-1"></a> <span class="fu">paste0</span>(<span class="st">"isignal-"</span>, isignal, <span class="st">"-gibbs.RDS"</span>)))</span></code></pre></div>
<div id="simulation-helpers" class="section level2 hasAnchor" number="6.1">
<h2>
<span class="header-section-number">6.1</span> Simulation helpers<a href="simulations.html#simulation-helpers" class="anchor-section" aria-label="Anchor link to header"></a>
</h2>
<p>Here is a helper function for obtaining the membership probabilities of a new
dataset |ynew| given GMM model |gmm_model|.</p>
<div class="sourceCode" id="cb38"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb38-1"><a href="simulations.html#cb38-1" tabindex="-1"></a><span class="co">#' Get the membership probabilities of a new dataset |ynew| given GMM model |gmm_model|.</span></span>
<span id="cb38-2"><a href="simulations.html#cb38-2" tabindex="-1"></a><span class="co">#'</span></span>
<span id="cb38-3"><a href="simulations.html#cb38-3" tabindex="-1"></a><span class="co">#' @param gmm_model from mclust or me.weighted</span></span>
<span id="cb38-4"><a href="simulations.html#cb38-4" tabindex="-1"></a><span class="co">#' @param ynew new data to get predicted posterior membership for.</span></span>
<span id="cb38-5"><a href="simulations.html#cb38-5" tabindex="-1"></a><span class="co">#'</span></span>
<span id="cb38-6"><a href="simulations.html#cb38-6" tabindex="-1"></a><span class="co">#' @return estimated posterior probabilities from an estimated GMM model</span></span>
<span id="cb38-7"><a href="simulations.html#cb38-7" tabindex="-1"></a><span id="get_posterior_probs">get_posterior_probs</span> <span class="ot"><-</span> <span class="cf">function</span>(gmm_model, ynew){</span>
<span id="cb38-8"><a href="simulations.html#cb38-8" tabindex="-1"></a> <span class="fu">stopifnot</span>(<span class="fu">ncol</span>(ynew) <span class="sc">==</span> <span class="dv">1</span>)</span>
<span id="cb38-9"><a href="simulations.html#cb38-9" tabindex="-1"></a> numclust <span class="ot">=</span> <span class="fu">length</span>(gmm_model<span class="sc">$</span>parameters<span class="sc">$</span>mean)</span>
<span id="cb38-10"><a href="simulations.html#cb38-10" tabindex="-1"></a> mn <span class="ot">=</span> gmm_model<span class="sc">$</span>parameters<span class="sc">$</span>mean</span>
<span id="cb38-11"><a href="simulations.html#cb38-11" tabindex="-1"></a> sd <span class="ot">=</span> gmm_model<span class="sc">$</span>parameters<span class="sc">$</span>variance<span class="sc">$</span>sigmasq <span class="sc">%>%</span> <span class="fu">sqrt</span>()</span>
<span id="cb38-12"><a href="simulations.html#cb38-12" tabindex="-1"></a> pro <span class="ot">=</span> gmm_model<span class="sc">$</span>parameters<span class="sc">$</span>pro</span>
<span id="cb38-13"><a href="simulations.html#cb38-13" tabindex="-1"></a> post_prob <span class="ot">=</span> <span class="fu">lapply</span>(<span class="dv">1</span><span class="sc">:</span>numclust, <span class="cf">function</span>(iclust){</span>
<span id="cb38-14"><a href="simulations.html#cb38-14" tabindex="-1"></a> weighted_d1 <span class="ot">=</span> <span class="fu">dnorm</span>(ynew, <span class="at">mean =</span> mn[iclust], <span class="at">sd =</span> sd[iclust]) <span class="sc">*</span> pro[iclust]</span>
<span id="cb38-15"><a href="simulations.html#cb38-15" tabindex="-1"></a> }) <span class="sc">%>%</span> <span class="fu">do.call</span>(cbind, .)</span>
<span id="cb38-16"><a href="simulations.html#cb38-16" tabindex="-1"></a> post_prob <span class="ot">=</span> post_prob<span class="sc">/</span><span class="fu">rowSums</span>(post_prob)</span>
<span id="cb38-17"><a href="simulations.html#cb38-17" tabindex="-1"></a></span>
<span id="cb38-18"><a href="simulations.html#cb38-18" tabindex="-1"></a> <span class="do">## Give up on posterior probability calculation and make all things 1/2, when</span></span>
<span id="cb38-19"><a href="simulations.html#cb38-19" tabindex="-1"></a> <span class="do">## this (rarely) happens.</span></span>
<span id="cb38-20"><a href="simulations.html#cb38-20" tabindex="-1"></a> <span class="cf">if</span>(<span class="fu">any</span>(<span class="fu">is.nan</span>(post_prob))) post_prob <span class="ot">=</span> <span class="fu">matrix</span>(<span class="dv">1</span><span class="sc">/</span><span class="dv">2</span>, <span class="at">nrow=</span><span class="fu">nrow</span>(post_prob), <span class="at">ncol=</span><span class="dv">2</span>)</span>
<span id="cb38-21"><a href="simulations.html#cb38-21" tabindex="-1"></a></span>
<span id="cb38-22"><a href="simulations.html#cb38-22" tabindex="-1"></a> <span class="fu">return</span>(post_prob)</span>
<span id="cb38-23"><a href="simulations.html#cb38-23" tabindex="-1"></a>}</span></code></pre></div>
<p>Also, here is a helper to split the data into (1) censored and (2) non-censored
(“inner”) particles; used in <code><a href="simulations.html#preimpute">preimpute</a>()</code>.</p>
<div class="sourceCode" id="cb39"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb39-1"><a href="simulations.html#cb39-1" tabindex="-1"></a><span class="co">#' A helper to split the data into (1) censored and (2) non-censored ("inner") particles.</span></span>
<span id="cb39-2"><a href="simulations.html#cb39-2" tabindex="-1"></a><span class="co">#'</span></span>
<span id="cb39-3"><a href="simulations.html#cb39-3" tabindex="-1"></a><span class="co">#' @param datobj List containing ylist, countslist, X, and Cbox.</span></span>
<span id="cb39-4"><a href="simulations.html#cb39-4" tabindex="-1"></a><span class="co">#'</span></span>
<span id="cb39-5"><a href="simulations.html#cb39-5" tabindex="-1"></a><span class="co">#' @return List of particles on /and/ inside censor boundary. And the directions</span></span>
<span id="cb39-6"><a href="simulations.html#cb39-6" tabindex="-1"></a><span class="co">#' in which they were censored.</span></span>
<span id="cb39-7"><a href="simulations.html#cb39-7" tabindex="-1"></a><span class="co">#'</span></span>
<span id="cb39-8"><a href="simulations.html#cb39-8" tabindex="-1"></a><span class="co">#' @export</span></span>
<span id="cb39-9"><a href="simulations.html#cb39-9" tabindex="-1"></a><span id="split_data">split_data</span> <span class="ot"><-</span> <span class="cf">function</span>(datobj){</span>
<span id="cb39-10"><a href="simulations.html#cb39-10" tabindex="-1"></a> <span class="do">## At each time point, set aside censored points.</span></span>
<span id="cb39-11"><a href="simulations.html#cb39-11" tabindex="-1"></a> censor.direction.full.list <span class="ot"><-</span> <span class="fu">lapply</span>(datobj<span class="sc">$</span>ylist,</span>
<span id="cb39-12"><a href="simulations.html#cb39-12" tabindex="-1"></a> <span class="cf">function</span>(x){</span>
<span id="cb39-13"><a href="simulations.html#cb39-13" tabindex="-1"></a> flowcut<span class="sc">:::</span><a href="helpers-functions.html#censorIndicator">censorIndicator</a>(x[,<span class="dv">1</span>, <span class="at">drop=</span><span class="cn">FALSE</span>],</span>
<span id="cb39-14"><a href="simulations.html#cb39-14" tabindex="-1"></a> datobj<span class="sc">$</span>Cbox[<span class="dv">1</span>,,<span class="at">drop=</span><span class="cn">FALSE</span>])})</span>
<span id="cb39-15"><a href="simulations.html#cb39-15" tabindex="-1"></a> censor.which.list <span class="ot">=</span> censor.direction.full.list <span class="sc">%>%</span></span>
<span id="cb39-16"><a href="simulations.html#cb39-16" tabindex="-1"></a> <span class="fu">lapply</span>(<span class="cf">function</span>(x) <span class="fu">apply</span>(x,<span class="dv">1</span>, <span class="cf">function</span>(xx){ <span class="fu">sum</span>(<span class="fu">abs</span>(xx)) <span class="sc">></span> <span class="dv">0</span> })) <span class="sc">%>%</span></span>
<span id="cb39-17"><a href="simulations.html#cb39-17" tabindex="-1"></a> <span class="fu">lapply</span>(which)</span>
<span id="cb39-18"><a href="simulations.html#cb39-18" tabindex="-1"></a> censored_ylist <span class="ot"><-</span> <span class="fu">mapply</span>(<span class="cf">function</span>(y, censor_which){y[censor_which,,drop<span class="ot">=</span><span class="cn">FALSE</span>]},</span>
<span id="cb39-19"><a href="simulations.html#cb39-19" tabindex="-1"></a> datobj<span class="sc">$</span>ylist, censor.which.list, <span class="at">SIMPLIFY =</span> <span class="cn">FALSE</span>)</span>
<span id="cb39-20"><a href="simulations.html#cb39-20" tabindex="-1"></a> censored_direction <span class="ot"><-</span> <span class="fu">mapply</span>(<span class="cf">function</span>(censor_direction, censor_which){</span>
<span id="cb39-21"><a href="simulations.html#cb39-21" tabindex="-1"></a> censor_direction[censor_which,,drop<span class="ot">=</span><span class="cn">FALSE</span>]</span>
<span id="cb39-22"><a href="simulations.html#cb39-22" tabindex="-1"></a> }, censor.direction.full.list, censor.which.list, <span class="at">SIMPLIFY =</span> <span class="cn">FALSE</span>)</span>
<span id="cb39-23"><a href="simulations.html#cb39-23" tabindex="-1"></a></span>
<span id="cb39-24"><a href="simulations.html#cb39-24" tabindex="-1"></a> <span class="do">## here are data</span></span>
<span id="cb39-25"><a href="simulations.html#cb39-25" tabindex="-1"></a> inner_ylist <span class="ot"><-</span> <span class="fu">mapply</span>(<span class="cf">function</span>(y, censor_which){</span>
<span id="cb39-26"><a href="simulations.html#cb39-26" tabindex="-1"></a> <span class="cf">if</span>(<span class="fu">length</span>(censor_which) <span class="sc">></span> <span class="dv">0</span>) <span class="fu">return</span>(y[<span class="sc">-</span>censor_which,,<span class="at">drop=</span><span class="cn">FALSE</span>])</span>
<span id="cb39-27"><a href="simulations.html#cb39-27" tabindex="-1"></a> <span class="cf">if</span>(<span class="sc">!</span><span class="fu">length</span>(censor_which) <span class="sc">></span> <span class="dv">0</span>) <span class="fu">return</span>(y)</span>
<span id="cb39-28"><a href="simulations.html#cb39-28" tabindex="-1"></a> }, datobj<span class="sc">$</span>ylist, censor.which.list, <span class="at">SIMPLIFY =</span> <span class="cn">FALSE</span>)</span>
<span id="cb39-29"><a href="simulations.html#cb39-29" tabindex="-1"></a> inner_countslist <span class="ot"><-</span> <span class="fu">mapply</span>(<span class="cf">function</span>(counts, censor_which){</span>
<span id="cb39-30"><a href="simulations.html#cb39-30" tabindex="-1"></a> <span class="cf">if</span>(<span class="fu">length</span>(censor_which) <span class="sc">></span> <span class="dv">0</span>) <span class="fu">return</span>(counts[<span class="sc">-</span>censor_which])</span>
<span id="cb39-31"><a href="simulations.html#cb39-31" tabindex="-1"></a> <span class="cf">if</span>(<span class="sc">!</span><span class="fu">length</span>(censor_which) <span class="sc">></span> <span class="dv">0</span>) <span class="fu">return</span>(counts)</span>
<span id="cb39-32"><a href="simulations.html#cb39-32" tabindex="-1"></a> }, datobj<span class="sc">$</span>countslist, censor.which.list, <span class="at">SIMPLIFY =</span> <span class="cn">FALSE</span>)</span>
<span id="cb39-33"><a href="simulations.html#cb39-33" tabindex="-1"></a></span>
<span id="cb39-34"><a href="simulations.html#cb39-34" tabindex="-1"></a> <span class="fu">return</span>(<span class="fu">list</span>(<span class="at">censored_ylist =</span> censored_ylist,</span>
<span id="cb39-35"><a href="simulations.html#cb39-35" tabindex="-1"></a> <span class="at">censored_direction =</span> censored_direction,</span>
<span id="cb39-36"><a href="simulations.html#cb39-36" tabindex="-1"></a> <span class="at">inner_ylist =</span> inner_ylist,</span>
<span id="cb39-37"><a href="simulations.html#cb39-37" tabindex="-1"></a> <span class="at">inner_countslist =</span> inner_countslist,</span>
<span id="cb39-38"><a href="simulations.html#cb39-38" tabindex="-1"></a> <span class="at">censor.which.list =</span> censor.which.list</span>
<span id="cb39-39"><a href="simulations.html#cb39-39" tabindex="-1"></a> ))</span>
<span id="cb39-40"><a href="simulations.html#cb39-40" tabindex="-1"></a>}</span></code></pre></div>
<p>Here is a function that will pre-impute data for a 2-cluster 1-dimensional
dataset; used in <code><a href="simulations.html#preimpute">preimpute</a>()</code>.</p>
<div class="sourceCode" id="cb40"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb40-1"><a href="simulations.html#cb40-1" tabindex="-1"></a><span class="co">#' At each time point, we will (1) set aside the censored points, and (2) estimate</span></span>
<span id="cb40-2"><a href="simulations.html#cb40-2" tabindex="-1"></a><span class="co">#' a 2-component GMM (2) take the censored points, and "impute" them according to those</span></span>
<span id="cb40-3"><a href="simulations.html#cb40-3" tabindex="-1"></a><span class="co">#' mixtures. (After that, we'll (3) fit flowmix on the resulting imputed data.)</span></span>
<span id="cb40-4"><a href="simulations.html#cb40-4" tabindex="-1"></a><span class="co">#'</span></span>
<span id="cb40-5"><a href="simulations.html#cb40-5" tabindex="-1"></a><span class="co">#' This is only designed for 1-dimensional data with two clusters.</span></span>
<span id="cb40-6"><a href="simulations.html#cb40-6" tabindex="-1"></a><span class="co">#'</span></span>
<span id="cb40-7"><a href="simulations.html#cb40-7" tabindex="-1"></a><span class="co">#' @param datobj List with ylist (particles), countslist, X, and Cbox.</span></span>
<span id="cb40-8"><a href="simulations.html#cb40-8" tabindex="-1"></a><span class="co">#'</span></span>
<span id="cb40-9"><a href="simulations.html#cb40-9" tabindex="-1"></a><span class="co">#' @return datobj with imputed ylist.</span></span>
<span id="cb40-10"><a href="simulations.html#cb40-10" tabindex="-1"></a><span class="co">#'</span></span>
<span id="cb40-11"><a href="simulations.html#cb40-11" tabindex="-1"></a><span class="co">#' @export</span></span>
<span id="cb40-12"><a href="simulations.html#cb40-12" tabindex="-1"></a><span id="preimpute">preimpute</span> <span class="ot"><-</span> <span class="cf">function</span>(datobj){</span>
<span id="cb40-13"><a href="simulations.html#cb40-13" tabindex="-1"></a></span>
<span id="cb40-14"><a href="simulations.html#cb40-14" tabindex="-1"></a> <span class="do">## Setup</span></span>
<span id="cb40-15"><a href="simulations.html#cb40-15" tabindex="-1"></a> <span class="fu">stopifnot</span>(<span class="fu">ncol</span>(datobj<span class="sc">$</span>ylist[[<span class="dv">1</span>]]) <span class="sc">==</span> <span class="dv">1</span>)</span>
<span id="cb40-16"><a href="simulations.html#cb40-16" tabindex="-1"></a> <span class="fu">stopifnot</span>(<span class="st">"Cbox"</span> <span class="sc">%in%</span> <span class="fu">names</span>(datobj))</span>
<span id="cb40-17"><a href="simulations.html#cb40-17" tabindex="-1"></a></span>
<span id="cb40-18"><a href="simulations.html#cb40-18" tabindex="-1"></a> <span class="do">## Split the data</span></span>
<span id="cb40-19"><a href="simulations.html#cb40-19" tabindex="-1"></a> splitres <span class="ot">=</span> <a href="simulations.html#split_data">split_data</a>(datobj)</span>
<span id="cb40-20"><a href="simulations.html#cb40-20" tabindex="-1"></a> inner_ylist <span class="ot">=</span> splitres<span class="sc">$</span>inner_ylist</span>
<span id="cb40-21"><a href="simulations.html#cb40-21" tabindex="-1"></a> inner_countslist <span class="ot">=</span> splitres<span class="sc">$</span>inner_countslist</span>
<span id="cb40-22"><a href="simulations.html#cb40-22" tabindex="-1"></a> censored_ylist <span class="ot">=</span> splitres<span class="sc">$</span>censored_ylist</span>
<span id="cb40-23"><a href="simulations.html#cb40-23" tabindex="-1"></a> censored_direction <span class="ot">=</span> splitres<span class="sc">$</span>censored_direction</span>
<span id="cb40-24"><a href="simulations.html#cb40-24" tabindex="-1"></a> censor.which.list <span class="ot">=</span> splitres<span class="sc">$</span>censor.which.list</span>
<span id="cb40-25"><a href="simulations.html#cb40-25" tabindex="-1"></a></span>
<span id="cb40-26"><a href="simulations.html#cb40-26" tabindex="-1"></a> <span class="do">## What happens when the "inner" data is empty-ish at some times? We sample</span></span>
<span id="cb40-27"><a href="simulations.html#cb40-27" tabindex="-1"></a> <span class="do">## some points from the rest of the data, and fill the empty-ish cytograms in</span></span>
<span id="cb40-28"><a href="simulations.html#cb40-28" tabindex="-1"></a> <span class="do">## so that there are at least 5 particles in each cytogram.</span></span>
<span id="cb40-29"><a href="simulations.html#cb40-29" tabindex="-1"></a> inner_ntlist <span class="ot">=</span> <span class="fu">sapply</span>(inner_ylist, nrow)</span>
<span id="cb40-30"><a href="simulations.html#cb40-30" tabindex="-1"></a> min_num_particles <span class="ot">=</span> <span class="dv">5</span></span>
<span id="cb40-31"><a href="simulations.html#cb40-31" tabindex="-1"></a> times_empty <span class="ot">=</span> <span class="fu">which</span>(inner_ntlist <span class="sc"><</span> min_num_particles)</span>
<span id="cb40-32"><a href="simulations.html#cb40-32" tabindex="-1"></a> collapsed_ylist <span class="ot">=</span></span>
<span id="cb40-33"><a href="simulations.html#cb40-33" tabindex="-1"></a> inner_ylist[<span class="sc">-</span>times_empty] <span class="sc">%>%</span></span>
<span id="cb40-34"><a href="simulations.html#cb40-34" tabindex="-1"></a> <span class="fu">lapply</span>(<span class="cf">function</span>(y){<span class="fu">sample</span>(y, <span class="at">size=</span><span class="fu">ceiling</span>(<span class="fu">nrow</span>(y)<span class="sc">/</span><span class="dv">3</span>), <span class="at">replace=</span><span class="cn">FALSE</span>)}) <span class="sc">%>%</span> </span>
<span id="cb40-35"><a href="simulations.html#cb40-35" tabindex="-1"></a> <span class="fu">Reduce</span>(c, .)</span>
<span id="cb40-36"><a href="simulations.html#cb40-36" tabindex="-1"></a></span>
<span id="cb40-37"><a href="simulations.html#cb40-37" tabindex="-1"></a> <span class="cf">if</span>(<span class="fu">length</span>(times_empty) <span class="sc">></span> <span class="dv">0</span>){</span>
<span id="cb40-38"><a href="simulations.html#cb40-38" tabindex="-1"></a> <span class="cf">for</span>(tt_empty <span class="cf">in</span> times_empty){</span>
<span id="cb40-39"><a href="simulations.html#cb40-39" tabindex="-1"></a> nt <span class="ot">=</span> <span class="fu">length</span>(inner_ylist[[tt_empty]])</span>
<span id="cb40-40"><a href="simulations.html#cb40-40" tabindex="-1"></a> extra_points <span class="ot">=</span> <span class="fu">sample</span>(collapsed_ylist, min_num_particles <span class="sc">-</span> nt) <span class="sc">%>%</span> <span class="fu">cbind</span>()</span>
<span id="cb40-41"><a href="simulations.html#cb40-41" tabindex="-1"></a> inner_ylist[[tt_empty]] <span class="ot"><-</span> <span class="fu">rbind</span>(inner_ylist[[tt_empty]], extra_points)</span>
<span id="cb40-42"><a href="simulations.html#cb40-42" tabindex="-1"></a> inner_countslist[[tt_empty]] <span class="ot"><-</span> <span class="fu">c</span>(inner_countslist[[tt_empty]], <span class="fu">rep</span>(<span class="dv">1</span>, <span class="fu">length</span>(extra_points)))</span>
<span id="cb40-43"><a href="simulations.html#cb40-43" tabindex="-1"></a> }</span>
<span id="cb40-44"><a href="simulations.html#cb40-44" tabindex="-1"></a> }</span>
<span id="cb40-45"><a href="simulations.html#cb40-45" tabindex="-1"></a> <span class="fu">stopifnot</span>(<span class="fu">all</span>(<span class="fu">sapply</span>(inner_ylist, nrow)<span class="sc">>=</span>min_num_particles))</span>
<span id="cb40-46"><a href="simulations.html#cb40-46" tabindex="-1"></a></span>
<span id="cb40-47"><a href="simulations.html#cb40-47" tabindex="-1"></a> <span class="do">## Fit 2-mixture Gaussian at each time point.</span></span>
<span id="cb40-48"><a href="simulations.html#cb40-48" tabindex="-1"></a> gmm_model_list <span class="ot"><-</span> <span class="fu">mapply</span>(<span class="cf">function</span>(y, counts){</span>
<span id="cb40-49"><a href="simulations.html#cb40-49" tabindex="-1"></a></span>
<span id="cb40-50"><a href="simulations.html#cb40-50" tabindex="-1"></a> <span class="do">## z is just a random initialization of the memberships of the points.</span></span>
<span id="cb40-51"><a href="simulations.html#cb40-51" tabindex="-1"></a> z <span class="ot">=</span> mclust<span class="sc">::</span><span class="fu">unmap</span>(<span class="fu">sample</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>, <span class="at">size =</span> <span class="fu">length</span>(y), <span class="at">replace=</span><span class="cn">TRUE</span>))</span>
<span id="cb40-52"><a href="simulations.html#cb40-52" tabindex="-1"></a></span>
<span id="cb40-53"><a href="simulations.html#cb40-53" tabindex="-1"></a> <span class="do">## Fit a Gaussian mixture model for weighted data.</span></span>
<span id="cb40-54"><a href="simulations.html#cb40-54" tabindex="-1"></a> <span class="do">##res = mclust::me.weighted(as.numeric(y), modelName = "V", weights = counts, z = z)</span></span>
<span id="cb40-55"><a href="simulations.html#cb40-55" tabindex="-1"></a> res <span class="ot">=</span> mclust<span class="sc">::</span><span class="fu">Mclust</span>(<span class="fu">as.numeric</span>(y), <span class="at">G=</span><span class="dv">2</span>, <span class="at">modelName =</span> <span class="st">"V"</span>, <span class="at">verbose =</span> <span class="cn">FALSE</span>, <span class="at">warn =</span> <span class="cn">FALSE</span>)<span class="do">##, weights = counts, z = z)</span></span>
<span id="cb40-56"><a href="simulations.html#cb40-56" tabindex="-1"></a> <span class="cf">if</span>(<span class="fu">is.null</span>(res)){</span>
<span id="cb40-57"><a href="simulations.html#cb40-57" tabindex="-1"></a> res <span class="ot">=</span> mclust<span class="sc">::</span><span class="fu">Mclust</span>(<span class="fu">as.numeric</span>(y), <span class="at">G=</span><span class="dv">2</span>, <span class="at">modelName =</span> <span class="st">"E"</span>, <span class="at">verbose =</span> <span class="cn">FALSE</span>, <span class="at">warn =</span> <span class="cn">FALSE</span>)<span class="do">##, weights = counts, z = z)</span></span>
<span id="cb40-58"><a href="simulations.html#cb40-58" tabindex="-1"></a> res<span class="sc">$</span>parameters<span class="sc">$</span>variance<span class="sc">$</span>sigmasq <span class="ot">=</span> <span class="fu">rep</span>(res<span class="sc">$</span>parameters<span class="sc">$</span>variance<span class="sc">$</span>sigmasq,<span class="dv">2</span>)</span>
<span id="cb40-59"><a href="simulations.html#cb40-59" tabindex="-1"></a> }</span>
<span id="cb40-60"><a href="simulations.html#cb40-60" tabindex="-1"></a> assertthat<span class="sc">::</span><span class="fu">assert_that</span>(<span class="sc">!</span><span class="fu">is.null</span>(res))</span>
<span id="cb40-61"><a href="simulations.html#cb40-61" tabindex="-1"></a> <span class="fu">return</span>(res)</span>
<span id="cb40-62"><a href="simulations.html#cb40-62" tabindex="-1"></a> }, inner_ylist, inner_countslist, <span class="at">SIMPLIFY =</span> <span class="cn">FALSE</span>)</span>
<span id="cb40-63"><a href="simulations.html#cb40-63" tabindex="-1"></a></span>
<span id="cb40-64"><a href="simulations.html#cb40-64" tabindex="-1"></a> <span class="do">## Impute the points</span></span>
<span id="cb40-65"><a href="simulations.html#cb40-65" tabindex="-1"></a> imputed_censored_ylist <span class="ot">=</span> <span class="fu">mapply</span>(<span class="cf">function</span>(gmm_model, ynew, dirs){</span>
<span id="cb40-66"><a href="simulations.html#cb40-66" tabindex="-1"></a></span>
<span id="cb40-67"><a href="simulations.html#cb40-67" tabindex="-1"></a> <span class="do">## Impute based on 2-cluster model</span></span>
<span id="cb40-68"><a href="simulations.html#cb40-68" tabindex="-1"></a> <span class="cf">if</span>(<span class="fu">nrow</span>(ynew) <span class="sc">==</span> <span class="dv">0</span>) <span class="fu">return</span>(ynew)</span>
<span id="cb40-69"><a href="simulations.html#cb40-69" tabindex="-1"></a> ynew_imputed <span class="ot">=</span> ynew</span>
<span id="cb40-70"><a href="simulations.html#cb40-70" tabindex="-1"></a> post_probs <span class="ot">=</span> <a href="simulations.html#get_posterior_probs">get_posterior_probs</a>(gmm_model, ynew)</span>
<span id="cb40-71"><a href="simulations.html#cb40-71" tabindex="-1"></a> new_mems <span class="ot">=</span> post_probs <span class="sc">%>%</span> <span class="fu">apply</span>(<span class="dv">1</span>, <span class="cf">function</span>(ps){ <span class="fu">sample</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>, <span class="dv">1</span>, <span class="at">prob=</span>ps) })</span>
<span id="cb40-72"><a href="simulations.html#cb40-72" tabindex="-1"></a></span>
<span id="cb40-73"><a href="simulations.html#cb40-73" tabindex="-1"></a> <span class="do">## Get the gaussian means and SDs</span></span>
<span id="cb40-74"><a href="simulations.html#cb40-74" tabindex="-1"></a> means <span class="ot">=</span> gmm_model<span class="sc">$</span>parameters<span class="sc">$</span>mean</span>
<span id="cb40-75"><a href="simulations.html#cb40-75" tabindex="-1"></a> sds <span class="ot">=</span> gmm_model<span class="sc">$</span>parameters<span class="sc">$</span>variance<span class="sc">$</span>sigmasq <span class="sc">%>%</span> <span class="fu">sqrt</span>()</span>
<span id="cb40-76"><a href="simulations.html#cb40-76" tabindex="-1"></a></span>
<span id="cb40-77"><a href="simulations.html#cb40-77" tabindex="-1"></a> <span class="do">## Information for imputing each points</span></span>
<span id="cb40-78"><a href="simulations.html#cb40-78" tabindex="-1"></a> impute_mat <span class="ot">=</span> <span class="fu">tibble</span>(<span class="at">new_mems =</span> new_mems, <span class="at">dir =</span> dirs)</span>
<span id="cb40-79"><a href="simulations.html#cb40-79" tabindex="-1"></a></span>
<span id="cb40-80"><a href="simulations.html#cb40-80" tabindex="-1"></a> <span class="do">## Impute the points</span></span>
<span id="cb40-81"><a href="simulations.html#cb40-81" tabindex="-1"></a> <span class="cf">for</span>(irow <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="fu">nrow</span>(impute_mat)){</span>
<span id="cb40-82"><a href="simulations.html#cb40-82" tabindex="-1"></a> iclust <span class="ot">=</span> impute_mat<span class="sc">$</span>new_mems[irow]</span>
<span id="cb40-83"><a href="simulations.html#cb40-83" tabindex="-1"></a> <span class="cf">if</span>(impute_mat<span class="sc">$</span>dir[irow] <span class="sc">==</span> <span class="dv">1</span>){</span>
<span id="cb40-84"><a href="simulations.html#cb40-84" tabindex="-1"></a> imputed_point <span class="ot">=</span> truncnorm<span class="sc">::</span><span class="fu">rtruncnorm</span>(<span class="dv">1</span>, <span class="at">a =</span> datobj<span class="sc">$</span>Cbox[<span class="dv">1</span>,<span class="dv">2</span>], <span class="at">mean =</span> means[iclust], <span class="at">sd =</span> sds[iclust])</span>
<span id="cb40-85"><a href="simulations.html#cb40-85" tabindex="-1"></a> }</span>
<span id="cb40-86"><a href="simulations.html#cb40-86" tabindex="-1"></a> <span class="cf">if</span>(impute_mat<span class="sc">$</span>dir[irow] <span class="sc">==</span> <span class="sc">-</span><span class="dv">1</span>){</span>
<span id="cb40-87"><a href="simulations.html#cb40-87" tabindex="-1"></a> imputed_point <span class="ot">=</span> truncnorm<span class="sc">::</span><span class="fu">rtruncnorm</span>(<span class="dv">1</span>, <span class="at">b =</span> datobj<span class="sc">$</span>Cbox[<span class="dv">1</span>,<span class="dv">1</span>], <span class="at">mean =</span> means[iclust], <span class="at">sd =</span> sds[iclust])</span>
<span id="cb40-88"><a href="simulations.html#cb40-88" tabindex="-1"></a> }</span>
<span id="cb40-89"><a href="simulations.html#cb40-89" tabindex="-1"></a> ynew_imputed[irow] <span class="ot">=</span> imputed_point</span>
<span id="cb40-90"><a href="simulations.html#cb40-90" tabindex="-1"></a> }</span>
<span id="cb40-91"><a href="simulations.html#cb40-91" tabindex="-1"></a> <span class="fu">return</span>(ynew_imputed)</span>
<span id="cb40-92"><a href="simulations.html#cb40-92" tabindex="-1"></a> }, gmm_model_list, censored_ylist, censored_direction, <span class="at">SIMPLIFY =</span> <span class="cn">FALSE</span>)</span>
<span id="cb40-93"><a href="simulations.html#cb40-93" tabindex="-1"></a> <span class="fu">stopifnot</span>(<span class="fu">all</span>(<span class="fu">sapply</span>(imputed_censored_ylist, nrow) <span class="sc">==</span> <span class="fu">sapply</span>(censored_ylist, nrow)))</span>
<span id="cb40-94"><a href="simulations.html#cb40-94" tabindex="-1"></a></span>
<span id="cb40-95"><a href="simulations.html#cb40-95" tabindex="-1"></a> <span class="do">## Return the datobj (ylist, countslist, and flowmix).</span></span>
<span id="cb40-96"><a href="simulations.html#cb40-96" tabindex="-1"></a> TT <span class="ot">=</span> <span class="fu">length</span>(datobj<span class="sc">$</span>ylist)</span>
<span id="cb40-97"><a href="simulations.html#cb40-97" tabindex="-1"></a> new_ylist <span class="ot">=</span> datobj<span class="sc">$</span>ylist</span>
<span id="cb40-98"><a href="simulations.html#cb40-98" tabindex="-1"></a> <span class="cf">for</span>(tt <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span>TT){</span>
<span id="cb40-99"><a href="simulations.html#cb40-99" tabindex="-1"></a> inds <span class="ot">=</span> censor.which.list[[tt]]</span>
<span id="cb40-100"><a href="simulations.html#cb40-100" tabindex="-1"></a> <span class="cf">if</span>(<span class="fu">length</span>(inds) <span class="sc">==</span> <span class="dv">0</span>) <span class="cf">next</span></span>
<span id="cb40-101"><a href="simulations.html#cb40-101" tabindex="-1"></a> ynew <span class="ot">=</span> datobj<span class="sc">$</span>ylist[[tt]]</span>
<span id="cb40-102"><a href="simulations.html#cb40-102" tabindex="-1"></a> ynew[inds] <span class="ot">=</span> imputed_censored_ylist[[tt]]</span>
<span id="cb40-103"><a href="simulations.html#cb40-103" tabindex="-1"></a> new_ylist[[tt]] <span class="ot">=</span> ynew</span>
<span id="cb40-104"><a href="simulations.html#cb40-104" tabindex="-1"></a> }</span>
<span id="cb40-105"><a href="simulations.html#cb40-105" tabindex="-1"></a> <span class="fu">stopifnot</span>(<span class="fu">all</span>(<span class="fu">sapply</span>(new_ylist, nrow) <span class="sc">==</span> <span class="fu">sapply</span>(datobj<span class="sc">$</span>ylist, nrow)))</span>
<span id="cb40-106"><a href="simulations.html#cb40-106" tabindex="-1"></a> new_datobj <span class="ot">=</span> datobj</span>
<span id="cb40-107"><a href="simulations.html#cb40-107" tabindex="-1"></a> new_datobj<span class="sc">$</span>ylist <span class="ot">=</span> new_ylist</span>
<span id="cb40-108"><a href="simulations.html#cb40-108" tabindex="-1"></a> <span class="fu">return</span>(new_datobj)</span>
<span id="cb40-109"><a href="simulations.html#cb40-109" tabindex="-1"></a>}</span></code></pre></div>
</div>
</div>
</section>
</div>
</div>
</div>
<a href="gibbs-sampler.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="real-data-example.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script><script src="libs/gitbook-2.6.7/js/clipboard.min.js"></script><script src="libs/gitbook-2.6.7/js/plugin-search.js"></script><script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script><script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script><script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script><script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script><script src="libs/gitbook-2.6.7/js/plugin-clipboard.js"></script><script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": true,
"twitter": true,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"whatsapp": false,
"all": ["facebook", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": null,
"text": null
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": null,
"search": {
"engine": "fuse",
"options": null
},
"toc": {
"collapse": "subsection"
}
});
});
</script><!-- dynamically load mathjax for compatibility with self-contained --><script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
var src = "true";
if (src === "" || src === "true") src = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.9/latest.js?config=TeX-MML-AM_CHTML";
if (location.protocol !== "file:")
if (/^https?:/.test(src))
src = src.replace(/^https?:/, '');
script.src = src;
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>