<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/confounded-dose-response-effects-of-treatment-adherence-fitting-bayesian-instrumental-variable-models-using-brms/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://rpsychologist.com/adherence-analysis-IV-brms"> R Psychologist - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Something that never ceases to amaze (depress) me, is how extremely common it is to see casual claims in RCTs, that are not part of the randomization. For instance, the relationship between treatment adherence and outcome, or between alliance and outcome, are often analyzed but seldom experimentally manipulated. This is basically observational research disguised as experimental, but without DAGs, instrumental variables, propensity scores, or <em>any</em> other technique used in observational research. Hence, the effects will <em>almost surely</em> be biased in an unknown direction. This is a post I’ve been planning to write for a long time, but put off since the theory and methods are so well covered in, for instance, epidemiology. However, when <code>brms</code> added support for multivariate formulas, I figured I could show how some of the techniques can be implemented in R, and their Bayesian counterparts using <code>brms</code>. So, in this post I do three things:</p>
<ol>
<li>Rant about how quickly clinical psychology researchers forget about <strong>casual inference</strong>, once they start analyzing non-randomized variables, such as the effect of <strong>treatment adherence</strong> (or “compliance”) on treatment outcome in a randomized controlled trial. </li>
<li>Show that common regression methods either, makes absolutely no sense, or result in badly biased estimates of the casual dose-response effect, when a third variable influences both adherence and outcome, i.e. when the dose-response relationship is confounded. While <strong>instrumental variable</strong> methods <em>may</em> allow for unbiased estimates in these scenarios. </li>
<li>Show how <strong>Bayesian intrumental variable</strong> regression can be easily fit using <code>brms</code> new multivariate syntax. And also do a small simulation study to compare the results to <code>lavaan</code>, <code>AER::ivreg</code> and biased <code>lm()</code> models. </li>
</ol>
<h1>Motivating example</h1>
<h2>The effect of treatment adherence (psychotherapy dose-response effects)</h2>
<p>Let’s assume we’ve performed an RCT and compared some psychotherapy condition to some control condition. Patients in these types of trials seldom complete all scheduled sessions, or adhere 100 % to the treatment. It’s rather common for researchers to try to see if there is some relationship between adherence and treatment outcome. Which is a valid question, if there is no added effect of actually “receiving” more of the treatment, then that <em>should</em> be a problem for your theory, or at least some of the components of that theory. However, what usually happens is that researchers somehow forget that the variable “adherence to therapy” is not experimentally manipulated. Unless we have randomized patients to receive 1, 2, …, 10 sessions, or to do 1, 2, …, 10, exposure exercises (and somehow ensured adherence was 100% in all conditions)–then we are now longer in RCT country!</p>
<p><center><br>
<img alt="&quot;Confounding bat country meme&quot;" src="https://i1.wp.com/rpsychologist.com/img/bat-country.gif?w=456" data-recalc-dims="1"></center></p>
<p><em>Never stop to acknowledge you’re doing non-randomized comparisons!</em></p>
<h2>A simple model of confounded adherence effects</h2>
<p>A really simple scenario with one know (or unknown) confounder is shown in the DAG below.<br>
<center><br>
<img alt="&quot;Adherence dose-response DAG&quot;" src="https://i0.wp.com/rpsychologist.com/img/compliance/DAG-compliance.png?w=456" data-recalc-dims="1"><br>
</center></p>
<p>Each arrow represent <em>casual</em> influence of one variable on another. <code>Y</code> is the observed outcome, <code>adherence</code> is often measured by some proxy, like the number of sessions completed–that we assume is a good indicator of treatment “dose”, it could be any other proxy for treatment adherence. So, what the graph is saying, is that treatment allocation (randomization) influences the outcome (Y) via the amount of treatment received (adherence). Thus, unless something went horribly wrong, randomization should be strongly correlated with S, and only influence Y through the treatment received (S). By definition this makes randomization an <em>instrumental variable</em>. However, the problem is that the effect of S on Y is most likely confounded (U). For instance, baseline functioning might be related both to a better prognosis (better outcome) and treatment adherence. It is easy to come up with scenarios were the relationship could be either positive or negative. Higher baseline functioning could be related to a better prognosis, and to better treatment adherence, or, a patient could be too depressed to actually adhere to the treatment. It is also plausible that worse baseline functioning make you more motivated to adhere to your treatment. Whatever the relationship might be, it is clear that the casual effect of treatment adherence will be confounded.</p>
<p>We will deal with three types of scenarios were a variable <span>\(U\)</span> confounds the estimate of treatment adherence:</p>
<ul>
<li><strong>Observed confounder</strong>: <span>\(U\)</span> is an observed variable that is adjusted for.</li>
<li><strong>Residual confounding</strong>: our known confounder <span>\(U\)</span> is measured with error.</li>
<li><strong>Hidden confounding</strong>: <span>\(U\)</span> is either unknown or unmeasured, and not adjusted for.</li>
</ul>
<h1>The data generating model</h1>
<p>First, let’s remind ourselves what is commonly estimates in an RCT is the <em>average treatment effect</em> (ATE), or some related estimate, </p>
<div>$$<br>
\Delta = E[Y_i(C) – Y_i(Tx)].<br>
$$</div>
<p>
Where <span>\(Y_i(C)\)</span> is the would-be outcome for patient <em>i</em> after participating in the control condition, and <span>\(Y_i(Tx)\)</span> the would be outcome for patient <em>i</em> after participating in the treatment condition. Obviously, we can never observe both <span>\(Y_i(C)\)</span> and <span>\(Y_i(Tx)\)</span> for the same patient. But thanks to randomization when can infer the ATE. We also assume that the individual treatment effect <span>\(Y_i(C) – Y_i(Tx)\)</span> is the same for all individuals, given the same level of adherence. </p>
<p>The data generating model (DGM) is, thus, that adherence depends on <span>\(U_i\)</span>, the confounder variable,
</p>
<div>$$ S_i = 7 – f(U_i) + e_{1i}.$$</div>
<p>
Where <span>\(f()\)</span> is just a function that centers and scales the confounding variable, so that the mean adherence is 7, and <span>\(e_i\)</span> is just random variation that influence the relationship between <span>\(U_i\)</span> and <span>\(S_i\)</span>. Moreover, the <em>observed</em> measure of adherence could be noisy measure of a patient’s actual adherence, <span>\(S_i\)</span>, e.g. sessions completed,
</p>
<div>$$ \text{Sessions}_i = S_i + e_{2i}.$$</div>
<p>To generate the outcome, we first simulate <span>\(Y_i(C)\)</span>, and then calculate <span>\(Y_i(Tx)\)</span>,
</p>
<div>$$ Y(Tx)_i = Y(C)_i – \beta_S S_i.$$</div>
<p>
So that <span>\(\beta_S\)</span> is the casual effect of treatment adherence,</p>
<div>$$ E(\Delta_i | S_i) = \beta_s S_i.$$</div>
<p>Lastly, the outcome is generated as,
</p>
<div>$$ Y_i = Y_i(C) [1-R_i(Tx)] + Y_i(Tx) R_i(Tx).$$</div>
<p>
Where <span>\(R_i(Tx)\)</span> is 1 for participants allocated to the treatment condition and 0 otherwise.</p>
<h1>Different ways to answer the same question</h1>
<p>Now, that we have a DGM, let’s generate some fake data and fit different models. We’ll start with a standard OLS regression, the first model would make sense in the absence of any hidden or residual confounding (extremely unlikely). The second model (“naïve OLS”) makes little sense, since it only analyze the treatment group, and as we’ll see, further confounds prognosis with the casual effect of treatment adherence. Sadly, this way of analyzing psychotherapy trials is really common… Anyway, lets generate some fake data. I’ll use a two-level random intercept-only model as a starting point, and assume that the confounder (baseline functioning), is measured without error.</p>
<div>
<pre><span></span><span>library</span><span>(</span>powerlmm<span>)</span>

