<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>MC Donohue</title>
<link>https://mcdonohue.github.io/blog/</link>
<atom:link href="https://mcdonohue.github.io/blog/index.xml" rel="self" type="application/rss+xml"/>
<description>An Alzheimer&#39;s Disease biostatistics blog built with Quarto</description>
<generator>quarto-1.8.27</generator>
<lastBuildDate>Wed, 25 Mar 2026 07:00:00 GMT</lastBuildDate>
<item>
  <title>Design-induced artifacts when “disease clocks” are plugged into second-stage analyses of symptom onset</title>
  <dc:creator>Philip S. Insel</dc:creator>
  <dc:creator>Michael C. Donohue</dc:creator>
  <link>https://mcdonohue.github.io/blog/posts/design-artifacts/</link>
  <description><![CDATA[ 





<p>Source files are available on <a href="https://github.com/mcdonohue/blog/tree/main/posts/design-artifacts">GitHub</a></p>
<p>Preprint published on <em>medRxiv</em>. doi:<a href="https://doi.org/10.64898/2026.03.26.26349230">10.64898/2026.03.26.26349230</a></p>
<p>Plasma markers of phosphorylated tau-217 (such as %p-tau217) have rapidly emerged as robust markers of underlying Alzheimer’s Disease (AD) pathology.<span class="citation" data-cites="warmenhoven2025comprehensive"><sup>1</sup></span> Building on this excitement, recent work has focused on longitudinal measurements of plasma p-tau217, with this work suggesting significant change over time in at risk groups,<span class="citation" data-cites="mattsson2020longitudinal"><sup>2</sup></span> although with milder associations with concurrent cognitive changes.<span class="citation" data-cites="insel2025concurrent"><sup>3</sup></span></p>
<p>The article by Petersen et al.<span class="citation" data-cites="petersen2026"><sup>4</sup></span> expands in this direction by applying disease clock models (Sampled Iterative Local Approximation [SILA]<span class="citation" data-cites="betthauser2022multi"><sup>5</sup></span> and Temporal Integration of Rate Accumulation [TIRA]<span class="citation" data-cites="schindler2021predicting"><sup>6</sup></span>) to derive estimates of the age at plasma %p-tau217 positivity, based on estimation of group-level accumulation combined with individual level %p-tau217 magnitude. This measure of age at %p-tau217 positivity, defined as baseline age minus the estimated time from %p-tau217 positivity is then used to claim prediction of the age at onset of symptomatic (AD). We show that this apparent predictive performance is largely artifactual, arising from two structural features of the analysis: (1) restriction to individuals who progressed to symptomatic AD during limited follow-up, which induces a strong artifactual association between baseline age and age at symptom onset, and (2) the use of constructed predictors and outcomes that share age and timing components, making the resulting associations partly self-referential. More generally, this artifact arises when the outcome is constrained by an earlier time variable and the predictor contains that same or a related time component, inducing associations through shared structure rather than independent signal. Using digitized data from the published figures, we decomposed the clock-derived measure into baseline age and estimated time from plasma %p-tau217 positivity, quantified shared and unique explained variance using commonality analysis, and evaluated a null scenario by replacing the biomarker component of the clock measure with a randomly generated time from %p-tau217 positivity, drawn over the observed range. This randomization preserves the range of the predictor while removing any biomarker information, allowing the contribution of structural relationships to be assessed directly. In ADNI, baseline age alone explained substantially more variance in age at symptom onset than the reported plasma %p-tau217 clock-derived predictors (<img src="https://latex.codecogs.com/png.latex?R%5E2%5Capprox0.78"> versus <img src="https://latex.codecogs.com/png.latex?0.337"> and <img src="https://latex.codecogs.com/png.latex?0.470"> for TIRA and SILA), the estimated time from %p-tau217 component contributed little additional information, and randomly generated values produced a similar associations to baseline age alone (<img src="https://latex.codecogs.com/png.latex?R%5E2%5Capprox0.79">). These results indicate that the reported predictive performance of the plasma %p-tau217 clock is driven largely by structural age relationships rather than independent biomarker information.</p>
<p>The primary analysis links estimated age at %p-tau217 positivity to observed age at symptom onset in known progressors and forms a central results of the paper. However, this analysis evaluates when progression occurs in individuals who progress within a limited follow-up window—a substantially less clinically relevant problem than predicting onset in an all-comers population, which represents a key goal in the field. In the restricted sample, baseline age must precede age at symptom onset, and the interval between them is bounded by the observation period of the study. As a result, baseline age alone strongly predicts age at symptom onset, even in the absence of biomarker information.</p>
<p>This structure carries directly into the clock-based analysis, where age at symptom onset is regressed on estimated age at %p-tau217 positivity. Because the plasma %p-tau217 clock combines baseline age with an estimated timing component, and the outcome is itself constrained by baseline age, the resulting association reflects a mixture of structural age relationships and any true biomarker signal. To separate these components, the clock can be decomposed into baseline age and estimated time from %p-tau217 positivity, and age at symptom onset modeled as a function of both terms, allowing the independent contribution of the biomarker timing component to be assessed.</p>
<p>In their Alzforum response to this critique,<span class="citation" data-cites="alzforum_ptau217_clock"><sup>7</sup></span> the authors confirmed that in ADNI, baseline age alone predicts age at symptom onset with an <img src="https://latex.codecogs.com/png.latex?R%5E2"> of approximately 0.78 (Figure&nbsp;1, left panels). The pink regions in Figure&nbsp;1 depict the structural dependence that arises given that observed symptom onset occurs at most 10.4 years after baseline. In contrast to baseline age, the estimated age of %p-tau217 positivity achieves substantially lower <img src="https://latex.codecogs.com/png.latex?R%5E2"> values (0.337 and 0.470 for TIRA and SILA, respectively; Figure 4 in Petersen et al.<span class="citation" data-cites="petersen2026"><sup>4</sup></span> and Figure&nbsp;1, right panels). Thus, incorporating the biomarker-derived estimate of time from %p-tau217 positivity does not improve explained variance relative to baseline age alone and, in fact, reduces it. Consistent with this, the estimated time from %p-tau217 positivity shows little association with age at symptom onset, and adding it to baseline age does not improve model fit (Figure&nbsp;1). Patterns obtained using a randomly generated time from %p-tau217 positivity, drawn uniformly over the observed range, are qualitatively similar to those obtained using the SILA and TIRA estimates (Figure&nbsp;1, bottom).</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div id="fig-tira-sila" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center" data-fig-pos="!h">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-tira-sila-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://mcdonohue.github.io/blog/posts/design-artifacts/fig-tira-sila-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" data-fig-pos="!h">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-tira-sila-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Relationships between the predicted outcome (age of symptom onset) and three predictors: age at baseline (first plasma visit, CDR=0), estimated time from %p-tau217 positivity, and their difference: baseline age minus estimated time from %p-tau217 positivity (i.e., age of %p-tau217 positivity). Time from %p-tau217 positivity is estimated by the TIRA method (top row), the SILA method (middle row), or by randomly sampling from a uniform distribution over the observed range of estimates (bottom row). Panels include fitted linear regression estimates, summary statistics for each predictor, and a dashed identity line. To illustrate the contribution of time from %p-tau217 positivity (middle column), the rightmost panels show faint gray lines connecting each person’s age at baseline to their estimated age of %p-tau217 positivity. The pink regions span vertically from the age at baseline (identity line) to the maximum observed follow‑up, defined as age at baseline plus 10.4 years. <strong>Note:</strong> Figures and analyses are based on digitized estimates derived from figures available on Alzforum<span class="citation" data-cites="alzforum_ptau217_clock"><sup>7</sup></span> using WebPlotDigitizer.<span class="citation" data-cites="WebPlotDigitizer"><sup>8</sup></span>
</figcaption>
</figure>
</div>
</div>
</div>
<p>To further quantify these relationships, Table&nbsp;1 presents a commonality analysis<span class="citation" data-cites="nimon2008r"><sup>9</sup></span> partitioning the unique and shared variance between baseline age and the estimated age of %p-tau217 positivity (via SILA, TIRA, or the random comparator) as predictors of age at symptom onset. While a substantial proportion of explained variance is shared between predictors (50% for SILA; 42% for TIRA; 36% for random), the unique contributions differ markedly. Baseline age independently accounts for a large proportion of the variance (44% for SILA; 56% for TIRA; 64% for random), whereas the estimated age of %p-tau217 positivity contributes minimally (6% for SILA; 3% for TIRA; 0.07% for random). These results indicate that nearly all explained variance is either shared with or uniquely attributable to baseline age, with little additional contribution, only 3% to 6%, from the biomarker-derived timing component.</p>
<div id="tbl-commonality" class="cell quarto-float quarto-figure quarto-figure-center anchored" data-layout-align="center" data-tbl-colwidths="[30,15,15,15,15]">
<figure class="quarto-float quarto-float-tbl figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-tbl" id="tbl-commonality-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;1: <strong>Commonality analysis results for age of symptom onset across SILA, TIRA, and a random variable.</strong> This table presents the unique and shared variance contributions for baseline age and age of %p-tau217 positivity in predicting onset age. Unique effects represent the variance accounted for by a single predictor, while common effects represent the shared variance attributed to the overlap between predictors. Total and Adjusted <img src="https://latex.codecogs.com/png.latex?R%5E2"> on the bottom row are from the linear models with both predictors. <strong>Note:</strong> Analyses are based on digitized estimates derived from figures available on Alzforum<span class="citation" data-cites="alzforum_ptau217_clock"><sup>7</sup></span> using WebPlotDigitizer.<span class="citation" data-cites="WebPlotDigitizer"><sup>8</sup></span>
</figcaption>
<div aria-describedby="tbl-commonality-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="tabwid"><style>.cl-b39b72ce{table-layout:auto;width:100%;}.cl-b38a17a4{font-family:'Times New Roman';font-size:10pt;font-weight:normal;font-style:normal;text-decoration:none;color:rgba(0, 0, 0, 1.00);background-color:transparent;}.cl-b38a17ae{font-family:'Times New Roman';font-size:10pt;font-weight:normal;font-style:italic;text-decoration:none;color:rgba(0, 0, 0, 1.00);background-color:transparent;}.cl-b399a700{margin:0;text-align:left;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);padding-bottom:4pt;padding-top:4pt;padding-left:5pt;padding-right:5pt;line-height: 1;background-color:transparent;}.cl-b399a701{margin:0;text-align:center;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);padding-bottom:4pt;padding-top:4pt;padding-left:5pt;padding-right:5pt;line-height: 1;background-color:transparent;}.cl-b399b6dc{background-color:rgba(207, 207, 207, 1.00);vertical-align: middle;border-bottom: 1pt solid rgba(0, 0, 0, 1.00);border-top: 2pt solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b6e6{background-color:rgba(207, 207, 207, 1.00);vertical-align: middle;border-bottom: 1pt solid rgba(0, 0, 0, 1.00);border-top: 2pt solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 1pt solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b6e7{background-color:rgba(207, 207, 207, 1.00);vertical-align: middle;border-bottom: 1pt solid rgba(0, 0, 0, 1.00);border-top: 2pt solid rgba(0, 0, 0, 1.00);border-left: 1pt solid rgba(0, 0, 0, 1.00);border-right: 1pt solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b6e8{background-color:rgba(207, 207, 207, 1.00);vertical-align: middle;border-bottom: 1pt solid rgba(0, 0, 0, 1.00);border-top: 2pt solid rgba(0, 0, 0, 1.00);border-left: 1pt solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b6f0{background-color:rgba(207, 207, 207, 1.00);vertical-align: middle;border-bottom: 1pt solid rgba(0, 0, 0, 1.00);border-top: 2pt solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b6f1{background-color:transparent;vertical-align: middle;border-bottom: 1pt solid rgba(0, 0, 0, 1.00);border-top: 1pt solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b6f2{background-color:transparent;vertical-align: middle;border-bottom: 1pt solid rgba(0, 0, 0, 1.00);border-top: 1pt solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b6f3{background-color:transparent;vertical-align: middle;border-bottom: 1pt solid rgba(0, 0, 0, 1.00);border-top: 1pt solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 1pt solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b6fa{background-color:transparent;vertical-align: middle;border-bottom: 1pt solid rgba(0, 0, 0, 1.00);border-top: 1pt solid rgba(0, 0, 0, 1.00);border-left: 1pt solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b6fb{background-color:rgba(249, 249, 249, 1.00);vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b6fc{background-color:rgba(249, 249, 249, 1.00);vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b6fd{background-color:rgba(249, 249, 249, 1.00);vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 1pt solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b704{background-color:rgba(249, 249, 249, 1.00);vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 1pt solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b705{background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b706{background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b70e{background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 1pt solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b70f{background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 1pt solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b710{background-color:rgba(249, 249, 249, 1.00);vertical-align: middle;border-bottom: 1pt solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b711{background-color:rgba(249, 249, 249, 1.00);vertical-align: middle;border-bottom: 1pt solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b718{background-color:rgba(249, 249, 249, 1.00);vertical-align: middle;border-bottom: 1pt solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 1pt solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b719{background-color:rgba(249, 249, 249, 1.00);vertical-align: middle;border-bottom: 1pt solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 1pt solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b71a{background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 1pt solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b71b{background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 1pt solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b722{background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 1pt solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 1pt solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b723{background-color:transparent;vertical-align: middle;border-bottom: 0 solid rgba(0, 0, 0, 1.00);border-top: 1pt solid rgba(0, 0, 0, 1.00);border-left: 1pt solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b724{background-color:rgba(249, 249, 249, 1.00);vertical-align: middle;border-bottom: 2pt solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b725{background-color:rgba(249, 249, 249, 1.00);vertical-align: middle;border-bottom: 2pt solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b72c{background-color:rgba(249, 249, 249, 1.00);vertical-align: middle;border-bottom: 2pt solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 0 solid rgba(0, 0, 0, 1.00);border-right: 1pt solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}.cl-b399b72d{background-color:rgba(249, 249, 249, 1.00);vertical-align: middle;border-bottom: 2pt solid rgba(0, 0, 0, 1.00);border-top: 0 solid rgba(0, 0, 0, 1.00);border-left: 1pt solid rgba(0, 0, 0, 1.00);border-right: 0 solid rgba(0, 0, 0, 1.00);margin-bottom:0;margin-top:0;margin-left:0;margin-right:0;}</style><table data-quarto-disable-processing="true" class="cl-b39b72ce"><thead><tr style="overflow-wrap:break-word;"><th class="cl-b399b6dc"><p class="cl-b399a700"><span class="cl-b38a17a4"></span></p></th><th colspan="2" class="cl-b399b6e6"><p class="cl-b399a701"><span class="cl-b38a17a4">SILA</span></p></th><th colspan="2" class="cl-b399b6e7"><p class="cl-b399a701"><span class="cl-b38a17a4">TIRA</span></p></th><th colspan="2" class="cl-b399b6e8"><p class="cl-b399a701"><span class="cl-b38a17a4">Random</span></p></th></tr><tr style="overflow-wrap:break-word;"><th class="cl-b399b6f1"><p class="cl-b399a700"><span class="cl-b38a17a4">Effect</span></p></th><th class="cl-b399b6f2"><p class="cl-b399a701"><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/katex@0.15.2/dist/katex.min.css" data-external="1"><span class="cl-b38a17a4"><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css" data-external="1">
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>R</mi><mn>2</mn></msup></mrow><annotation encoding="application/x-tex">R^2</annotation></semantics></math></span></span></span></p></th><th class="cl-b399b6f3"><p class="cl-b399a701"><span class="cl-b38a17a4">% Total</span></p></th><th class="cl-b399b6fa"><p class="cl-b399a701"><span class="cl-b38a17a4"><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css" data-external="1">
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>R</mi><mn>2</mn></msup></mrow><annotation encoding="application/x-tex">R^2</annotation></semantics></math></span></span></span></p></th><th class="cl-b399b6f3"><p class="cl-b399a701"><span class="cl-b38a17a4">% Total</span></p></th><th class="cl-b399b6fa"><p class="cl-b399a701"><span class="cl-b38a17a4"><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css" data-external="1">
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>R</mi><mn>2</mn></msup></mrow><annotation encoding="application/x-tex">R^2</annotation></semantics></math></span></span></span></p></th><th class="cl-b399b6f2"><p class="cl-b399a701"><span class="cl-b38a17a4">% Total</span></p></th></tr></thead><tbody><tr style="overflow-wrap:break-word;"><td class="cl-b399b6fb"><p class="cl-b399a700"><span class="cl-b38a17ae">Unique to baseline age                       </span></p></td><td class="cl-b399b6fc"><p class="cl-b399a701"><span class="cl-b38a17a4">0.37</span></p></td><td class="cl-b399b6fd"><p class="cl-b399a701"><span class="cl-b38a17a4">43.78</span></p></td><td class="cl-b399b704"><p class="cl-b399a701"><span class="cl-b38a17a4">0.46</span></p></td><td class="cl-b399b6fd"><p class="cl-b399a701"><span class="cl-b38a17a4">55.75</span></p></td><td class="cl-b399b704"><p class="cl-b399a701"><span class="cl-b38a17a4">0.51</span></p></td><td class="cl-b399b6fc"><p class="cl-b399a701"><span class="cl-b38a17a4">64.37</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-b399b705"><p class="cl-b399a700"><span class="cl-b38a17ae">Unique to age of %p-tau217 positivity                  </span></p></td><td class="cl-b399b706"><p class="cl-b399a701"><span class="cl-b38a17a4">0.05</span></p></td><td class="cl-b399b70e"><p class="cl-b399a701"><span class="cl-b38a17a4">6.07</span></p></td><td class="cl-b399b70f"><p class="cl-b399a701"><span class="cl-b38a17a4">0.02</span></p></td><td class="cl-b399b70e"><p class="cl-b399a701"><span class="cl-b38a17a4">2.63</span></p></td><td class="cl-b399b70f"><p class="cl-b399a701"><span class="cl-b38a17a4">0.00</span></p></td><td class="cl-b399b706"><p class="cl-b399a701"><span class="cl-b38a17a4">0.07</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-b399b710"><p class="cl-b399a700"><span class="cl-b38a17ae">Common to both</span></p></td><td class="cl-b399b711"><p class="cl-b399a701"><span class="cl-b38a17a4">0.42</span></p></td><td class="cl-b399b718"><p class="cl-b399a701"><span class="cl-b38a17a4">50.15</span></p></td><td class="cl-b399b719"><p class="cl-b399a701"><span class="cl-b38a17a4">0.34</span></p></td><td class="cl-b399b718"><p class="cl-b399a701"><span class="cl-b38a17a4">41.62</span></p></td><td class="cl-b399b719"><p class="cl-b399a701"><span class="cl-b38a17a4">0.28</span></p></td><td class="cl-b399b711"><p class="cl-b399a701"><span class="cl-b38a17a4">35.56</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-b399b71a"><p class="cl-b399a700"><span class="cl-b38a17ae">Total </span><span class="cl-b38a17ae"><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css" data-external="1">
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>R</mi><mn>2</mn></msup></mrow><annotation encoding="application/x-tex">R^2</annotation></semantics></math></span></span></span></p></td><td class="cl-b399b71b"><p class="cl-b399a701"><span class="cl-b38a17a4">0.84</span></p></td><td class="cl-b399b722"><p class="cl-b399a701"><span class="cl-b38a17a4">100.00</span></p></td><td class="cl-b399b723"><p class="cl-b399a701"><span class="cl-b38a17a4">0.82</span></p></td><td class="cl-b399b722"><p class="cl-b399a701"><span class="cl-b38a17a4">100.00</span></p></td><td class="cl-b399b723"><p class="cl-b399a701"><span class="cl-b38a17a4">0.79</span></p></td><td class="cl-b399b71b"><p class="cl-b399a701"><span class="cl-b38a17a4">100.00</span></p></td></tr><tr style="overflow-wrap:break-word;"><td class="cl-b399b724"><p class="cl-b399a700"><span class="cl-b38a17ae">Adj. </span><span class="cl-b38a17ae"><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css" data-external="1">
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><msup><mi>R</mi><mn>2</mn></msup></mrow><annotation encoding="application/x-tex">R^2</annotation></semantics></math></span></span></span></p></td><td class="cl-b399b725"><p class="cl-b399a701"><span class="cl-b38a17a4">0.83</span></p></td><td class="cl-b399b72c"><p class="cl-b399a701"><span class="cl-b38a17a4"></span></p></td><td class="cl-b399b72d"><p class="cl-b399a701"><span class="cl-b38a17a4">0.80</span></p></td><td class="cl-b399b72c"><p class="cl-b399a701"><span class="cl-b38a17a4"></span></p></td><td class="cl-b399b72d"><p class="cl-b399a701"><span class="cl-b38a17a4">0.77</span></p></td><td class="cl-b399b725"><p class="cl-b399a701"><span class="cl-b38a17a4"></span></p></td></tr></tbody></table></div>
</div>
</figure>
</div>
<p>The authors have also highlighted analyses that exclude age from the outcome. However, this does not remove the structural issue; rather, it shifts the source of the constraint from baseline age to age at %p-tau217 positivity. In Supplementary Figure 6, the analysis is framed as regressing time from %p-tau217 positivity to symptom onset on the estimated age at %p-tau217 positivity. The quantity that induces the artifact depends on how the outcome is defined. When the outcome is age at symptom onset, it is constrained by baseline age, and baseline age therefore induces the structural association, as shown above. If the outcome is instead the time from %p-tau217 positivity to symptom onset (i.e., duration of %p-tau217 positivity), then the constraint shifts to age at %p-tau217 positivity. Consider someone who becomes %p-tau217 positive at age 90: their duration of %p-tau217 positivity cannot be 15 years, since this would imply an age of 105, which is not observed in the data. Similarly, someone who becomes %p-tau217 positive at age 60 cannot have very short durations, since symptom onset must occur at the observed age range of the cohort. Thus, age at p-tau positivity tightly constrains the possible values of duration, and regressing duration on age at %p-tau217 positivity induces a strong negative association, with older ages at positivity associated with shorter durations, even in the absence of a true underlying relationship. This is the same structural mechanism described above, now operating through age at %p-tau217 positivity rather than baseline age.</p>
<p>In our analysis (Figure&nbsp;2), when the estimated age of %p-tau217 positivity is replaced with age at baseline minus the randomly generated time from %p-tau217 positivity, the <img src="https://latex.codecogs.com/png.latex?R%5E2"> increases from 0.57 (TIRA) or 0.39 (SILA) to 0.68 (random). This shows that the magnitude of the association in Supplementary Fig. 6 can be reproduced using a predictor that contains no biomarker information, indicating that the observed relationship is driven by the structural dependence of the outcome on the predictor, rather than by the clock-estimated time to %p-tau217 positivity. This structural dependence is represented by the pink regions which show where the data must lie given that the observed ages of symptom onset occur between 69 to 94 years.</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div id="fig-supp-fig-6" class="quarto-float quarto-figure quarto-figure-center anchored" data-fig-align="center" data-fig-pos="!h">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-supp-fig-6-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://mcdonohue.github.io/blog/posts/design-artifacts/fig-supp-fig-6-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" data-fig-pos="!h">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-supp-fig-6-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Relationships between age of symptom onset minus age of %p-tau217 positivity versus age of positivity (TIRA, SILA, or Random). Panels include fitted linear regression estimates, and summary statistics for each. The pink shaded regions show the range of possible values given the observed ages of symptom onset, which span from 69 to 94 years. <strong>Note:</strong> Figures and analyses are based on digitized estimates derived from figures available on Alzforum<span class="citation" data-cites="alzforum_ptau217_clock"><sup>7</sup></span> using WebPlotDigitizer<span class="citation" data-cites="WebPlotDigitizer"><sup>8</sup></span>.
</figcaption>
</figure>
</div>
</div>
</div>
<p>The survival analyses, and in particular the event time summaries shown in Petersen et al Figure 3b, reflect the same structural issue described above for the duration analysis in Supplementary Figure 6. These summaries are derived from the survival analysis and compare time from %p-tau217 positivity to symptom onset across groups defined by age at %p-tau217 positivity, making them a grouped version of the same duration outcome. Although the full survival analysis includes nonprogressors, the relationship between age at %p-tau217 positivity and time to symptom onset remains subject to the same structural constraints. Individuals who become %p-tau217 positive at older ages necessarily have less remaining observable time before symptom onset, censoring, and competing risks, including death. As a result, duration from %p-tau217 positivity to onset is inherently constrained by %p-tau217 age at positivity. Thus, just as in the duration regression, analyses that summarize time from %p-tau217 positivity to symptom onset as a function of age at %p-tau217 positivity embed this constraint, and shorter observed durations at older ages arise from these structural limits rather than from differences in disease progression.</p>
<p>Taken together, these analyses demonstrate that combining baseline age with estimated time from %p-tau217 positivity into a single predictor, and plugging it into second-stage analyses of progression within limited follow-up, induces structural dependencies in the data. The resulting associations reflect a mixture of shared age components from constructed variables, bounded follow-up, and structural constraints. Consistent with this, we find that the clock-derived formulation of time from %p-tau217 positivity contributes little independent information in these analyses. This limited contribution likely reflects, in part, the strong modeling assumption that individuals follow a common accumulation trajectory, such that time from %p-tau217 positivity can be inferred from a group-level mean trajectory given a single observation. Under this assumption, variation in individual trajectories is minimized, reducing the potential for the estimated timing component to provide additional predictive value. These structural features, including shared age components, bounded follow up, and constructed variables can produce strong and apparently meaningful relationships even in the absence of biomarker timing information, masking the underlying artifacts and giving the appearance of predictive performance that does not generalize beyond the constrained setting in which it is evaluated. Importantly, these structural issues are not unique to plasma p-tau217 clock models, but are also present in prior work applying similar frameworks to estimate age at amyloid positivity from amyloid PET and relating this quantity to age at symptom onset, as illustrated in Figure 3 in Schindler et al.,<span class="citation" data-cites="schindler2021predicting"><sup>6</sup></span> where the same combination of constructed predictors, shared age components, and restricted follow-up can induce analogous artifacts. Analyses using disease-time “clock” models with these structural features are becoming increasingly common, underscoring the importance of carefully evaluating their statistical properties.<span class="citation" data-cites="brown2025temporal"><sup>10</sup></span></p>
<p>These methodological objections are not intended to dismiss the significance of plasma p-tau217 as a predictor. Indeed, we and others have found it to be a robust predictor of cognitive decline in cognitively unimpaired populations. These analyses have yielded more modest effect sizes, indicating that plasma p-tau217 is associated with subsequent cognitive decline, but with limited precision at the individual level, especially when incorporating plasma p-tau217 changes over time.<span class="citation" data-cites="insel2025concurrent"><sup>3</sup></span> In this same sample of <img src="https://latex.codecogs.com/png.latex?N=1,629"> cognitively unimpaired (median follow-up six years), a model incorporating baseline p-tau217, hippocampal atrophy, amyloid PET, APOE <img src="https://latex.codecogs.com/png.latex?%5Cepsilon4"> status, and demographics achieved a cross-validated area under the precision–recall curve (AUPRC) of approximately 70% for discriminating clinical decline from stability.<span class="citation" data-cites="li2025divergent"><sup>11</sup></span></p>
<p>Notably, 77% of the 1,629 cognitively unimpaired individuals in this cohort remained cognitively stable throughout follow-up. While accuracy metrics can be inflated by focusing only on individuals who decline (a small minority) and ignoring the stable majority, such an approach does not reflect the clinical reality of population screening. Given the clear clinical potential of plasma measures of AD, and the availability of direct-to-consumer p-tau217 measurements, it is critical that scientific research evaluating these tools portrays an accurate representation of what these measurements can and cannot provide. Rather than assuming that all individuals are on a common declining trajectory, the latent class approach we employed explicitly estimates the probability that an individual will be a progressor.<span class="citation" data-cites="proust2017estimation"><sup>12</sup></span> It is clinically imperative to convey the degree of certainty regarding an individual’s trajectory before counseling them on their “clock” under an unsupported assumption of inevitable decline.</p>
<section id="data-and-code-availability" class="level1">
<h1>Data and code availability</h1>
<p>To reproduce these analyses, see data and code at GitHub: <a href="https://github.com/mcdonohue/blog/tree/main/posts/design-artifacts" class="uri">https://github.com/mcdonohue/blog/tree/main/posts/design-artifacts</a></p>
</section>
<section id="references" class="level1 unnumbered">
<h1 class="unnumbered">References</h1>
<div id="refs" class="references csl-bib-body">
<div id="ref-warmenhoven2025comprehensive" class="csl-entry">
<div class="csl-left-margin">1. </div><div class="csl-right-inline">Warmenhoven N, Salvadó G, Janelidze S, et al. A comprehensive head-to-head comparison of key plasma phosphorylated tau 217 biomarker tests. <em>Brain</em>. 2025;148(2):416-431.</div>
</div>
<div id="ref-mattsson2020longitudinal" class="csl-entry">
<div class="csl-left-margin">2. </div><div class="csl-right-inline">Mattsson-Carlgren N, Janelidze S, Palmqvist S, et al. <span class="nocase">Longitudinal plasma p-tau217 is increased in early stages of Alzheimer’s disease</span>. <em>Brain</em>. 2020;143(11):3234-3241.</div>
</div>
<div id="ref-insel2025concurrent" class="csl-entry">
<div class="csl-left-margin">3. </div><div class="csl-right-inline">Insel PS, Mattsson-Carlgren N, Langford O, et al. <span class="nocase">Concurrent changes in plasma phosphorylated tau 217, tau PET, and cognition in preclinical Alzheimer disease</span>. <em>JAMA Neurology</em>. 2025;82(10):985-993.</div>
</div>
<div id="ref-petersen2026" class="csl-entry">
<div class="csl-left-margin">4. </div><div class="csl-right-inline">Petersen KK, Milà-Alomà M, Li Y, et al. Predicting onset of symptomatic <span>Alzheimerʼs</span> disease with plasma p-tau217 clocks. <em>Nature Medicine</em>. Published online 2026. doi:<a href="https://doi.org/10.1038/s41591-026-04206-y">10.1038/s41591-026-04206-y</a></div>
</div>
<div id="ref-betthauser2022multi" class="csl-entry">
<div class="csl-left-margin">5. </div><div class="csl-right-inline">Betthauser TJ, Bilgel M, Koscik RL, et al. Multi-method investigation of factors influencing amyloid onset and impairment in three cohorts. <em>Brain</em>. 2022;145(11):4065-4079.</div>
</div>
<div id="ref-schindler2021predicting" class="csl-entry">
<div class="csl-left-margin">6. </div><div class="csl-right-inline">Schindler SE, Li Y, Buckles VD, et al. Predicting symptom onset in sporadic <span>Alzheimer</span> disease with amyloid <span>PET</span>. <em>Neurology</em>. 2021;97(18):e1823-e1834.</div>
</div>
<div id="ref-alzforum_ptau217_clock" class="csl-entry">
<div class="csl-left-margin">7. </div><div class="csl-right-inline">Not if, but when: Blood p-Tau217 clock tells time of <span>AD</span> onset. 2025. <a href="https://www.alzforum.org/news/research-news/not-if-when-blood-p-tau217-clock-tells-time-ad-onset#comment-57821">https://www.alzforum.org/news/research-news/not-if-when-blood-p-tau217-clock-tells-time-ad-onset#comment-57821</a></div>
</div>
<div id="ref-WebPlotDigitizer" class="csl-entry">
<div class="csl-left-margin">8. </div><div class="csl-right-inline">Rohatgi A. WebPlotDigitizer. <a href="https://automeris.io">https://automeris.io</a></div>
</div>
<div id="ref-nimon2008r" class="csl-entry">
<div class="csl-left-margin">9. </div><div class="csl-right-inline">Nimon K, Lewis M, Kane R, Haynes RM. An <span>R</span> package to compute commonality coefficients in the multiple regression case: An introduction to the package and a practical example. <em>Behavior research methods</em>. 2008;40(2):457-466.</div>
</div>
<div id="ref-brown2025temporal" class="csl-entry">
<div class="csl-left-margin">10. </div><div class="csl-right-inline">Brown CA, Cousins KAQ, Korecka M, et al. Temporal modeling of amyloid and tau trajectories in <span class="nocase">Alzheimer’s</span> disease using <span>PET</span> and plasma biomarkers. <em>Annals of Neurology</em>. n/a(n/a). doi:<a href="https://doi.org/10.1002/ana.78194">10.1002/ana.78194</a></div>
</div>
<div id="ref-li2025divergent" class="csl-entry">
<div class="csl-left-margin">11. </div><div class="csl-right-inline">Li R, Langford O, Insel PS, et al. Divergent patterns of cognitive decline in preclinical <span class="nocase">Alzheimer’s</span> disease: Implications for secondary prevention trials. <em>Alzheimer’s &amp; Dementia</em>. Published online In press. doi:<a href="https://doi.org/10.64898/2025.12.15.25342305">10.64898/2025.12.15.25342305</a></div>
</div>
<div id="ref-proust2017estimation" class="csl-entry">
<div class="csl-left-margin">12. </div><div class="csl-right-inline">Proust-Lima C, Philipps V, Liquet B. <span class="nocase">Estimation of extended mixed models using latent classes and latent processes: the R package lcmm</span>. <em>Journal of Statistical Software</em>. 2017;78:1-56.</div>
</div>
</div>


</section>

 ]]></description>
  <guid>https://mcdonohue.github.io/blog/posts/design-artifacts/</guid>
  <pubDate>Wed, 25 Mar 2026 07:00:00 GMT</pubDate>
</item>
<item>
  <title>Constrained vs unconstrained longitudinal data analysis</title>
  <link>https://mcdonohue.github.io/blog/posts/constrained-longitudinal-data-analysis/</link>
  <description><![CDATA[ 





<section id="constrained-longitudinal-data-analysis" class="level1">
<h1>Constrained Longitudinal Data Analysis</h1>
<p><a href="http://www.jstor.org/stable/25053123">Liang and Zeger (2000)</a> describe a constrained longitudinal data analysis for comparing the time course of two randomized groups in which the model constains the two groups to share a common mean at baseline. <a href="https://www.ncbi.nlm.nih.gov/pubmed/19764951">Lu (2010)</a> compared the power of this approach to a longitudinal analysis of covariance approach. We conduct a few simple simulation studies to interrogate the effect of the constraint on power to detect group differences. We do not simulate any missing data here.</p>
<section id="packages-used" class="level2">
<h2 class="anchored" data-anchor-id="packages-used">Packages used</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(longpower)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(nlme)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(mvtnorm)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(parallel)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(contrast)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyr)</span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span></code></pre></div></div>
</div>
</section>
</section>
<section id="pre-post-design-with-two-repeated-measures-simulated-via-random-intercept-model" class="level1">
<h1>Pre-post design with two repeated measures simulated via random intercept model</h1>
<section id="simulation-settings" class="level2">
<h2 class="anchored" data-anchor-id="simulation-settings">Simulation settings</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">sigma_residual <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb2-2">sigma_random_intercept <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb2-3">sigma_random_slope <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb2-4">Time <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb2-5">Beta <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'(Intercept)'</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt:time'</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>)</span>
<span id="cb2-6">Beta.null <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'(Intercept)'</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt:time'</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb2-7">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># per group</span></span>
<span id="cb2-8">N <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>n</span></code></pre></div></div>
</div>
</section>
<section id="analytic-calculations" class="level2">
<h2 class="anchored" data-anchor-id="analytic-calculations">Analytic calculations</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">cov.t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(t1, t2, sig2.i, sig2.s, cov.s.i){</span>
<span id="cb3-2">        sig2.i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> t1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>t2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>sig2.s <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> (t1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>t2)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>cov.s.i</span>
<span id="cb3-3">}</span>
<span id="cb3-4">m <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(Time)</span>
<span id="cb3-5">R <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">outer</span>(Time, Time, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x,y){<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cov.t</span>(x,y, sigma_random_intercept<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, sigma_random_slope<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)})</span>
<span id="cb3-6">R <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> R <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag</span>(sigma_residual<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, m, m)</span>
<span id="cb3-7"></span>
<span id="cb3-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">power.t.test</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n=</span>n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">delta=</span>Beta[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt:time'</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>sigma_random_intercept))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
     Two-sample t test power calculation 

              n = 200
          delta = 0.4
             sd = 1.414214
      sig.level = 0.05
          power = 0.8055549
    alternative = two.sided