<span># Skeleton to base data on</span>
sp <span>&lt;-</span> study_parameters<span>(</span>n1 <span>=</span> <span>11</span><span>,</span>
                      n2 <span>=</span> <span>100</span><span>,</span>
                      T_end <span>=</span> <span>10</span><span>,</span>
                      fixed_intercept <span>=</span> <span>10</span><span>,</span>
                      fixed_slope <span>=</span> <span>-.5</span><span>,</span>
                      icc_pre_subject <span>=</span> <span>0.8</span><span>,</span>
                      sigma_error <span>=</span> <span>3</span><span>,</span>
                      cohend <span>=</span> <span>0</span><span>)</span>

<span>## Some fake data, code at the end of post</span>
dpp <span>&lt;-</span> data_generator<span>(</span>update<span>(</span>sp<span>,</span> n2 <span>=</span> <span>5000</span><span>),</span> 
                      confounder <span>=</span> <span>&quot;intercept_subject&quot;</span><span>,</span> 
                      label <span>=</span> <span>&quot;example&quot;</span><span>,</span>
                      beta_s <span>=</span> <span>1</span><span>,</span> 
                      c_scale <span>=</span> <span>3</span><span>,</span>
                      hidden_confounder_sd <span>=</span> <span>0</span><span>,</span>
                      measure_error_sd <span>=</span> <span>0</span><span>)()</span>
</pre>
</div>
<h3>OLS</h3>
<p>First, we fit the two standard OLS models where we are adjusting for pretest scores. </p>
<div>
<pre><span></span><span># lm</span>
<span>## Assume no confounding</span>
OLS <span>&lt;-</span> lm<span>(</span>y <span>~</span> session <span>+</span> pre<span>,</span> data <span>=</span> dpp<span>)</span>
<span>summary</span><span>(</span>OLS<span>)</span>

<span>## Naïve OLS (tx only)</span>
naive <span>&lt;-</span> dpp <span>%>%</span> 
    filter<span>(</span>treatment <span>==</span> <span>1</span><span>)</span> <span>%>%</span> 
    lm<span>(</span>y <span>~</span> session <span>+</span> pre<span>,</span> data <span>=</span> <span>.</span><span>)</span>
<span>summary</span><span>(</span>naive<span>)</span>
</pre>
</div>
<h3>Instrumental variables (IV)</h3>
<p>Now, let’s try to use treatment assignment as an <em>instrument</em> to better uncover the casual effect of adherence. Two-stage least-squares (2SLS) is a common method to estimate an IV model. The point of this post is not to explain IV regression, it’s explained well in standard econometric textbooks (<a rel="nofollow" href="https://www.amazon.com/gp/product/0262232588/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0262232588&linkCode=as2&tag=rpsyc-20&linkId=8b0629e282068c90e6ecc6f093bcf1b4" target="_blank">Wooldridge, 2010</a><img border="0" src="http://ir-na.amazon-adsystem.com/e/ir?t=rpsyc-20&l=am2&o=1&a=0262232588" alt="" width="1" height="1">; <a rel="nofollow" href="https://www.amazon.com/gp/product/130527010X/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=130527010X&linkCode=as2&tag=rpsyc-20&linkId=4941b5cb6ce034f04eed61345e7a656a" target="_blank">Wooldridge, 2015</a><img border="0" src="http://ir-na.amazon-adsystem.com/e/ir?t=rpsyc-20&l=am2&o=1&a=130527010X" alt="" width="1" height="1">). And in the context of psychotherapy research in the articles by <a rel="nofollow" href="https://doi.org/10.1177/0962280208097243" target="_blank">Maracy and Dunn (2011)</a>, or <a rel="nofollow" href="http://doi.wiley.com/10.1002/sim.2891" target="_blank">Dunn and Bentall (2007)</a>. The <em>very</em> short explanation, is that 2SLS is basically to regress <code>session</code> on the instruments, then regress <code>y</code> on the predicted sessions from Step 1. This can be done using <code>lm()</code> to get point estimates, but the standard errors will be highly suspicious. </p>
<div>
<pre><span></span><span># IV</span>
<span>## Two-step manual</span>
step1 <span>&lt;-</span> lm<span>(</span>session <span>~</span> treatment <span>+</span> pre<span>:</span>treatment<span>,</span> data <span>=</span> dpp<span>)</span>
dpp<span>$</span>session_hat <span>&lt;-</span> predict<span>(</span>step1<span>)</span>
IV_ols <span>&lt;-</span> lm<span>(</span>y <span>~</span> session_hat <span>+</span> pre<span>,</span> data <span>=</span> dpp<span>)</span>
<span>summary</span><span>(</span>IV_ols<span>)</span>
</pre>
</div>
<p>However, 2SLS is easier done with <code>ivreg()</code> from the package <code>AER</code>. The same structural model is fit using this code.</p>
<div>
<pre><span></span><span>library</span><span>(</span>AER<span>)</span>
<span>## ivreg (2SLS)</span>
IV <span>&lt;-</span> ivreg<span>(</span>y <span>~</span> session <span>+</span> pre <span>|</span> treatment <span>+</span> treatment<span>:</span>pre<span>,</span> data <span>=</span> dpp<span>)</span>
<span>summary</span><span>(</span>IV<span>)</span>
</pre>
</div>
<p>The model can also be thought of as a special case of a structural equation model, and fit with e.g. <code>lavaan</code>. </p>
<div>
<pre><span></span><span>library</span><span>(</span>lavaan<span>)</span>
l_model <span>&lt;-</span> <span>' session ~ treatment + treatment:pre</span>
<span> y ~ session + pre</span>
<span> session ~~ y '</span>
IV_lavaan <span>&lt;-</span> sem<span>(</span>l_model<span>,</span> data <span>=</span> dpp<span>)</span>
<span>summary</span><span>(</span>IV_lavaan<span>)</span>
</pre>
</div>
<p>Lastly, using the same approach, a Bayesian IV model can be fit using <code>brms</code> (>2.0.0).</p>
<div>
<pre><span></span><span>library</span><span>(</span>brms<span>)</span>
f1 <span>&lt;-</span> bf<span>(</span>session <span>~</span> treatment <span>+</span> treatment<span>:</span>pre<span>)</span>
f2 <span>&lt;-</span> bf<span>(</span>y <span>~</span> session <span>+</span> pre<span>)</span>
IV_brm <span>&lt;-</span> brm<span>(</span>f1 <span>+</span> f2<span>,</span> data <span>=</span> dpp<span>,</span> cores <span>=</span> <span>4</span><span>)</span>

<span>summary</span><span>(</span>IV_brm<span>)</span>
</pre>
</div>
<p>This is allows extending the model a lot using <code>brms</code> highly flexible modelling syntax. Moreover, once you’ve exhausted <code>brms</code> capabilities you can get the <code>stancode()</code> and further customize the model yourself. </p>
<h1>A small simulation study</h1>
<p>Let’s also do a small simulation study, to better show the impact of observed confounding, residual confounding, and hidden confounding. We’ll use the OLS and IV models shown above. For each of the three scenarios 5000 data sets were generated, with 100 patients per condition. The correlations between the variables are shown in the figure below, the actual values are arbitrary chosen, and the resulting bias, and its direction, is not meant to reflect real data, instead the values are meant to highlight the different types of confounding. </p>
<p><img alt="Confounding of treatment adherence effect, different scenarious" title="Confounding of treatment adherence effect, different scenarious" src="https://i1.wp.com/rpsychologist.com/img/compliance/adherence-confounding-setup-1.png?w=456" data-recalc-dims="1"></p>
<p>In the figure, <em>s</em> is the latent adherence variable, <em>session</em> the observed adherence, <em>intercept_subject</em> is the baseline confounder, that is measured with or without error by <em>pre</em>, and <em>y</em> is the outcome.</p>
<h3>Relative bias</h3>
<p>The relative bias of the estimated adherence effect is shown in the figure below.<br>
<img alt="Treatment adherence confounding relative bias, OLS and instrumental variables" title="Treatment adherence confounding relative bias, OLS and instrumental variables" src="https://i0.wp.com/rpsychologist.com/img/compliance/adherence-confounding-relative-bias-1.png?w=456" data-recalc-dims="1"></p>
<p>In the figure, we see for the OLS models, that when there’s:</p>
<ul>
<li><strong>Hidden confounding</strong>:
<ul>
<li>Adjusting for the baseline variable does not help, which is as expected, since it is not related to <em>adherence</em>.</li>
</ul>
</li>
<li><strong>Observed confounder</strong>:
<ul>
<li>The estimates are biased when the confounder is omitted.</li>
<li>However, the effect is attenuated since <em>session</em> is a noisy proxy of adherence. </li>
</ul>
</li>
<li><strong>Residual confounding</strong>:
<ul>
<li>Controlling for the confounder reduces bias, but due to residual confounding estimates are still biased.</li>
</ul>
</li>
</ul>
<p>And the overall pattern is that:</p>
<ul>
<li>the naïve OLS is as bad idea, and is the most affected by bias in all scenarios. We use control groups for a reason, don’t ignore the control group!</li>
<li>IV estimates are unbiased.</li>
</ul>
<p>We see the same results if look at the coverage probabilities of the confidence intervals. </p>
<h3>Coverage probabilities of the CIs</h3>
<p><img alt="Treatment adherence confounding confidence interval coverage, OLS and instrumental variables" title="Treatment adherence confounding confidence interval coverage, OLS and instrumental variables" src="https://i1.wp.com/rpsychologist.com/img/compliance/adherence-confounding-CI-coverage-1.png?w=456" data-recalc-dims="1"></p>
<h2>When less is more–collider bias</h2>
<p>Let’s throw in another example of how things can go wrong, this time by adjusting for a collider. We extend the first DAG, and add another covariate <em>therapeutic alliance</em>. A researcher might think that by controlling for X (alliance), the dose-response relationship will be more clearly revealed. Or they might just do a multivariable regression and think <em>“hey, both therapeutic alliance and adherence probably predict treatment outcome, let’s throw both in and see what p-value we get”</em>. However, the problem is that alliance might neither be a predictor of outcome, nor a confounder of the adherence and outcome relationship, instead it could be a common effect of adherence and a hidden confounder (i.e., a collider). Obviously, this example is made up and <em>alliance</em> is just a placeholder–there’s so much going on during a psychological treatment that it would be easy to come up with other colliders. In the DAG bellow, conditioning on alliance opens up a path between adherence and the hidden confounder, that bias the relationship between adherence and outcome <em>even</em> more.<br>
<center><br>
<img alt="&quot;Adherence dose-response DAG&quot;" src="https://i2.wp.com/rpsychologist.com/img/compliance/DAG-collider-compliance.png?w=456" data-recalc-dims="1"><br>
</center></p>
<p>If we also simulate this model, and assume that <span>\(U_1\)</span> is measured with error, and use the same IV regressions, but add an OLS model that also includes the collider we get these results.<br>
<img alt="Treatment adherence confounding and collider relative bias, OLS and instrumental variables" title="Treatment adherence confounding and collider relative bias, OLS and instrumental variables" src="https://i2.wp.com/rpsychologist.com/img/compliance/adherence-confounding-collider-relative-bias-1.png?w=456" data-recalc-dims="1"></p>
<p>What we see is that:</p>
<ul>
<li>The OLS models that control for the baseline confounder is still biased due to residual confounding, and the naïve OLS is badly biased.</li>
<li>By adjusting for the collider the bias increase, it would even have been better to not adjust for anything.</li>
<li>The IV models are unbiased, and as you see below, their CIs have nominal coverage probabilities as well.</li>
</ul>
<p><img alt="Treatment adherence confounding and collider CI coverage, OLS and instrumental variables" title="Treatment adherence confounding and collider CI coverage, OLS and instrumental variables" src="https://i0.wp.com/rpsychologist.com/img/compliance/adherence-confounding-collider-CI-coverage-1.png?w=456" data-recalc-dims="1"></p>
<h1>Summary</h1>
<p>Lastly, to summarize this post:</p>
<ul>
<li>Don’t do naïve regressions on treatment adherence dose-response effects. It makes no sense even in the absence of confounding. </li>
<li>Measurement error <em>will</em> bias your regression estimates even if you adjust for confounders.</li>
<li>Confounders and colliders are everywhere, make your assumptions explicit. We don’t need more zombie studies claiming XYZ is related to treatment outcome.</li>
<li>IV regression does not automagically solve all problems. But if you think hard about your data it can be a very useful tool in treatment studies. </li>
<li><code>brms</code> new multivariate syntax make it extremely easy to fit Bayesian IV models, that can be extended to much more complicated scenarios than what I covered in this article.</li>
</ul>
<h1>References</h1>
<ul>
<li>Dunn, G., &amp; Bentall, R. (2007). Modelling treatment-effect heterogeneity in randomized controlled trials of complex interventions (psychological treatments). Statistics in Medicine, 26(26), 4719–4745. https://doi.org/10.1002/sim.2891</li>
<li>Mohammad Maracy, &amp; Graham Dunn. (2011). Estimating dose-response effects in psychological treatment trials: the role of instrumental variables. Statistical Methods in Medical Research, 20(3), 191–215. https://doi.org/10.1177/0962280208097243</li>
<li><a rel="nofollow" href="https://www.amazon.com/gp/product/0262232588/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0262232588&linkCode=as2&tag=rpsyc-20&linkId=8b0629e282068c90e6ecc6f093bcf1b4" target="_blank">Wooldridge, J. M. (2010). Econometric analysis of cross section and panel data. MIT press.</a><img border="0" src="http://ir-na.amazon-adsystem.com/e/ir?t=rpsyc-20&l=am2&o=1&a=0262232588" alt="" width="1" height="1"></li>
<li><a rel="nofollow" href="https://www.amazon.com/gp/product/130527010X/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=130527010X&linkCode=as2&tag=rpsyc-20&linkId=4941b5cb6ce034f04eed61345e7a656a" target="_blank">Wooldridge, J. M. (2015). Introductory econometrics: A modern approach. Nelson Education.</a><img border="0" src="http://ir-na.amazon-adsystem.com/e/ir?t=rpsyc-20&l=am2&o=1&a=130527010X" alt="" width="1" height="1"></li>
</ul>
<h1>Code</h1>
<h2>Simulation functions</h2>
<div>
<pre><span></span><span>library</span><span>(</span>dplyr<span>)</span>
<span>library</span><span>(</span>tidyr<span>)</span>
<span>library</span><span>(</span>ggplot2<span>)</span>
<span>library</span><span>(</span>parallel<span>)</span>
<span>library</span><span>(</span>lavaan<span>)</span>
<span>library</span><span>(</span>AER<span>)</span>
<span>library</span><span>(</span>brms<span>)</span>