NOTE: n is number in *each* group</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lmmpower</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n=</span>n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">delta=</span>Beta[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt:time'</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t=</span>Time, </span>
<span id="cb5-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sig2.e=</span>sigma_residual<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sig2.s=</span>sigma_random_slope<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, </span>
<span id="cb5-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sig2.i=</span>sigma_random_intercept, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cov.s.i=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
     Longitudinal linear model slope power calculation (Diggle et al 2002, page 29) 

              N = 400
              n = 200, 200
          delta = 0.4
         sigma2 = 1
      sig.level = 0.05
          power = 0.8074296
    alternative = two.sided

 NOTE: N is *total* sample size and n is sample size in *each* group 

 R:
     [,1] [,2]
[1,]    2    1
[2,]    1    2</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diggle.linear.power</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n=</span>n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">delta=</span>Beta[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt:time'</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t=</span>Time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">R=</span>R)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
     Longitudinal linear model slope power calculation (Diggle et al 2002, page 29) 

              N = 400
              n = 200, 200
          delta = 0.4
         sigma2 = 1
      sig.level = 0.05
          power = 0.8074296
    alternative = two.sided

 NOTE: N is *total* sample size and n is sample size in *each* group 

 R:
     [,1] [,2]
[1,]    2    1
[2,]    1    2</code></pre>
</div>
</div>
</section>
<section id="set-up-data-for-simulation" class="level2">
<h2 class="anchored" data-anchor-id="set-up-data-for-simulation">Set up data for simulation</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">dd0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand.grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> Time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N)</span>
<span id="cb9-2">dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>trt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>id <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> N<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb9-3">dd0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model.matrix</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> id<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>trt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dd0))</span>
<span id="cb9-4">dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Trt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>trt)</span>
<span id="cb9-5">dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Y0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(dd0[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(Beta)]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> Beta</span>
<span id="cb9-6">dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Y0.null <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(dd0[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(Beta.null)]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> Beta.null</span>
<span id="cb9-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(dd0, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>Y0, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color=</span>Trt)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://mcdonohue.github.io/blog/posts/constrained-longitudinal-data-analysis/index_files/figure-html/sim_data_setup-1.png" class="img-fluid figure-img" width="1142"></p>
</figure>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20130318</span>)</span>
<span id="cb10-2">dd1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merge</span>(dd0, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb10-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N, </span>
<span id="cb10-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ran.intercept =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd=</span>sigma_random_intercept),</span>
<span id="cb10-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ran.slope     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd=</span>sigma_random_slope)</span>
<span id="cb10-6">))</span>
<span id="cb10-7">dd1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>e <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(dd1), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd=</span>sigma_residual)</span>
<span id="cb10-8"></span>
<span id="cb10-9">dd1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(dd1, Y0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ran.intercept <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ran.slope<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>time <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> e)</span>
<span id="cb10-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(dd1, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>Y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color=</span>Trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> id)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://mcdonohue.github.io/blog/posts/constrained-longitudinal-data-analysis/index_files/figure-html/sim_data_setup-2.png" class="img-fluid figure-img" width="1142"></p>
</figure>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># unconstrained:</span></span>
<span id="cb11-2">fit.LDA <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lme</span>(Y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>trt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">random=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>id, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>dd1)</span>
<span id="cb11-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">printCoefmat</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(fit.LDA)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>tTable)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>                 Value  Std.Error         DF t-value   p-value    
(Intercept)   0.994529   0.101708 398.000000  9.7783 &lt; 2.2e-16 ***
trt          -0.172352   0.143837 398.000000 -1.1982  0.231533    
time          0.481055   0.097516 398.000000  4.9331 1.191e-06 ***
trt:time      0.450842   0.137908 398.000000  3.2692  0.001173 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># constrained:</span></span>
<span id="cb13-2">fit.cLDA <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lme</span>(Y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">random=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>id, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>dd1)</span>
<span id="cb13-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">printCoefmat</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(fit.cLDA)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>tTable)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>                 Value  Std.Error         DF t-value   p-value    
(Intercept)   0.908353   0.071944 399.000000 12.6259 &lt; 2.2e-16 ***
time          0.520646   0.091756 398.000000  5.6742 2.686e-08 ***
time:trt      0.371660   0.121052 398.000000  3.0703  0.002285 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">newdat0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand.grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trt=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time=</span>Time)</span>
<span id="cb15-2"></span>
<span id="cb15-3">mods <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">LDA =</span> fit.LDA, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cLDA=</span>fit.cLDA)</span>
<span id="cb15-4"></span>
<span id="cb15-5">pd <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">do.call</span>(rbind, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(mods), <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(name){</span>
<span id="cb15-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(newdat0, </span>
<span id="cb15-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Y=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(mods[[name]], newdat0, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">level=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>),</span>
<span id="cb15-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> name</span>
<span id="cb15-9">  ) </span>
<span id="cb15-10">}))</span>
<span id="cb15-11">pd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Trt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(pd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>trt)</span>
<span id="cb15-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(pd, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>Y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> Trt)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_grid</span>(.<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>model)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://mcdonohue.github.io/blog/posts/constrained-longitudinal-data-analysis/index_files/figure-html/sim_data_setup-3.png" class="img-fluid figure-img" width="1142"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="simulate" class="level2">
<h2 class="anchored" data-anchor-id="simulate">Simulate</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20130318</span>)</span>
<span id="cb16-2">results <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">do.call</span>(rbind, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mclapply</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x){</span>
<span id="cb16-3">  dd1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merge</span>(dd0, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb16-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N,</span>
<span id="cb16-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ran.intercept =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd=</span>sigma_random_intercept),</span>
<span id="cb16-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ran.slope     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd=</span>sigma_random_slope)</span>
<span id="cb16-7">  ))</span>
<span id="cb16-8">  dd1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>e <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(dd1), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd=</span>sigma_residual)</span>
<span id="cb16-9">  dd1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(dd1, Y0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ran.intercept <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ran.slope<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>time <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> e)</span>
<span id="cb16-10">  dd1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Y.null <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(dd1, Y0.null <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ran.intercept <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ran.slope<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>time <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> e)</span>
<span id="cb16-11">  ddw <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merge</span>(</span>
<span id="cb16-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subset</span>(dd1, time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'id'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y.null'</span>)),</span>
<span id="cb16-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subset</span>(dd1, time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'id'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y.null'</span>)),</span>
<span id="cb16-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'id'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt'</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">suffixes=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'.0'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'.1'</span>)</span>
<span id="cb16-15">  )</span>
<span id="cb16-16">  ddw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(ddw, Y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>Y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.0</span>)</span>
<span id="cb16-17">  ddw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>d.null <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(ddw, Y.null<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>Y.null<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.0</span>)</span>
<span id="cb16-18"></span>
<span id="cb16-19">  prepost <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb16-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ttest.power   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t.test</span>(         d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>ddw)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p.value,</span>
<span id="cb16-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ttest.alpha   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t.test</span>(    d.null<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>ddw)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p.value,</span>
<span id="cb16-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ancova1.power =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(     d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>Y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.0</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>ddw))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>coef[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Pr(&gt;|t|)'</span>],</span>
<span id="cb16-23">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ancova1.alpha =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(d.null<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>Y.null<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.0</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>ddw))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>coef[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Pr(&gt;|t|)'</span>],</span>
<span id="cb16-24">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ancova2.power =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(     d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale</span>(Y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.0</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>ddw))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>coef[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Pr(&gt;|t|)'</span>],</span>
<span id="cb16-25">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ancova2.alpha =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(d.null<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale</span>(Y.null<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.0</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>ddw))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>coef[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Pr(&gt;|t|)'</span>]</span>
<span id="cb16-26">  )</span>
<span id="cb16-27"></span>
<span id="cb16-28">  fits <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb16-29">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">LDA.power  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lme</span>(     Y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">random=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>id, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>dd1),</span>
<span id="cb16-30">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">LDA.alpha  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lme</span>(Y.null<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">random=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>id, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>dd1),</span>
<span id="cb16-31">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cLDA.power =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lme</span>(     Y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">random=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>id, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>dd1),</span>
<span id="cb16-32">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cLDA.alpha =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lme</span>(Y.null<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">random=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>id, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>dd1)</span>
<span id="cb16-33">  )</span>
<span id="cb16-34"></span>
<span id="cb16-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unlist</span>(prepost),</span>
<span id="cb16-36">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">slopes =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unlist</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(fits, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(fit){</span>
<span id="cb16-37">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(fit)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>tTable[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'time:trt'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'p-value'</span>]</span>
<span id="cb16-38">    })),</span>
<span id="cb16-39">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">contrasts =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unlist</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(fits[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(fit){</span>
<span id="cb16-40">      pv <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">try</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrast</span>(fit,</span>
<span id="cb16-41">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">a=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time=</span>Time[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(Time)], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trt=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>),</span>
<span id="cb16-42">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time=</span>Time[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(Time)], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trt=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Pvalue), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">silent =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb16-43">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">class</span>(pv) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'try-error'</span>){</span>
<span id="cb16-44">        fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">update</span>(fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">random=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>id)</span>
<span id="cb16-45">        pv <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">try</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrast</span>(fit,</span>
<span id="cb16-46">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">a=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time=</span>Time[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(Time)], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trt=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>),</span>
<span id="cb16-47">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time=</span>Time[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(Time)], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trt=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Pvalue), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">silent =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb16-48">      }</span>
<span id="cb16-49">      pv</span>
<span id="cb16-50">    }))</span>
<span id="cb16-51">  )</span>
<span id="cb16-52">}, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mc.cores =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))</span></code></pre></div></div>
</div>
</section>
<section id="simulated-power-results" class="level2">
<h2 class="anchored" data-anchor-id="simulated-power-results">Simulated power results (%)</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Pr(p&lt;0.05)'</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(results, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x){</span>
<span id="cb17-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(x)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb17-3">})))</span></code></pre></div></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;"></th>
<th style="text-align: right;">Pr(p&lt;0.05)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">ttest.power</td>
<td style="text-align: right;">80.28</td>
</tr>
<tr class="even">
<td style="text-align: left;">ttest.alpha</td>
<td style="text-align: right;">4.79</td>
</tr>
<tr class="odd">
<td style="text-align: left;">ancova1.power</td>
<td style="text-align: right;">90.15</td>
</tr>
<tr class="even">
<td style="text-align: left;">ancova1.alpha</td>
<td style="text-align: right;">5.14</td>
</tr>
<tr class="odd">
<td style="text-align: left;">ancova2.power</td>
<td style="text-align: right;">90.15</td>
</tr>
<tr class="even">
<td style="text-align: left;">ancova2.alpha</td>
<td style="text-align: right;">5.13</td>
</tr>
<tr class="odd">
<td style="text-align: left;">slopes.LDA.power</td>
<td style="text-align: right;">80.28</td>
</tr>
<tr class="even">
<td style="text-align: left;">slopes.LDA.alpha</td>
<td style="text-align: right;">4.79</td>
</tr>
<tr class="odd">
<td style="text-align: left;">slopes.cLDA.power</td>
<td style="text-align: right;">90.32</td>
</tr>
<tr class="even">
<td style="text-align: left;">slopes.cLDA.alpha</td>
<td style="text-align: right;">5.15</td>
</tr>
<tr class="odd">
<td style="text-align: left;">contrasts.LDA.power</td>
<td style="text-align: right;">80.56</td>
</tr>
<tr class="even">
<td style="text-align: left;">contrasts.LDA.alpha</td>
<td style="text-align: right;">5.40</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
</section>
<section id="four-repeated-measures-simulated-via-random-slope-model" class="level1">
<h1>Four repeated measures simulated via random slope model</h1>
<section id="simulation-settings-1" class="level2">
<h2 class="anchored" data-anchor-id="simulation-settings-1">Simulation settings</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">sigma_residual <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb18-2">sigma_random_intercept <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb18-3">sigma_random_slope <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb18-4">Time <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span></span>
<span id="cb18-5">Beta <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'(Intercept)'</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt:time'</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.434</span>)</span>
<span id="cb18-6">Beta.null <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'(Intercept)'</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt:time'</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb18-7">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># per group</span></span>
<span id="cb18-8">N <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>n</span></code></pre></div></div>
</div>
</section>
<section id="analytic-calculations-1" class="level2">
<h2 class="anchored" data-anchor-id="analytic-calculations-1">Analytic calculations</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1">m <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(Time)</span>
<span id="cb19-2">R <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">outer</span>(Time, Time, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x,y){<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cov.t</span>(x,y, sigma_random_intercept<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, sigma_random_slope<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)})</span>
<span id="cb19-3">R <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> R <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag</span>(sigma_residual<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, m, m)</span>
<span id="cb19-4"></span>
<span id="cb19-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lmmpower</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n=</span>n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">delta=</span>Beta[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt:time'</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t=</span>Time, </span>
<span id="cb19-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sig2.e=</span>sigma_residual<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sig2.s=</span>sigma_random_slope<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, </span>
<span id="cb19-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sig2.i=</span>sigma_random_intercept<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cov.s.i=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
     Longitudinal linear model slope power calculation (Diggle et al 2002, page 29) 

              N = 200
              n = 100, 100
          delta = 0.434
         sigma2 = 1
      sig.level = 0.05
          power = 0.7999644
    alternative = two.sided

 NOTE: N is *total* sample size and n is sample size in *each* group 

 R:
     [,1] [,2] [,3] [,4]
[1,]    2    1    1    1
[2,]    1    3    3    4
[3,]    1    3    6    7
[4,]    1    4    7   11</code></pre>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diggle.linear.power</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n=</span>n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">delta=</span>Beta[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt:time'</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t=</span>Time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">R=</span>R)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
     Longitudinal linear model slope power calculation (Diggle et al 2002, page 29) 

              N = 200
              n = 100, 100
          delta = 0.434
         sigma2 = 1
      sig.level = 0.05
          power = 0.7999644
    alternative = two.sided

 NOTE: N is *total* sample size and n is sample size in *each* group 

 R:
     [,1] [,2] [,3] [,4]