<span>#' Extract results from lm and ivreg</span>
<span>#' @param x object</span>
<span>#' @param model character; name of model</span>
get_res <span>&lt;-</span> <span>function</span><span>(</span>x<span>,</span> model<span>)</span> <span>{</span>
    est <span>&lt;-</span>  coef<span>(</span>x<span>)</span>
    ind <span>&lt;-</span> <span>names</span><span>(</span>est<span>)</span> <span>==</span> <span>&quot;session&quot;</span>
    est <span>&lt;-</span> est<span>[</span>ind<span>]</span>
    se <span>&lt;-</span> <span>sqrt</span><span>(</span><span>diag</span><span>(</span>vcov<span>(</span>x<span>)))[</span>ind<span>]</span>
    <span>data.frame</span><span>(</span>model <span>=</span> model<span>,</span>
               para <span>=</span> <span>names</span><span>(</span>est<span>),</span>
               est <span>=</span> est<span>,</span>
               se <span>=</span> se<span>,</span>
               lwr <span>=</span> est <span>-</span> <span>1.96</span> <span>*</span> se<span>,</span>
               upr <span>=</span> est <span>+</span> <span>1.96</span> <span>*</span> se<span>)</span>
<span>}</span>

<span>#' brms: fit and extract results</span>
<span>#' @param dpp new pre-post data to fit</span>
<span>#' @param fit_brm brms-object to update</span>
get_brm <span>&lt;-</span> <span>function</span><span>(</span>dpp<span>,</span> fit_brm<span>)</span> <span>{</span>
    fit <span>&lt;-</span> <span>suppressMessages</span><span>(</span>update<span>(</span>fit_brm<span>,</span> newdata <span>=</span> dpp<span>,</span> iter <span>=</span> <span>2000</span><span>))</span>
    est <span>&lt;-</span> fixef<span>(</span>fit<span>)</span>
    est <span>&lt;-</span> est<span>[</span><span>rownames</span><span>(</span>est<span>)</span> <span>==</span> <span>&quot;y_session&quot;</span><span>,</span> <span>]</span>
    <span>data.frame</span><span>(</span>model <span>=</span><span>&quot;IV (brms)&quot;</span><span>,</span>
               para <span>=</span> <span>&quot;session&quot;</span><span>,</span>
               est <span>=</span> est<span>[</span><span>1</span><span>],</span> 
               se <span>=</span> est<span>[</span><span>2</span><span>],</span>
               lwr <span>=</span> est<span>[</span><span>3</span><span>],</span>
               upr <span>=</span> est<span>[</span><span>4</span><span>])</span>

<span>}</span>
<span>#' lavaan: creates function that fit and extract results</span>
<span>#' @param model character; lavaan model syntax</span>
<span>#' @return function; accepts new data via &quot;dpp&quot;</span>
make_lavaan_func <span>&lt;-</span> <span>function</span><span>(</span>model<span>)</span> <span>{</span>
    <span>function</span><span>(</span>dpp<span>)</span> <span>{</span>
        fit_lavaan <span>&lt;-</span> <span>tryCatch</span><span>({</span>sem<span>(</span>model<span>,</span> data<span>=</span>dpp<span>)},</span> 
                               error <span>=</span> <span>function</span><span>(</span>e<span>)</span> <span>{</span><span>NA</span><span>},</span> 
                               warning <span>=</span> <span>function</span><span>(</span>w<span>)</span> <span>{</span><span>NA</span><span>})</span>
        <span>if</span><span>(</span><span>isS4</span><span>(</span>fit_lavaan<span>))</span> <span>{</span>
            se <span>&lt;-</span> <span>sqrt</span><span>(</span><span>diag</span><span>(</span>vcov<span>(</span>fit_lavaan<span>)))</span>
            ind <span>&lt;-</span> <span>names</span><span>(</span>se<span>)</span> <span>==</span> <span>&quot;y~session&quot;</span>
            est <span>&lt;-</span> coef<span>(</span>fit_lavaan<span>)</span>
            est <span>&lt;-</span> est<span>[</span>ind<span>]</span>
            se <span>&lt;-</span> se<span>[</span>ind<span>]</span>
        <span>}</span> <span>else</span> <span>if</span> <span>(</span><span>is.na</span><span>(</span>fit_lavaan<span>))</span> <span>{</span>
            se <span>&lt;-</span> <span>NA</span>
            est <span>&lt;-</span> <span>NA</span>
        <span>}</span>
        <span>data.frame</span><span>(</span>model <span>=</span> <span>&quot;IV (lavaan)&quot;</span><span>,</span>
                   para <span>=</span> <span>&quot;session&quot;</span><span>,</span>
                   est <span>=</span> est<span>,</span>
                   se <span>=</span> se<span>,</span>
                   lwr <span>=</span> est <span>-</span> <span>1.96</span> <span>*</span> se<span>,</span>
                   upr <span>=</span> est <span>+</span> <span>1.96</span> <span>*</span> se<span>)</span>
    <span>}</span>
<span>}</span>

<span>#' add confounding to pre-post data</span>
<span>#' @param x prepost data</span>
<span>#' @param confounder charater; name of column in x to use as confounder</span>
<span>#' @param n2 number of total subjects</span>
<span>#' @beta_s dose-response parameter</span>
<span>#' @c_scale scale confounding distribution</span>
add_confounding <span>&lt;-</span> <span>function</span><span>(</span>x<span>,</span> confounder<span>,</span> n2<span>,</span> beta_s<span>,</span> c_scale<span>)</span> <span>{</span>
    x<span>$</span>confounder <span>&lt;-</span> x<span>[,</span> confounder<span>]</span>
    x <span>&lt;-</span> x <span>%>%</span> 
        mutate<span>(</span>s <span>=</span>  <span>(</span><span>7</span> <span>-</span> <span>(</span>confounder <span>-</span> <span>mean</span><span>(</span>confounder<span>))</span><span>/</span>sd<span>(</span>confounder<span>)</span> <span>*</span> c_scale<span>)</span> <span>-</span> hidden_confounder<span>*</span><span>0.5</span> <span>+</span> rnorm<span>(</span>n2<span>,</span> sd <span>=</span> <span>1</span><span>),</span>
               s <span>=</span> <span>ifelse</span><span>(</span>s <span></span> <span>1</span><span>,</span> <span>1</span><span>,</span> s<span>)</span> <span>*</span> treatment<span>,</span>
               session <span>=</span> s <span>+</span> rnorm<span>(</span>n2<span>,</span> sd <span>=</span> <span>2</span><span>),</span>
               session <span>=</span> <span>ifelse</span><span>(</span>session <span></span> <span>1</span><span>,</span> <span>1</span><span>,</span> session<span>)</span> <span>*</span> treatment<span>,</span>
               yt <span>=</span> yc <span>-</span> beta_s <span>*</span> s<span>,</span>
               y <span>=</span> yt <span>*</span> treatment <span>+</span> yc <span>*</span> <span>(</span><span>1</span><span>-</span>treatment<span>),</span>
               id <span>=</span> <span>1</span><span>:</span>n2<span>)</span>

    x
<span>}</span>
add_collider <span>&lt;-</span> <span>function</span><span>(</span>x<span>,</span> confounder<span>,</span> n2<span>,</span> beta_s<span>,</span> c_scale<span>)</span> <span>{</span>
    x<span>$</span>confounder <span>&lt;-</span> x<span>[,</span> confounder<span>]</span>
    x <span>&lt;-</span> x <span>%>%</span> 
        mutate<span>(</span>s <span>=</span>  <span>(</span><span>7</span> <span>-</span> <span>(</span>confounder <span>-</span> <span>mean</span><span>(</span>confounder<span>))</span><span>/</span>sd<span>(</span>confounder<span>)</span> <span>*</span> c_scale<span>)</span> <span>+</span> rnorm<span>(</span>n2<span>,</span> sd <span>=</span> <span>1</span><span>),</span>
               s <span>=</span> <span>ifelse</span><span>(</span>s <span></span> <span>1</span><span>,</span> <span>1</span><span>,</span> s<span>)</span> <span>*</span> treatment<span>,</span>
               session <span>=</span> s <span>+</span> rnorm<span>(</span>n2<span>,</span> sd <span>=</span> <span>0</span><span>),</span>
               session <span>=</span> <span>ifelse</span><span>(</span>session <span></span> <span>1</span><span>,</span> <span>1</span><span>,</span> session<span>)</span> <span>*</span> treatment<span>,</span>
               cred <span>=</span> <span>(</span>session <span>+</span> hidden_confounder<span>)</span> <span>*</span> treatment<span>,</span>
               yt <span>=</span> yc <span>-</span> beta_s <span>*</span> s<span>,</span>
               y <span>=</span> yt <span>*</span> treatment <span>+</span> yc <span>*</span> <span>(</span><span>1</span><span>-</span>treatment<span>),</span>
               id <span>=</span> <span>1</span><span>:</span>n2<span>)</span>

    x
<span>}</span>