[1,]    2    1    1    1
[2,]    1    3    3    4
[3,]    1    3    6    7
[4,]    1    4    7   11</code></pre>
</div>
</div>
</section>
<section id="set-up-data-for-simulation-1" class="level2">
<h2 class="anchored" data-anchor-id="set-up-data-for-simulation-1">Set up data for simulation</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1">dd0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand.grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> Time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N)</span>
<span id="cb23-2">dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>trt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>id <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> N<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb23-3">dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>time)</span>
<span id="cb23-4">dd0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model.matrix</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> id<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>trt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>t, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dd0))</span>
<span id="cb23-5">dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>time)</span>
<span id="cb23-6">dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Trt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>trt)</span>
<span id="cb23-7">dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Y0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(dd0[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(Beta)]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> Beta</span>
<span id="cb23-8">dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Y0.null <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(dd0[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(Beta.null)]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> Beta.null</span>
<span id="cb23-9"></span>
<span id="cb23-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(dd0, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>Y0, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color=</span>Trt)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://mcdonohue.github.io/blog/posts/constrained-longitudinal-data-analysis/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="1142"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20130318</span>)</span>
<span id="cb24-2">dd1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merge</span>(dd0, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb24-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N, </span>
<span id="cb24-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ran.intercept =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd=</span>sigma_random_intercept),</span>
<span id="cb24-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ran.slope     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd=</span>sigma_random_slope)</span>
<span id="cb24-6">))</span>
<span id="cb24-7">dd1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>e <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(dd1), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd=</span>sigma_residual)</span>
<span id="cb24-8"></span>
<span id="cb24-9">dd1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(dd1, Y0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ran.intercept <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ran.slope<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>time <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> e)</span>
<span id="cb24-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(dd1, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>Y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color=</span>Trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> id)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://mcdonohue.github.io/blog/posts/constrained-longitudinal-data-analysis/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="1142"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># unconstrained:</span></span>
<span id="cb25-2">fit.LDA <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lme</span>(Y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>trt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">random=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>id, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>dd1)</span>
<span id="cb25-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">printCoefmat</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(fit.LDA)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>tTable)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>                Value Std.Error        DF t-value   p-value    
(Intercept)   0.94916   0.13384 598.00000  7.0919 3.749e-12 ***
trt           0.14495   0.18928 198.00000  0.7658  0.444712    
time          0.31195   0.11256 598.00000  2.7714  0.005755 ** 
trt:time      0.48156   0.15918 598.00000  3.0251  0.002592 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># constrained:</span></span>
<span id="cb27-2">fit.cLDA <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lme</span>(Y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">random=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>id, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>dd1)</span>
<span id="cb27-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">printCoefmat</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(fit.cLDA)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>tTable)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>                 Value  Std.Error         DF t-value   p-value    
(Intercept)   1.021636   0.094539 598.000000 10.8065 &lt; 2.2e-16 ***
time          0.290433   0.108989 598.000000  2.6648 0.0079117 ** 
time:trt      0.524594   0.148934 598.000000  3.5223 0.0004604 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1">newdat0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand.grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trt=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time=</span>Time)</span>
<span id="cb29-2"></span>
<span id="cb29-3">mods <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">LDA =</span> fit.LDA, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cLDA=</span>fit.cLDA)</span>
<span id="cb29-4"></span>
<span id="cb29-5">pd <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">do.call</span>(rbind, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(mods), <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(name){</span>
<span id="cb29-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(newdat0, </span>
<span id="cb29-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Y=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(mods[[name]], newdat0, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">level=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>),</span>
<span id="cb29-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> name</span>
<span id="cb29-9">  ) </span>
<span id="cb29-10">}))</span>
<span id="cb29-11">pd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Trt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(pd<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>trt)</span>
<span id="cb29-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(pd, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>Y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> Trt)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_grid</span>(.<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>model)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://mcdonohue.github.io/blog/posts/constrained-longitudinal-data-analysis/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="1142"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="simulate-1" class="level2">
<h2 class="anchored" data-anchor-id="simulate-1">Simulate</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20130318</span>)</span>
<span id="cb30-2">results <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">do.call</span>(rbind, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mclapply</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x){</span>
<span id="cb30-3">  dd1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merge</span>(dd0, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb30-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N,</span>
<span id="cb30-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ran.intercept =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd=</span>sigma_random_intercept),</span>
<span id="cb30-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ran.slope     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd=</span>sigma_random_slope)</span>
<span id="cb30-7">  ))</span>
<span id="cb30-8">  dd1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>e <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(dd1), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd=</span>sigma_residual)</span>
<span id="cb30-9">  dd1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(dd1, Y0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ran.intercept <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ran.slope<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>time <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> e)</span>
<span id="cb30-10">  dd1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Y.null <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(dd1, Y0.null <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ran.intercept <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> ran.slope<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>time <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> e)</span>
<span id="cb30-11"></span>
<span id="cb30-12">  fits.cts <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb30-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">LDA.power  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lme</span>(     Y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">random=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>id, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>dd1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">control=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lmeControl</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">returnObject=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)),</span>
<span id="cb30-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">LDA.alpha  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lme</span>(Y.null<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">random=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>id, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>dd1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">control=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lmeControl</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">returnObject=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)),</span>
<span id="cb30-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cLDA.power =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lme</span>(     Y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">random=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>id, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>dd1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">control=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lmeControl</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">returnObject=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)),</span>
<span id="cb30-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cLDA.alpha =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lme</span>(Y.null<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">random=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>id, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>dd1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">control=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lmeControl</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">returnObject=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span>
<span id="cb30-17">  )</span>
<span id="cb30-18"></span>
<span id="cb30-19">  fits.cat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb30-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">LDA.power  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gls</span>(     Y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>(t1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>t2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>t3)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>dd1,</span>
<span id="cb30-21">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">correlation =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">corCompSymm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">form =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> time <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> id), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">varIdent</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">form =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> time)),</span>
<span id="cb30-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">LDA.alpha  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gls</span>(Y.null<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>(t1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>t2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>t3)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>dd1,</span>
<span id="cb30-23">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">correlation =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">corCompSymm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">form =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> time <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> id), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">varIdent</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">form =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> time)),</span>
<span id="cb30-24">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cLDA.power =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gls</span>(     Y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>t1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>t2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>t3<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>(t1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>t2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>t3)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>dd1,</span>
<span id="cb30-25">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">correlation =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">corCompSymm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">form =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> time <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> id), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">varIdent</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">form =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> time)),</span>
<span id="cb30-26">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cLDA.alpha =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gls</span>(Y.null<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>t1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>t2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>t3<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>(t1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>t2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>t3)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>dd1,</span>
<span id="cb30-27">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">correlation =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">corCompSymm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">form =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> time <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> id), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">varIdent</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">form =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> time))</span>
<span id="cb30-28">  )</span>
<span id="cb30-29"></span>
<span id="cb30-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">slopes =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unlist</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(fits.cts, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(fit){</span>
<span id="cb30-31">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(fit)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>tTable[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'time:trt'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'p-value'</span>]</span>
<span id="cb30-32">    })),</span>
<span id="cb30-33">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">contrasts.cts =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unlist</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(fits.cts[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(fit){</span>
<span id="cb30-34">      pv <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">try</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrast</span>(fit,</span>
<span id="cb30-35">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">a=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time=</span>Time[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(Time)], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trt=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>),</span>
<span id="cb30-36">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time=</span>Time[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(Time)], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trt=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Pvalue), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">silent =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb30-37">      <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">class</span>(pv) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'try-error'</span>){</span>
<span id="cb30-38">        fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">update</span>(fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">random=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>id)</span>
<span id="cb30-39">        pv <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">try</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrast</span>(fit,</span>
<span id="cb30-40">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">a=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time=</span>Time[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(Time)], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trt=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>),</span>
<span id="cb30-41">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time=</span>Time[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(Time)], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trt=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Pvalue), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">silent =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb30-42">      }</span>
<span id="cb30-43">      pv</span>
<span id="cb30-44">    })),</span>
<span id="cb30-45">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">contrasts.cat =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb30-46">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unlist</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(fits.cat[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(fit){</span>
<span id="cb30-47">        pv <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">try</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrast</span>(fit,</span>
<span id="cb30-48">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">a=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t1=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t2=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t3=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trt=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>),</span>
<span id="cb30-49">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t1=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t2=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t3=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trt=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Pvalue), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">silent =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb30-50">        pv</span>
<span id="cb30-51">      })),</span>
<span id="cb30-52">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unlist</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(fits.cat[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>], <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(fit){</span>
<span id="cb30-53">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(fit)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>tTable[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'t3:trt'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'p-value'</span>]</span>
<span id="cb30-54">      }))</span>
<span id="cb30-55">    )</span>
<span id="cb30-56">  )</span>
<span id="cb30-57">}, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mc.cores =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))</span></code></pre></div></div>
</div>
</section>
<section id="simulated-power-results-1" class="level2">
<h2 class="anchored" data-anchor-id="simulated-power-results-1">Simulated power results (%)</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1">knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Pr(p&lt;0.05)'</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(results, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x){</span>
<span id="cb31-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(x)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb31-3">})))</span></code></pre></div></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;"></th>
<th style="text-align: right;">Pr(p&lt;0.05)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">slopes.LDA.power</td>
<td style="text-align: right;">79.74</td>
</tr>
<tr class="even">
<td style="text-align: left;">slopes.LDA.alpha</td>
<td style="text-align: right;">5.03</td>
</tr>
<tr class="odd">
<td style="text-align: left;">slopes.cLDA.power</td>
<td style="text-align: right;">81.65</td>
</tr>
<tr class="even">
<td style="text-align: left;">slopes.cLDA.alpha</td>
<td style="text-align: right;">5.08</td>
</tr>
<tr class="odd">
<td style="text-align: left;">contrasts.cts.LDA.power</td>
<td style="text-align: right;">81.12</td>
</tr>
<tr class="even">
<td style="text-align: left;">contrasts.cts.LDA.alpha</td>
<td style="text-align: right;">5.96</td>
</tr>
<tr class="odd">
<td style="text-align: left;">contrasts.cat.LDA.power</td>
<td style="text-align: right;">81.09</td>
</tr>
<tr class="even">
<td style="text-align: left;">contrasts.cat.LDA.alpha</td>
<td style="text-align: right;">5.51</td>
</tr>
<tr class="odd">
<td style="text-align: left;">contrasts.cat.cLDA.power</td>
<td style="text-align: right;">87.53</td>
</tr>
<tr class="even">
<td style="text-align: left;">contrasts.cat.cLDA.alpha</td>
<td style="text-align: right;">10.93</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
</section>
<section id="pre-post-design-simulated-with-fixed-variance-of-pre-post-difference-and-various-pre-post-correlations" class="level1">
<h1>Pre-post design simulated with fixed variance of pre-post difference and various pre-post correlations</h1>
<section id="simulation-settings-2" class="level2">
<h2 class="anchored" data-anchor-id="simulation-settings-2">Simulation settings</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1">Time <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb32-2">Beta <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'(Intercept)'</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0514</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt:time'</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0514</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.30</span>)</span>
<span id="cb32-3">Beta.null <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'(Intercept)'</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0514</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt:time'</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb32-4">corr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>)</span>
<span id="cb32-5">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># per group</span></span>
<span id="cb32-6">N <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>n</span>
<span id="cb32-7">sd_diff <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0309</span></span></code></pre></div></div>
</div>
</section>
<section id="analytic-calculations-2" class="level2">
<h2 class="anchored" data-anchor-id="analytic-calculations-2">Analytic calculations</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">power.t.test</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n=</span>n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">delta=</span>Beta[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt:time'</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd=</span>sd_diff)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
     Two-sample t test power calculation 

              n = 64
          delta = 0.01542
             sd = 0.0309
      sig.level = 0.05
          power = 0.7999361
    alternative = two.sided

NOTE: n is number in *each* group</code></pre>
</div>
</div>
</section>
<section id="set-up-data-for-simulation-2" class="level2">
<h2 class="anchored" data-anchor-id="set-up-data-for-simulation-2">Set up data for simulation</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb35" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1">dd0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand.grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> Time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N)</span>
<span id="cb35-2">dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>trt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>id <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> N<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb35-3">dd0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model.matrix</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> id<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>trt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dd0))</span>
<span id="cb35-4">dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Trt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>trt)</span>
<span id="cb35-5">dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Y0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(dd0[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(Beta)]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> Beta</span>
<span id="cb35-6">dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Y0.null <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(dd0[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(Beta.null)]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> Beta.null</span></code></pre></div></div>
</div>
</section>
<section id="simulate-2" class="level2">
<h2 class="anchored" data-anchor-id="simulate-2">Simulate</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20130318</span>)</span>
<span id="cb36-2">results <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">do.call</span>(rbind, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mclapply</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x){</span>
<span id="cb36-3">  ll <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(corr, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(rho){</span>
<span id="cb36-4">    sd_resid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(sd_diff<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>rho)))</span>
<span id="cb36-5">    cov_resid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> rho <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> sd_resid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb36-6">    resids <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gather</span>(</span>
<span id="cb36-7">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb36-8">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N,</span>
<span id="cb36-9">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rmvnorm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n=</span>N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(sd_resid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,cov_resid,cov_resid,sd_resid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))), </span>
<span id="cb36-10">      time, e, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb36-11">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gsub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'X'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>, time))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb36-12">    dd1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merge</span>(dd0, resids, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'id'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'time'</span>))</span>
<span id="cb36-13">    dd1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(dd1, Y0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> e)</span>
<span id="cb36-14">    dd1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Y.null <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(dd1, Y0.null <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> e)</span>
<span id="cb36-15">    ddw <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merge</span>(</span>
<span id="cb36-16">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subset</span>(dd1, time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'id'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y.null'</span>)),</span>
<span id="cb36-17">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subset</span>(dd1, time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'id'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Y.null'</span>)),</span>
<span id="cb36-18">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'id'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt'</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">suffixes=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'.0'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'.1'</span>)</span>
<span id="cb36-19">    )</span>
<span id="cb36-20">    ddw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(ddw, Y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>Y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.0</span>)</span>
<span id="cb36-21">    ddw<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>d.null <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(ddw, Y.null<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>Y.null<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.0</span>)</span>
<span id="cb36-22"></span>
<span id="cb36-23">    prepost <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb36-24">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ttest.power   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t.test</span>(         d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>ddw)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p.value,</span>
<span id="cb36-25">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ttest.alpha   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t.test</span>(    d.null<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>ddw)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p.value,</span>
<span id="cb36-26">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ancova1.power =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(     d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>Y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.0</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>ddw))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>coef[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Pr(&gt;|t|)'</span>],</span>
<span id="cb36-27">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ancova1.alpha =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(d.null<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>Y.null<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.0</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>ddw))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>coef[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Pr(&gt;|t|)'</span>],</span>
<span id="cb36-28">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ancova2.power =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(     d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale</span>(Y<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.0</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>ddw))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>coef[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Pr(&gt;|t|)'</span>],</span>
<span id="cb36-29">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ancova2.alpha =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(d.null<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale</span>(Y.null<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.0</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>ddw))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>coef[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Pr(&gt;|t|)'</span>]</span>
<span id="cb36-30">    )</span>
<span id="cb36-31"></span>
<span id="cb36-32">    fits <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb36-33">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">LDA.power  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lme</span>(     Y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">random=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>id, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>dd1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">control=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lmeControl</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">returnObject=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)),</span>
<span id="cb36-34">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">LDA.alpha  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lme</span>(Y.null<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">random=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>id, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>dd1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">control=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lmeControl</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">returnObject=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)),</span>
<span id="cb36-35">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cLDA.power =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lme</span>(     Y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">random=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>id, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>dd1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">control=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lmeControl</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">returnObject=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)),</span>
<span id="cb36-36">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cLDA.alpha =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lme</span>(Y.null<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">random=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>id, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>dd1, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">control=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lmeControl</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">returnObject=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span>
<span id="cb36-37">    )</span>
<span id="cb36-38"></span>
<span id="cb36-39">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unlist</span>(prepost),</span>
<span id="cb36-40">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">slopes =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unlist</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(fits, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(fit){</span>
<span id="cb36-41">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(fit)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>tTable[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'time:trt'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'p-value'</span>]</span>
<span id="cb36-42">      })),</span>
<span id="cb36-43">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">contrasts =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unlist</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(fits[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(fit){</span>
<span id="cb36-44">        pv <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">try</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrast</span>(fit,</span>
<span id="cb36-45">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">a=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time=</span>Time[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(Time)], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trt=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>),</span>
<span id="cb36-46">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time=</span>Time[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(Time)], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trt=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Pvalue), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">silent =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb36-47">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">class</span>(pv) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'try-error'</span>){</span>
<span id="cb36-48">          fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">update</span>(fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">random=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span>id)</span>
<span id="cb36-49">          pv <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">try</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrast</span>(fit,</span>
<span id="cb36-50">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">a=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time=</span>Time[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(Time)], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trt=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>),</span>
<span id="cb36-51">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time=</span>Time[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(Time)], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trt=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Pvalue), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">silent =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb36-52">        }</span>
<span id="cb36-53">        pv</span>
<span id="cb36-54">      }))</span>
<span id="cb36-55">    )</span>
<span id="cb36-56">  })</span>
<span id="cb36-57">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(ll) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> corr</span>
<span id="cb36-58">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unlist</span>(ll)</span>
<span id="cb36-59">}, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mc.cores =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))</span></code></pre></div></div>
</div>
</section>
<section id="simulated-power-results-2" class="level2">
<h2 class="anchored" data-anchor-id="simulated-power-results-2">Simulated power results (%)</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb37-1">knitr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Pr(p&lt;0.05)'</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(results, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x){</span>
<span id="cb37-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(x)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb37-3">})))</span></code></pre></div></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;"></th>
<th style="text-align: right;">Pr(p&lt;0.05)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">0.2.ttest.power</td>
<td style="text-align: right;">68.03</td>
</tr>
<tr class="even">
<td style="text-align: left;">0.2.ttest.alpha</td>
<td style="text-align: right;">4.87</td>
</tr>
<tr class="odd">
<td style="text-align: left;">0.2.ancova1.power</td>
<td style="text-align: right;">58.75</td>
</tr>
<tr class="even">
<td style="text-align: left;">0.2.ancova1.alpha</td>
<td style="text-align: right;">4.73</td>
</tr>
<tr class="odd">
<td style="text-align: left;">0.2.ancova2.power</td>
<td style="text-align: right;">58.82</td>
</tr>
<tr class="even">
<td style="text-align: left;">0.2.ancova2.alpha</td>
<td style="text-align: right;">4.73</td>
</tr>
<tr class="odd">
<td style="text-align: left;">0.2.slopes.LDA.power</td>
<td style="text-align: right;">67.97</td>
</tr>
<tr class="even">
<td style="text-align: left;">0.2.slopes.LDA.alpha</td>
<td style="text-align: right;">4.87</td>
</tr>
<tr class="odd">
<td style="text-align: left;">0.2.slopes.cLDA.power</td>
<td style="text-align: right;">58.13</td>
</tr>
<tr class="even">
<td style="text-align: left;">0.2.slopes.cLDA.alpha</td>
<td style="text-align: right;">4.85</td>
</tr>
<tr class="odd">
<td style="text-align: left;">0.2.contrasts.LDA.power</td>
<td style="text-align: right;">58.24</td>
</tr>
<tr class="even">
<td style="text-align: left;">0.2.contrasts.LDA.alpha</td>
<td style="text-align: right;">4.59</td>
</tr>
<tr class="odd">
<td style="text-align: left;">0.5.ttest.power</td>
<td style="text-align: right;">67.45</td>
</tr>
<tr class="even">
<td style="text-align: left;">0.5.ttest.alpha</td>
<td style="text-align: right;">5.17</td>
</tr>
<tr class="odd">
<td style="text-align: left;">0.5.ancova1.power</td>
<td style="text-align: right;">67.18</td>
</tr>
<tr class="even">
<td style="text-align: left;">0.5.ancova1.alpha</td>
<td style="text-align: right;">4.97</td>
</tr>
<tr class="odd">
<td style="text-align: left;">0.5.ancova2.power</td>
<td style="text-align: right;">67.29</td>
</tr>
<tr class="even">
<td style="text-align: left;">0.5.ancova2.alpha</td>
<td style="text-align: right;">4.97</td>
</tr>
<tr class="odd">
<td style="text-align: left;">0.5.slopes.LDA.power</td>
<td style="text-align: right;">67.41</td>
</tr>
<tr class="even">
<td style="text-align: left;">0.5.slopes.LDA.alpha</td>
<td style="text-align: right;">5.17</td>
</tr>
<tr class="odd">
<td style="text-align: left;">0.5.slopes.cLDA.power</td>
<td style="text-align: right;">67.11</td>
</tr>
<tr class="even">
<td style="text-align: left;">0.5.slopes.cLDA.alpha</td>
<td style="text-align: right;">5.08</td>
</tr>
<tr class="odd">
<td style="text-align: left;">0.5.contrasts.LDA.power</td>
<td style="text-align: right;">67.42</td>
</tr>
<tr class="even">
<td style="text-align: left;">0.5.contrasts.LDA.alpha</td>
<td style="text-align: right;">5.03</td>
</tr>
<tr class="odd">
<td style="text-align: left;">0.7.ttest.power</td>
<td style="text-align: right;">67.72</td>
</tr>
<tr class="even">
<td style="text-align: left;">0.7.ttest.alpha</td>
<td style="text-align: right;">5.02</td>
</tr>
<tr class="odd">
<td style="text-align: left;">0.7.ancova1.power</td>
<td style="text-align: right;">67.82</td>
</tr>
<tr class="even">
<td style="text-align: left;">0.7.ancova1.alpha</td>
<td style="text-align: right;">5.05</td>
</tr>
<tr class="odd">
<td style="text-align: left;">0.7.ancova2.power</td>
<td style="text-align: right;">67.99</td>
</tr>
<tr class="even">
<td style="text-align: left;">0.7.ancova2.alpha</td>
<td style="text-align: right;">5.06</td>
</tr>
<tr class="odd">
<td style="text-align: left;">0.7.slopes.LDA.power</td>
<td style="text-align: right;">67.64</td>
</tr>
<tr class="even">
<td style="text-align: left;">0.7.slopes.LDA.alpha</td>
<td style="text-align: right;">5.02</td>
</tr>
<tr class="odd">
<td style="text-align: left;">0.7.slopes.cLDA.power</td>
<td style="text-align: right;">67.78</td>
</tr>
<tr class="even">
<td style="text-align: left;">0.7.slopes.cLDA.alpha</td>
<td style="text-align: right;">4.95</td>
</tr>
<tr class="odd">
<td style="text-align: left;">0.7.contrasts.LDA.power</td>
<td style="text-align: right;">55.50</td>
</tr>
<tr class="even">
<td style="text-align: left;">0.7.contrasts.LDA.alpha</td>
<td style="text-align: right;">5.09</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>So the t-test power is fixed at 80% (by design), the unconstrained LDA has nearly identical power to t-test; while ANCOVA and constrained LDA have similar power (always better power than t-test, but power decreases with increasing pre-post correlation). ANCOVA2 has similar power as ANCOVA1 (not surprising since no interaction between baseline outcome and group was simulated).</p>
<p>This confirms that the cLDA is preferred whenever we have two timepoints in a randomized study; just as ANCOVA is always preferred to t-test. Of course, cLDA has the added advantage of accommodating missing data.</p>