<span>#' Create data generator</span>
<span>#'</span>
<span>#' @param pp powerlmm::study_parameters-object</span>
<span>#' @param confounder character; passed to 'add_confounding'</span>
<span>#' @param beta_s passed to 'add_confounding'</span>
<span>#' @param c_scale passed to 'add_confounding'</span>
<span>#' </span>
<span>#' @return function with no arguments that generate data.frame</span>
data_generator <span>&lt;-</span> <span>function</span><span>(</span>pp<span>,</span> confounder<span>,</span> label<span>,</span> beta_s<span>,</span> c_scale<span>,</span> hidden_confounder_sd <span>=</span> <span>0</span><span>,</span> measure_error_sd <span>=</span> <span>0</span><span>,</span> collider <span>=</span> <span>FALSE</span><span>)</span> <span>{</span>
    <span>function</span><span>()</span> <span>{</span>
        d <span>&lt;-</span> simulate_data<span>(</span>pp<span>)</span> <span># powerlmm::simulate_data</span>

        dpp <span>&lt;-</span> d <span>%>%</span> 
            filter<span>(</span>time <span>%in%</span> <span>c</span><span>(</span><span>0</span><span>,</span> <span>10</span><span>),</span>
                   treatment <span>==</span> <span>0</span><span>)</span> <span>%>%</span> 
            mutate<span>(</span>time <span>=</span> <span>factor</span><span>(</span>time<span>,</span> labels <span>=</span> <span>c</span><span>(</span><span>&quot;pre&quot;</span><span>,</span> <span>&quot;post&quot;</span><span>)),</span>
                   treatment <span>=</span> <span>rep</span><span>(</span><span>0</span><span>:</span><span>1</span><span>,</span> each <span>=</span> pp<span>$</span>n2<span>))</span> <span>%>%</span> 
            spread<span>(</span>time<span>,</span> y<span>)</span> <span>%>%</span> 
            mutate<span>(</span>hidden_confounder <span>=</span> rnorm<span>(</span>pp<span>$</span>n2<span>,</span> sd <span>=</span> hidden_confounder_sd<span>),</span>
                   yc <span>=</span> post <span>+</span> hidden_confounder<span>)</span>

        <span>if</span><span>(</span>collider<span>)</span> <span>{</span>
            dpp <span>&lt;-</span> add_collider<span>(</span>dpp<span>,</span> confounder <span>=</span> confounder<span>,</span> pp<span>$</span>n2<span>,</span> beta_s<span>,</span> c_scale<span>)</span>
        <span>}</span> <span>else</span> <span>{</span>
            dpp <span>&lt;-</span> add_confounding<span>(</span>dpp<span>,</span> confounder <span>=</span> confounder<span>,</span> pp<span>$</span>n2<span>,</span> beta_s<span>,</span> c_scale<span>)</span>

        <span>}</span>
        dpp <span>&lt;-</span> dpp <span>%>%</span>  
            mutate<span>(</span>pre <span>=</span> intercept_subject <span>+</span> rnorm<span>(</span>pp<span>$</span>n2<span>,</span> sd <span>=</span> measure_error_sd<span>),</span>
                   label <span>=</span> label<span>)</span>
        dpp
    <span>}</span>
<span>}</span>


<span>#' Simulation function</span>
<span>#'</span>
<span>#' @param i i:th simulation</span>
<span>#' @param data_gen data_generator</span>
<span>#' @param fit_brm prefit brms-object</span>

sim <span>&lt;-</span> <span>function</span><span>(</span>i<span>,</span> data_gen<span>,</span> fit_brm<span>)</span> <span>{</span>
    dpp <span>&lt;-</span> data_gen<span>()</span>
    d_t <span>&lt;-</span> dpp <span>%>%</span> 
        filter<span>(</span>treatment <span>==</span> <span>1</span><span>)</span>

    OLS <span>&lt;-</span> lm<span>(</span>y <span>~</span> session<span>,</span> data <span>=</span> dpp<span>)</span>
    OLS_pre <span>&lt;-</span> lm<span>(</span>y <span>~</span> pre <span>+</span> session<span>,</span> data <span>=</span> dpp<span>)</span>
    OLS_tx <span>&lt;-</span> lm<span>(</span>y <span>~</span> session<span>,</span> data <span>=</span> d_t<span>)</span>
    OLS_pre_tx <span>&lt;-</span> lm<span>(</span>y <span>~</span> pre <span>+</span> session<span>,</span> data <span>=</span> d_t<span>)</span>
    IV_ivreg <span>&lt;-</span> ivreg<span>(</span>y <span>~</span> session <span>+</span> pre <span>|</span> treatment <span>+</span> treatment<span>:</span>pre<span>,</span> data <span>=</span> dpp<span>)</span>
    IV_brm <span>&lt;-</span> get_brm<span>(</span>dpp<span>,</span> fit_brm<span>)</span>
    IV_lavaan <span>&lt;-</span> get_lavaan<span>(</span>dpp<span>)</span>
    res <span>&lt;-</span> <span>mapply</span><span>(</span>get_res<span>,</span> 
                  <span>list</span><span>(</span>OLS<span>,</span> OLS_pre<span>,</span> OLS_tx<span>,</span> OLS_pre_tx<span>,</span> IV_ivreg<span>),</span> 
                  model <span>=</span> <span>c</span><span>(</span><span>&quot;OLS s&quot;</span><span>,</span> <span>&quot;OLS s + pre&quot;</span><span>,</span> <span>&quot;OLS s (tx)&quot;</span><span>,</span> <span>&quot;OLS s + pre (tx)&quot;</span><span>,</span> <span>&quot;IV (ivreg)&quot;</span><span>),</span> 
                  SIMPLIFY <span>=</span> <span>FALSE</span><span>)</span>
    res <span>&lt;-</span> <span>c</span><span>(</span>res<span>,</span> <span>list</span><span>(</span>IV_brm<span>),</span> <span>list</span><span>(</span>IV_lavaan<span>))</span>
    res <span>&lt;-</span> <span>do.call</span><span>(</span><span>rbind</span><span>,</span> res<span>)</span>
    res<span>$</span>sim <span>&lt;-</span> i

    res
<span>}</span>
sim_collider <span>&lt;-</span> <span>function</span><span>(</span>i<span>,</span> data_gen<span>,</span> fit_brm<span>)</span> <span>{</span>
    dpp <span>&lt;-</span> data_gen<span>()</span>
    d_t <span>&lt;-</span> dpp <span>%>%</span> 
        filter<span>(</span>treatment <span>==</span> <span>1</span><span>)</span>

    OLS <span>&lt;-</span> lm<span>(</span>y <span>~</span> session<span>,</span> data <span>=</span> dpp<span>)</span>
    OLS_pre <span>&lt;-</span> lm<span>(</span>y <span>~</span> pre <span>+</span> session<span>,</span> data <span>=</span> dpp<span>)</span>
    OLS_pre_cred <span>&lt;-</span> lm<span>(</span>y <span>~</span> pre <span>+</span> session <span>+</span> cred<span>,</span> data <span>=</span> dpp<span>)</span>
    OLS_tx <span>&lt;-</span> lm<span>(</span>y <span>~</span> session<span>,</span> data <span>=</span> d_t<span>)</span>
    OLS_pre_tx <span>&lt;-</span> lm<span>(</span>y <span>~</span> pre <span>+</span> session<span>,</span> data <span>=</span> d_t<span>)</span>
    OLS_pre_tx_cred <span>&lt;-</span> lm<span>(</span>y <span>~</span> pre <span>+</span> session <span>+</span> cred<span>,</span> data <span>=</span> d_t<span>)</span>
    IV_ivreg <span>&lt;-</span> ivreg<span>(</span>y <span>~</span> session <span>+</span> pre <span>|</span> treatment <span>+</span> treatment<span>:</span>pre<span>,</span> data <span>=</span> dpp<span>)</span>
    IV_brm <span>&lt;-</span> get_brm<span>(</span>dpp<span>,</span> fit_brm<span>)</span>
    IV_lavaan <span>&lt;-</span> get_lavaan<span>(</span>dpp<span>)</span>
    res <span>&lt;-</span> <span>mapply</span><span>(</span>get_res<span>,</span> 
                  <span>list</span><span>(</span>OLS<span>,</span> OLS_pre<span>,</span> OLS_pre_cred<span>,</span> OLS_tx<span>,</span> OLS_pre_tx<span>,</span> OLS_pre_tx_cred<span>,</span> IV_ivreg<span>),</span> 
                  model <span>=</span> <span>c</span><span>(</span><span>&quot;OLS s&quot;</span><span>,</span> <span>&quot;OLS s + pre&quot;</span><span>,</span><span>&quot;OLS s + pre + collider&quot;</span><span>,</span>
                            <span>&quot;OLS s (tx)&quot;</span><span>,</span> <span>&quot;OLS s + pre (tx)&quot;</span><span>,</span> <span>&quot;OLS s + pre + collider (tx)&quot;</span><span>,</span>
                            <span>&quot;IV (ivreg)&quot;</span><span>),</span> 
                  SIMPLIFY <span>=</span> <span>FALSE</span><span>)</span>
    res <span>&lt;-</span> <span>c</span><span>(</span>res<span>,</span> <span>list</span><span>(</span>IV_brm<span>),</span> <span>list</span><span>(</span>IV_lavaan<span>))</span>
    res <span>&lt;-</span> <span>do.call</span><span>(</span><span>rbind</span><span>,</span> res<span>)</span>
    res<span>$</span>sim <span>&lt;-</span> i

    res
<span>}</span>
<span>#' Summarise simulation results</span>
<span>#'</span>
<span>#' @param x Sim results from sim()</span>
<span>#' @param type character; label to use for type of confounding</span>
<span>#' @param theta true value of session dose-response coef</span>
summarise_sim <span>&lt;-</span> <span>function</span><span>(</span>x<span>,</span> type<span>,</span> theta <span>=</span> <span>-1</span><span>)</span> <span>{</span>
    x <span>&lt;-</span> x <span>%>%</span> group_by<span>(</span>model<span>)</span> <span>%>%</span>
        summarise<span>(</span>est_M <span>=</span> <span>mean</span><span>(</span>est<span>,</span> na.rm <span>=</span> <span>TRUE</span><span>),</span> 
                  est_SE <span>=</span> <span>mean</span><span>(</span>se<span>,</span> na.rm <span>=</span> <span>TRUE</span><span>),</span>
                  est_SD <span>=</span> sd<span>(</span>est<span>,</span> na.rm <span>=</span> <span>TRUE</span><span>),</span>
                  cover <span>=</span> <span>mean</span><span>(</span>lwr <span></span> theta <span>&amp;</span> theta <span></span> upr<span>,</span> na.rm <span>=</span> <span>TRUE</span><span>))</span> <span>%>%</span> 
        mutate<span>(</span>est_RB <span>=</span> <span>((</span>est_M <span>-</span> theta<span>)</span><span>/</span>theta<span>)</span> <span>*</span> <span>100</span><span>,</span>
               se_RB <span>=</span> <span>(</span>est_SE <span>-</span> est_SD<span>)</span><span>/</span>est_SD<span>)</span>

    x<span>$</span>type <span>&lt;-</span> type

    x
<span>}</span>
</pre>
</div>
<h2>Setup and run the simulation.</h2>
<div>
<pre><span></span><span>source</span><span>(</span><span>&quot;functions.R&quot;</span><span>)</span>
<span>library</span><span>(</span>methods<span>)</span>

<span># Sim setup</span>
nsim <span>&lt;-</span> <span>5000</span>
cores <span>&lt;-</span> <span>16</span>

sp <span>&lt;-</span> study_parameters<span>(</span>n1 <span>=</span> <span>11</span><span>,</span>
                       n2 <span>=</span> <span>100</span><span>,</span>
                       T_end <span>=</span> <span>10</span><span>,</span>
                       fixed_intercept <span>=</span> <span>10</span><span>,</span>
                       fixed_slope <span>=</span> <span>-.5</span><span>,</span>
                       icc_pre_subject <span>=</span> <span>0.8</span><span>,</span>
                       sigma_error <span>=</span> <span>3</span><span>,</span>
                       cohend <span>=</span> <span>0</span><span>)</span>

<span># start clusters</span>
cl <span>&lt;-</span> makeCluster<span>(</span>cores<span>)</span>
clusterEvalQ<span>(</span>cl<span>,</span> <span>{</span>
    <span>library</span><span>(</span>powerlmm<span>)</span>
    <span>library</span><span>(</span>dplyr<span>)</span>
    <span>library</span><span>(</span>tidyr<span>)</span>
    <span>library</span><span>(</span>AER<span>)</span>
    <span>library</span><span>(</span>brms<span>)</span>
    <span>library</span><span>(</span>lavaan<span>)</span>
    <span>library</span><span>(</span>methods<span>)</span>
<span>})</span>

<span># brms setup</span>
<span># precompile model</span>

<span>if</span><span>(</span><span>!</span><span>exists</span><span>(</span><span>&quot;fit_brm&quot;</span><span>))</span> <span>{</span>
    dpp <span>&lt;-</span> data_generator<span>(</span>p_pre<span>,</span> <span>&quot;intercept_subject&quot;</span><span>,</span> label <span>=</span> <span>&quot;setup&quot;</span><span>,</span> beta_s <span>=</span> <span>1</span><span>,</span> c_scale <span>=</span> <span>3</span><span>)()</span>
    f1 <span>&lt;-</span> bf<span>(</span>session <span>~</span> treatment <span>+</span> treatment<span>:</span>pre<span>)</span>
    f2 <span>&lt;-</span> bf<span>(</span>y <span>~</span> session <span>+</span> pre<span>)</span>
    fit_brm <span>&lt;-</span> brm<span>(</span>f1 <span>+</span> f2<span>,</span> data <span>=</span> dpp<span>,</span> chains <span>=</span> <span>1</span><span>,</span> cores <span>=</span> <span>1</span><span>,</span> iter <span>=</span> <span>1</span><span>)</span>

<span>}</span>

<span># lavaan setup</span>
l_model <span>&lt;-</span> <span>' session ~ 1 + treatment + treatment:pre</span>
<span> y ~ 1 + session + pre</span>
<span> session ~~ y'</span>
get_lavaan <span>&lt;-</span> make_lavaan_func<span>(</span>l_model<span>)</span>