</section>
</section>

 ]]></description>
  <category>mixed-effects</category>
  <category>lme</category>
  <category>nlme</category>
  <category>R</category>
  <category>constrained longitudinal data analysis</category>
  <category>power</category>
  <category>simulation</category>
  <category>pre-post</category>
  <guid>https://mcdonohue.github.io/blog/posts/constrained-longitudinal-data-analysis/</guid>
  <pubDate>Fri, 21 Oct 2016 07:00:00 GMT</pubDate>
</item>
<item>
  <title>Cross-Validation of Optimized Composites for Preclinical Alzheimer’s</title>
  <link>https://mcdonohue.github.io/blog/posts/aaic-2016-toronto/</link>
  <description><![CDATA[ 





<section id="slides-and-poster-from-aaic-2016-toronto" class="level2">
<h2 class="anchored" data-anchor-id="slides-and-poster-from-aaic-2016-toronto">Slides and poster from AAIC 2016, Toronto</h2>
<p><a href="AAIC_PIA_2016_composite_cv.pdf">Slides from PIA Day at AAIC 2016: Clinical Trials Advancement and Methods</a></p>
<p><a href="AAIC_poster_2016.pdf">Poster P3-034</a></p>


</section>

 ]]></description>
  <category>AAIC2016</category>
  <category>composites</category>
  <category>cross-validation</category>
  <category>preclincal AD</category>
  <guid>https://mcdonohue.github.io/blog/posts/aaic-2016-toronto/</guid>
  <pubDate>Mon, 25 Jul 2016 07:00:00 GMT</pubDate>