clusterExport<span>(</span>cl<span>,</span> <span>c</span><span>(</span><span>&quot;sp&quot;</span><span>,</span>
                    <span>&quot;get_res&quot;</span><span>,</span>
                    <span>&quot;add_confounding&quot;</span><span>,</span> 
                    <span>&quot;add_collider&quot;</span><span>,</span>
                    <span>&quot;fit_brm&quot;</span><span>,</span> 
                    <span>&quot;get_brm&quot;</span><span>,</span> 
                    <span>&quot;get_lavaan&quot;</span><span>,</span>
                    <span>&quot;l_model&quot;</span><span>))</span>

<span># run sim</span>
ptm <span>&lt;-</span> <span>proc.time</span><span>()</span>

<span># Observed </span>
res1 <span>&lt;-</span> parLapply<span>(</span>cl<span>,</span> 
                  <span>1</span><span>:</span>nsim<span>,</span> 
                  sim<span>,</span>
                  data_gen <span>=</span> data_generator<span>(</span>sp<span>,</span> <span>&quot;intercept_subject&quot;</span><span>,</span> 
                                            label <span>=</span> <span>&quot;Observed confounder&quot;</span><span>,</span>
                                            beta_s <span>=</span> <span>1</span><span>,</span>
                                            c_scale <span>=</span> <span>3</span><span>,</span> 
                                            hidden_confounder_sd <span>=</span> <span>0</span><span>,</span> 
                                            measure_error_sd <span>=</span> <span>0</span><span>),</span>
                  fit_brm <span>=</span> fit_brm<span>)</span>
res1 <span>&lt;-</span> <span>do.call</span><span>(</span><span>rbind</span><span>,</span> res1<span>)</span>

<span># Residual confound</span>
res2 <span>&lt;-</span> parLapply<span>(</span>cl<span>,</span> 
                  <span>1</span><span>:</span>nsim<span>,</span> 
                  sim<span>,</span> data_gen <span>=</span> data_generator<span>(</span>sp<span>,</span> 
                                                 <span>&quot;intercept_subject&quot;</span><span>,</span> 
                                                 label <span>=</span> <span>&quot;Residual confounding&quot;</span><span>,</span>
                                                 beta_s <span>=</span> <span>1</span><span>,</span> 
                                                 c_scale <span>=</span> <span>3</span><span>,</span> 
                                                 hidden_confounder_sd <span>=</span> <span>0</span><span>,</span> 
                                                 measure_error_sd <span>=</span> <span>6</span><span>),</span>
                  fit_brm <span>=</span> fit_brm<span>)</span>
res2 <span>&lt;-</span> <span>do.call</span><span>(</span><span>rbind</span><span>,</span> res2<span>)</span>

<span># Hidden confound</span>
res3 <span>&lt;-</span> parLapply<span>(</span>cl<span>,</span> 
                  <span>1</span><span>:</span>nsim<span>,</span> 
                  sim<span>,</span> 
                  data_gen <span>=</span> data_generator<span>(</span>sp<span>,</span> <span>&quot;intercept_subject&quot;</span><span>,</span> 
                                            label <span>=</span> <span>&quot;Hidden confounding&quot;</span><span>,</span> 
                                            beta_s <span>=</span> <span>1</span><span>,</span> 
                                            c_scale <span>=</span> <span>0</span><span>,</span> 
                                            hidden_confounder_sd <span>=</span> <span>6</span><span>,</span> 
                                            measure_error_sd <span>=</span> <span>0</span><span>),</span> 
                  fit_brm <span>=</span> fit_brm<span>)</span>
res3 <span>&lt;-</span> <span>do.call</span><span>(</span><span>rbind</span><span>,</span> res3<span>)</span>


x <span>&lt;-</span> <span>mapply</span><span>(</span>summarise_sim<span>,</span> 
            <span>list</span><span>(</span>res1<span>,</span> res2<span>,</span> res3<span>),</span> 
            type <span>=</span> <span>list</span><span>(</span><span>&quot;Known confounder&quot;</span><span>,</span> <span>&quot;Residual confounder&quot;</span><span>,</span> <span>&quot;Residual + hidden confounder&quot;</span><span>),</span> 
            SIMPLIFY <span>=</span> <span>FALSE</span><span>)</span>
x <span>&lt;-</span> <span>do.call</span><span>(</span><span>rbind</span><span>,</span> x<span>)</span>



<span># Collider ----------------------------------------------------------------</span>
res_c <span>&lt;-</span> parLapply<span>(</span>cl<span>,</span> 
                  <span>1</span><span>:</span>nsim<span>,</span> 
                  sim_collider<span>,</span>
                  data_gen <span>=</span> data_generator<span>(</span>sp<span>,</span> <span>&quot;intercept_subject&quot;</span><span>,</span> 
                                            label <span>=</span> <span>&quot;Collider&quot;</span><span>,</span>
                                            beta_s <span>=</span> <span>1</span><span>,</span>
                                            c_scale <span>=</span> <span>3</span><span>,</span> 
                                            hidden_confounder_sd <span>=</span> <span>5</span><span>,</span> 
                                            measure_error_sd <span>=</span> <span>4</span><span>,</span>
                                            collider <span>=</span> <span>TRUE</span><span>),</span>
                  fit_brm <span>=</span> fit_brm<span>)</span>
res_c <span>&lt;-</span> <span>do.call</span><span>(</span><span>rbind</span><span>,</span> res_c<span>)</span>

stopCluster<span>(</span>cl<span>)</span>
<span>(</span>et <span>&lt;-</span> <span>proc.time</span><span>()</span> <span>-</span> ptm<span>)</span>
x_c <span>&lt;-</span> summarise_sim<span>(</span>res_c<span>,</span> type <span>=</span> <span>&quot;collider&quot;</span><span>,</span> theta <span>=</span> <span>-1</span><span>)</span>

<span>saveRDS</span><span>(</span><span>list</span><span>(</span><span>&quot;res1&quot;</span> <span>=</span> res1<span>,</span>
          <span>&quot;res2&quot;</span> <span>=</span> res2<span>,</span> 
          <span>&quot;res3&quot;</span> <span>=</span> res3<span>,</span> 
          <span>&quot;res_c&quot;</span> <span>=</span> res_c<span>,</span>
          <span>&quot;x&quot;</span> <span>=</span> x<span>,</span> 
          <span>&quot;x_c&quot;</span> <span>=</span> x_c<span>,</span>
          <span>&quot;et&quot;</span> <span>=</span> et<span>),</span> 
     file <span>=</span> <span>&quot;sim_res.rds&quot;</span><span>)</span>
</pre>
</div>
<p></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://rpsychologist.com/adherence-analysis-IV-brms"> R Psychologist - R</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/Syn8-MjtCIM" height="1"></a></div>