</item>
<item>
  <title>GLS, MMRM, and Power</title>
  <link>https://mcdonohue.github.io/blog/posts/gls-mmrm-power/</link>
  <description><![CDATA[ 





<section id="power-for-mmrm" class="level1">
<h1>Power for MMRM</h1>
<p>We use the power calculation formula of <a href="http://dx.doi.org/10.2202/1557-4679.1098">Lu, Luo and Chen (2008)</a> for Mixed Models of Repeated Measures, which accomodates general missing data patterns. This formula is implemented in the <a href="http://cran.r-project.org/web/packages/longpower/index.html">longpower</a> package. We also verify the formula with a simple simulation.</p>
<section id="load-packages" class="level2">
<h2 class="anchored" data-anchor-id="load-packages">Load packages</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(longpower)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(nlme)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(mvtnorm)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(parallel)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(contrast)</span></code></pre></div></div>
</div>
</section>
<section id="simulation-settings" class="level2">
<h2 class="anchored" data-anchor-id="simulation-settings">Simulation settings</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">gls_sigma <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb2-2">gls_weights <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.75</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>)</span>
<span id="cb2-3">placebo_mean <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb2-4">active_mean <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.8</span>)</span>
<span id="cb2-5">retention <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>)</span>
<span id="cb2-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># compound symmetric R</span></span>
<span id="cb2-7">R <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb2-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag</span>(R) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb2-9">N <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span></span></code></pre></div></div>
</div>
</section>
<section id="analytic-calculations" class="level2">
<h2 class="anchored" data-anchor-id="analytic-calculations">Analytic calculations</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">power.mmrm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">power=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.80</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ra =</span> retention, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Ra=</span>R, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N=</span>N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigmaa =</span> gls_sigma<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>gls_weights[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>])</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
     Power for Mixed Model of Repeated Measures (Lu, Luo, &amp; Chen, 2008) 

             n1 = 100
             n2 = 100
     retention1 = 1.0, 0.9, 0.8, 0.7
     retention2 = 1.0, 0.9, 0.8, 0.7
          delta = 1.798283
      sig.level = 0.05
          power = 0.8
    alternative = two.sided</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">power.t.test</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">power=</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.80</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> N<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>retention[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> gls_sigma<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>gls_weights[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>])</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
     Two-sample t test power calculation 

              n = 70
          delta = 1.907529
             sd = 4
      sig.level = 0.05
          power = 0.8
    alternative = two.sided

NOTE: n is number in *each* group</code></pre>
</div>
</div>
<p>The t-test delta is larger, which is expect given the conservative handling of the assumed retention rate. The MMRM makes use of partial informatin from dropouts.</p>
</section>
<section id="set-up-data-for-simulation" class="level2">
<h2 class="anchored" data-anchor-id="set-up-data-for-simulation">Set up data for simulation</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">dd0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand.grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N)</span>
<span id="cb7-2">dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Time <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>time)</span>
<span id="cb7-3">dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>trt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>id <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> N<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb7-4">dd0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">model.matrix</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>id<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>trt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>Time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dd0))</span>
<span id="cb7-5">dd0[,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt:time1'</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(dd0, trt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>time1)</span>
<span id="cb7-6">dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Trt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>trt)</span>
<span id="cb7-7">dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>time <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.factor</span>(dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Time)</span>
<span id="cb7-8">dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Y0[dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>trt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subset</span>(dd0, trt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt:time1'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt:time2'</span>, </span>
<span id="cb7-9">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt:time3'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'trt:time4'</span>))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> active_mean</span>
<span id="cb7-10">dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Y0[dd0<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>trt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subset</span>(dd0, trt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'time1'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'time2'</span>, </span>
<span id="cb7-11">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'time3'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'time4'</span>))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> placebo_mean</span>
<span id="cb7-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(dd0, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>Time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>Y0, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color=</span>Trt)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://mcdonohue.github.io/blog/posts/gls-mmrm-power/index_files/figure-html/sim_data_setup-1.png" class="img-fluid figure-img" width="1142"></p>
</figure>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20130318</span>)</span>
<span id="cb8-2">dd1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merge</span>(dd0, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">drop =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>))))</span>
<span id="cb8-3">dd1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>e <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rmvnorm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n=</span>N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma=</span>gls_sigma<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag</span>(gls_weights)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span>R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag</span>(gls_weights))))</span>
<span id="cb8-4">dd1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(dd1, Y0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> e)</span>
<span id="cb8-5">dd1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subset</span>(dd1, Time <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> drop)</span>
<span id="cb8-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(dd1, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>Time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>Y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color=</span>Trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> id)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://mcdonohue.github.io/blog/posts/gls-mmrm-power/index_files/figure-html/sim_data_setup-2.png" class="img-fluid figure-img" width="1142"></p>
</figure>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">fit0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gls</span>(Y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>(time1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>time2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>time3<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>time4)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>(time1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>time2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>time3<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>time4)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>trt, </span>
<span id="cb9-2">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">correlation =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">corCompSymm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">form =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Time <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> id),</span>
<span id="cb9-3">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">varIdent</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">form =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> Time),</span>
<span id="cb9-4">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>dd1)</span>
<span id="cb9-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(fit0)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>tTable</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>               Value Std.Error    t-value      p-value
time1     -0.2498166 0.1857705 -1.3447591 0.1791507434
time2     -0.7536542 0.2990315 -2.5203167 0.0119523864
time3     -0.3253077 0.3665450 -0.8874973 0.3751245464
time4     -0.8114158 0.4966029 -1.6339329 0.1027351200
time1:trt  0.2980273 0.2627192  1.1343948 0.2570281147
time2:trt  1.5625956 0.4238492  3.6866786 0.0002452827
time3:trt  1.3327856 0.5136925  2.5945204 0.0096761153
time4:trt  2.7390980 0.6883514  3.9792145 0.0000765598</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">fit1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gls</span>(Y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>Trt, </span>
<span id="cb11-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">correlation =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">corCompSymm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">form =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Time <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> id),</span>
<span id="cb11-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">varIdent</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">form =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> Time),</span>
<span id="cb11-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data=</span>dd1)</span>
<span id="cb11-5"></span>
<span id="cb11-6">(ctrast <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrast</span>(fit1, </span>
<span id="cb11-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(dd1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>time), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Trt=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(dd1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Trt)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]),</span>
<span id="cb11-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(dd1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>time), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Trt=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(dd1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Trt)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>gls model parameter contrast

  Contrast      S.E.      Lower     Upper    t  df Pr(&gt;|t|)
 0.2980273 0.2627192 -0.2178097 0.8138642 1.13 681   0.2570
 1.5625955 0.4238486  0.7303884 2.3948026 3.69 681   0.0002
 1.3327868 0.5136947  0.3241710 2.3414026 2.59 681   0.0097
 2.7390986 0.6883513  1.3875528 4.0906444 3.98 681   0.0001</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">newdat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(</span>
<span id="cb13-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrast</span>(fit1, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(dd1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>time), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Trt=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(dd1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Trt)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])),</span>
<span id="cb13-3">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Trt =</span> Trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Estimate =</span> Contrast, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">SE =</span> SE,</span>
<span id="cb13-4">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Lower =</span> Lower, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Upper =</span> Upper, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'t-value'</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> testStat, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'p-value'</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> Pvalue)</span>
<span id="cb13-5">       ),</span>
<span id="cb13-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contrast</span>(fit1, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(dd1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>time), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Trt=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">levels</span>(dd1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Trt)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>])),</span>
<span id="cb13-7">       <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">time =</span> time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Trt =</span> Trt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Estimate =</span> Contrast, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">SE =</span> SE,</span>
<span id="cb13-8">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Lower =</span> Lower, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Upper =</span> Upper, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'t-value'</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> testStat, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'p-value'</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> Pvalue)</span>
<span id="cb13-9">       )</span>
<span id="cb13-10">  )</span>
<span id="cb13-11">newdat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Time <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(newdat<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>time)</span>
<span id="cb13-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(newdat, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>Estimate, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>Time, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> Trt)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> Upper, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin =</span> Lower), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'identity'</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://mcdonohue.github.io/blog/posts/gls-mmrm-power/index_files/figure-html/sim_data_setup-3.png" class="img-fluid figure-img" width="1142"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="simulate-using-parallel" class="level2">
<h2 class="anchored" data-anchor-id="simulate-using-parallel">Simulate using parallel</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20130318</span>)  </span>
<span id="cb14-2">results <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mclapply</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x){</span>
<span id="cb14-3">    dd1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merge</span>(dd0, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">drop =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>))))</span>
<span id="cb14-4">    dd1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>e <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rmvnorm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n=</span>N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sigma=</span>gls_sigma<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag</span>(gls_weights)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span>R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag</span>(gls_weights))))</span>
<span id="cb14-5">    dd1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(dd1, Y0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> e)</span>
<span id="cb14-6">    dd1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subset</span>(dd1, Time <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> drop)</span>
<span id="cb14-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">try</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">update</span>(fit0, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dd1))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>tTable[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'time4:trt'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'p-value'</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">silent =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb14-8">}, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mc.cores =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div></div>
</div>
</section>
<section id="simulated-power-results" class="level2">
<h2 class="anchored" data-anchor-id="simulated-power-results">Simulated power results</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(results <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(results)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>79.99</code></pre>
</div>
</div>


</section>
</section>

 ]]></description>
  <category>mixed-effects</category>
  <category>MMRM</category>
  <category>gls</category>
  <category>lme</category>
  <category>nlme</category>
  <category>R</category>
  <category>power</category>
  <category>simulation</category>
  <guid>https://mcdonohue.github.io/blog/posts/gls-mmrm-power/</guid>
  <pubDate>Tue, 15 Apr 2014 07:00:00 GMT</pubDate>
</item>
<item>
  <title>Covariates and Power</title>
  <link>https://mcdonohue.github.io/blog/posts/covariates-and-power/</link>
  <description><![CDATA[ 





<p>We run some simple simulations to demonstrate the effect of covariates on power.</p>
<section id="example-binary-covariate-associated-with-outcome" class="level1">
<h1>Example: Binary covariate associated with outcome</h1>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">mean_response <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expand.grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">treatment =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'placebo'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'active'</span>),</span>
<span id="cb1-2">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gender =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'male'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'female'</span>))</span>
<span id="cb1-3">mean_response<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mean_response <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(mean_response<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>treatment <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'active'</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>)</span>
<span id="cb1-4">mean_response<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mean_response <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> mean_response<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mean_response <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(mean_response<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>gender <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'female'</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(mean_response, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(treatment, mean_response, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> gender)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb1-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'dodge'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'identity'</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggtitle</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Assumed mean response pattern"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://mcdonohue.github.io/blog/posts/covariates-and-power/index_files/figure-html/plot1-1.png" class="img-fluid figure-img" width="1142"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20130514</span>)</span>
<span id="cb2-2">nobs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span></span>
<span id="cb2-3">sim_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(mean_response, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>nobs))</span>
<span id="cb2-4">sim_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>noise <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n=</span>nobs)</span>
<span id="cb2-5">sim_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(sim_data, mean_response <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> noise)</span>
<span id="cb2-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t.test</span>(Y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>treatment, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> sim_data)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
    Welch Two Sample t-test

data:  Y by treatment
t = -1.8682, df = 116.35, p-value = 0.06425
alternative hypothesis: true difference in means between group placebo and group active is not equal to 0
95 percent confidence interval:
 -0.70109150  0.02046901
sample estimates:
mean in group placebo  mean in group active 
            0.4848632             0.8251744 </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>treatment <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> gender, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> sim_data))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
lm(formula = Y ~ treatment + gender, data = sim_data)

Residuals:
    Min      1Q  Median      3Q     Max 
-2.4407 -0.6779  0.1592  0.6109  2.8580 

Coefficients:
                Estimate Std. Error t value Pr(&gt;|t|)  
(Intercept)       0.3263     0.1564   2.086   0.0391 *
treatmentactive   0.3403     0.1806   1.885   0.0620 .
genderfemale      0.3172     0.1806   1.757   0.0816 .
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.989 on 117 degrees of freedom
Multiple R-squared:  0.05369,   Adjusted R-squared:  0.03751 
F-statistic: 3.319 on 2 and 117 DF,  p-value: 0.03963</code></pre>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(sim_data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(treatment, Y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> gender)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'dodge'</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggtitle</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Example simulated dataset"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://mcdonohue.github.io/blog/posts/covariates-and-power/index_files/figure-html/boxplot1-1.png" class="img-fluid figure-img" width="1142"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20130514</span>)</span>
<span id="cb7-2">nsim <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</span></span>
<span id="cb7-3">t_test_result <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> lm_result <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, nsim, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb7-4"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>nsim){</span>
<span id="cb7-5">  sim_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>noise <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n=</span>nobs)</span>
<span id="cb7-6">  sim_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(sim_data, mean_response <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> noise)</span>
<span id="cb7-7">  tt1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t.test</span>(Y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>treatment, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> sim_data)</span>
<span id="cb7-8">  lm1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lm</span>(Y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>treatment <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> gender, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> sim_data))</span>
<span id="cb7-9">  t_test_result[i, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(tt1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>estimate <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), tt1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>p.value)</span>
<span id="cb7-10">  lm_result[i, ] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> lm1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>coef[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'treatmentactive'</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Estimate'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Pr(&gt;|t|)'</span>)]</span>
<span id="cb7-11">}</span></code></pre></div></div>
</div>
<table class="caption-top table">
<colgroup>
<col style="width: 26%">
<col style="width: 45%">
<col style="width: 28%">
</colgroup>
<thead>
<tr class="header">
<th>S=10,000 Simulations</th>
<th>Simulated Power</th>
<th>Average treatment effect</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>t-test</td>
<td>77.26</td>
<td>0.5000603</td>
</tr>
<tr class="even">
<td>linear model with gender</td>
<td>77.73</td>
<td>0.5000603</td>
</tr>
</tbody>
</table>


</section>

 ]]></description>
  <category>covariates</category>
  <category>power</category>
  <category>simulation</category>
  <guid>https://mcdonohue.github.io/blog/posts/covariates-and-power/</guid>
  <pubDate>Tue, 14 May 2013 07:00:00 GMT</pubDate>
</item>
<item>
  <title>GLS, MMRM, and Missing Data</title>
  <link>https://mcdonohue.github.io/blog/posts/gls-mmrm-missing-data/</link>
  <description><![CDATA[ 





<section id="the-issue" class="level1">
<h1>The issue</h1>
<p>The <code>gls</code> routine in the <code>nlme</code> package is sensitive to the data order when using <code>corCompSymm</code>, particularly when data is missing. This is alluded to in the <code>corCompSymm</code> documentation. Below are some examples.</p>
<section id="no-missingness" class="level2">
<h2 class="anchored" data-anchor-id="no-missingness">No missingness</h2>
<p>When there is no missingness, there is no problem provided the data is sorted by time:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(nlme)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20130506</span>)</span>
<span id="cb1-3">fm1Orth.gls <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gls</span>( distance <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Sex <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">I</span>(age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>), </span>
<span id="cb1-4">  Orthodont,</span>
<span id="cb1-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">correlation =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">corSymm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">form =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> Subject),</span>
<span id="cb1-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">varIdent</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">form =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> age) )</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(fm1Orth.gls)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>tTable</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>                           Value  Std.Error   t-value      p-value
(Intercept)           24.9371232 0.47286660 52.736063 7.204876e-77
SexFemale             -2.2717427 0.74083959 -3.066443 2.760865e-03
I(age - 11)            0.8268037 0.08221771 10.056272 5.037413e-17
SexFemale:I(age - 11) -0.3504390 0.12881040 -2.720580 7.640131e-03</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">fm2Orth.gls <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gls</span>( distance <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Sex <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">I</span>(age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>), </span>
<span id="cb3-2">  Orthodont[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(Orthodont)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, ], <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># reverse order</span></span>
<span id="cb3-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">correlation =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">corSymm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">form =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> Subject),</span>
<span id="cb3-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">varIdent</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">form =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> age) )</span>
<span id="cb3-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(fm2Orth.gls)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>tTable <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># same as sorted</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>                           Value  Std.Error   t-value      p-value
(Intercept)           24.9371337 0.47286148 52.736658 7.196740e-77
SexFemale             -2.2717535 0.74083155 -3.066491 2.760460e-03
I(age - 11)            0.8268026 0.08221817 10.056203 5.039201e-17
SexFemale:I(age - 11) -0.3504379 0.12881111 -2.720557 7.640630e-03</code></pre>
</div>
</div>
<p>But if the data is not sorted by time, we get inconsistent results. In fact, here the estimation does not converge:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">fm3Orth.gls <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gls</span>( distance <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Sex <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">I</span>(age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>), </span>
<span id="cb5-2">  Orthodont[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(Orthodont), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>), ],</span>
<span id="cb5-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">correlation =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">corSymm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">form =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> Subject),</span>
<span id="cb5-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">varIdent</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">form =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> age) )</span></code></pre></div></div>
</div>
</section>
<section id="missingness" class="level2">
<h2 class="anchored" data-anchor-id="missingness">Missingness</h2>
<p>When there is missingness, sorting does not help:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generate some missingness</span></span>
<span id="cb6-2">Orthodont<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>missing <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>), </span>
<span id="cb6-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(Orthodont), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>), </span>
<span id="cb6-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb6-5"></span>
<span id="cb6-6">fm4Orth.gls <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gls</span>( distance <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Sex <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">I</span>(age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>), </span>
<span id="cb6-7">  Orthodont, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subset =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>missing,</span>
<span id="cb6-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">correlation =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">corSymm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">form =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> Subject),</span>
<span id="cb6-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">varIdent</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">form =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> age) )</span>
<span id="cb6-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(fm4Orth.gls)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>tTable</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>                           Value  Std.Error   t-value      p-value
(Intercept)           24.6507074 0.49253896 50.048239 1.936427e-66
SexFemale             -1.8766129 0.76806327 -2.443305 1.655051e-02
I(age - 11)            0.6845546 0.07834144  8.738091 1.420426e-13
SexFemale:I(age - 11) -0.1694028 0.11714233 -1.446128 1.516937e-01</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">fm5Orth.gls <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gls</span>( distance <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Sex <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">I</span>(age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>), </span>
<span id="cb8-2">  Orthodont[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(Orthodont)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, ], <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># reverse order</span></span>
<span id="cb8-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subset =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>missing,</span>
<span id="cb8-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">correlation =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">corSymm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">form =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> Subject),</span>
<span id="cb8-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">varIdent</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">form =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> age) )</span>
<span id="cb8-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(fm5Orth.gls)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>tTable</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>                           Value Std.Error   t-value      p-value
(Intercept)           24.7529072 0.4946600 50.040246 1.962908e-66
SexFemale             -2.0630955 0.7715183 -2.674072 8.932466e-03
I(age - 11)            0.7260246 0.0785608  9.241563 1.305493e-14
SexFemale:I(age - 11) -0.2256612 0.1175943 -1.918981 5.822792e-02</code></pre>
</div>
</div>
</section>
</section>
<section id="the-fix" class="level1">
<h1>The fix</h1>
<p>By default, corSymm(form = ~ 1 | Subject) uses the order of observations to estimate the correlation components. In this case, the (i,j) component of the correlation matrix represents the correlation between the ith and jth observation. If instead we want the correlation between the ith and jth time point to be represented, we need to use corSymm(form = ~ t | Subject), where t are the consecutive integers indexing time.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">Orthodont<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>t <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(Orthodont<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>age, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>)))</span>
<span id="cb10-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(Orthodont, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(t, age))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>   age
t    8 10 12 14
  1 27  0  0  0
  2  0 27  0  0
  3  0  0 27  0
  4  0  0  0 27</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">fm6Orth.gls <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gls</span>( distance <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Sex <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">I</span>(age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>), </span>
<span id="cb12-2">  Orthodont, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subset =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>missing,</span>
<span id="cb12-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">correlation =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">corSymm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">form =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> t <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> Subject),</span>
<span id="cb12-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">varIdent</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">form =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> age) )</span>
<span id="cb12-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(fm6Orth.gls)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>tTable</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>                           Value  Std.Error   t-value      p-value
(Intercept)           24.7022356 0.49498531 49.904987 2.470935e-66
SexFemale             -2.0520703 0.77463325 -2.649086 9.566568e-03
I(age - 11)            0.6883899 0.08674632  7.935667 6.270004e-12
SexFemale:I(age - 11) -0.1690610 0.12987781 -1.301693 1.964188e-01</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">fm7Orth.gls <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gls</span>( distance <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> Sex <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">I</span>(age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>), </span>
<span id="cb14-2">  Orthodont[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(Orthodont)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, ], </span>
<span id="cb14-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subset =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>missing,</span>
<span id="cb14-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">correlation =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">corSymm</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">form =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> t <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> Subject),</span>
<span id="cb14-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">varIdent</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">form =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> age) )</span>
<span id="cb14-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(fm7Orth.gls)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>tTable</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>                           Value  Std.Error   t-value      p-value
(Intercept)           24.7022356 0.49498315 49.905205 2.470016e-66
SexFemale             -2.0520687 0.77462980 -2.649096 9.566314e-03
I(age - 11)            0.6883909 0.08674715  7.935602 6.271927e-12
SexFemale:I(age - 11) -0.1690623 0.12987912 -1.301690 1.964198e-01</code></pre>
</div>
</div>
<p>Note this is not an issue with compound symmetry structure, corCompSymm, because the correlation parameters are exchangeable with respect to time.</p>


</section>

 ]]></description>
  <category>GLS</category>
  <category>MMRM</category>
  <category>mixed-effects</category>
  <category>lme</category>
  <category>nlme</category>
  <category>R</category>
  <guid>https://mcdonohue.github.io/blog/posts/gls-mmrm-missing-data/</guid>
  <pubDate>Mon, 06 May 2013 07:00:00 GMT</pubDate>
</item>
<item>
  <title>grace: Growth curves by Alternating Conditional Expectation</title>
  <link>https://mcdonohue.github.io/blog/posts/grace/</link>
  <description><![CDATA[ 





<section id="estimating-long-term-progression-from-short-term-data" class="level2">
<h2 class="anchored" data-anchor-id="estimating-long-term-progression-from-short-term-data">Estimating long-term progression from short-term data</h2>
<p>Diseases that progress over long periods of time are often studied by observing cohorts at different stages of disease for shorter periods of time. We apply an Alternating Conditional Expectation (ACE) algorithm to estimate long-term multivariate monotone growth curves from short-term observations with unknown relative observation times. The iterative algorithm is demonstrated with the movies below.</p>
<p>Slides from AAIC 2012 are available <a href="DonohueAAIC2012.pdf">here</a>.</p>
<p>An R package to fit this model is available from <a href="https://bitbucket.org/mdonohue/grace" class="uri">https://bitbucket.org/mdonohue/grace</a></p>
</section>
<section id="alzheimers-disease-data-amyloid" class="level1">
<h1>Alzheimer’s Disease Data (Amyloid+)</h1>
<section id="fit" class="level2">
<h2 class="anchored" data-anchor-id="fit">Fit</h2>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://mcdonohue.github.io/blog/posts/grace/index_files/figure-html/am-fit-1.gif" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="disease-time-shifts" class="level2">
<h2 class="anchored" data-anchor-id="disease-time-shifts">Disease Time Shifts</h2>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://mcdonohue.github.io/blog/posts/grace/index_files/figure-html/am-gamma-1.gif" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="residuals" class="level2">
<h2 class="anchored" data-anchor-id="residuals">Residuals</h2>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://mcdonohue.github.io/blog/posts/grace/index_files/figure-html/am-resid-1.gif" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="alzheimers-disease-data-apoe-epsilon4" class="level1">
<h1>Alzheimer’s Disease Data (Apoe <img src="https://latex.codecogs.com/png.latex?%5Cepsilon4+">)]</h1>
<section id="fit-1" class="level2">
<h2 class="anchored" data-anchor-id="fit-1">Fit</h2>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://mcdonohue.github.io/blog/posts/grace/index_files/figure-html/ap-fit-1.gif" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="disease-time-shifts-1" class="level2">
<h2 class="anchored" data-anchor-id="disease-time-shifts-1">Disease Time Shifts</h2>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://mcdonohue.github.io/blog/posts/grace/index_files/figure-html/ap-gamma-1.gif" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="residuals-1" class="level2">
<h2 class="anchored" data-anchor-id="residuals-1">Residuals</h2>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://mcdonohue.github.io/blog/posts/grace/index_files/figure-html/ap-resid-1.gif" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="simulated-data" class="level1">
<h1>Simulated data</h1>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://mcdonohue.github.io/blog/posts/grace/index_files/figure-html/sm-fit-1.gif" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<section id="disease-time-shifts-2" class="level2">
<h2 class="anchored" data-anchor-id="disease-time-shifts-2">Disease Time Shifts</h2>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://mcdonohue.github.io/blog/posts/grace/index_files/figure-html/sm-gamma-1.gif" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="residuals-2" class="level2">
<h2 class="anchored" data-anchor-id="residuals-2">Residuals</h2>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://mcdonohue.github.io/blog/posts/grace/index_files/figure-html/sm-resid-1.gif" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>


</section>
</section>

 ]]></description>
  <category>disease time</category>
  <category>grace</category>
  <guid>https://mcdonohue.github.io/blog/posts/grace/</guid>
  <pubDate>Wed, 18 Jul 2012 07:00:00 GMT</pubDate>
</item>
</channel>
</rss>
