<?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>Philipp K. Masur: Blog</title>
<link>https://philippmasur.de/blog/</link>
<atom:link href="https://philippmasur.de/blog/index.xml" rel="self" type="application/rss+xml"/>
<description>R, statistics and open science tutorials, research notes and news.</description>
<image>
<url>https://philippmasur.de/assets/img/og-default.png</url>
<title>Philipp K. Masur: Blog</title>
<link>https://philippmasur.de/blog/</link>
<height>76</height>
<width>144</width>
</image>
<generator>quarto-1.10.19</generator>
<lastBuildDate>Tue, 23 Jun 2026 22:00:00 GMT</lastBuildDate>
<item>
  <title>Why replication matters: Insights from replicating 10 online privacy studies</title>
  <link>https://philippmasur.de/blog/2026-06-24-why-replication-matters/</link>
  <description><![CDATA[ 

<!-- Icon sprite: 24×24 line icons, 1.6 stroke. Every shape has pathLength="1"
     so CSS can animate stroke-dashoffset 1 → 0 ("draw" the icon).
     Use: <svg class="pm-icon"><use href="#i-mail"/></svg> -->





<p>Several times, I tried to get a replication project funded. Every time, reviews came back with a the same results: a worthwhile idea, but not feasible. Often the concern were the costs, other times the sample size we would need. Overall, between the lines, there seemed to be a vague doubt about whether replication is really “original” enough to even deserve a grant. Well, times have changed now, but for the longest time, replication was (and probably still is) something our field admires in principle, but avoids in practice. In communication science, direct replications make up only around 1.8% of published work (Keating &amp; Totzkay, 2019). This is what happens when a thing is hard to fund, hard to publish, and slow to be cited.</p>
<p>So out of frustration, at some point, I stopped trying to get funds (Update: I finally managed to get a many-labs project funded!!!). Instead, I thought, let me show you that it is possible anyway. I had the idea to built a consecutive replication project inside a Master’s course at the VU. Over four years, each year’s group of students systematically replicated up to three of <strong>ten influential online privacy studies</strong>. These were all fielded through the same recruitment platform (CloudResearch) and all powered to detect the smallest effect size the original study had reported. In practice, this usually meant samples of more than N = 780 participants. We always preregistered the design, we reused the original measures, and we evaluated each study against the replication criteria proposed by LeBel et al.&nbsp;(2018): whether the replicated effect sizes were similar in direction and significance, as well as whether the original effect size fell inside our replication confidence interval.</p>
<p>The studies’ range was large and included several key privacy frameworks: the privacy calculus (Krasnova et al., 2010; Hallam &amp; Zanella, 2017), context collapse (Vitak, 2012), the privacy paradox (Dienlin &amp; Trepte, 2015), disclosure management (Masur &amp; Scharkow, 2016), privacy literacy (Park, 2013), privacy tool use (Litt, 2013), antecedents of self-disclosure (Zlatolas et al., 2017), and privacy fatigue and cynicism (Choi et al., 2018; Lutz et al., 2020). The project was very successful so far. The first three replications are now published in the <em>Journal of Communication</em> (<a href="https://doi.org/10.1093/joc/jqaf007">Link</a>; Masur &amp; Ranzini, 2025), and the two privacy cynicism studies have now been published as a chapter in the Edward Elgar volume on privacy cynicism, apathy, and resignation (<a href="https://www.elgaronline.com/edcollchap/book/9781035353248/chapter10.xml">Link</a>; Masur, 2026).</p>
<p>In this blogpost, I will not zoom in on specific findings related to the individual studies but rather describe the bigger picture that emerges when you look at all of them at once. I already presented this bigger picture at the Media Psychology Conference 2025 in Duisburg, but I thought I could share it here as well.</p>
<section id="the-big-picture-most-findings-survive" class="level2">
<h2 class="anchored" data-anchor-id="the-big-picture-most-findings-survive">The big picture: Most findings survive</h2>
<p>The overarching takeaway is quite reassuring, but perhaps also humbling. Cross-sectional, survey-based privacy findings broadly replicate. When we plot every original effect against its replication, the resulting scatterplot tracks the diagonal of perfect replication reasonably well, and most paths stay statistically significant (see Figure 1). Privacy research thus overall replicates. At least, if we look at the amount of individual paths that replicated in direction and significance. Yet, the devil lies in the details (read on for the surprises!).</p>
<p><a href="img/plot1-1024x815.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://philippmasur.de/blog/2026-06-24-why-replication-matters/img/plot1-1024x815.png" class="img-fluid"></a></p>
<p>One insights is that the effect sizes are slightly smaller on average and more wide-spread in the replications (see Figure 2). That shift should be familiar to anyone who has followed the reproducibility work in psychology (e.g., Open Science Collaboration, 2015). Even when an effect is real, the original literature tends to overestimate its size. An explanation might be that first significant results are the lucky ones, and luck does not (necessarily) replicate. So the honest summary is: the direction of our findings is mostly trustworthy, the exact effect sizes perhaps rather less so.</p>
<p><a href="img/plot3-1024x922.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://philippmasur.de/blog/2026-06-24-why-replication-matters/img/plot3-1024x922.png" class="img-fluid"></a></p>
</section>
<section id="not-every-finding-is-equally-replicable" class="level2">
<h2 class="anchored" data-anchor-id="not-every-finding-is-equally-replicable">Not every finding is equally replicable</h2>
<p>Averages hide the more interesting story, which is how much the studies differ from one another. At one end, some models mapped onto the new data almost flawlessly (e.g., Choi et al., 2018; Lutz et al., 2020; Hallam &amp; Zanella, 2017). At the other, a few core assumptions turned out to be far more volatile (e.g., Dienlin &amp; Trepte, 2015; Masur &amp; Scharkow, 2016). When you separate “same direction and significance” from “exact replication” (the original estimate is inside the replication’s confidence interval, see Figure 3), the gap between the two bars is itself the finding: many studies kept most of their paths pointing the right way, while far fewer reproduced the original effect sizes.</p>
<p><a href="img/plot5-1-1024x782.png" class="lightbox" data-gallery="quarto-lightbox-gallery-3"><img src="https://philippmasur.de/blog/2026-06-24-why-replication-matters/img/plot5-1-1024x782.png" class="img-fluid"></a></p>
<p>That said, the overall good picture should not fool ourselves into believing that everything “replicates” (at least by direction and significance). At times, while the overall amount of paths that replicated was high, primary hypotheses (rather than paths e.g., related to control variables) did not replicate (e.g., Dienlin &amp; Trepte, 2015; Zlatolas et al., 2017). Noteworthy, the often contested relationship between privacy concerns and self-disclosure (if non-significant mostly termed the “privacy paradox”) often did not replicate (e.g., Dienlin &amp; Trepte, 2015; Vitak, 2012; Hallam &amp; Zanella, 2017). Other times, key assumptions (e.g., benefits clearly predicting self-disclosure) also did not replicate (e.g., Krasnova et al., 2010). Looking at Figure 4, we do see quite some differences. Particularly if the scatterplots between original and replicated effect sizes widens, it deserves a closer look.</p>
<p><a href="img/plot2-1024x444.png" class="lightbox" data-gallery="quarto-lightbox-gallery-4"><img src="https://philippmasur.de/blog/2026-06-24-why-replication-matters/img/plot2-1024x444.png" class="img-fluid"></a></p>
</section>
<section id="what-makes-a-finding-replicable-predictors-of-replication-success" class="level2">
<h2 class="anchored" data-anchor-id="what-makes-a-finding-replicable-predictors-of-replication-success">What makes a finding replicable? Predictors of replication success</h2>
<p>Because we had ten studies rather than just one, we could ask questions that no single replication can answer: For example, what predicts whether a finding survives? Tentatively, three patterns stand out.</p>
<p>First, the statistical power of the original study seems to matter. The lower the original power, the less likely a path was to replicate, in direction and especially in magnitude (see Figure 5). Second, and relatedly, small original effects were the most likely to not replicate. Third, theoretical and measurement parsimony helped: studies built on a few clear mechanisms and previously validated scales were considerably more stable than complex models which were based on ad-hoc, single-study operationalizations. Taken together, these point to a single practical lesson. Replicability is perhaps not that mysterious. It is largely dependent on adequate power, realistic effect-size expectations, and proper measurement (as well as good theory, of course).</p>
<p><a href="img/plot4-1024x469.png" class="lightbox" data-gallery="quarto-lightbox-gallery-5"><img src="https://philippmasur.de/blog/2026-06-24-why-replication-matters/img/plot4-1024x469.png" class="img-fluid"></a></p>
</section>
<section id="exploring-analytical-robustness" class="level2">
<h2 class="anchored" data-anchor-id="exploring-analytical-robustness">Exploring Analytical Robustness</h2>
<p>By conducting several replications inside one survey, we had also a second, less common opportunity: we could swap measures and investigate what happens to the relationships. This is where the project stopped being a verification exercise and started saying something about our constructs. For example, when we exchanged one privacy-concern scale for another, or used perceived privacy <em>risks</em> instead of privacy <em>concerns</em>, the famous privacy paradox shifted dynamically. In our specification curve analysis of the concern-behavior link (see Figure 6), the single largest driver of whether concern predicted <em>less</em> or <em>more</em> disclosure was simply which of these two operationalizations we chose. This maps onto an earlier specification curve study on the same question (Masur, 2023), and here it holds even with validated multi-item scales rather than single survey items.</p>
<p><a href="img/Picture1-1024x513.png" class="lightbox" data-gallery="quarto-lightbox-gallery-6"><img src="https://philippmasur.de/blog/2026-06-24-why-replication-matters/img/Picture1-1024x513.png" class="img-fluid"></a></p>
<p>We should take this very seriously. A relationship whose sign flips depending on whether you measure “risk” or “concern” may not obviously describe one stable phenomenon. It may be reporting a measurement choice that the field has been treating as a theoretical puzzle. The privacy cynicism studies told a similar story: whether fatigue, resignation, or mistrust predicted protective behavior depended heavily on which control variables entered the model. Some of our most cited effects are robust. Others are conditional on decisions we rarely discuss in more depth.</p>
<p>There is something else to learn from these replications. When a finding fails to replicate, it is genuinely hard to tell whether the original was a false positive or whether the world has simply changed. Comparing data collected in 2016 with data collected in 2025, we saw real shifts in how people share. The between disclosing through private messages and disclosing through public status updates has largely vanished, plausibly because the platforms themselves changed, with Stories and other, more ephemeral formats redefining what “public” even means. So when a path does not replicate, the honest answer is often that we cannot say what the cause is. At times, it may be different measures, at other times, different populations. But more often than not, it may also be shifting cultures and norms, evolving platform architectures, or a new world. Only further replication and systematically exploring these causes, will tease them apart.</p>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>I want to highlight one benefit I did not anticipate when I started this project with students. Embedding replication in a MA course turned out to be an surprisingly good way to teach empirical science. It moves students out of the abstract textbook and drops them straight into the messy reality of our science. We had to grapple with undocumented procedures, measures that no longer fit the way they once did, or decisions the original authors never reported. Students who have wrestled with a real model that will not converge started to better understand power, measurement, and researcher degrees of freedom.</p>
<p>One more thing: I believe that no study is worth not to replicate. Even the non-contested, highly cited pillars of privacy research deserve a second look, and several of them did not replicate perfectly. The culture is shifting, slowly (Dienlin et al., 2020). Replication is no longer routinely dismissed as unoriginal, and a shared vocabulary for what counts as a replication is finally emerging (see also Vermeulen et al., 2024). But single-lab efforts like this one can only take us so far. To put online privacy science on a truly solid and replicable ground, we need to move toward large-scale, international, and many-labs collaborations.</p>
<p>So this is the modest case I wanted to make by doing rather than just proposing. Replication is feasible. In this project, we learned things about our constructs that no original study could have told us. Thus, let us fund, conduct, and fairly review replications!</p>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<ul>
<li>Choi, H., Park, J., &amp; Jung, Y. (2018). The role of privacy fatigue in online privacy behavior. <em>Computers in Human Behavior, 81</em>, 42–51.</li>
<li>Dienlin, T., Johannes, N., Bowman, N. D., Masur, P. K., Engesser, S., Kümpel, A. S., … de Vreese, C. (2021). An agenda for open science in Communication. <em>Journal of Communication, 71</em>(1), 1-26. https://doi.org/10.1093/joc/jqz052</li>
<li>Dienlin, T., &amp; Trepte, S. (2015). Is the privacy paradox a relic of the past? An in-depth analysis of privacy attitudes and privacy behaviors. <em>European Journal of Social Psychology, 45</em>(3), 285–297.</li>
<li>Hallam, C., &amp; Zanella, G. (2017). Online self-disclosure: The privacy paradox explained as a temporally discounted balance between concerns and rewards. <em>Computers in Human Behavior, 68</em>, 217–227.</li>
<li>Keating, D. M., &amp; Totzkay, D. (2019). We do publish (conceptual) replications (sometimes): Publication trends in communication science, 2007–2016. <em>Communication Studies, 70</em>(1), 1–18.</li>
<li>Krasnova, H., Spiekermann, S., Koroleva, K., &amp; Hildebrandt, T. (2010). Online social networks: Why we disclose. <em>Journal of Information Technology, 25</em>(2), 109–125.</li>
<li>LeBel, E. P., McCarthy, R. J., Earp, B. D., Elson, M., &amp; Vanpaemel, W. (2018). A unified framework to quantify the credibility of scientific findings. <em>Advances in Methods and Practices in Psychological Science, 1</em>(3), 389–402.</li>
<li>Litt, E. (2013). Understanding social network site users’ privacy tool use. <em>Computers in Human Behavior, 29</em>(4), 1649–1656.</li>
<li>Lutz, C., Hoffmann, C. P., &amp; Ranzini, G. (2020). Data capitalism and the user: An exploration of privacy cynicism in Germany. <em>New Media &amp; Society, 22</em>(7), 1168–1187.</li>
<li>Masur, P. K. (2023). Understanding the effects of conceptual and analytical choices on “finding” the privacy paradox: A specification curve analysis of large-scale survey data. <em>Information, Communication &amp; Society, 26</em>(8), 1631–1652.</li>
<li>Masur, P. K. (2026). Privacy fatigue and cynicism in the digital age: A close replication of Choi et al.&nbsp;(2018) and Lutz et al.&nbsp;(2020). In C. Lutz, C. P. Hoffmann, &amp; A. Tamò-Larrieux (Eds.), <em>Advancing the study of privacy cynicism, apathy and resignation in the digital society</em>. Edward Elgar.</li>
<li>Masur, P. K., &amp; Ranzini, G. (2025). Privacy calculus, privacy paradox, and context collapse: A replication of three key studies in communication privacy research. <em>Journal of Communication</em>. https://doi.org/10.1093/joc/jqaf007</li>
<li>Masur, P. K., &amp; Scharkow, M. (2016). Disclosure management on social network sites: Individual privacy perceptions and user-directed privacy strategies. <em>Social Media + Society, 2</em>(1).</li>
<li>Open Science Collaboration. (2015). Estimating the reproducibility of psychological science. <em>Science, 349</em>(6251), aac4716.</li>
<li>Park, Y. J. (2013). Digital literacy and privacy behavior online. <em>Communication Research, 40</em>(2), 215–236.</li>
<li>Popper, K. R. (1959). <em>The logic of scientific discovery</em>. Hutchinson.</li>
<li>Vermeulen, I. E., Masur, P. K., Beukeboom, C. J., &amp; Johnson, B. K. (2024). Direct replication in experimental communication science: A conceptual and practical exploration. <em>Media and Communication</em>. https://doi.org/10.17645/mac.7971</li>
<li>Vitak, J. (2012). The impact of context collapse and privacy on social network site disclosures. <em>Journal of Broadcasting &amp; Electronic Media, 56</em>(4), 451–470.</li>
<li>Zlatolas, L. N., Welzer, T., Heričko, M., &amp; Hölbl, M. (2015). Privacy antecedents for SNS self-disclosure: The case of Facebook. Computers in Human Behavior, 45, 158-167.</li>
</ul>


<template id="pm-footer-icons"><a href="#" data-pm-mail="p.k.masur|vu.nl" aria-label="Email" title="Email"><svg class="pm-icon" aria-hidden="true"><use href="#i-mail"></use></svg></a><a href="https://scholar.google.com/citations?user=XA_WthYAAAAJ" target="_blank" rel="noopener" aria-label="Google Scholar" title="Google Scholar"><svg class="pm-icon" aria-hidden="true"><use href="#i-scholar"></use></svg></a><a href="https://orcid.org/0000-0003-3065-7305" target="_blank" rel="noopener" aria-label="ORCID" title="ORCID"><svg class="pm-icon" aria-hidden="true"><use href="#i-orcid"></use></svg></a><a href="https://osf.io/yuqv5/" target="_blank" rel="noopener" aria-label="Open Science Framework" title="Open Science Framework"><svg class="pm-icon" aria-hidden="true"><use href="#i-osf"></use></svg></a><a href="https://github.com/masurp" target="_blank" rel="noopener" aria-label="GitHub" title="GitHub"><svg class="pm-icon" aria-hidden="true"><use href="#i-github"></use></svg></a><a href="https://bsky.app/profile/masurphil.bsky.social" target="_blank" rel="noopener" aria-label="Bluesky" title="Bluesky"><svg class="pm-icon" aria-hidden="true"><use href="#i-bluesky"></use></svg></a><a href="https://research.vu.nl/en/persons/philipp-k-masur" target="_blank" rel="noopener" aria-label="VU Amsterdam" title="VU Amsterdam"><svg class="pm-icon" aria-hidden="true"><use href="#i-landmark"></use></svg></a></template>
</section>

 ]]></description>
  <category>Open Science</category>
  <category>Privacy</category>
  <guid>https://philippmasur.de/blog/2026-06-24-why-replication-matters/</guid>
  <pubDate>Tue, 23 Jun 2026 22:00:00 GMT</pubDate>
  <media:content url="https://philippmasur.de/blog/2026-06-24-why-replication-matters/featured.png" medium="image" type="image/png" height="114" width="144"/>
</item>
<item>
  <title>What is ‘item sampling’ and why is it useful? Or how can we can measure person characteristics with large item pools?</title>
  <link>https://philippmasur.de/blog/2024-06-19-item-sampling/</link>
  <description><![CDATA[ 

<!-- Icon sprite: 24×24 line icons, 1.6 stroke. Every shape has pathLength="1"
     so CSS can animate stroke-dashoffset 1 → 0 ("draw" the icon).
     Use: <svg class="pm-icon"><use href="#i-mail"/></svg> -->





<p>I guess every social scientist at one point has been confronted with a quite common dilemma: We know that we would need a quite large number of items to assess a certain latent concept comprehensively and validly, but we do not have enough space in our questionnaire to use the entire long scale or even inventory. More often than not, we then choose the “best” items (e.g., with the highest factor loadings). However, by reducing the number of items, we are likely reducing the scale’s validity as well. After all, we reduce the diversity and breadth of the manifest indicators that make up the scale.</p>
<p>An alternative to choosing a static subset of a scale that we administer to all participants in the exact same way is so-called <em>item sampling</em>. Here, the idea is to randomly select a subset of items for each respondent from a larger pool. This way, each participant answers a different subset of items. Due to the randomness in this sampling procedure, we can still gather sufficient information overall to estimate the latent trait for each individual participant. The question is, however, how many missing items per person are acceptable? And to what extent does the quality of the resulting scores depend on the size of the item pool, the size of the sample, and the original quality of the scale (e.g., as expressed by reliability indices such as Cronbach’s alpha)?</p>
<section id="statistical-background" class="level2">
<h2 class="anchored" data-anchor-id="statistical-background">Statistical background</h2>
<p>Classical Test Theory (CTT) offers a framework where each observed score (<img src="https://latex.codecogs.com/png.latex?X">) is conceptualized as the sum of a true score (<img src="https://latex.codecogs.com/png.latex?T">) and the error score (<img src="https://latex.codecogs.com/png.latex?e">):</p>
<p><img src="https://latex.codecogs.com/png.latex?X%20=%20T%20+%20e"></p>
<p>In this context, the true score represents the actual level of the latent trait, and the error score encompasses all item-specific variance, measurement errors, or random noise.</p>
<p>CTT defines reliability as the proportion of variance in the observed scores that is attributable to the true scores. When items are randomly sampled, reliability can still be maintained by ensuring that the items are representative of the entire item pool and that the sampling process introduces minimal bias. The basic idea of item sampling is thus as follows:</p>
<ul>
<li><strong>Random Selection of Items:</strong> Instead of administering the entire set of items (or a fixed subset) to every respondent, a small, randomly selected subset is given to each respondent, resulting in unique short scales per participant. This reduces the burden on respondents and shortens the questionnaire.</li>
<li><strong>Scoring with planned missing data:</strong> Because the items are missing completely at random (by design), we can estimate each person’s score from the items they answered, for example with a measurement model estimated by full information maximum likelihood (FIML). Alternatively, the unadministered items can be imputed. As we will see below, the first option is clearly preferable.</li>
</ul>
<p>Despite the reduction in the number of items per respondent, the randomness and overlap between different respondents’ subsets ensure that the overall information about the latent trait is preserved <em>at the level of the sample</em> (e.g., means, variances, and correlations of the scale). For each individual person, however, the precision of their score depends on how many items they answered, as the following sections show.</p>
<section id="mathematical-derivation" class="level3">
<h3 class="anchored" data-anchor-id="mathematical-derivation">Mathematical derivation</h3>
<p>Given a large item pool, if we denote the total number of items by <img src="https://latex.codecogs.com/png.latex?k"> and the number of items sampled for each respondent by <img src="https://latex.codecogs.com/png.latex?k_s">, the reliability of the item-sampled test can be approximated by the Spearman-Brown prophecy formula (see e.g., de Vet et al., 2017)</p>
<p><img src="https://latex.codecogs.com/png.latex?%5Calpha_%7Bnew%7D%20=%20%5Cfrac%7Bn*%5Calpha%7D%7B1+(n-1)*%5Calpha%7D"></p>
<p>where:</p>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?%5Calpha_%7Bnew%7D"> is the “new” reliability after item sampling</li>
<li><img src="https://latex.codecogs.com/png.latex?n%20=%20k_s/k"> is the ratio of sampled items to total items</li>
<li><img src="https://latex.codecogs.com/png.latex?%5Calpha"> is the original reliability of the full item pool</li>
</ul>
<p>By ensuring <img src="https://latex.codecogs.com/png.latex?k_s/k"> is sufficiently large, the reliability of the scale can be preserved to a certain degree. But how large should these two parameters be? Let’s compute the reliability after sampling 5 items from a pool of 10 given an alpha of .9:</p>
<div class="cell" data-layout-align="center">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set parameters</span></span>
<span id="cb1-2">alpha <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;">9</span></span>
<span id="cb1-3">k_s <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;">5</span></span>
<span id="cb1-4">k <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;">10</span></span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Formula from above</span></span>
<span id="cb1-7">new_alpha <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ((k_s<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>k)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>alpha)<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>((k_s<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>k)<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>alpha)</span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(new_alpha, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; [1] 0.82</code></pre>
</div>
</div>
<p>As we can see, the resulting reliability is still .82 (compared to the original .90), which may be completely sufficient for some contexts. Now, let’s explore the relationship between the ratio of sampled to total items and alpha more systematically. Yet, so far, we did not account for the impact of sample size. Further, the above formula only tells us whether the reliability can be preserved. What we also would like to know is whether the validity is preserved (e.g., whether the correlation between the short and the full scale is large).</p>
<p>The impact of sample size can be included by considering the standard error of the reliability estimate and the standard error of the correlation. Larger sample sizes typically result in more precise estimates. For <img src="https://latex.codecogs.com/png.latex?k_s"> (essentially) parallel items, the standard error of Cronbach’s alpha is approximately (van Zyl, Neudecker, &amp; Nel, 2000):</p>
<p><img src="https://latex.codecogs.com/png.latex?SE_%7B%5Calpha%7D%20%5Capprox%20(1-%5Calpha)%5Csqrt%7B%5Cfrac%7B2k_s%7D%7B(k_s-1)N%7D%7D"></p>
<p>where:</p>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?%5Calpha"> is the reliability of the reduced scale</li>
<li><img src="https://latex.codecogs.com/png.latex?k_s"> is the number of items in the reduced scale</li>
<li><img src="https://latex.codecogs.com/png.latex?N"> is the sample size</li>
</ul>
<p>For parallel items, the expected correlation between the score based on the sampled items and the full-scale score follows from the same assumptions: <img src="https://latex.codecogs.com/png.latex?r%20=%20%5Csqrt%7B%5Calpha_%7Bnew%7D/%5Calpha%7D">. Note that this is a <em>part–whole</em> correlation. The sampled items are part of the full scale, so both scores share not only the true score but also the measurement error of the shared items. Its standard error (<em>SE<sub>r</sub></em>) can be approximated by:</p>
<p><img src="https://latex.codecogs.com/png.latex?SE_r%20=%20%5Cfrac%7B1-r%5E2%7D%7B%5Csqrt%7BN%7D%7D"></p>
<p>where:</p>
<ul>
<li><img src="https://latex.codecogs.com/png.latex?r"> is the expected correlation between the two scales</li>
<li><img src="https://latex.codecogs.com/png.latex?N"> is the sample size</li>
</ul>
<p>These standard errors indicate the precision of our reliability and correlation estimates. While the formulas for the reliability and validity provide point estimates, the standard errors show how these estimates would vary with different sample sizes.</p>
</section>
<section id="systematic-approach" class="level3">
<h3 class="anchored" data-anchor-id="systematic-approach">Systematic approach</h3>
<p>So let’s put all these formulas in an R function:</p>
<div class="cell" data-layout-align="center">
<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;">library</span>(tidyverse)</span>
<span id="cb3-2"></span>
<span id="cb3-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Custom function to compute reliability and correlation</span></span>
<span id="cb3-4">reli_new <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>(alpha, k_s, k, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>) {</span>
<span id="cb3-5"></span>
<span id="cb3-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ratio between sample items and total items</span></span>
<span id="cb3-7">  n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> k_s<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>k</span>
<span id="cb3-8"></span>
<span id="cb3-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Spearman-Brown formula</span></span>
<span id="cb3-10">  alpha_new <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ((n)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>alpha)<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>((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;">1</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>alpha)</span>
<span id="cb3-11"></span>
<span id="cb3-12">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Standard error of alpha_new (van Zyl, Neudecker &amp; Nel, 2000)</span></span>
<span id="cb3-13">  se_a <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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> alpha_new) <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;">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> k_s <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> ((k_s <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> N))</span>
<span id="cb3-14"></span>
<span id="cb3-15">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Correlation between scales (part-whole correlation for parallel items)</span></span>
<span id="cb3-16">  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;">sqrt</span>(alpha_new<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>alpha)</span>
<span id="cb3-17">  se_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 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="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;">sqrt</span>(N)</span>
<span id="cb3-18"></span>
<span id="cb3-19">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create output table</span></span>
<span id="cb3-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> N,</span>
<span id="cb3-21">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> k,</span>
<span id="cb3-22">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k_s =</span> k_s,</span>
<span id="cb3-23">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> n,</span>
<span id="cb3-24">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> alpha,</span>
<span id="cb3-25">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha_new =</span> alpha_new,</span>
<span id="cb3-26">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se_a =</span> se_a,</span>
<span id="cb3-27">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r =</span> r,</span>
<span id="cb3-28">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se_r =</span> se_r)</span>
<span id="cb3-29">}</span>
<span id="cb3-30"></span>
<span id="cb3-31"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check function</span></span>
<span id="cb3-32">(check <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;">reli_new</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k_s =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</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>#&gt; # A tibble: 1 × 9
#&gt;       N     k   k_s     n alpha alpha_new   se_a     r    se_r
#&gt;   &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;     &lt;dbl&gt;  &lt;dbl&gt; &lt;dbl&gt;   &lt;dbl&gt;
#&gt; 1   100    10     5   0.5   0.9     0.818 0.0287 0.953 0.00909</code></pre>
</div>
</div>
<p>As we can see, given <img src="https://latex.codecogs.com/png.latex?n"> = .5 and <img src="https://latex.codecogs.com/png.latex?k"> = 10, an alpha level of <img src="https://latex.codecogs.com/png.latex?%5Calpha"> = .9 and a sample size of <img src="https://latex.codecogs.com/png.latex?N"> = 100, the reduced scale should have an alpha of <img src="https://latex.codecogs.com/png.latex?%5Calpha_%7Bnew%7D"> = 0.82, 95%-CI [0.76, 0.87]. Further, the reduced scale should still correlate highly with the original scale: <em>r</em> = 0.95, 95%-CI [0.94, 0.97].</p>
<p>We can now compute these quantities across a range of conditions to explore the effect of item sampling more systematically:</p>
<div class="cell" data-layout-align="center">
<details class="code-fold">
<summary>Plot styling</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Transparent background and muted text, so the figures work in light and dark mode</span></span>
<span id="cb5-2">theme_blog <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>() {</span>
<span id="cb5-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.background  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"transparent"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>),</span>
<span id="cb5-5">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"transparent"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>),</span>
<span id="cb5-6">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.minor =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb5-7">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.major =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#8a8f9840"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>),</span>
<span id="cb5-8">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">strip.text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#7a7f88"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>),</span>
<span id="cb5-9">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#7a7f88"</span>),</span>
<span id="cb5-10">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#7a7f88"</span>),</span>
<span id="cb5-11">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.text  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#7a7f88"</span>),</span>
<span id="cb5-12">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#7a7f88"</span>),</span>
<span id="cb5-13">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>)</span>
<span id="cb5-14">}</span>
<span id="cb5-15">blues <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;">"#c4e4f6"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#8cc8ec"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#4aa8e0"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#1680bd"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#0b5a87"</span>)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># sequential: alpha .75 -&gt; .95</span></span></code></pre></div></div>
</details>
</div>
<div class="cell" data-layout-align="center">
<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;"># Expected reliability for a 20-item pool across sampling ratios and original alphas</span></span>
<span id="cb6-2">analytic <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;">alpha =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">75</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">05</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k_s =</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;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</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;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmap_dfr</span>(reli_new, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-4">  <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;">N =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N ="</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span>(N, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">big.mark =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>)), <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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N = 100"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N = 1,000"</span>)))</span>
<span id="cb6-5"></span>
<span id="cb6-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(analytic, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(n, alpha_new, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(alpha), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(alpha))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#8a8f98"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_ribbon</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;">ymin =</span> alpha_new <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;">1.96</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> se_a, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> alpha_new <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;">1.96</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> se_a),</span>
<span id="cb6-9">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</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>
<span id="cb6-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> N) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_colour_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> blues) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> blues) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>percent) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Share of items each person answers (k_s / k)"</span>,</span>
<span id="cb6-16">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Expected reliability (alpha)"</span>,</span>
<span id="cb6-17">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alpha of the full scale"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alpha of the full scale"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blog</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-4-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://philippmasur.de/blog/2024-06-19-item-sampling/index_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Expected reliability after item sampling as a function of the share of items each person answers, one line per original alpha from .75 to .95, with 95% confidence bands for samples of 100 and 1,000 respondents and 20 items. With an original alpha of .90, answering half of the items gives a reliability of about .82."></a></p>
</figure>
</div>
</div>
</div>
<p>We can see that the ability to “preserve” the reliability drops as the number of items per person decreases. It further decreases faster if the original reliability was already low. Yet, there are some interesting insights here: For example, if the original reliability is .9, we can “afford” up to 50% missings per person and still end up with a reliability of .80. If we only use a third of the items, we can still reach reliabilities of .75, which again may be acceptable in some contexts. The sample size does not change these expected values, it only affects how precisely we can estimate them.</p>
<div class="cell" data-layout-align="center">
<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;">ggplot</span>(analytic, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(n, r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(alpha), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(alpha))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</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;">9</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#8a8f98"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_ribbon</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;">ymin =</span> r <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;">1.96</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> se_r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmin</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, r <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;">1.96</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> se_r)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</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>
<span id="cb7-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> N) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_colour_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> blues) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> blues) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>percent) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Share of items each person answers (k_s / k)"</span>,</span>
<span id="cb7-10">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Expected correlation with the full scale"</span>,</span>
<span id="cb7-11">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alpha of the full scale"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alpha of the full scale"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blog</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-5-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://philippmasur.de/blog/2024-06-19-item-sampling/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Expected correlation between the item-sampled score and the full-scale score as a function of the share of items answered, one line per original alpha, with 95% confidence bands for 100 and 1,000 respondents. With an original alpha of .80, answering half of the items still gives a correlation above .90."></a></p>
</figure>
</div>
</div>
</div>
<p>With regard to the validity of the reduced scale, we can see that for scales with a reliability of at least .8, we can afford up to 50% missings and still have good convergent validity with the full scale (r &gt; .90), and up to 70% missings if a validity of r &gt; .80 is sufficient.</p>
<p>Bear in mind that this is just the result based on the formulas outlined above. Practically, however, more things may have to be taken into account:</p>
<ul>
<li>Normal Distribution Assumption: The formulas assume that the underlying data is normally distributed, which might not be the case in reality (e.g., with Likert-type items).</li>
<li>Equal Item Parameters: The formulas assume that all items have equal difficulty and discrimination parameters, while in reality, items can vary significantly.</li>
<li>Multidimensionality: Real data might have multiple underlying factors, while the theoretical formula assumes unidimensionality.</li>
<li>Nonlinear Relationships: Real data might include non-linear relationships between items and the latent trait, which are not considered in the formulas.</li>
<li>Sampling Variability: In real data, there is inherent variability due to random sampling. This can lead to differences between theoretical expectations and observed outcomes.</li>
<li>Computation of the scales’ scores: The way we handle the missing items when computing each person’s score could significantly impact the results.</li>
</ul>
<p>To explore these issues a bit further, let’s simulate some data and introduce missings per person systematically.</p>
</section>
</section>
<section id="simulation-approach" class="level2">
<h2 class="anchored" data-anchor-id="simulation-approach">Simulation approach</h2>
<p>The key idea of the simulation is to generate data for which we <em>know</em> each person’s true score. Then we can measure directly how well the scores based on the sampled items recover it: the squared correlation between a score and the true score is exactly the reliability of that score. This is the same quantity the Spearman–Brown formula predicts, so we can compare the two.</p>
<div class="cell" data-layout-align="center">
<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;">library</span>(lavaan)</span>
<span id="cb8-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(mice)</span>
<span id="cb8-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(furrr)</span></code></pre></div></div>
</div>
<section id="simulating-likert-type-items-with-known-true-scores" class="level3">
<h3 class="anchored" data-anchor-id="simulating-likert-type-items-with-known-true-scores">Simulating Likert-type items with known true scores</h3>
<p>I simulate items from a one-factor model. First, I compute the average inter-item correlation that corresponds to the alpha level we are providing, and from it the factor loading of each item. Each person gets a true score <img src="https://latex.codecogs.com/png.latex?%5Ctheta">, and each item response is <img src="https://latex.codecogs.com/png.latex?%5Clambda%5Ctheta"> plus random error. To make the data look like a typical questionnaire, I cut the responses into 5 answer options. Because real items also differ in how easy they are to agree with, each item gets its own difficulty (a shift of the answer thresholds).</p>
<div class="cell" data-layout-align="center">
<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"><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;">123</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for reproducibility</span></span>
<span id="cb9-2"></span>
<span id="cb9-3">sim_items <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>(N, k, alpha, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">answer_options =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>) {</span>
<span id="cb9-4"></span>
<span id="cb9-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Average inter-item correlation implied by alpha, and the resulting loading</span></span>
<span id="cb9-6">  rbar   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> alpha <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (k <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> alpha <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (k <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="cb9-7">  lambda <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>(rbar)</span>
<span id="cb9-8"></span>
<span id="cb9-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># True scores, item difficulties and continuous responses</span></span>
<span id="cb9-10">  theta      <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>(N)</span>
<span id="cb9-11">  difficulty <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;">runif</span>(k, <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;">75</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">75</span>)</span>
<span id="cb9-12">  latent <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> theta <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%o%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(lambda, k) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-13">    <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;">rnorm</span>(N <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> k, <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;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> lambda<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>)), N, k) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span></span>
<span id="cb9-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(difficulty, N, k, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">byrow =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb9-15"></span>
<span id="cb9-16">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cut into Likert-type answer options (1 to 5)</span></span>
<span id="cb9-17">  cuts  <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;">qnorm</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</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="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> answer_options <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;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>answer_options]</span>
<span id="cb9-18">  items <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">findInterval</span>(latent, cuts) <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>, N, k,</span>
<span id="cb9-19">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dimnames =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"i"</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>k)))</span>
<span id="cb9-20"></span>
<span id="cb9-21">  <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;">items =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(items), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta =</span> theta)</span>
<span id="cb9-22">}</span>
<span id="cb9-23"></span>
<span id="cb9-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check function</span></span>
<span id="cb9-25">test <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;">sim_items</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>)</span>
<span id="cb9-26"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(test<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>items)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt;   i1 i2 i3 i4 i5 i6 i7 i8 i9 i10
#&gt; 1  3  1  2  1  2  1  3  3  2   5
#&gt; 2  2  1  1  2  2  1  4  3  5   4
#&gt; 3  4  3  5  5  2  2  4  5  5   5
#&gt; 4  3  4  4  2  4  2  4  4  4   5
#&gt; 5  5  2  5  3  3  1  4  4  4   4
#&gt; 6  5  4  5  5  5  4  5  2  5   5</code></pre>
</div>
</div>
<p>As we can see, the function creates a data set with 10 items and <em>n</em> = 100 participants. Let’s check the alpha level of this scale:</p>
<div class="cell" data-layout-align="center">
<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">psych<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;">alpha</span>(test<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>items)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>total</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt;  raw_alpha std.alpha   G6(smc) average_r     S/N        ase  mean        sd
#&gt;  0.8482603 0.8487234 0.8451815 0.3594018 5.61041 0.02258244 3.164 0.8487091
#&gt;   median_r
#&gt;  0.3625075</code></pre>
</div>
</div>
<p>It is slightly lower than the specified .90. Cutting continuous responses into five categories always loses a bit of information. That’s why the simulation below compares everything with the alpha of the simulated (Likert-type) data, not with the input value.</p>
</section>
<section id="introducing-item-sampling" class="level3">
<h3 class="anchored" data-anchor-id="introducing-item-sampling">Introducing item sampling</h3>
<p>Next, we need a function that simulates the item sampling procedure. A random item sampling procedure is the same as introducing a certain number of missings per person (i.e., per row): for each person, only a random subset of <code>round(n * k)</code> items is kept.</p>
<div class="cell" data-layout-align="center">
<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">sample_items <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>(items, n) {</span>
<span id="cb13-2">  k   <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;">ncol</span>(items)</span>
<span id="cb13-3">  k_s <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;">round</span>(n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> k)</span>
<span id="cb13-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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(items))) {</span>
<span id="cb13-5">    items[i, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(k, k <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> k_s)] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span></span>
<span id="cb13-6">  }</span>
<span id="cb13-7">  items</span>
<span id="cb13-8">}</span>
<span id="cb13-9"></span>
<span id="cb13-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check function</span></span>
<span id="cb13-11">test_na <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_items</span>(test<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>items, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)</span>
<span id="cb13-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(test_na)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt;   i1 i2 i3 i4 i5 i6 i7 i8 i9 i10
#&gt; 1  3  1  2  1 NA  1  3 NA  2   5
#&gt; 2  2  1 NA  2  2  1 NA  3  5   4
#&gt; 3  4  3  5  5  2 NA  4 NA  5   5
#&gt; 4  3  4  4  2  4 NA  4  4  4  NA
#&gt; 5  5  2  5  3  3  1  4 NA  4  NA
#&gt; 6  5  4  5  5  5 NA  5  2  5  NA</code></pre>
</div>
</div>
<p>Here, I sample 80% of the items per person (i.e., 2 of the 10 items are missing). In fact, this corresponds to an overall amount of missings in the data set of 20%:</p>
<div class="cell" data-layout-align="center">
<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;">mean</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(test_na))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; [1] 0.2</code></pre>
</div>
</div>
</section>
<section id="three-ways-to-score-item-sampled-data" class="level3">
<h3 class="anchored" data-anchor-id="three-ways-to-score-item-sampled-data">Three ways to score item-sampled data</h3>
<p>How do we compute a score for each person if everyone answered different items? I compare three approaches:</p>
<ol type="1">
<li><strong>Mean of the answered items.</strong> The simplest option, and probably the most common in practice.</li>
<li><strong>Difficulty-adjusted mean.</strong> Each item is first centered at its sample mean, then the answered items are averaged. This way, a person who happened to get “easy” items is not advantaged. Under a one-factor model with equal loadings, this is equivalent to a model-based score estimated from the answered items (e.g., a FIML factor score).</li>
<li><strong>Imputation first.</strong> The unanswered items are imputed (one imputation with predictive mean matching, as implemented in <code>mice</code>), and factor scores are then computed from a CFA on the completed data.</li>
</ol>
<div class="cell" data-layout-align="center">
<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">score_mean <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>(items) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowMeans</span>(items, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb17-2"></span>
<span id="cb17-3">score_adjusted <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>(items) {</span>
<span id="cb17-4">  centered <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;">sweep</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(items), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colMeans</span>(items, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span>
<span id="cb17-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowMeans</span>(centered, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb17-6">}</span>
<span id="cb17-7"></span>
<span id="cb17-8">score_cfa <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>(items) {</span>
<span id="cb17-9">  model <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;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"G =~"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(items), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">collapse =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" + "</span>))</span>
<span id="cb17-10">  scores <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;">lavPredict</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cfa</span>(model, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> items, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">std.lv =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)))</span>
<span id="cb17-11">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The sign of a factor is arbitrary: align it with the sum score</span></span>
<span id="cb17-12">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># (all items are positively keyed)</span></span>
<span id="cb17-13">  <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;">cor</span>(scores, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(items)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) scores <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>scores</span>
<span id="cb17-14">  scores</span>
<span id="cb17-15">}</span>
<span id="cb17-16"></span>
<span id="cb17-17">score_imputed <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>(items) {</span>
<span id="cb17-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">score_cfa</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">complete</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mice</span>(items, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">m =</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;">printFlag =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)))</span>
<span id="cb17-19">}</span>
<span id="cb17-20"></span>
<span id="cb17-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reliability of each score = squared correlation with the true score</span></span>
<span id="cb17-22"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</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;">mean     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">score_mean</span>(test_na),</span>
<span id="cb17-23">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">adjusted =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">score_adjusted</span>(test_na),</span>
<span id="cb17-24">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">imputed  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">score_imputed</span>(test_na)),</span>
<span id="cb17-25">       \(score) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor</span>(score, test<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>theta)<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;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt;     mean adjusted  imputed 
#&gt;     0.79     0.80     0.76</code></pre>
</div>
</div>
</section>
<section id="putting-it-all-together" class="level3">
<h3 class="anchored" data-anchor-id="putting-it-all-together">Putting it all together</h3>
<p>Now, we put it all together in one function that simulates a data set, samples the items, scores the data in all three ways, and computes two outcomes for each score:</p>
<ul>
<li>its <strong>reliability</strong>: the squared correlation with the true score, and</li>
<li>its <strong>convergent validity</strong>: the correlation with the score based on all items (the full-data mean for the two mean-based scores, the full-data factor score for the imputation approach).</li>
</ul>
<p>It also stores the alpha of the full simulated scale and the reliability the Spearman–Brown formula predicts from it.</p>
<div class="cell" data-layout-align="center">
<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">alpha_full <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>(items) {</span>
<span id="cb19-2">  k <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;">ncol</span>(items)</span>
<span id="cb19-3">  k <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (k <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> (<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;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(items, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, var)) <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;">var</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(items)))</span>
<span id="cb19-4">}</span>
<span id="cb19-5"></span>
<span id="cb19-6">sim_func <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>(N, k, alpha, n) {</span>
<span id="cb19-7">  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;">sim_items</span>(N, k, alpha)</span>
<span id="cb19-8">  sampled <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_items</span>(d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>items, n)</span>
<span id="cb19-9">  a <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;">alpha_full</span>(d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>items)</span>
<span id="cb19-10"></span>
<span id="cb19-11">  full <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;">mean =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">score_mean</span>(d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>items), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">adjusted =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">score_mean</span>(d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>items),</span>
<span id="cb19-12">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">imputed =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">score_cfa</span>(d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>items))</span>
<span id="cb19-13">  part <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;">mean =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">score_mean</span>(sampled), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">adjusted =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">score_adjusted</span>(sampled),</span>
<span id="cb19-14">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">imputed =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">score_imputed</span>(sampled))</span>
<span id="cb19-15"></span>
<span id="cb19-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</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;">k =</span> k, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k_s =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> k), <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;">alpha =</span> alpha, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha_emp =</span> a,</span>
<span id="cb19-17">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(part),</span>
<span id="cb19-18">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r_full =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mapply</span>(cor, part, full),</span>
<span id="cb19-19">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reliability =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(part, cor, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>theta)<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-20">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reliability_full =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(full, cor, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>theta)<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-21">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">spearman_brown =</span> n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> a <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> (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;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> a))</span>
<span id="cb19-22">}</span>
<span id="cb19-23"></span>
<span id="cb19-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check sim function</span></span>
<span id="cb19-25"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sim_func</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">85</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; # A tibble: 3 × 11
#&gt;       N     k   k_s     n alpha alpha_emp method   r_full reliability
#&gt;   &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;     &lt;dbl&gt; &lt;chr&gt;     &lt;dbl&gt;       &lt;dbl&gt;
#&gt; 1   500    20    10   0.5  0.85     0.823 mean      0.912       0.684
#&gt; 2   500    20    10   0.5  0.85     0.823 adjusted  0.926       0.707
#&gt; 3   500    20    10   0.5  0.85     0.823 imputed   0.858       0.607
#&gt; # ℹ 2 more variables: reliability_full &lt;dbl&gt;, spearman_brown &lt;dbl&gt;</code></pre>
</div>
</div>
<p>We set up a grid that contains the parameter ranges we want to explore and run the function 200 times for each of the 270 combinations. That is 54,000 simulated data sets, each scored in three ways. I use the package <code>furrr</code> to parallelize the procedure; <code>furrr_options(seed = ...)</code> makes the parallel random numbers reproducible.</p>
<div class="cell" data-layout-align="center">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Setup simulation grid</span></span>
<span id="cb21-2">setup <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>
<span id="cb21-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</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;">250</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>),</span>
<span id="cb21-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</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;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>),</span>
<span id="cb21-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">75</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">05</span>),</span>
<span id="cb21-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</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;">3</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)</span>
<span id="cb21-7">)</span>
<span id="cb21-8"></span>
<span id="cb21-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The simulation takes a while: its results are saved next to this post and</span></span>
<span id="cb21-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># re-used (delete sims/results.rds to run it again)</span></span>
<span id="cb21-11">n_sim <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>
<span id="cb21-12"><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;">file.exists</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sims/results.rds"</span>)) {</span>
<span id="cb21-13">  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;">readRDS</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sims/results.rds"</span>)</span>
<span id="cb21-14">} <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb21-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plan</span>(multisession, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">workers =</span> parallelly<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;">availableCores</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">omit =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb21-16">  results <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> setup[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_len</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(setup)), n_sim), ] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb21-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">future_pmap_dfr</span>(sim_func, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.options =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">furrr_options</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2024</span>))</span>
<span id="cb21-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dir.create</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sims"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">showWarnings =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb21-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">saveRDS</span>(results, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sims/results.rds"</span>)</span>
<span id="cb21-20">}</span>
<span id="cb21-21"></span>
<span id="cb21-22">results</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; # A tibble: 162,000 × 11
#&gt;        N     k   k_s     n alpha alpha_emp method   r_full reliability
#&gt;    &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;     &lt;dbl&gt; &lt;chr&gt;     &lt;dbl&gt;       &lt;dbl&gt;
#&gt;  1   250    10     3   0.3  0.75     0.771 mean      0.780       0.408
#&gt;  2   250    10     3   0.3  0.75     0.771 adjusted  0.811       0.451
#&gt;  3   250    10     3   0.3  0.75     0.771 imputed   0.647       0.297
#&gt;  4   500    10     3   0.3  0.75     0.718 mean      0.740       0.397
#&gt;  5   500    10     3   0.3  0.75     0.718 adjusted  0.779       0.436
#&gt;  6   500    10     3   0.3  0.75     0.718 imputed   0.508       0.165
#&gt;  7  1000    10     3   0.3  0.75     0.727 mean      0.753       0.410
#&gt;  8  1000    10     3   0.3  0.75     0.727 adjusted  0.781       0.435
#&gt;  9  1000    10     3   0.3  0.75     0.727 imputed   0.597       0.251
#&gt; 10   250    20     6   0.3  0.75     0.741 mean      0.777       0.438
#&gt; # ℹ 161,990 more rows
#&gt; # ℹ 2 more variables: reliability_full &lt;dbl&gt;, spearman_brown &lt;dbl&gt;</code></pre>
</div>
</div>
</section>
<section id="reliability-of-the-item-sampled-scores" class="level3">
<h3 class="anchored" data-anchor-id="reliability-of-the-item-sampled-scores">Reliability of the item-sampled scores</h3>
<p>The first figure shows the reliability of each person’s score, depending on the share of items they answered and the alpha of the full scale (for a sample of 500 and a pool of 20 items). The dashed line is the reliability predicted by the Spearman–Brown formula.</p>
<div class="cell" data-layout-align="center">
<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">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;">filter</span>(summ, 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;">500</span>, k <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;">20</span>)</span>
<span id="cb23-2"></span>
<span id="cb23-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(d, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(n, rel, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> method)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</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;">y =</span> sb), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#8a8f98"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_pointrange</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;">ymin =</span> rel_lo, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> rel_hi), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb23-6">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">position_dodge</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">04</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">position_dodge</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">04</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> lab, <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;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_colour_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> method_cols, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> method_labels) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>percent, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</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;">3</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Share of items each person answers"</span>,</span>
<span id="cb23-12">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Reliability of the person scores"</span>,</span>
<span id="cb23-13">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb23-14">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Points: mean of 200 replications, bars: 95% of replications. Dashed: Spearman–Brown prediction."</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blog</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.caption =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#7a7f88"</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-15-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-3"><img src="https://philippmasur.de/blog/2024-06-19-item-sampling/index_files/figure-html/unnamed-chunk-15-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Simulated reliability of item-sampled scores for 500 respondents and 20 items, by share of items answered and full-scale alpha. The two mean-based scores follow the Spearman–Brown prediction closely; the scores based on imputed data are clearly less reliable in every condition."></a></p>
</figure>
</div>
</div>
</div>
<p>Three things stand out:</p>
<ol type="1">
<li><strong>The Spearman–Brown formula works well.</strong> Averaged across all 270 conditions, the reliability of the difficulty-adjusted mean is 0.71, practically identical to the predicted 0.71 (the mean absolute deviation is only 0.005). The simple mean of the answered items is only slightly worse; its small disadvantage comes from items differing in difficulty, so getting “easy” or “hard” items adds a bit of noise.</li>
<li><strong>Imputing first makes things worse.</strong> Scores based on the imputed data are less reliable in every condition, on average by 0.11 (for N = 500 and k = 20). Imputation cannot add information about a person; it fills in plausible values that include random noise, and that noise ends up in the scores.</li>
<li><strong>The loss is largest when few items are answered.</strong> It grows from 0.04 when people answer 80% of the items to 0.22 when they answer 30%. For example, with a scale of alpha = .85 and half of the items answered, the reliability is 0.70 for the difficulty-adjusted mean but only 0.58 with imputation.</li>
</ol>
</section>
<section id="correlation-with-the-full-scale-convergent-validity" class="level3">
<h3 class="anchored" data-anchor-id="correlation-with-the-full-scale-convergent-validity">Correlation with the full scale (convergent validity)</h3>
<p>Next, we can look at the correlation between the item-sampled scores and the scores based on all items. The dashed line shows the part–whole correlation expected from the formulas above.</p>
<div class="cell" data-layout-align="center">
<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;">ggplot</span>(d, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(n, r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> method)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</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;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(sb <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> alpha_emp)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#8a8f98"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</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;">9</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#8a8f9860"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_pointrange</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;">ymin =</span> r_lo, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> r_hi), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb24-5">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">position_dodge</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">04</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">position_dodge</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">04</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> lab, <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;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_colour_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> method_cols, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> method_labels) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>percent, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</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;">3</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Share of items each person answers"</span>,</span>
<span id="cb24-11">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Correlation with the full-scale score"</span>,</span>
<span id="cb24-12">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blog</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-16-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-4"><img src="https://philippmasur.de/blog/2024-06-19-item-sampling/index_files/figure-html/unnamed-chunk-16-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Simulated correlation between item-sampled scores and full-scale scores for 500 respondents and 20 items. The mean-based scores match the expected part-whole correlation; the imputation-based scores correlate less with the full scale."></a></p>
</figure>
</div>
</div>
</div>
<p>The picture is the same: the mean-based scores correlate with the full-scale scores as expected, the imputation-based scores less so. If the original scale has a high reliability (alpha ≥ .85) and people answer at least half of the items, the item-sampled scores correlate <em>r</em> &gt; .90 with the full scale.</p>
</section>
<section id="do-sample-size-and-item-pool-size-matter" class="level3">
<h3 class="anchored" data-anchor-id="do-sample-size-and-item-pool-size-matter">Do sample size and item pool size matter?</h3>
<p>Interestingly, sample size and the size of the item pool hardly matter:</p>
<div class="cell" data-layout-align="center">
<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">summ <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb25-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(method <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;">"adjusted"</span>, alpha <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;">85</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;">5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb25-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(N), rel, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(k))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</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;">yintercept =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(sb)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#8a8f98"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_pointrange</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;">ymin =</span> rel_lo, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> rel_hi), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">position_dodge</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_colour_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</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;">"10"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#8cc8ec"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"20"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#1497d6"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"40"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#0b5a87"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sample size"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Reliability"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Items in the pool"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blog</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-17-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-5"><img src="https://philippmasur.de/blog/2024-06-19-item-sampling/index_files/figure-html/unnamed-chunk-17-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Reliability of the difficulty-adjusted mean for alpha = .85 and half of the items answered, for 250, 500 and 1,000 respondents and pools of 10, 20 and 40 items. The average reliability is the same in all conditions; larger samples only make it vary less between replications."></a></p>
</figure>
</div>
</div>
</div>
<p>For a given alpha of the full scale and a given <em>share</em> of items answered, the expected reliability is the same for 250 or 1,000 respondents and for pools of 10 or 40 items. A larger sample only makes the result less variable. (Note that in this simulation, a larger pool with the same alpha means weaker individual items. In practice, extending a scale usually <em>increases</em> its alpha, and then a larger pool does help.)</p>
</section>
<section id="so-how-does-the-simulation-compare-to-the-mathematical-approach-outlined-above" class="level3">
<h3 class="anchored" data-anchor-id="so-how-does-the-simulation-compare-to-the-mathematical-approach-outlined-above">So how does the simulation compare to the mathematical approach outlined above?</h3>
<p>Let’s look at one example (N = 500, k = 20, 12 sampled items, alpha = .80):</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1">summ <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb26-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</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;">500</span>, k <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;">20</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;">6</span>, alpha <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;">8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb26-3">  <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;">method =</span> method_labels[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(method)]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb26-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(method, alpha_emp, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Spearman-Brown</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> sb, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reliability =</span> rel, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r_full =</span> r)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; # A tibble: 3 × 5
#&gt;   method                           alpha_emp `Spearman-Brown` reliability r_full
#&gt;   &lt;chr&gt;                                &lt;dbl&gt;            &lt;dbl&gt;       &lt;dbl&gt;  &lt;dbl&gt;
#&gt; 1 Difficulty-adjusted mean             0.768            0.665       0.663  0.931
#&gt; 2 Imputation (PMM) + factor scores     0.768            0.665       0.555  0.855
#&gt; 3 Mean of answered items               0.768            0.665       0.646  0.918</code></pre>
</div>
</div>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Mathematical results (with the alpha of the simulated Likert-type data)</span></span>
<span id="cb28-2">a <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;">unique</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(summ, 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;">500</span>, k <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;">20</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;">6</span>, alpha <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;">8</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>alpha_emp, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))</span>
<span id="cb28-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reli_new</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> a, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k_s =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb28-4">  <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;">ll =</span> r <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;">1.96</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>se_r,</span>
<span id="cb28-5">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ul =</span> r <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;">1.96</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>se_r) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb28-6">  dplyr<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;">select</span>(N, k, k_s, alpha, alpha_new, r, ll, ul)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; # A tibble: 1 × 8
#&gt;       N     k   k_s alpha alpha_new     r    ll    ul
#&gt;   &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;     &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
#&gt; 1   500    20    12 0.768     0.665 0.931 0.919 0.942</code></pre>
</div>
</div>
<p>When we measure reliability as the precision of the person scores and use the alpha of the simulated Likert-type data, the simulation and the mathematical approach agree closely for the two mean-based scores.</p>
</section>
</section>
<section id="conclusion-and-recommendations" class="level2">
<h2 class="anchored" data-anchor-id="conclusion-and-recommendations">Conclusion and recommendations</h2>
<p>So what does this all mean? From my point of view, the simulations indicate that item sampling is a viable strategy for efficiently measuring latent traits in social science research (e.g., scales of 10-40 items with Likert-type items):</p>
<ul>
<li><strong>The Spearman–Brown formula is a good planning tool.</strong> Given the alpha of the full scale and the share of items each person answers, it predicts the reliability of the person scores well. Just make sure to plug in the alpha of your actual (Likert-type) data. If the full scale has an alpha of about .90 and people answer half of the items, their scores still have a reliability of about 0.78 and correlate <em>r</em> = 0.94 with the full-scale scores. With a full-scale alpha of .80 to .85, half of the items yields reliabilities of only about 0.62 to 0.70; to reach .80, people then need to answer most of the items.</li>
<li><strong>Score people on the items they answered; don’t impute.</strong> A (difficulty-adjusted) mean or a model-based score from the answered items (e.g., FIML or IRT) is clearly better than imputing the missing items first. Imputation makes the data set look complete, but it adds noise to every person’s score.</li>
<li><strong>Sample size is about precision, not about reliability.</strong> Larger samples estimate everything more precisely, but they do not make each person’s score more reliable.</li>
</ul>
<p>This technique reduces respondent burden and shortens questionnaires. At the level of the sample (e.g., means and correlations of the scale), item sampling preserves the information of the full item pool. At the level of individuals, the reliability of their scores follows Spearman–Brown, and that is something we can plan for.</p>
</section>
<section id="references" class="level1">
<h1>References</h1>
<ul>
<li>de Vet, H. C., Mokkink, L. B., Mosmuller, D. G., &amp; Terwee, C. B. (2017). Spearman–Brown prophecy formula and Cronbach’s alpha: different faces of reliability and opportunities for new applications. <em>Journal of Clinical Epidemiology, 85</em>, 45-49.</li>
<li>Husek, T. R, &amp; Sirotnik, K. (1967). <em>Item Sampling in Educational Research.</em> <a href="https://files.eric.ed.gov/fulltext/ED013975.pdf" class="uri">https://files.eric.ed.gov/fulltext/ED013975.pdf</a></li>
<li>van Zyl, J. M., Neudecker, H., &amp; Nel, D. G. (2000). On the distribution of the maximum likelihood estimator of Cronbach’s alpha. <em>Psychometrika, 65</em>(3), 271-280.</li>
</ul>
<div class="cell" data-layout-align="center">
<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;">sessionInfo</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; R version 4.4.0 (2024-04-24)
#&gt; Platform: aarch64-apple-darwin20
#&gt; Running under: macOS 26.6.2
#&gt; 
#&gt; Matrix products: default
#&gt; BLAS:   /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib 
#&gt; LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0
#&gt; 
#&gt; locale:
#&gt; [1] C.UTF-8/C.UTF-8/C.UTF-8/C/C.UTF-8/C.UTF-8
#&gt; 
#&gt; time zone: Europe/Amsterdam
#&gt; tzcode source: internal
#&gt; 
#&gt; attached base packages:
#&gt; [1] stats     graphics  grDevices utils     datasets  methods   base     
#&gt; 
#&gt; other attached packages:
#&gt;  [1] furrr_0.3.1     future_1.70.0   mice_3.16.0     lavaan_0.6-19  
#&gt;  [5] lubridate_1.9.3 forcats_1.0.0   stringr_1.6.0   dplyr_1.2.0    
#&gt;  [9] purrr_1.2.1     readr_2.1.5     tidyr_1.3.2     tibble_3.3.1   
#&gt; [13] ggplot2_4.0.2   tidyverse_2.0.0
#&gt; 
#&gt; loaded via a namespace (and not attached):
#&gt;  [1] gtable_0.3.6       shape_1.4.6.1      xfun_0.57          htmlwidgets_1.6.4 
#&gt;  [5] psych_2.4.3        lattice_0.22-6     tzdb_0.4.0         quadprog_1.5-8    
#&gt;  [9] vctrs_0.7.2        tools_4.4.0        Rdpack_2.6.6       generics_0.1.4    
#&gt; [13] parallel_4.4.0     stats4_4.4.0       pan_1.9            pkgconfig_2.0.3   
#&gt; [17] jomo_2.7-6         Matrix_1.7-0       RColorBrewer_1.1-3 S7_0.2.1          
#&gt; [21] lifecycle_1.0.5    compiler_4.4.0     farver_2.1.2       mnormt_2.1.1      
#&gt; [25] codetools_0.2-20   htmltools_0.5.8.1  yaml_2.3.12        glmnet_4.1-8      
#&gt; [29] nloptr_2.2.1       pillar_1.11.1      MASS_7.3-60.2      reformulas_0.4.4  
#&gt; [33] iterators_1.0.14   rpart_4.1.23       boot_1.3-30        foreach_1.5.2     
#&gt; [37] mitml_0.4-5        parallelly_1.46.1  nlme_3.1-164       tidyselect_1.2.1  
#&gt; [41] digest_0.6.39      stringi_1.8.7      listenv_0.10.1     labeling_0.4.3    
#&gt; [45] splines_4.4.0      fastmap_1.2.0      grid_4.4.0         cli_3.6.5         
#&gt; [49] magrittr_2.0.4     utf8_1.2.6         survival_3.5-8     broom_1.0.12      
#&gt; [53] pbivnorm_0.6.0     withr_3.0.2        scales_1.4.0       backports_1.5.0   
#&gt; [57] timechange_0.3.0   rmarkdown_2.30     globals_0.19.1     nnet_7.3-19       
#&gt; [61] lme4_2.0-1         hms_1.1.3          evaluate_1.0.5     knitr_1.51        
#&gt; [65] rbibutils_2.4.1    rlang_1.1.7        Rcpp_1.1.1         glue_1.8.0        
#&gt; [69] minqa_1.2.8        jsonlite_2.0.0     R6_2.6.1</code></pre>
</div>
</div>


<template id="pm-footer-icons"><a href="#" data-pm-mail="p.k.masur|vu.nl" aria-label="Email" title="Email"><svg class="pm-icon" aria-hidden="true"><use href="#i-mail"></use></svg></a><a href="https://scholar.google.com/citations?user=XA_WthYAAAAJ" target="_blank" rel="noopener" aria-label="Google Scholar" title="Google Scholar"><svg class="pm-icon" aria-hidden="true"><use href="#i-scholar"></use></svg></a><a href="https://orcid.org/0000-0003-3065-7305" target="_blank" rel="noopener" aria-label="ORCID" title="ORCID"><svg class="pm-icon" aria-hidden="true"><use href="#i-orcid"></use></svg></a><a href="https://osf.io/yuqv5/" target="_blank" rel="noopener" aria-label="Open Science Framework" title="Open Science Framework"><svg class="pm-icon" aria-hidden="true"><use href="#i-osf"></use></svg></a><a href="https://github.com/masurp" target="_blank" rel="noopener" aria-label="GitHub" title="GitHub"><svg class="pm-icon" aria-hidden="true"><use href="#i-github"></use></svg></a><a href="https://bsky.app/profile/masurphil.bsky.social" target="_blank" rel="noopener" aria-label="Bluesky" title="Bluesky"><svg class="pm-icon" aria-hidden="true"><use href="#i-bluesky"></use></svg></a><a href="https://research.vu.nl/en/persons/philipp-k-masur" target="_blank" rel="noopener" aria-label="VU Amsterdam" title="VU Amsterdam"><svg class="pm-icon" aria-hidden="true"><use href="#i-landmark"></use></svg></a></template>
</section>

 ]]></description>
  <category>R &amp; Statistics</category>
  <guid>https://philippmasur.de/blog/2024-06-19-item-sampling/</guid>
  <pubDate>Tue, 18 Jun 2024 22:00:00 GMT</pubDate>
  <media:content url="https://philippmasur.de/blog/2024-06-19-item-sampling/featured.png" medium="image" type="image/png" height="77" width="144"/>
</item>
<item>
  <title>What is statistical power? And how to conduct power analysis in R?</title>
  <link>https://philippmasur.de/blog/2024-05-28-statistical-power/</link>
  <description><![CDATA[ 

<!-- Icon sprite: 24×24 line icons, 1.6 stroke. Every shape has pathLength="1"
     so CSS can animate stroke-dashoffset 1 → 0 ("draw" the icon).
     Use: <svg class="pm-icon"><use href="#i-mail"/></svg> -->





<blockquote class="blockquote">
<p>“The power of a statistical test is the probability that it will yield statistically significant results. Since statistical significance is so earnestly sought and devoutly wished for by behavioral scientists, one would think that the a priori probability of its accomplishment would be routinely determined and well understood. Quite surprisingly, this is not the case. Instead, if we take as evidence the research literature, we find evidence that statistical power is frequently not understood and, in reports of research where it is clearly relevant, the issue is not addressed.” Cohen, 1988, p.&nbsp;1.</p>
</blockquote>
<p>Sufficiently powered studies are of utmost importance for the field of Communication Science (or any other discipline obviously). Only with sufficiently large sample sizes, we are able to detect the effects we are interested (usually rather small) with a appropriately low false negative rate. Unfortunately, there is evidence that many subfields in Communication Science are suffering from underpowered studies (see e.g., Lanier et al., 2019; Elson &amp; Przybylski, 2017). As I am often asked about what power actually is and how to conduct power analysis, the following blogpost aims to introduce the concept of power and provide guidelines for how to incorporate power analyses as a standard practice in confirmatory research. It is my hope that it can work as a starting point for engaging with this topic more deeply.</p>
<p>I will first introduce the concept of statistical power in an a fictional example based on simulated data. I then define power more formally and introduce factors that affect power. I then introduce the concept of “power analysis” more broadly. At the core of this post, I show how one can conduct “simple” power analysis in R (using the package <code>pwr</code> and simulations; obviously power analyses can become quite complex, a while ago e.g., I wrote a <a href="../../blog/2020-03-11-power-simulations-sem/">blogpost about simulating power for SEMs</a>, which is more difficult and requires more assumptions.&nbsp; ). Finally, I outline some guidelines and provide examples for how to report power analyses in academic work.</p>
<section id="a-simple-example-using-simulations" class="level2">
<h2 class="anchored" data-anchor-id="a-simple-example-using-simulations">A simple example (using simulations)</h2>
<p>Power can best be understood through an example. Let’s imagine that we are interested in testing whether people perceive a stronger norm to disclose themselves on social media, when the majority of other users disclose themselves (cf.&nbsp;the study by Masur, DiFranzo &amp; Bazarova, 2021). In this experiment, participants are exposed to a social media feed that varies with regard to how many of the users disclosed themselves visually (5% vs.&nbsp;80%). Afterwards, we measure norm perceptions. The assumption is that the more people disclose themselves, the higher the perceived norm.</p>
<p>Just because we can, we will simulate the true effect in the population from which the draw our sample in the experiment:</p>
<div class="cell" data-layout-align="center">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load package</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span></code></pre></div></div>
</div>
<div class="cell" data-layout-align="center">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set seed to ensure reproducibility</span></span>
<span id="cb2-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;">5</span>)</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulate true effect in the population</span></span>
<span id="cb2-5">n <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;">1e5</span></span>
<span id="cb2-6">control <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>(n, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb2-7">treatment <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>(n, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb2-8">pop <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;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">condition =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</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;">"control"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"treatment"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> n),</span>
<span id="cb2-9">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outcome =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(control, treatment))</span>
<span id="cb2-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(pop)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; # A tibble: 6 × 2
#&gt;   condition outcome
#&gt;   &lt;chr&gt;       &lt;dbl&gt;
#&gt; 1 control      3.16
#&gt; 2 control      5.38
#&gt; 3 control      2.74
#&gt; 4 control      4.07
#&gt; 5 control      5.71
#&gt; 6 control      3.40</code></pre>
</div>
</div>
<p>This data set now represent the population from which we draw and how they would react to the different stimuli. Let’s quickly plot the distributions to get a better feel for the strength effect.</p>
<div class="cell" data-layout-align="center">
<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">p1 <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;">ggplot</span>(pop, <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> outcome, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> condition)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</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;">+</span></span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_void</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>)</span>
<span id="cb4-5">p1</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-3-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://philippmasur.de/blog/2024-05-28-statistical-power/index_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Two overlapping density curves of the outcome in the population: the control group centred around 4 and the treatment group centred around 5. The curves overlap considerably."></a></p>
</figure>
</div>
</div>
</div>
<p>We can see that two distributions overlap considerably, but the central tendencies clearly suggest a difference. Now, let’s conduct our experiment. We sample <em>n</em> = 10 people from this population, which are by<br>
definition randomly either in the control or in the treatment condition.</p>
<div class="cell" data-layout-align="center">
<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;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>)</span>
<span id="cb5-2">exp <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_n</span>(pop, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb5-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(exp)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; # A tibble: 6 × 2
#&gt;   condition outcome
#&gt;   &lt;chr&gt;       &lt;dbl&gt;
#&gt; 1 control      4.10
#&gt; 2 treatment    4.76
#&gt; 3 control      3.82
#&gt; 4 control      6.62
#&gt; 5 control      3.09
#&gt; 6 treatment    2.92</code></pre>
</div>
</div>
<p>Let’s see how these 10 people map onto the true population. We can do so by plotting them as points on the x-axis.</p>
<div class="cell" data-layout-align="center">
<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">p1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</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;">x =</span> outcome, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</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;">fill =</span> condition),</span>
<span id="cb7-3">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> exp, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-5-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://philippmasur.de/blog/2024-05-28-statistical-power/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="The two population density curves with the 10 sampled participants drawn as circles on the x-axis, coloured by condition. Control and treatment participants are scattered across similar values, so the group difference is hard to see."></a></p>
</figure>
</div>
</div>
</div>
<p>It seems we got a bit unlucky. The difference that is clearly visible in the population is not as apparent in the sample. Let’s confirm our suspicion and actually test the difference using a standard t-test:</p>
<div class="cell" data-layout-align="center">
<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;">t.test</span>(outcome <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> condition, exp)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; 
#&gt;  Welch Two Sample t-test
#&gt; 
#&gt; data:  outcome by condition
#&gt; t = -0.81441, df = 5.688, p-value = 0.4482
#&gt; alternative hypothesis: true difference in means between group control and group treatment is not equal to 0
#&gt; 95 percent confidence interval:
#&gt;  -3.225805  1.630822
#&gt; sample estimates:
#&gt;   mean in group control mean in group treatment 
#&gt;                3.994465                4.791956</code></pre>
</div>
</div>
<p>Indeed, the difference is not significant, even though we know that the effect does exist in the population. So what went wrong? Well, the study was <strong>underpowered</strong>. In other words, the sample size was too small to have a sufficiently high change to detect the effect.</p>
<p>So what would happen if we sample more people (e.g., <em>n</em> = 100)?</p>
<div class="cell" data-layout-align="center">
<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;">11</span>)</span>
<span id="cb10-2">exp2 <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_n</span>(pop, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb10-3"></span>
<span id="cb10-4">p1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</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;">x =</span> outcome, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</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;">fill =</span> condition),</span>
<span id="cb10-6">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> exp2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-7-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-3"><img src="https://philippmasur.de/blog/2024-05-28-statistical-power/index_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="The two population density curves with 100 sampled participants drawn as circles on the x-axis. The control participants cluster further left and the treatment participants further right, mirroring the population distributions."></a></p>
</figure>
</div>
</div>
</div>
<p>Now, it seems that the two groups (shown as circles in the figure above) are more “in line” with the distribution of the population. Let’s again test the mean difference and voila, the difference now is significant.</p>
<div class="cell" data-layout-align="center">
<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t.test</span>(outcome <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> condition, exp2)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; 
#&gt;  Welch Two Sample t-test
#&gt; 
#&gt; data:  outcome by condition
#&gt; t = -4.67, df = 91.213, p-value = 1.033e-05
#&gt; alternative hypothesis: true difference in means between group control and group treatment is not equal to 0
#&gt; 95 percent confidence interval:
#&gt;  -1.3850141 -0.5584102
#&gt; sample estimates:
#&gt;   mean in group control mean in group treatment 
#&gt;                4.047700                5.019412</code></pre>
</div>
</div>
<p>What we can learn from this example is the following: The probability of finding a significant effect based on our sample (given that the effect actually exist) is contingent on several aspects:</p>
<ul>
<li>the size of our sample (n1&nbsp;=10, n2 = 100)</li>
<li>the strength of the true effect</li>
<li>the significance level (if we would have chosen a stricter level, such as e.g., 1%, the probability of finding a significant effect would be lower)</li>
<li>the type of analysis the we are conducting (in this case a t-test)</li>
</ul>
<p>And the probability of finding a significant effect given these parameters is what we are going to talk about in this chapter: it is what we call <strong>statistical power</strong>!</p>
</section>
<section id="what-is-power" class="level2">
<h2 class="anchored" data-anchor-id="what-is-power">What is power?</h2>
<p>The concept “statistical power” refers to the probability of correctly rejecting a null hypothesis (H0) when the<br>
alternative hypothesis (H1) is true. It can be formally written as 1-beta, and represents the chances of a <em>true positive</em> detection conditional on the actual existence of an effect to detect. As the power of a test increases, the probability of making a type II error, which means wrongly failing to reject the null hypothesis decreases (false negative result). In simpler words, the more power we have, the less likely we are to assume that there is no effect, even though the effect actually exists (see Figure 1).</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="img/fig-04.jpg" class="lightbox" data-gallery="quarto-lightbox-gallery-4" title="Figure 1: Difference between type I and type II error"><img src="https://philippmasur.de/blog/2024-05-28-statistical-power/img/fig-04.jpg" class="img-fluid figure-img" alt="Figure 1: Difference between type I and type II error"></a></p>
<figcaption>Figure 1: Difference between type I and type II error</figcaption>
</figure>
</div>
<p><em>Figure 1:</em> Difference between type I and type II error</p>
<p>Power is determined by several factors that are often contingent on the exact study that is conducted. But in any case, it is primarily determined by three factors:</p>
<p>—</p>
<ol type="1">
<li>The effect size</li>
<li>The significance level</li>
<li>The sample size</li>
</ol>
<p>—</p>
<p>The <strong>effect size</strong> can be a direct value of the quantity of interest (e.g, <em>b</em>) in a regression analysis or the mean difference in a t-test), but it can also be a standardized measure that also accounts for the variability in the population (e.g., <em>beta</em>) in regression analysis or Cohen’s <em>d</em>&nbsp;in a t-test). In a power analysis (see further below), this parameter is oft utmost interest. Before we collect data, we can only guess the magnitude of the effect. At times, we might have information from prior research that informs our estimation. But even if no similar study exists, we can make informed guesses. For example, Rain, Levine &amp; Weber (2018) found an average effect size of <em>r</em> = .21 across 149 meta-analyses spanning 60 years of communication research. This could be a useful baseline for a power analysis (if one can reasonably assume that one’s own study is not different than the average communication science study!). Another alternative would be to consider the smallest effect size that one would still consider sufficient support for the hypothesis under investigation. For example, Purington et al.&nbsp;(2021) investigate the impact of a experiential learning platform on subsequent knowledge increase as assessed in a 8-item knowledge test. They argued that the smallest (unstandardized) effect size that would still speak for the effectiveness of the platform corresponds to participants who received the intervention having 1 more correct answer in the test compared to this who did not receive the intervention. The concept of the smallest effect size of interest (SESOI) can also be applied more generally. For a given hypothesis, what would be the smallest standardized effect size that still supports the hypothesis? Often, we may then resort to an effect size of e.g., <em>r&nbsp;</em>= |.10|, which represents a “small” effect according to Cohen (1988).</p>
<p>The <strong>significance level</strong> refers to the false positive rate and is usually set to 5%. One way to increase the power of a hypothesis test would be to use a larger significance level (e.g., 10%). This increases the chance of rejecting the null hypothesis (obtaining a statistically significant result) when the null hypothesis is false; that is, the effect actually exists. But, at the same time, it heightens the risk of obtaining a statistically significant result when the null<br>
hypothesis is not false; that is, the effect does not actually exist. Thus, although a less conservative significance level may increase power to detect a statistically significant result when the effect is true (lower false negative rate), it heightens the probability of obtaining a statistically significant effect when the effect does not exist (higher false positive rate).</p>
<p>The <strong>sample size</strong> greatly determines power as it affects the sampling error inherent in a test. The larger the sample size, the more it resembles the population from which it was drawn. If all other factors are kept equal, effects are generally harder to detect in smaller samples. Thus increasing the sample size is often the easiest way to boost the statistical power of a test.</p>
<p>Try it yourself: change the true effect, the sample size and the significance level, then run a few studies and see how often they come out significant.</p>
<figure class="pm-explainer figure"><iframe src="../../explainers/power/index.html" title="Interactive explainer: power" loading="lazy" style="height:880px"></iframe><figcaption> Interactive: sampling distributions, simulated studies and the power curve, computed in your browser.</figcaption></figure>
<p>If you prefer code: the cell below runs in your browser (via <a href="https://docs.r-wasm.org/">webR</a>), so you can edit it and press <em>Run Code</em>. It simulates 1,000 studies and compares the share of significant results with the analytical answer from <code>power.t.test()</code>.</p>
<div class="pm-live-hint">
<p> Editable R, running in your browser: the first run takes a few seconds to load R.</p>
</div>
<div class="cell" data-layout-align="center">
<div>
<div id="webr-1" class="exercise-cell">

</div>
<script type="webr-1-contents">
eyJhdHRyIjp7ImVkaXQiOnRydWUsImV2YWwiOnRydWV9LCJjb2RlIjoibiA8LSA1MCAgICAgICMgcGFydGljaXBhbnRzIHBlciBncm91cFxuZCA8LSAwLjMgICAgICMgdHJ1ZSBlZmZlY3Qgc2l6ZSAoQ29oZW4ncyBkKVxuXG5wX3ZhbHVlcyA8LSByZXBsaWNhdGUoMTAwMCwge1xuICBjb250cm9sICAgPC0gcm5vcm0obiwgbWVhbiA9IDAsIHNkID0gMSlcbiAgdHJlYXRtZW50IDwtIHJub3JtKG4sIG1lYW4gPSBkLCBzZCA9IDEpXG4gIHQudGVzdCh0cmVhdG1lbnQsIGNvbnRyb2wpJHAudmFsdWVcbn0pXG5cbm1lYW4ocF92YWx1ZXMgPCAuMDUpICAgICAgICAgICAgICAgICAgICAgICAgICAjIHNpbXVsYXRlZCBwb3dlclxucG93ZXIudC50ZXN0KG4gPSBuLCBkZWx0YSA9IGQsIHNkID0gMSkkcG93ZXIgICMgYW5hbHl0aWNhbCBwb3dlciJ9
</script>
</div>
</div>
<p>Given the entanglement of these factors with power, we can compute each of them based on all others. So if the effect size, the sample size, and the significance level is known, we can compute the power. And<br>
vice versa, if we know the effect size, the significance level and the power, we can compute the necessary sample size. This is used in the so-called “power analysis”, which we discuss in the next section.</p>
<p>Before we do so, however, we have to note that next to these three pivotal factors, there are also other aspects influencing the power of a hypothesis test. For example, measurement precision also affects power. The better or measures (i.e., higher reliability), the higher the power. Another influential factor is the design of the study and what analysis is used (see further below).</p>
</section>
<section id="power-analysis" class="level2">
<h2 class="anchored" data-anchor-id="power-analysis">Power analysis</h2>
<p>A power analysis most often refers to calculating the minimum sample size required to detect an effect of a certain magnitude given a certain significance level and power goal. For example, before I collect my data, I would like to know how many participants I should recruit. Given the norm of the field, I set the significance level to 5%. I further have heard that I should test my effect with an equally strict power, so I set the power to 95%. Based on prior research, I assume that the effect size of interest should be <em>r</em> = .10. The type of analysis is a correlation analysis. So, I know all three important factors as well as the type of analysis. How can I compute the necessary sample size? The formula for calculating the necessary sample size for a correlation test is:</p>
<p><a href="img/Screenshot-2024-05-28-at-13.49.54-300x95.png" class="lightbox" data-gallery="quarto-lightbox-gallery-5"><img src="https://philippmasur.de/blog/2024-05-28-statistical-power/img/Screenshot-2024-05-28-at-13.49.54-300x95.png" class="img-fluid"></a></p>
<p>where <em>n</em> is the sample size, <em>Z~1-alpha/2~</em>&nbsp;is the z-score corresponding to the desired level of significance, <em>Z1-beta</em>&nbsp;is the z-score corresponding to the desired power of the test, and <em>r</em>&nbsp;is the expected magnitude of the correlation between the two variables. For our example, it can thus be computed like so:</p>
<p><a href="img/Screenshot-2024-05-28-at-13.50.02-231x300.png" class="lightbox" data-gallery="quarto-lightbox-gallery-6"><img src="https://philippmasur.de/blog/2024-05-28-statistical-power/img/Screenshot-2024-05-28-at-13.50.02-231x300.png" class="img-fluid"></a></p>
<p>This means that the study will need to have a sample size of at least 1,296 in order to have a power of 95%, a significance level of 5%, and an effect size of <em>r</em> = 0.10. As mentioned earlier, this is just an approximation, and the actual sample size needed may vary depending on the specific characteristics of the study.</p>
<p>However, it has to be noted that the example above is what we would call an “a priori power analysis”. The term “power analysis” is broader and refers to at least three types of analyses that differ with regard to when they are usually conducted and their respective goals (see Table below).</p>
<table class="caption-top table">
<colgroup>
<col style="width: 23%">
<col style="width: 52%">
<col style="width: 23%">
</colgroup>
<thead>
<tr class="header">
<th>Type of analysis</th>
<th>Definition</th>
<th>What is estimated?</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>A priori power analysis</td>
<td>An analysis that is usually computed before the data is collected to estimate the necessary sample size that allows to test a certain effect with sufficient power given the level of significance</td>
<td>The sample size (<em>n</em>)</td>
</tr>
<tr class="even">
<td>Post-hoc sensitivity analysis</td>
<td>An analysis that is usually conducted after the data collection to find out what effect size can still be tested with sufficient power given the significance level and the size of collected data set</td>
<td>The effect size (e.g., <em>r</em>, <em>beta coefficient</em>&nbsp;or Cohen’s <em>d</em>)</td>
</tr>
<tr class="odd">
<td>Post-hoc power analysis</td>
<td>An analysis, usually after the data collection, to find out what power was achieved given the collected sample, the significance level and the found effect size</td>
<td>Power (1-beta-error)</td>
</tr>
</tbody>
</table>
</section>
<section id="power-analysis-in-r" class="level2">
<h2 class="anchored" data-anchor-id="power-analysis-in-r">Power analysis in <code>R</code></h2>
<p>There are different ways to compute power (or rather the necessay sample size) in R. Very broadly speaking, we can differentiate between calculating power (e.g., with packages such as <code>pwr</code>) or by simulating power (e.g., with packages such as <code>MonteCarlo</code>. In the following, I will exemplify both approaches.</p>
<section id="power-calculations" class="level3">
<h3 class="anchored" data-anchor-id="power-calculations">Power Calculations</h3>
<p>For simple analyses, we can often solve the above described challenge of computing the necessary sample size mathematically. In R, we have the package <code>pwr</code>, which provides various functions that can be used for both sensitivity and power analyses.</p>
<p>Let’s first investigate the power of our example above.</p>
<div class="cell" data-layout-align="center">
<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;"># Load package</span></span>
<span id="cb13-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(pwr)</span>
<span id="cb13-3"></span>
<span id="cb13-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Computing Cohen's d based on the results of our first experiment</span></span>
<span id="cb13-5">cohens_d <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;">4.79-3.99</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;">1.43</span></span>
<span id="cb13-6"></span>
<span id="cb13-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Estimate power</span></span>
<span id="cb13-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pwr.t.test</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">d =</span> cohens_d,                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Effect size</span></span>
<span id="cb13-9">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,                       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sample size per group</span></span>
<span id="cb13-10">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sig.level =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">05</span>,             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Alpha error</span></span>
<span id="cb13-11">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alternative =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"two.sided"</span>)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Type of test</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; 
#&gt;      Two-sample t test power calculation 
#&gt; 
#&gt;               n = 5
#&gt;               d = 0.5594406
#&gt;       sig.level = 0.05
#&gt;           power = 0.12256
#&gt;     alternative = two.sided
#&gt; 
#&gt; NOTE: n is number in *each* group</code></pre>
</div>
</div>
<p>We can see that the power to detect a significant effect (Cohen’s <em>d</em> = 0.56) given the sample size (<em>n</em> = 10), the significance level (5%) and the type of test (two-sided) was very low (power = 12%). It was hence not surprising that we did not find a significant effect.</p>
<p>So what was the power in the second experiment with <em>n</em> = 100 participants?</p>
<div class="cell" data-layout-align="center">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Computing Cohen's d based on the results of our first experiment</span></span>
<span id="cb15-2">cohens_d <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;">5.02-4.05</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;">1.13</span></span>
<span id="cb15-3"></span>
<span id="cb15-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Post-hoc power analysis</span></span>
<span id="cb15-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pwr.t.test</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">d =</span> cohens_d,</span>
<span id="cb15-6">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>,</span>
<span id="cb15-7">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sig.level =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">05</span>,</span>
<span id="cb15-8">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alternative =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"two.sided"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; 
#&gt;      Two-sample t test power calculation 
#&gt; 
#&gt;               n = 50
#&gt;               d = 0.8584071
#&gt;       sig.level = 0.05
#&gt;           power = 0.9889793
#&gt;     alternative = two.sided
#&gt; 
#&gt; NOTE: n is number in *each* group</code></pre>
</div>
</div>
<p>Clearly, we have a 99% power to detect the effect of interest. Technically, we have just conducted a “post-hoc power analysis”. So we estimate the power given the found effect and the collected sample size.</p>
<p>As a second example, let’s actually conduct the a priori power analysis that we already solved mathematically earlier. Let’s imagine that we want to conduct a survey to investigate the relationship between social media use and well-being. The first step is quantifying our assumption about the strength of the potential effect (e.g., is the effect rather small or rather strong?). If we are unsure, we may want to define a smallest effect size of interest (SESOI), which we deem still relevant enough to support our hypotheses (e.g., the more social media use leads to less well-being). In this case, I chose <em>r</em> = .1 as I deem anything below this typical threshold would be too small to support this hypothesis (a fairly vague assumption).</p>
<div class="cell" data-layout-align="center">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Estimate necessary sample size</span></span>
<span id="cb17-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pwr.r.test</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r =</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;"># Assumed effect size</span></span>
<span id="cb17-3">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sig.level =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">05</span>,             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># alpha error</span></span>
<span id="cb17-4">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">power =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</span>,                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Power (1 - beta error)</span></span>
<span id="cb17-5">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alternative =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"two.sided"</span>)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Type of test</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; 
#&gt;      approximate correlation power calculation (arctangh transformation) 
#&gt; 
#&gt;               n = 1292.88
#&gt;               r = 0.1
#&gt;       sig.level = 0.05
#&gt;           power = 0.95
#&gt;     alternative = two.sided</code></pre>
</div>
</div>
<p>The analysis suggest that we would need <em>n</em> = 1,293 participants to have sufficient (here 95%) power to detect an effect of <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> = .10.</p>
<p>Of course, we can also explore what kind of sample sizes we need to achieve different levels of power:</p>
<div class="cell" data-layout-align="center">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set power range</span></span>
<span id="cb19-2">power <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;">seq</span>(.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">05</span>)</span>
<span id="cb19-3"></span>
<span id="cb19-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create function to extract sample size from pwr.r.test</span></span>
<span id="cb19-5">extract_n <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>(x) {</span>
<span id="cb19-6">  pwr.res <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;">pwr.r.test</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,                      </span>
<span id="cb19-7">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sig.level =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">05</span>,            </span>
<span id="cb19-8">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">power =</span> x,                 </span>
<span id="cb19-9">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alternative =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"two.sided"</span>)</span>
<span id="cb19-10">  pwr.res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n</span>
<span id="cb19-11">}</span>
<span id="cb19-12"></span>
<span id="cb19-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Combine in table</span></span>
<span id="cb19-14">pwr.table <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;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">power =</span> power,</span>
<span id="cb19-15">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sample_size =</span> power <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb19-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map_dbl</span>(<span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extract_n</span>(x)))</span>
<span id="cb19-17">pwr.table</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; # A tibble: 10 × 2
#&gt;    power sample_size
#&gt;    &lt;dbl&gt;       &lt;dbl&gt;
#&gt;  1  0.5         384.
#&gt;  2  0.55        434.
#&gt;  3  0.6         489.
#&gt;  4  0.65        548.
#&gt;  5  0.7         615.
#&gt;  6  0.75        692.
#&gt;  7  0.8         782.
#&gt;  8  0.85        894.
#&gt;  9  0.9        1046.
#&gt; 10  0.95       1293.</code></pre>
</div>
</div>
<div class="cell" data-layout-align="center">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot sample curve</span></span>
<span id="cb21-2">pwr.table <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb21-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</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;">x =</span> sample_size, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> power)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb21-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb21-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb21-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb21-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sample Size"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Power"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-14-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-7"><img src="https://philippmasur.de/blog/2024-05-28-statistical-power/index_files/figure-html/unnamed-chunk-14-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Red power curve for a correlation of r = .10: power rises from 50% at about 384 participants to 95% at about 1,293 participants. A dashed grey line marks 80% power, reached at about 782 participants."></a></p>
</figure>
</div>
</div>
</div>
<p>Again, we can also explore the necessary sample sizes for different effect sizes and power levels:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1">power <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;">seq</span>(.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">05</span>)</span>
<span id="cb22-2">effect <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;">seq</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;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">05</span>)</span>
<span id="cb22-3">grid <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>(power, effect)</span>
<span id="cb22-4"></span>
<span id="cb22-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract sample sizes</span></span>
<span id="cb22-6">extract_param <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>(x, y) {</span>
<span id="cb22-7">  pwr.res <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;">pwr.r.test</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r =</span> y,                      </span>
<span id="cb22-8">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sig.level =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">05</span>,            </span>
<span id="cb22-9">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">power =</span> x,                 </span>
<span id="cb22-10">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alternative =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"two.sided"</span>)</span>
<span id="cb22-11">  pwr.res<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n</span>
<span id="cb22-12">}</span>
<span id="cb22-13"></span>
<span id="cb22-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot curves</span></span>
<span id="cb22-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(grid, </span>
<span id="cb22-16">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmap_dbl</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>power, grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>effect), extract_param)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb22-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</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;">x =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> power, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> effect, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> effect)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-19">  <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>
<span id="cb22-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sample Size"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Power"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-15-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-8"><img src="https://philippmasur.de/blog/2024-05-28-statistical-power/index_files/figure-html/unnamed-chunk-15-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Power curves for correlations from r = .10 to r = .50, from dark to light blue. The curve for r = .10 needs about 780 participants for 80% power (dashed line) and about 1,300 for 95%; larger effects need far fewer, and for r = .50 fewer than 50 participants suffice."></a></p>
</figure>
</div>
</div>
</div>
</section>
<section id="simulation-based-power-analysis" class="level3">
<h3 class="anchored" data-anchor-id="simulation-based-power-analysis">Simulation-based power analysis</h3>
<p>In a third example, let’s consider a slightly different scenario: Let’s imagine an 2×2 between-person experimental design, but we have no idea about the standardized effect size, neither for the two main effects, nor for the interaction. But we have some idea about how the factors should affect our outcome variable. In this case, we can simulate data that correspond to our assumptions. By then varying the sample size (as well as any other parameter that we find interesting to explore), we can approximate power through iterations of the simulation.<br>
So first, let’s simulate data that corresponds to such a typical experimental design.</p>
<div class="cell" data-layout-align="center">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load package</span></span>
<span id="cb23-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(MonteCarlo)</span></code></pre></div></div>
</div>
<div class="cell" data-layout-align="center">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ensure reproducibility</span></span>
<span id="cb24-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;">42</span>)</span>
<span id="cb24-3"></span>
<span id="cb24-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define parameters</span></span>
<span id="cb24-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;">500</span></span>
<span id="cb24-6">means <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;">2.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.75</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># a1b1, a2b1, a1b2, a2b2</span></span>
<span id="cb24-7">sd <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="cb24-8"></span>
<span id="cb24-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulate data</span></span>
<span id="cb24-10">dv <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>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> means , <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> sd) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb24-11">iv1 <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;">rep</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;">"a1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a2"</span>),<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</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>
<span id="cb24-12">iv2 <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;">rep</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;">"b1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"b2"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</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;">4</span>)</span>
<span id="cb24-13">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;">data.frame</span>(dv, iv1, iv2)</span>
<span id="cb24-14"></span>
<span id="cb24-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check data simulation</span></span>
<span id="cb24-16">d <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb24-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(iv1, iv2) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb24-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">m =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(dv)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb24-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</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;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(iv1), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> m, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> iv2, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> iv2)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</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>
<span id="cb24-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylim</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;">5</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DV"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"IV 1"</span>,</span>
<span id="cb24-25">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"IV 2"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-17-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-9"><img src="https://philippmasur.de/blog/2024-05-28-statistical-power/index_files/figure-html/unnamed-chunk-17-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Interaction plot of the simulated 2×2 design: mean DV for IV 1 levels a1 and a2, with one line per IV 2 level. The b1 line rises only slightly from a1 to a2 (from about 2.4 to 2.7), whereas the b2 line rises more steeply, from about 3 to about 4."></a></p>
</figure>
</div>
</div>
</div>
<p>As we can see, the interaction is clearly visible, albeit rather small in its magnitude.</p>
<p>Next, we put this code in a self-defined function that thus simulates the data, fits the models, extracts p-values, and the significance (based on p &lt; .05). This function will be passed to a grid of possible parameters.</p>
<div class="cell" data-layout-align="center">
<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;"># Simulation function</span></span>
<span id="cb25-2">sim_func <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>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, </span>
<span id="cb25-3">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">means =</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;">2.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.75</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>),</span>
<span id="cb25-4">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb25-5">  </span>
<span id="cb25-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulate data</span></span>
<span id="cb25-7">  dv <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;">round</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;">mean =</span> means , <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> sd),<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb25-8">  iv1 <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;">rep</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;">"a1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a2"</span>),<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</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>
<span id="cb25-9">  iv2 <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;">rep</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;">"b1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"b2"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</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;">4</span>)</span>
<span id="cb25-10">  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;">data.frame</span>(dv, iv1, iv2)</span>
<span id="cb25-11">  </span>
<span id="cb25-12">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fit models</span></span>
<span id="cb25-13">  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;">lm</span>(dv <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> iv1, d)     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Main effet of IV1</span></span>
<span id="cb25-14">  fit2 <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;">lm</span>(dv <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> iv2, d)     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Main effect of IV2</span></span>
<span id="cb25-15">  fit3 <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;">lm</span>(dv <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> iv1<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>iv2, d) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Interaction between both</span></span>
<span id="cb25-16">  </span>
<span id="cb25-17">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract p-values and compute significance</span></span>
<span id="cb25-18">  p_1 <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>(fit1)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>coef[<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;">4</span>]</span>
<span id="cb25-19">  sig_1 <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>(p_1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">05</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="cb25-20">  p_2 <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>(fit2)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>coef[<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;">4</span>]</span>
<span id="cb25-21">  sig_2 <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>(p_2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">05</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="cb25-22">  p_3 <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>(fit3)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>coef[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]</span>
<span id="cb25-23">  sig_3 <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>(p_3 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">05</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="cb25-24">  </span>
<span id="cb25-25"></span>
<span id="cb25-26">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># return values as list</span></span>
<span id="cb25-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p_1"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> p_1,</span>
<span id="cb25-28">              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sig_1"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> sig_1,</span>
<span id="cb25-29">              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p_2"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> p_2,</span>
<span id="cb25-30">              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sig_2"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> sig_2,</span>
<span id="cb25-31">              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p_3"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> p_3,</span>
<span id="cb25-32">              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sig_3"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> sig_3))</span>
<span id="cb25-33">}</span></code></pre></div></div>
</div>
<p>Now, we create a list that defines how the parameters, in this case the sample size and the standard devations, should vary. The good part about this simulation approach is that we can explore a range of<br>
different combinations. We could even vary the actual effects (i.e., mean differences).</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Parameters that should vary across simulations</span></span>
<span id="cb26-2">n_grid <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;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">600</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb26-3">sd_grid <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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb26-4"></span>
<span id="cb26-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Collect simulation parameters in list</span></span>
<span id="cb26-6">(param_list <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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> n_grid,</span>
<span id="cb26-7">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sd"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> sd_grid))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; $n
#&gt; [1] 100 200 300 400 500 600
#&gt; 
#&gt; $sd
#&gt; [1] 1.0 1.5 2.0</code></pre>
</div>
</div>
<p>Now, we can run the actual simulation using the function <code>MonteCarlo()</code>, which requires us to provide the function we created above, the number of iterations per combinations of the parameters, the number of cores we want to use (we can parallelize here, which saves a lot of time!), and the parameter list. Here, we use 1,000 runs per combination, but for stable solutions, we should actually use around 10,000!</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1">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;">MonteCarlo</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">func =</span> sim_func,            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pass test function</span></span>
<span id="cb28-2">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrep =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>,                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># number of tests</span></span>
<span id="cb28-3">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncpus =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,                   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># number of cores to be used</span></span>
<span id="cb28-4">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">param_list =</span> param_list, )   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># provide parameters</span></span></code></pre></div></div>
</div>
<div class="cell" data-layout-align="center">
<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">df <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;">MakeFrame</span>(result)</span>
<span id="cb29-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(df)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt;     n sd          p_1 sig_1          p_2 sig_2          p_3 sig_3
#&gt; 1 100  1 2.482367e-03     1 3.051370e-05     1 1.254294e-01     0
#&gt; 2 200  1 5.234942e-03     1 5.559082e-08     1 5.141067e-04     1
#&gt; 3 300  1 5.229393e-07     1 2.278427e-13     1 1.114671e-01     0
#&gt; 4 400  1 3.545625e-08     1 3.673009e-11     1 1.094738e-05     1
#&gt; 5 500  1 1.240856e-06     1 2.743887e-20     1 6.631778e-05     1
#&gt; 6 600  1 1.895437e-12     1 1.446259e-19     1 1.019437e-03     1</code></pre>
</div>
</div>
<p>As we can see, the data frame now includes the parameters (n, sd) and the p-values and significance (true, false) related to all three effects that we were interested in. We can now plot so-called power curves that tell us how much power we achieve (on average) for each specification. We first need to summarize the data by counting the number of significant results per combination of sample size and standard deviation. We then pass this to a simple line plot with facets.</p>
<div class="cell" data-layout-align="center">
<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">df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb31-2">  dplyr<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;">select</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;">contains</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb31-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gather</span>(key, value, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>n, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>sd) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb31-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(n, sd, key) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb31-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">power =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(value)<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>(value)<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 class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb31-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</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;">x =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> power, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> key)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb31-7">  <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>
<span id="cb31-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb31-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb31-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb31-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>sd) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb31-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb31-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Power Curves"</span>,</span>
<span id="cb31-14">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sample size (n)"</span>, </span>
<span id="cb31-15">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"power (1-beta)"</span>, </span>
<span id="cb31-16">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"type of effect"</span>,</span>
<span id="cb31-17">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Note: facets represent different standard deviations"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-22-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-10"><img src="https://philippmasur.de/blog/2024-05-28-statistical-power/index_files/figure-html/unnamed-chunk-22-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Power curves in three facets for standard deviations of 1, 1.5 and 2, with one line per effect (sig_1 and sig_2 for the main effects, sig_3 for the interaction). Dashed lines mark 80% and 95% power. The interaction needs the largest samples, and all curves shift towards larger samples as the standard deviation grows."></a></p>
</figure>
</div>
</div>
</div>
<p>For this example, we can clearly see that the interaction effect (blue) requires larger sample sizes to reach a sufficient power (dashed lines are 80% and 95% power respectively). We can also see that all three effects require larger sample sizes if the variance in the outcome variables increases.</p>
<p>In sum, we can see that simulating data a priori allows us to explore in more depth how parameters that might vary in studies affect the minimum sample size. Particularly in situation in which we do not have a clear cut assumption about the effect size of interest, such simulation approaches can be very helpful. For more in-depth tutorials, see also the following resources:</p>
<ul>
<li>Workshop “Power Analysis through Simulations in R” by Niklas Johannes (<a href="https://niklasjohannes.github.io/power-workshop/">github page</a>)</li>
<li>Shiny App that Runs Simulation-based Power Analyses for ANOVAs by Daniel Lakens (<a href="https://shiny.ieis.tue.nl/anova_power/">app</a>)</li>
<li>My blogpost on computing power for structural equation models (<a href="../../blog/2020-03-11-power-simulations-sem/">post</a>)</li>
</ul>
</section>
<section id="section" class="level3">
<h3 class="anchored" data-anchor-id="section"></h3>
</section>
<section id="how-to-report-a-power-analysis" class="level3">
<h3 class="anchored" data-anchor-id="how-to-report-a-power-analysis">How to report a power analysis?</h3>
<p>First, best practice would be to conduct a power analysis <em>a priori</em>, i.e., before the data collection. You want to make sure that you have a sufficiently large sample to detect your effect(s) of interest before you waste any money on recruiting participants At best, you report on your power analysis in your preregistration as a justification of your planned sample size. Here, the description can be as comprehensive as necessary. An a priori power analysis can be rather humbling at this point. Sometimes, we may find that we need an unaffordable number of participants for our study. In this case, we should try to find ways to increase the power otherwise (e.g., improving measurement, switching from between- to within-person designs, increasing the impact of a manipulation,…) or (as bad this may be) not doing the study at all.</p>
<p>Second, although an a priori power analysis should be in the preregistration, you nonetheless want to summarize it in your paper. Here, I would suggest to include it at the beginning of the method section, directly before the sample description. This way, readers can compare your obtained sample size with your planned sample size. If you recruited less people than planned, it could be worthwhile to include a sensitivity analysis after the sample description to clearly state which effect sizes you can still interpret with the a priori set power threshold.</p>
<p>An example for the reporting of an a priori power analysis that was based on simply using the <code>pwr.anova.test</code> function from the package <code>pwr</code> (Masur, DiFranzo &amp; Bazarova, 2021):</p>
<blockquote class="blockquote">
<p>We based our minimum sample size on power calculations for the main effects of the experimental manipulations and a potential interaction between both. Assuming a smallest effect size of interest of <em>f</em> = .15 for main effects and interaction effects on both norm perceptions and the likelihood of disclosing oneself resulted in a minimum sample size of <em>N</em> = 536 (given a significance level of 5% and a power of 80%).</p>
</blockquote>
<p>When using a simulation-based approach, it obvously makes sense to describe the exact data simulation approach, how parameters were varied, how many runs per combinations were computed, etc. The overall goal is to be as transparent as possible.</p>
</section>
<section id="references" class="level3">
<h3 class="anchored" data-anchor-id="references">References</h3>
<p>Cohen, J. (1988). Statistical power analysis for the behavioral sciences. Second edition. Lawrence Erlbaum.</p>
<p>Elson, M., &amp; Przybylski, A. K. (2017). The science of technology and human behavior. Journal of Media Psychology.</p>
<p>Lanier, M., Waddell, T. F., Elson, M., Tamul, D. J., Ivory, J. D., &amp; Przybylski, A. (2019). Virtual reality check: Statistical power, reported results, and the validity of research on the psychology of virtual reality and immersive environments. Computers in Human Behavior, 100, 70-78.</p>
<p>Masur, P. K., DiFranzo, D., &amp; Bazarova, N. N. (2021). Behavioral contagion on social media: Effects of social norms, design interventions, and critical media literacy on self-disclosure. Plos one, 16(7), e0254670.</p>
<p>Purington A. , Masur, P. K. , Zou, E. W., Whitlock, J. &amp; Bazarova, N. (2021). Preregistration for study 2 – Outcome Evaluation. https://osf.io/e4zdr</p>
<p>Rains, S. A., Levine, T. R., &amp; Weber, R. (2018). Sixty years of quantitative communication research summarized: Lessons from 149 meta-analyses. Annals of the International Communication Association, 42(2), 105-124.</p>
<div class="callout callout-style-simple callout-note no-icon callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>1 comment from the original post
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>These comments were left on the earlier WordPress version of this post.</p>
<div class="archived-comment">
<p><strong>Moritz</strong> · 23 August 2024</p>
<p>Philipp, thanks for this excellent post, a very useful resource! I find simulation-based power analysis extremely intuitive, and thanks to R/tidyverse quite accessible.</p>
</div>
</div>
</div>
</div>


<template id="pm-footer-icons"><a href="#" data-pm-mail="p.k.masur|vu.nl" aria-label="Email" title="Email"><svg class="pm-icon" aria-hidden="true"><use href="#i-mail"></use></svg></a><a href="https://scholar.google.com/citations?user=XA_WthYAAAAJ" target="_blank" rel="noopener" aria-label="Google Scholar" title="Google Scholar"><svg class="pm-icon" aria-hidden="true"><use href="#i-scholar"></use></svg></a><a href="https://orcid.org/0000-0003-3065-7305" target="_blank" rel="noopener" aria-label="ORCID" title="ORCID"><svg class="pm-icon" aria-hidden="true"><use href="#i-orcid"></use></svg></a><a href="https://osf.io/yuqv5/" target="_blank" rel="noopener" aria-label="Open Science Framework" title="Open Science Framework"><svg class="pm-icon" aria-hidden="true"><use href="#i-osf"></use></svg></a><a href="https://github.com/masurp" target="_blank" rel="noopener" aria-label="GitHub" title="GitHub"><svg class="pm-icon" aria-hidden="true"><use href="#i-github"></use></svg></a><a href="https://bsky.app/profile/masurphil.bsky.social" target="_blank" rel="noopener" aria-label="Bluesky" title="Bluesky"><svg class="pm-icon" aria-hidden="true"><use href="#i-bluesky"></use></svg></a><a href="https://research.vu.nl/en/persons/philipp-k-masur" target="_blank" rel="noopener" aria-label="VU Amsterdam" title="VU Amsterdam"><svg class="pm-icon" aria-hidden="true"><use href="#i-landmark"></use></svg></a></template>
<script type="webr-data">
eyJvcHRpb25zIjp7ImJhc2VVcmwiOiJodHRwczovL3dlYnIuci13YXNtLm9yZy92MC42LjAvIn0sInBhY2thZ2VzIjp7InBrZ3MiOlsiZXZhbHVhdGUiLCJrbml0ciIsImh0bWx0b29scyJdLCJyZXBvcyI6W119LCJyZW5kZXJfZGYiOiJkZWZhdWx0In0=
</script>
<script type="ojs-module-contents">
eyJjb250ZW50cyI6W3siY2VsbE5hbWUiOiJ3ZWJyLXdpZGdldC0xIiwiaW5saW5lIjpmYWxzZSwibWV0aG9kTmFtZSI6ImludGVycHJldFF1aWV0Iiwic291cmNlIjoie1xuICAvLyBXYWl0IGZvciBvdXRwdXQgdG8gYmUgd3JpdHRlbiB0byB0aGUgRE9NLCB0aGVuIHRyaWdnZXIgd2lkZ2V0IHJlbmRlcmluZ1xuICBhd2FpdCBfd2Vicl92YWx1ZV8xO1xuICBpZiAod2luZG93LkhUTUxXaWRnZXRzKSB7XG4gICAgd2luZG93LkhUTUxXaWRnZXRzLnN0YXRpY1JlbmRlcigpO1xuICB9XG4gIGlmICh3aW5kb3cuUGFnZWRUYWJsZURvYykge1xuICAgIHdpbmRvdy5QYWdlZFRhYmxlRG9jLmluaXRBbGwoKTtcbiAgfVxufVxuIn0seyJjZWxsTmFtZSI6IndlYnItMSIsImlubGluZSI6ZmFsc2UsIm1ldGhvZE5hbWUiOiJpbnRlcnByZXQiLCJzb3VyY2UiOiJ2aWV3b2YgX3dlYnJfZWRpdG9yXzEgPSB7XG4gIGNvbnN0IHsgV2ViUkV4ZXJjaXNlRWRpdG9yLCBiNjREZWNvZGUgfSA9IHdpbmRvdy5fZXhlcmNpc2Vfb2pzX3J1bnRpbWU7XG4gIGNvbnN0IHNjcmlwdENvbnRlbnQgPSBkb2N1bWVudC5xdWVyeVNlbGVjdG9yKGBzY3JpcHRbdHlwZT1cXFwid2Vici0xLWNvbnRlbnRzXFxcIl1gKS50ZXh0Q29udGVudDtcbiAgY29uc3QgYmxvY2sgPSBKU09OLnBhcnNlKGI2NERlY29kZShzY3JpcHRDb250ZW50KSk7XG5cbiAgY29uc3Qgb3B0aW9ucyA9IE9iamVjdC5hc3NpZ24oeyBpZDogYHdlYnItMS1jb250ZW50c2AgfSwgYmxvY2suYXR0cik7XG4gIGNvbnN0IGVkaXRvciA9IG5ldyBXZWJSRXhlcmNpc2VFZGl0b3Iod2ViUk9qcy53ZWJSUHJvbWlzZSwgYmxvY2suY29kZSwgb3B0aW9ucyk7XG5cbiAgcmV0dXJuIGVkaXRvci5jb250YWluZXI7XG59XG5fd2Vicl92YWx1ZV8xID0gd2ViUk9qcy5wcm9jZXNzKF93ZWJyX2VkaXRvcl8xLCB7fSk7XG4ifSx7ImNlbGxOYW1lIjoid2Vici1wcmVsdWRlIiwiaW5saW5lIjpmYWxzZSwibWV0aG9kTmFtZSI6ImludGVycHJldFF1aWV0Iiwic291cmNlIjoid2ViUk9qcyA9IHtcbiAgY29uc3QgeyBXZWJSLCBDaGFubmVsVHlwZSB9ID0gd2luZG93Ll9leGVyY2lzZV9vanNfcnVudGltZS5XZWJSO1xuICBjb25zdCB7XG4gICAgV2ViUkV2YWx1YXRvcixcbiAgICBXZWJSRW52aXJvbm1lbnRNYW5hZ2VyLFxuICAgIHNldHVwUixcbiAgICBiNjREZWNvZGUsXG4gICAgY29sbGFwc2VQYXRoXG4gIH0gPSB3aW5kb3cuX2V4ZXJjaXNlX29qc19ydW50aW1lO1xuXG4gIGNvbnN0IHN0YXR1c0NvbnRhaW5lciA9IGRvY3VtZW50LmdldEVsZW1lbnRCeUlkKFwiZXhlcmNpc2UtbG9hZGluZy1zdGF0dXNcIik7XG4gIGNvbnN0IGluZGljYXRvckNvbnRhaW5lciA9IGRvY3VtZW50LmdldEVsZW1lbnRCeUlkKFwiZXhlcmNpc2UtbG9hZGluZy1pbmRpY2F0b3JcIik7XG4gIGluZGljYXRvckNvbnRhaW5lci5jbGFzc0xpc3QucmVtb3ZlKFwiZC1ub25lXCIpO1xuXG4gIGxldCBzdGF0dXNUZXh0ID0gZG9jdW1lbnQuY3JlYXRlRWxlbWVudChcImRpdlwiKVxuICBzdGF0dXNUZXh0LmNsYXNzTGlzdCA9IFwiZXhlcmNpc2UtbG9hZGluZy1kZXRhaWxzXCI7XG4gIHN0YXR1c1RleHQgPSBzdGF0dXNDb250YWluZXIuYXBwZW5kQ2hpbGQoc3RhdHVzVGV4dCk7XG4gIHN0YXR1c1RleHQudGV4dENvbnRlbnQgPSBgSW5pdGlhbGlzZWA7XG5cbiAgLy8gSG9pc3QgaW5kaWNhdG9yIG91dCBmcm9tIGZpbmFsIHNsaWRlIHdoZW4gcnVubmluZyB1bmRlciByZXZlYWxcbiAgY29uc3QgcmV2ZWFsU3RhdHVzID0gZG9jdW1lbnQucXVlcnlTZWxlY3RvcihcIi5yZXZlYWwgLmV4ZXJjaXNlLWxvYWRpbmctaW5kaWNhdG9yXCIpO1xuICBpZiAocmV2ZWFsU3RhdHVzKSB7XG4gICAgcmV2ZWFsU3RhdHVzLnJlbW92ZSgpO1xuICAgIGRvY3VtZW50LnF1ZXJ5U2VsZWN0b3IoXCIucmV2ZWFsID4gLnNsaWRlc1wiKS5hcHBlbmRDaGlsZChyZXZlYWxTdGF0dXMpO1xuICB9XG5cbiAgLy8gTWFrZSBhbnkgcmV2ZWFsIHNsaWRlcyB3aXRoIGxpdmUgY2VsbHMgc2Nyb2xsYWJsZVxuICBkb2N1bWVudC5xdWVyeVNlbGVjdG9yQWxsKFwiLnJldmVhbCAuZXhlcmNpc2UtY2VsbFwiKS5mb3JFYWNoKChlbCkgPT4ge1xuICAgIGVsLmNsb3Nlc3QoJ3NlY3Rpb24uc2xpZGUnKS5jbGFzc0xpc3QuYWRkKFwic2Nyb2xsYWJsZVwiKTtcbiAgfSlcblxuICAvLyB3ZWJSIHN1cHBsZW1lbnRhbCBkYXRhIGFuZCBvcHRpb25zXG4gIGNvbnN0IGRhdGFDb250ZW50ID0gZG9jdW1lbnQucXVlcnlTZWxlY3Rvcihgc2NyaXB0W3R5cGU9XFxcIndlYnItZGF0YVxcXCJdYCkudGV4dENvbnRlbnQ7XG4gIGNvbnN0IGRhdGEgPSBKU09OLnBhcnNlKGI2NERlY29kZShkYXRhQ29udGVudCkpO1xuXG4gIC8vIEdyYWIgbGlzdCBvZiByZXNvdXJjZXMgdG8gYmUgZG93bmxvYWRlZFxuICBjb25zdCBmaWxlc0NvbnRlbnQgPSBkb2N1bWVudC5xdWVyeVNlbGVjdG9yKGBzY3JpcHRbdHlwZT1cXFwidmZzLWZpbGVcXFwiXWApLnRleHRDb250ZW50O1xuICBjb25zdCBmaWxlcyA9IEpTT04ucGFyc2UoYjY0RGVjb2RlKGZpbGVzQ29udGVudCkpO1xuXG4gIC8vIEluaXRpYWxpc2Ugd2ViUiBhbmQgc2V0dXAgZm9yIFIgY29kZSBldmFsdWF0aW9uXG4gIGxldCB3ZWJSUHJvbWlzZSA9IChhc3luYyAod2ViUikgPT4ge1xuICAgIHN0YXR1c1RleHQudGV4dENvbnRlbnQgPSBgRG93bmxvYWRpbmcgd2ViUmA7XG4gICAgYXdhaXQgd2ViUi5pbml0KCk7XG5cbiAgICAvLyBJbnN0YWxsIHByb3ZpZGVkIGxpc3Qgb2YgcGFja2FnZXNcbiAgICAvLyBFbnN1cmUgd2ViUiBkZWZhdWx0IHJlcG8gaXMgaW5jbHVkZWRcbiAgICBkYXRhLnBhY2thZ2VzLnJlcG9zLnB1c2goXCJodHRwczovL3JlcG8uci13YXNtLm9yZ1wiKVxuICAgIGF3YWl0IGRhdGEucGFja2FnZXMucGtncy5tYXAoKHBrZykgPT4gKCkgPT4ge1xuICAgICAgc3RhdHVzVGV4dC50ZXh0Q29udGVudCA9IGBEb3dubG9hZGluZyBwYWNrYWdlOiAke3BrZ31gO1xuICAgICAgcmV0dXJuIHdlYlIuZXZhbFJWb2lkKGBcbiAgICAgICAgd2Vicjo6aW5zdGFsbChwa2csIHJlcG9zID0gcmVwb3MpXG4gICAgICAgIGxpYnJhcnkocGtnLCBjaGFyYWN0ZXIub25seSA9IFRSVUUpXG4gICAgICBgLCB7IGVudjoge1xuICAgICAgICBwa2c6IHBrZyxcbiAgICAgICAgcmVwb3M6IGRhdGEucGFja2FnZXMucmVwb3MsXG4gICAgICB9fSk7XG4gICAgfSkucmVkdWNlKChjdXIsIG5leHQpID0+IGN1ci50aGVuKG5leHQpLCBQcm9taXNlLnJlc29sdmUoKSk7XG5cbiAgICAvLyBEb3dubG9hZCBhbmQgaW5zdGFsbCByZXNvdXJjZXNcbiAgICBhd2FpdCBmaWxlcy5tYXAoKGZpbGUpID0+IGFzeW5jICgpID0+IHtcbiAgICAgIGNvbnN0IG5hbWUgPSBmaWxlLnN1YnN0cmluZyhmaWxlLmxhc3RJbmRleE9mKCcvJykgKyAxKTtcbiAgICAgIHN0YXR1c1RleHQudGV4dENvbnRlbnQgPSBgRG93bmxvYWRpbmcgcmVzb3VyY2U6ICR7bmFtZX1gO1xuICAgICAgY29uc3QgcmVzcG9uc2UgPSBhd2FpdCBmZXRjaChmaWxlKTtcbiAgICAgIGlmICghcmVzcG9uc2Uub2spIHtcbiAgICAgICAgdGhyb3cgbmV3IEVycm9yKGBDYW4ndCBkb3dubG9hZCBcXGAke2ZpbGV9XFxgLiBFcnJvciAke3Jlc3BvbnNlLnN0YXR1c306IFwiJHtyZXNwb25zZS5zdGF0dXNUZXh0fVwiLmApO1xuICAgICAgfVxuICAgICAgY29uc3QgZGF0YSA9IGF3YWl0IHJlc3BvbnNlLmFycmF5QnVmZmVyKCk7XG5cbiAgICAgIC8vIFN0b3JlIFVSTHMgaW4gdGhlIGN3ZCB3aXRob3V0IGFueSBzdWJkaXJlY3Rvcnkgc3RydWN0dXJlXG4gICAgICBpZiAoZmlsZS5pbmNsdWRlcyhcIjovL1wiKSkge1xuICAgICAgICBmaWxlID0gbmFtZTtcbiAgICAgIH1cblxuICAgICAgLy8gQ29sbGFwc2UgaGlnaGVyIGRpcmVjdG9yeSBzdHJ1Y3R1cmVcbiAgICAgIGZpbGUgPSBjb2xsYXBzZVBhdGgoZmlsZSk7XG5cbiAgICAgIC8vIENyZWF0ZSBkaXJlY3RvcnkgdHJlZSwgaWdub3JpbmcgXCJkaXJlY3RvcnkgZXhpc3RzXCIgVkZTIGVycm9yc1xuICAgICAgY29uc3QgcGFydHMgPSBmaWxlLnNwbGl0KCcvJykuc2xpY2UoMCwgLTEpO1xuICAgICAgbGV0IHBhdGggPSAnJztcbiAgICAgIHdoaWxlIChwYXJ0cy5sZW5ndGggPiAwKSB7XG4gICAgICAgIHBhdGggKz0gcGFydHMuc2hpZnQoKSArICcvJztcbiAgICAgICAgY29uc3QgYW5hbHlzaXMgPSBhd2FpdCB3ZWJSLkZTLmFuYWx5emVQYXRoKHBhdGgpO1xuICAgICAgICBpZiAoIWFuYWx5c2lzLmV4aXN0cykge1xuICAgICAgICAgIHRyeSB7XG4gICAgICAgICAgICBhd2FpdCB3ZWJSLkZTLm1rZGlyKHBhdGgpO1xuICAgICAgICAgIH0gY2F0Y2ggKGUpIHtcbiAgICAgICAgICAgIHRocm93IG5ldyBFcnJvcihgRmlsZXN5c3RlbSBFcnJvcjogXCIke2UubWVzc2FnZX1cIi5gKTtcbiAgICAgICAgICB9XG4gICAgICAgIH1cbiAgICAgIH1cblxuICAgICAgLy8gV3JpdGUgdGhpcyBmaWxlIHRvIHRoZSBWRlNcbiAgICAgIHJldHVybiBhd2FpdCB3ZWJSLkZTLndyaXRlRmlsZShmaWxlLCBuZXcgVWludDhBcnJheShkYXRhKSk7XG4gICAgfSkucmVkdWNlKChjdXIsIG5leHQpID0+IGN1ci50aGVuKG5leHQpLCBQcm9taXNlLnJlc29sdmUoKSk7XG5cbiAgICBzdGF0dXNUZXh0LnRleHRDb250ZW50ID0gYEluc3RhbGxpbmcgd2ViUiBzaGltc2A7XG4gICAgYXdhaXQgd2ViUi5ldmFsUlZvaWQoYHdlYnI6OnNoaW1faW5zdGFsbCgpYCk7XG5cbiAgICBzdGF0dXNUZXh0LnRleHRDb250ZW50ID0gYFdlYlIgZW52aXJvbm1lbnQgc2V0dXBgO1xuICAgIGF3YWl0IHNldHVwUih3ZWJSLCBkYXRhKTtcblxuICAgIHN0YXR1c1RleHQucmVtb3ZlKCk7XG4gICAgaWYgKHN0YXR1c0NvbnRhaW5lci5jaGlsZHJlbi5sZW5ndGggPT0gMCkge1xuICAgICAgc3RhdHVzQ29udGFpbmVyLnBhcmVudE5vZGUucmVtb3ZlKCk7XG4gICAgfVxuICAgIHJldHVybiB3ZWJSO1xuICB9KShuZXcgV2ViUihkYXRhLm9wdGlvbnMpKTtcblxuICAvLyBLZWVwIHRyYWNrIG9mIGluaXRpYWwgT0pTIGJsb2NrIHJlbmRlclxuICBjb25zdCByZW5kZXJlZE9qcyA9IHt9O1xuXG4gIGNvbnN0IHByb2Nlc3MgPSBhc3luYyAoY29udGV4dCwgaW5wdXRzKSA9PiB7XG4gICAgY29uc3Qgd2ViUiA9IGF3YWl0IHdlYlJQcm9taXNlO1xuICAgIGNvbnN0IGV2YWx1YXRvciA9IG5ldyBXZWJSRXZhbHVhdG9yKHdlYlIsIGNvbnRleHQpXG4gICAgYXdhaXQgZXZhbHVhdG9yLnByb2Nlc3MoaW5wdXRzKTtcbiAgICByZXR1cm4gZXZhbHVhdG9yLmNvbnRhaW5lcjtcbiAgfVxuXG4gIHJldHVybiB7XG4gICAgcHJvY2VzcyxcbiAgICB3ZWJSUHJvbWlzZSxcbiAgICByZW5kZXJlZE9qcyxcbiAgfTtcbn1cbiJ9XX0=
</script>
<div id="exercise-loading-indicator" class="exercise-loading-indicator d-none d-flex align-items-center gap-2">
<div id="exercise-loading-status" class="d-flex gap-2">

</div>
<div class="spinner-grow spinner-grow-sm">

</div>
</div>
<script type="vfs-file">
W10=
</script>
</section>
</section>

 ]]></description>
  <category>R &amp; Statistics</category>
  <guid>https://philippmasur.de/blog/2024-05-28-statistical-power/</guid>
  <pubDate>Mon, 27 May 2024 22:00:00 GMT</pubDate>
  <media:content url="https://philippmasur.de/blog/2024-05-28-statistical-power/featured.png" medium="image" type="image/png" height="114" width="144"/>
</item>
<item>
  <title>Launch of the Digital Media and Behavior Lab</title>
  <link>https://philippmasur.de/blog/2024-02-14-dmb-lab-launch/</link>
  <description><![CDATA[ 

<!-- Icon sprite: 24×24 line icons, 1.6 stroke. Every shape has pathLength="1"
     so CSS can animate stroke-dashoffset 1 → 0 ("draw" the icon).
     Use: <svg class="pm-icon"><use href="#i-mail"/></svg> -->





<p>I am excited to announce that I am the new managing director of the <a href="https://dmb-lab.nl/">Digital Media and Behavior Lab</a>, which was officially launched this Monday, the 12th February 2024! Embedded in the faculty of social sciences of the Vrije Universiteit Amsterdam, the DM&amp;B lab serves as an incubator for collaborative research aimed at shaping a positive future for our increasingly digital society. Shifting from an information-driven society to a digital one, it becomes evident that digital technology has become an integral part of nearly every facet of our lives. Together with partners from within and outside academia, our aim is to chart a path towards a sustainable, prosocial, and resilient society.</p>
<p><a href="img/DMB_logo_white.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://philippmasur.de/blog/2024-02-14-dmb-lab-launch/img/DMB_logo_white.png" class="img-fluid"></a></p>
<p>The central mission of the Digital Media and Behaviour Lab is to address the challenges and opportunities stemming from the ubiquitous use of digital communication technologies across private, professional, and public domains. The goal is to discern multifaceted responses and recommendations that encompass the development of essential skills and literacies and propose guiding principles and design guidelines for digital platforms, journalistic practices, technology governance, and science communication. Ultimately, our aim is to chart a path towards a sustainable, prosocial, and resilient society. Among other things, we engage in the following:</p>
<ol type="1">
<li>We offer yearly seed funding for large international grant proposals</li>
<li>We kickstart and consolidate transdisciplinary networks of scientific and societal stakeholders and increase our societal outreach and impact via a research champion programme</li>
<li>We host and organize workshops and events within the scope of the lab</li>
<li>We build partnerships with media and journalists to engage in effective science communication.</li>
</ol>
<p>For more information, please reach out to us via <script type="text/javascript">
<!--
h='&#118;&#x75;&#46;&#110;&#108;';a='&#64;';n='&#100;&#x6d;&#98;&#x2d;&#108;&#x61;&#98;';e=n+a+h;
document.write('<a h'+'ref'+'="ma'+'ilto'+':'+e+'" clas'+'s="em' + 'ail email">'+e+'<\/'+'a'+'>');
// -->
</script><noscript>dmb-lab at vu dot nl</noscript> or visit our website at <a href="https://dmb-lab.nl/">www.dmb-lab.nl</a>.</p>


<template id="pm-footer-icons"><a href="#" data-pm-mail="p.k.masur|vu.nl" aria-label="Email" title="Email"><svg class="pm-icon" aria-hidden="true"><use href="#i-mail"></use></svg></a><a href="https://scholar.google.com/citations?user=XA_WthYAAAAJ" target="_blank" rel="noopener" aria-label="Google Scholar" title="Google Scholar"><svg class="pm-icon" aria-hidden="true"><use href="#i-scholar"></use></svg></a><a href="https://orcid.org/0000-0003-3065-7305" target="_blank" rel="noopener" aria-label="ORCID" title="ORCID"><svg class="pm-icon" aria-hidden="true"><use href="#i-orcid"></use></svg></a><a href="https://osf.io/yuqv5/" target="_blank" rel="noopener" aria-label="Open Science Framework" title="Open Science Framework"><svg class="pm-icon" aria-hidden="true"><use href="#i-osf"></use></svg></a><a href="https://github.com/masurp" target="_blank" rel="noopener" aria-label="GitHub" title="GitHub"><svg class="pm-icon" aria-hidden="true"><use href="#i-github"></use></svg></a><a href="https://bsky.app/profile/masurphil.bsky.social" target="_blank" rel="noopener" aria-label="Bluesky" title="Bluesky"><svg class="pm-icon" aria-hidden="true"><use href="#i-bluesky"></use></svg></a><a href="https://research.vu.nl/en/persons/philipp-k-masur" target="_blank" rel="noopener" aria-label="VU Amsterdam" title="VU Amsterdam"><svg class="pm-icon" aria-hidden="true"><use href="#i-landmark"></use></svg></a></template>

 ]]></description>
  <category>News</category>
  <guid>https://philippmasur.de/blog/2024-02-14-dmb-lab-launch/</guid>
  <pubDate>Tue, 13 Feb 2024 23:00:00 GMT</pubDate>
  <media:content url="https://philippmasur.de/blog/2024-02-14-dmb-lab-launch/featured.png" medium="image" type="image/png" height="102" width="144"/>
</item>
<item>
  <title>Book published: The Routledge Handbook of Privacy and Social Media</title>
  <link>https://philippmasur.de/blog/2023-06-27-routledge-handbook/</link>
  <description><![CDATA[ 

<!-- Icon sprite: 24×24 line icons, 1.6 stroke. Every shape has pathLength="1"
     so CSS can animate stroke-dashoffset 1 → 0 ("draw" the icon).
     Use: <svg class="pm-icon"><use href="#i-mail"/></svg> -->





<p><a href="https://www.routledge.com/The-Routledge-Handbook-of-Privacy-and-Social-Media/Trepte-Masur/p/book/9781032111612"><img src="https://philippmasur.de/blog/2023-06-27-routledge-handbook/img/Screenshot-2023-06-22-at-15.50.15-1-736x1024.png" class="img-fluid"></a></p>
<p>Over the last two decades, privacy has been a staple topic in communication science and other disciplines. Particularly since the advent of social media, privacy and self-disclosure processes have been central to many empirical investigations. By now, thousands of studies have been published, diverse theoretical accounts emerged, and heterogeneous – at times contradictory findings – paint a granular, but complex picture of the privacy research landscape.<br>
<br>
At this point in time, Sabine Trepte and I believed that it was time to pause and tack stock. In order to substantiate existing privacy research and outline fruitful avenues for future research, we gathered the wisdom and expertise of many great privacy scholars and edited the “<a href="https://www.routledge.com/The-Routledge-Handbook-of-Privacy-and-Social-Media/Trepte-Masur/p/book/9781032111612">Routledge Handbook of Privacy and Social Media</a>“, a compendium of 30 chapters by 60 international authors, which was published today</p>
<p>Renowned scholars in the fields of communication, psychology, philosophy, informatics, and law look back on the last two decades of privacy research and project how the topic will develop in the next decade. The first begins with an overview of key scholarship in online privacy and discusses the most prominent theoretical perspectives. The second part expands to focus on influential factors shaping privacy perceptions and behaviors, such as culture, literacy, gender, and trust. Part three continues with specific examinations of concerns around vulnerable populations such as children, adolescents, older adults, and minorities. It then looks at how privacy is managed and the implications of interacting with artificial intelligence. Finally, it concludes by discussing feasible solutions to some of the more pressing questions surrounding online privacy.</p>
<p><strong>Title:</strong> The Routledge Handbook of Privacy and Social Media<br>
<strong>Editors:</strong> Sabine Trepte &amp; Philipp K. Masur<br>
<strong>Copyright</strong>: 2023<br>
<strong>Publisher:</strong> Routledge, part of the Taylor &amp; Francis Group<br>
<strong>eBook ISBN:</strong> 978-1-003-24467-7<br>
<strong>Hardcover ISBN:</strong> 978-1-032-11161-2<br>
<strong>DOI:</strong> 10.4324/9781003244677<br>
</p>
<p>You can buy the hardcover book or the ebook <a href="https://www.routledge.com/The-Routledge-Handbook-of-Privacy-and-Social-Media/Trepte-Masur/p/book/9781032111612">here</a>.</p>


<template id="pm-footer-icons"><a href="#" data-pm-mail="p.k.masur|vu.nl" aria-label="Email" title="Email"><svg class="pm-icon" aria-hidden="true"><use href="#i-mail"></use></svg></a><a href="https://scholar.google.com/citations?user=XA_WthYAAAAJ" target="_blank" rel="noopener" aria-label="Google Scholar" title="Google Scholar"><svg class="pm-icon" aria-hidden="true"><use href="#i-scholar"></use></svg></a><a href="https://orcid.org/0000-0003-3065-7305" target="_blank" rel="noopener" aria-label="ORCID" title="ORCID"><svg class="pm-icon" aria-hidden="true"><use href="#i-orcid"></use></svg></a><a href="https://osf.io/yuqv5/" target="_blank" rel="noopener" aria-label="Open Science Framework" title="Open Science Framework"><svg class="pm-icon" aria-hidden="true"><use href="#i-osf"></use></svg></a><a href="https://github.com/masurp" target="_blank" rel="noopener" aria-label="GitHub" title="GitHub"><svg class="pm-icon" aria-hidden="true"><use href="#i-github"></use></svg></a><a href="https://bsky.app/profile/masurphil.bsky.social" target="_blank" rel="noopener" aria-label="Bluesky" title="Bluesky"><svg class="pm-icon" aria-hidden="true"><use href="#i-bluesky"></use></svg></a><a href="https://research.vu.nl/en/persons/philipp-k-masur" target="_blank" rel="noopener" aria-label="VU Amsterdam" title="VU Amsterdam"><svg class="pm-icon" aria-hidden="true"><use href="#i-landmark"></use></svg></a></template>

 ]]></description>
  <category>News</category>
  <category>Privacy</category>
  <guid>https://philippmasur.de/blog/2023-06-27-routledge-handbook/</guid>
  <pubDate>Mon, 26 Jun 2023 22:00:00 GMT</pubDate>
  <media:content url="https://philippmasur.de/blog/2023-06-27-routledge-handbook/featured.png" medium="image" type="image/png" height="119" width="144"/>
</item>
<item>
  <title>VENI Grant to Study Behavioral Contagion on Social Media</title>
  <link>https://philippmasur.de/blog/2022-06-15-veni-grant/</link>
  <description><![CDATA[ 

<!-- Icon sprite: 24×24 line icons, 1.6 stroke. Every shape has pathLength="1"
     so CSS can animate stroke-dashoffset 1 → 0 ("draw" the icon).
     Use: <svg class="pm-icon"><use href="#i-mail"/></svg> -->





<p>I am excited to share that I was recently awarded a VENI innovation grant by the Dutch Research Council (Nederlandse Organisatie voor Wetenschappelijk Onderzoek; NWO). Building on my previous work on social contagion and social influence on social media (see e.g., <a href="https://doi.org/10.1371/journal.pone.0254670">this study</a>), I will continue to study how behaviors spread across social network sites. More specifically, I will investigate how norms develop on social media, how they are perceived, and how such perceptions drive our own behavior, for good or bad. The project will run for 4 years from fall 2022 to fall 2026. A short article about its scope and aims can be found in <a href="https://vu.nl/en/news/2022/veni-grant-for-communication-scientist-philipp-masur">this article</a> published by the Vrije Universiteit Amsterdam.</p>
<p><strong>But what exactly is this project about?</strong></p>
<p>Social media can have a bad influence on us. On platforms such as Facebook, Instagram, or TikTok, users are constantly exposed to other people’s behaviors. By scrolling through news feeds, we are constantly confronted with other people’s posts who may be indicative of certain norms, behavioral rules, and what other’s value or approve of. In fact, algorithmic curation may make certain types of behaviors more prevalent compared to others. Oftentimes, particularly risky practices such as the sharing of intimate insights into one’s life or sexting as well as socially undesirable behaviors such as hate speech, incivility, or cyberbullying thereby seem to propagate through social networks on these platforms. The spreading of such problematic behaviors can have severe implications for individuals (privacy risks, misuse, identify theft, and consequences for well-being) and the society at large (incivility, violence, polarization, erosion of privacy, and commodification of information).</p>
<p>Existing research suggests that mere exposure to other people on social media can lead to the adoption of their behaviors. However, the exact psychological mechanisms that underlie such online <em>behavioral contagion effects</em> are still unknown. To what extent are they tied to complex cognitive processes that involve social norms, trust, and intentionality? And how do social media affordances facilitate and reinforce behavioral contagion? For example, the persistence of social media content, visible user interactions, as well as algorithmic curation can contribute to behavioral adoption rapidly spreading across social networks. This lack of knowledge is deeply problematic as it prevents us from identifying and developing effective countermeasures that protect against irrational adoption of problematic behaviors.</p>
<p>This project leverages a novel methodological framework that combines tracking, simulation, and experimental methods to study online behavioral contagion and its boundary conditions in unprecedented detail. By observing people’s behavioral adaptations in response to naturalistic, yet simulated and therefore controlled social media environments, the project allows drawing causal inferences between changes in prevalent behaviors, norm perceptions, and subsequent behavioral responses. Taking the role of social media affordances into account, it further allows testing specifically adapted media literacy and design interventions’ potential to buffer against negative behavioral contagion effects.</p>
<p>More information about the project will follow soon.</p>


<template id="pm-footer-icons"><a href="#" data-pm-mail="p.k.masur|vu.nl" aria-label="Email" title="Email"><svg class="pm-icon" aria-hidden="true"><use href="#i-mail"></use></svg></a><a href="https://scholar.google.com/citations?user=XA_WthYAAAAJ" target="_blank" rel="noopener" aria-label="Google Scholar" title="Google Scholar"><svg class="pm-icon" aria-hidden="true"><use href="#i-scholar"></use></svg></a><a href="https://orcid.org/0000-0003-3065-7305" target="_blank" rel="noopener" aria-label="ORCID" title="ORCID"><svg class="pm-icon" aria-hidden="true"><use href="#i-orcid"></use></svg></a><a href="https://osf.io/yuqv5/" target="_blank" rel="noopener" aria-label="Open Science Framework" title="Open Science Framework"><svg class="pm-icon" aria-hidden="true"><use href="#i-osf"></use></svg></a><a href="https://github.com/masurp" target="_blank" rel="noopener" aria-label="GitHub" title="GitHub"><svg class="pm-icon" aria-hidden="true"><use href="#i-github"></use></svg></a><a href="https://bsky.app/profile/masurphil.bsky.social" target="_blank" rel="noopener" aria-label="Bluesky" title="Bluesky"><svg class="pm-icon" aria-hidden="true"><use href="#i-bluesky"></use></svg></a><a href="https://research.vu.nl/en/persons/philipp-k-masur" target="_blank" rel="noopener" aria-label="VU Amsterdam" title="VU Amsterdam"><svg class="pm-icon" aria-hidden="true"><use href="#i-landmark"></use></svg></a></template>

 ]]></description>
  <category>News</category>
  <category>Communication</category>
  <guid>https://philippmasur.de/blog/2022-06-15-veni-grant/</guid>
  <pubDate>Tue, 14 Jun 2022 22:00:00 GMT</pubDate>
  <media:content url="https://philippmasur.de/blog/2022-06-15-veni-grant/featured.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>How to run IRT analyses in R</title>
  <link>https://philippmasur.de/blog/2022-05-13-irt-analyses-in-r/</link>
  <description><![CDATA[ 

<!-- Icon sprite: 24×24 line icons, 1.6 stroke. Every shape has pathLength="1"
     so CSS can animate stroke-dashoffset 1 → 0 ("draw" the icon).
     Use: <svg class="pm-icon"><use href="#i-mail"/></svg> -->





<p>This post provides an introduction to fitting item response theory (IRT) models in R. From my experience, most scholars in the social sciences have heard about IRT as an alternative to classical test theory (and its methods such as EFA or CFA), but have never really worked with it. I believe that this is unfortunate as it offers a lot of advantages and insights into the validity and reliability of tests and items. In this tutorial, I will use the R packages <code>mirt</code> (multidimensional item response theory, version 1.36.1; Chalmers, 2022, <a href="https://cran.r-project.org/web/packages/mirt/index.html">CRAN page</a>) and <code>ggmirt</code> (an extension I wrote to provide publication-ready plotting functions: <a href="https://github.com/masurp/ggmirt" class="uri">https://github.com/masurp/ggmirt</a>).</p>
<section id="introduction" class="level1">
<h1>Introduction</h1>
<p>IRT refers to a set of mathematical models which aim to explain the relationship between a latent ability, trait, or proficiency (denoted <img src="https://latex.codecogs.com/png.latex?%5Ctheta">) and its observable manifestations (e.g., multiple-choice questions, true-false items, items…). In contrast to classical test theory (CTT), IRT focuses on the pattern of responses and considers responses in probabilistic terms, rather than focusing on composite variables and linear regression theory. IRT thereby accounts for…</p>
<ul>
<li><em>item discrimination</em>: ability of an item to differentiate between respondents with different levels of proficiency.</li>
<li><em>item difficulty</em>: the likelihood of a correct response, expressed as the proficiency level at which 50% of the participant sample is estimated to answer an item correctly.</li>
<li>Depending on the model some other parameters such as e.g., <em>guessing probability</em>…</li>
</ul>
<p>Assessing item difficulties is useful in matching the test and trait levels of a target population and in ensuring that the entire range of the proficiency is covered. Therefore, there are several advantages to IRT models (compared to CTT models):</p>
<ol type="1">
<li><p>The stronger focus on item difficulty generally leads to scales that perform better at differentiating at the extremes of proficiency.</p></li>
<li><p>Item banks that are developed using IRT often provide a good basis for creating smaller scales or parallel tests that still exhibit a high validity and reliability.</p></li>
<li><p>It allows for computer adaptive testing (CAT). This approach can greatly approach the efficiency of testing. In CAT, the test items are selected to match each individual’s proficiency, so that the s/he will not be bored by easy items or frustrated by overly difficult items.</p></li>
<li><p>Specific IRT models (e.g., the Rasch model, see further below) has specific mathematical properties that are highly desirable, such as a) the number-correct score is a sufficient estimation of <img src="https://latex.codecogs.com/png.latex?%5Ctheta">, b) specific objectivity, which means that item and person parameters will be similar even if only a subset of the item pool is used or a different population is studied.</p></li>
</ol>
<p>The theoretical background of IRT models is to comprehensive to be covered here. Rather, this tutorial aims to introduce the main models and discuss their properties. For further study, we refer to the excellent book “Item Response Theory” by Christine DeMars. In this short introductory tutorial, we focus on three of the most used IRT models: the 3PL model, the 2PL model, and finally the 1PL or Rasch model. These models are named by the number of item parameter used in the function that models the relationship between <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> and the item response (0/1). Each model has unique properties but all of them are suited to estimate latent variables from binary items (e.g., knowledge tests), which we will deal with in this tutorial. There are also more complex IRT models, such as e.g., graded response models, which can be used for non-binary items (e.g., likert-type scales). Yet, these will be discussed in a more advanced tutorial.</p>
</section>
<section id="preparation-and-data" class="level1">
<h1>Preparation and Data</h1>
<p>Although we are going to focus on the the package <code>mirt</code> in this tutorial, there are actually several packages that can be used to estimated IRT models. The most common ones are <a href="https://cran.r-project.org/web/packages/ltm/index.html"><code>ltm</code></a>, <a href="https://cran.r-project.org/web/packages/eRm/index.html"><code>eRm</code></a>, or <a href="https://cran.r-project.org/web/packages/TAM/index.html"><code>TAM</code></a>. This article by Choi &amp; Asilkalkan (2019) also provides an overview of all available packages and their specific advantages: <a href="https://doi.org/10.1080/15366367.2019.1586404">https://doi-org.vu-nl.idm.oclc.org/10.1080/15366367.2019.1586404</a>. For this tutorial, we are going to load the <code>tidyverse</code> (for data wrangling and visualization) and <code>mirt</code> (for the main IRT analyses). And as noted before, we further are going to load my package <code>ggmirt</code>, which represents an extension to <code>mirt</code> and provides functions to plot more publication-ready figures and helps to assess item, person, and model fit.</p>
<div class="cell" data-layout-align="center">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Data wrangling</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Very comprehensive package for IRT analyses</span></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(mirt)</span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extension for 'mirt'</span></span>
<span id="cb1-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># devtools::install_github("masurp/ggmirt")</span></span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggmirt)</span></code></pre></div></div>
</div>
<p>For <code>ggmirt</code> package includes a convenient function to simulate data for IRT analyses. Let’s quickly create a data set with 500 observations and 10 items that should fit a 3PL, 2PL and perhaps even a 1PL model.</p>
<div class="cell" data-layout-align="center">
<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;">42</span>)</span>
<span id="cb2-2">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;">sim_irt</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">discrimination =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">seed =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(d)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="kable-table">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: right;">V1</th>
<th style="text-align: right;">V2</th>
<th style="text-align: right;">V3</th>
<th style="text-align: right;">V4</th>
<th style="text-align: right;">V5</th>
<th style="text-align: right;">V6</th>
<th style="text-align: right;">V7</th>
<th style="text-align: right;">V8</th>
<th style="text-align: right;">V9</th>
<th style="text-align: right;">V10</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
</tr>
<tr class="even">
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
</tr>
<tr class="odd">
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
</tr>
<tr class="even">
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
</tr>
<tr class="odd">
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">0</td>
</tr>
<tr class="even">
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">1</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>As you can see, each “participants” has answered 10 items that are binary. Imagine we would have administered a test (e.g., LSAT, PISA, knowledge test, exam,…) to 500 people. The score 1 means this person has answered a particular question/item <em>correctly</em>. The score 0 means, this person has answered this item falsely.</p>
</section>
<section id="pl-model" class="level1">
<h1>3PL model</h1>
<p>The 3PL model takes item discrimination (first parameter: <em>a</em>), item difficulty (second parameter: <em>b</em>), and guessing probability (third parameter: <em>c</em>) into account. As such, the 2PL and 1PL model (discussed below, are special cases, or constrained versions of the 3PL model). Take a look at Fig. 1 below. It shows a typical item characteristic curve (ICC, but not to be mistaken for the intra-class correlation). The x-axis shows the latent ability (<img src="https://latex.codecogs.com/png.latex?%5Ctheta">) ranging from -4 to 4, with 0 being the average ability in the studied population. The y-axis shows the probability of solving the item. The curve thus represents the probability of answering this item given a certain level on the latent ability.</p>
<p><a href="img/fig-01.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://philippmasur.de/blog/2022-05-13-irt-analyses-in-r/img/fig-01.png" class="img-fluid"></a></p>
<p>In this example, this 3PL model provides a smooth curve for this item because it is based on item discrimination (steepness of the slope), difficulty (point of inflexion at which the probability of answering the item correctly is 50%) and the guessing probability (slightly raised lower asymptote). The mathematical form of the 3PL model is:</p>
<p><img src="https://latex.codecogs.com/png.latex?P(%5Ctheta_j)%20=%20c_i%20+%20(1-c_i)%5Cfrac%7Be%5E%7B1.7a_i(%5Ctheta_j-b_i)%7D%7D%7B1+e%5E%7B1.7a_i(%5Ctheta_j-b_i)%7D%7D"></p>
<p>Here, the probability of a correct response <img src="https://latex.codecogs.com/png.latex?P(%5Ctheta)"> given a person’s ability <img src="https://latex.codecogs.com/png.latex?%5Ctheta"> is expressed as a function of all three item parameters and a person’s ability, where <img src="https://latex.codecogs.com/png.latex?i"> indicates the item and <img src="https://latex.codecogs.com/png.latex?j"> indicates an individual person. In the figure, the steeper the slope, the better the item is at differentiating people clearly in close proximity to its difficulty. The lower the asymptote, the lower the likelihood of selecting the right response by chance. As the item difficulty (point of inflexion is slightly above the average: 0.71), the item can be solved by a majority of potential participants.</p>
<p>So how do we fit such a model?</p>
<section id="fitting-the-model" class="level2">
<h2 class="anchored" data-anchor-id="fitting-the-model">Fitting the model</h2>
<p>To fit any IRT model, we simply use the function <code>mirt()</code>. In a first step, we specify the model itself. In this case, we simply want to estimate a uni-dimensional model, so we use the following string command <code>"Factor = item_1 - item_n"</code>. The syntax for specifying models is comparatively simple, but for more complex (e.g., multidimensional models), see the some examples using <code>?mirt</code>. It makes sense to save this string in a separate object.</p>
<p>Next, we provide the function itself with</p>
<ol type="a">
<li>the data set, which only contains the relevant variables (i.e., potential id variables have to be excluded)</li>
<li>the previously specifed model (string)</li>
<li>the type of model we want to estiamte (in this case “3PL”)</li>
</ol>
<p>I am adding <code>verbose = FALSE</code> to not print information about the iterations. But you can remove this as well.</p>
<div class="cell" data-layout-align="center">
<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">unimodel <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'F1 = 1-10'</span></span>
<span id="cb3-2"></span>
<span id="cb3-3">fit3PL <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;">mirt</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> d,</span>
<span id="cb3-4">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> unimodel,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># alternatively, we could also just specify model = 1 in this case</span></span>
<span id="cb3-5">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">itemtype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3PL"</span>,</span>
<span id="cb3-6">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb3-7">fit3PL</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; 
#&gt; Call:
#&gt; mirt(data = d, model = unimodel, itemtype = "3PL", verbose = FALSE)
#&gt; 
#&gt; Full-information item factor analysis with 1 factor(s).
#&gt; Converged within 1e-04 tolerance after 24 EM iterations.
#&gt; mirt version: 1.45.1 
#&gt; M-step optimizer: BFGS 
#&gt; EM acceleration: Ramsay 
#&gt; Number of rectangular quadrature: 61
#&gt; Latent density type: Gaussian 
#&gt; 
#&gt; Log-likelihood = -2738.689
#&gt; Estimated parameters: 30 
#&gt; AIC = 5537.377
#&gt; BIC = 5663.816; SABIC = 5568.594
#&gt; G2 (993) = 497.87, p = 1
#&gt; RMSEA = 0, CFI = NaN, TLI = NaN</code></pre>
</div>
</div>
<p>The created object is of class “SingleGroupClass” and contains all necessary information and data to assess the model. By running the object itself, we get some information about the type of estimation as well as some model fit indices (including AIC and BIC), which can be used to compare models with one another.</p>
</section>
<section id="understanding-irt-parameter" class="level2">
<h2 class="anchored" data-anchor-id="understanding-irt-parameter">Understanding IRT parameter</h2>
<p>To a certain degree, an IRT analysis is similar to a factor analysis in CTT. If we use the <code>summary()</code> function, we get the so-called factor solution including factor loadings (F1) and the communalities (h2), which are squared factor loadings and are interpreted as the variance accounted for in an item by the latent trait. Almost all of the items in this case have a substantive relationship (loadings &gt; .50) with the latent trait.</p>
<div class="cell" data-layout-align="center">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Factor solution</span></span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(fit3PL)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt;        F1    h2
#&gt; V1  0.707 0.500
#&gt; V2  0.513 0.264
#&gt; V3  0.535 0.286
#&gt; V4  0.727 0.529
#&gt; V5  0.574 0.329
#&gt; V6  0.566 0.320
#&gt; V7  0.603 0.364
#&gt; V8  0.443 0.196
#&gt; V9  0.718 0.516
#&gt; V10 0.480 0.230
#&gt; 
#&gt; SS loadings:  3.534 
#&gt; Proportion Var:  0.353 
#&gt; 
#&gt; Factor correlations: 
#&gt; 
#&gt;    F1
#&gt; F1  1</code></pre>
</div>
</div>
<p>In IRT, however, we are usually more interested in the actual IRT parameters as discussed above (discrimination, difficulty and guessing probability). They can be extracted as follows:</p>
<div class="cell" data-layout-align="center">
<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">params3PL <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;">coef</span>(fit3PL, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">IRTpars =</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;">simplify =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb7-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(params3PL<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>items, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># g = c = guessing parameter</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt;        a     b    g u
#&gt; V1  1.70  1.17 0.00 1
#&gt; V2  1.02 -0.58 0.00 1
#&gt; V3  1.08  0.35 0.00 1
#&gt; V4  1.80  0.81 0.09 1
#&gt; V5  1.19  0.52 0.00 1
#&gt; V6  1.17 -0.14 0.00 1
#&gt; V7  1.29  1.87 0.00 1
#&gt; V8  0.84  0.03 0.00 1
#&gt; V9  1.76  2.11 0.00 1
#&gt; V10 0.93 -0.05 0.01 1</code></pre>
</div>
</div>
<p>The values of the slope (a-parameters) parameters ranged from 0.84 to 1.80. This parameter is a measure of how well an item differentiates individuals with different theta levels. Larger values, or steeper slopes, are better at differentiating people. A slope also can be interpreted as an indicator of the strength of a relationship between and item and latent trait, with higher slope values corresponding to stronger relationships.</p>
<p>The location or difficulty parameters (b-parameter) is also listed for each item. Location parameters are interpreted as the value of theta that corresponds to a .50 probability of responding correctly at or above that location on an item. The location parameters show that the items cover a wide range of the latent trait.</p>
</section>
<section id="model-fit-person-fit-and-item-fit-evaluation" class="level2">
<h2 class="anchored" data-anchor-id="model-fit-person-fit-and-item-fit-evaluation">Model fit, person fit, and item fit evaluation</h2>
<p>Similar to factor analytical approaches, we can assess how well the model fits the data. Rather than using a a <img src="https://latex.codecogs.com/png.latex?%5Cchi%5E2"> statisic, we use a specific index, <em>M2</em>, which is specifically designed to assess the fit of item response models.</p>
<div class="cell" data-layout-align="center">
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">M2</span>(fit3PL)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="kable-table">
<table class="caption-top table table-sm table-striped small">
<colgroup>
<col style="width: 8%">
<col style="width: 12%">
<col style="width: 4%">
<col style="width: 13%">
<col style="width: 8%">
<col style="width: 10%">
<col style="width: 13%">
<col style="width: 12%">
<col style="width: 12%">
<col style="width: 5%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;"></th>
<th style="text-align: right;">M2</th>
<th style="text-align: right;">df</th>
<th style="text-align: right;">p</th>
<th style="text-align: right;">RMSEA</th>
<th style="text-align: right;">RMSEA_5</th>
<th style="text-align: right;">RMSEA_95</th>
<th style="text-align: right;">SRMSR</th>
<th style="text-align: right;">TLI</th>
<th style="text-align: right;">CFI</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">stats</td>
<td style="text-align: right;">17.77133</td>
<td style="text-align: right;">25</td>
<td style="text-align: right;">0.8519424</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0.0203603</td>
<td style="text-align: right;">0.033713</td>
<td style="text-align: right;">1.018438</td>
<td style="text-align: right;">1</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>As we can see, the M2 statistic is comparatively low and non-significant. So there are no concerning differences between the model and the data. This is further supported by a very low RMSEA and a CFA and TLI of 1.</p>
<p>In IRT, however, we are usually more interested in <em>item</em>– and <em>person</em>-fit indices. IRT allows us to assess how well each items fits the model and whether the indiviual response patterns align with the model.</p>
<p>Let us start with item fit: Similar to many other areas, different indices have been proposed to assess item fit. We can use the function <code>itemfit()</code> to get a variety of them. By default, we receive the <code>S_X2</code> by Orlando and Thissen (2000) and the corresponding dfs, RMSEA and p-values. This test should be non-significant to indicate good item fit. As we can see here, only item V9 shows a lower fit with the model. Proponents of the “Rasch Model” (see further below) often rather report infit and outfit statistics. We can get those by adding the argument <code>fit_stats = "infit"</code>). We get both mean-squared and standardized versions of these measures (Linacre provides some guidelines to interpret these: <a href="https://www.rasch.org/rmt/rmt162f.htm" class="uri">https://www.rasch.org/rmt/rmt162f.htm</a>). Roughly speaking the non-standardized values should be between .5 and 1.5 to not be degrading. In the package <code>ggmirt</code>, we can also use the function <code>itemfitPlot()</code> to inspect this visually.</p>
<div class="cell" data-layout-align="center">
<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;">itemfit</span>(fit3PL)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="kable-table">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">item</th>
<th style="text-align: right;">S_X2</th>
<th style="text-align: right;">df.S_X2</th>
<th style="text-align: right;">RMSEA.S_X2</th>
<th style="text-align: right;">p.S_X2</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">V1</td>
<td style="text-align: right;">4.708662</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">0.0188425</td>
<td style="text-align: right;">0.3185173</td>
</tr>
<tr class="even">
<td style="text-align: left;">V2</td>
<td style="text-align: right;">2.503355</td>
<td style="text-align: right;">5</td>
<td style="text-align: right;">0.0000000</td>
<td style="text-align: right;">0.7759897</td>
</tr>
<tr class="odd">
<td style="text-align: left;">V3</td>
<td style="text-align: right;">6.761645</td>
<td style="text-align: right;">5</td>
<td style="text-align: right;">0.0265720</td>
<td style="text-align: right;">0.2389791</td>
</tr>
<tr class="even">
<td style="text-align: left;">V4</td>
<td style="text-align: right;">3.700650</td>
<td style="text-align: right;">5</td>
<td style="text-align: right;">0.0000000</td>
<td style="text-align: right;">0.5932672</td>
</tr>
<tr class="odd">
<td style="text-align: left;">V5</td>
<td style="text-align: right;">4.093206</td>
<td style="text-align: right;">5</td>
<td style="text-align: right;">0.0000000</td>
<td style="text-align: right;">0.5360762</td>
</tr>
<tr class="even">
<td style="text-align: left;">V6</td>
<td style="text-align: right;">2.824089</td>
<td style="text-align: right;">5</td>
<td style="text-align: right;">0.0000000</td>
<td style="text-align: right;">0.7270838</td>
</tr>
<tr class="odd">
<td style="text-align: left;">V7</td>
<td style="text-align: right;">8.648072</td>
<td style="text-align: right;">5</td>
<td style="text-align: right;">0.0382381</td>
<td style="text-align: right;">0.1239519</td>
</tr>
<tr class="even">
<td style="text-align: left;">V8</td>
<td style="text-align: right;">2.901227</td>
<td style="text-align: right;">5</td>
<td style="text-align: right;">0.0000000</td>
<td style="text-align: right;">0.7152104</td>
</tr>
<tr class="odd">
<td style="text-align: left;">V9</td>
<td style="text-align: right;">11.610311</td>
<td style="text-align: right;">4</td>
<td style="text-align: right;">0.0617477</td>
<td style="text-align: right;">0.0204970</td>
</tr>
<tr class="even">
<td style="text-align: left;">V10</td>
<td style="text-align: right;">2.693987</td>
<td style="text-align: right;">5</td>
<td style="text-align: right;">0.0000000</td>
<td style="text-align: right;">0.7470379</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="cell" data-layout-align="center">
<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">itemfit</span>(fit3PL, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fit_stats =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"infit"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># typical for Rasch modeling</span></span></code></pre></div></div>
<div class="cell-output-display">
<div class="kable-table">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">item</th>
<th style="text-align: right;">outfit</th>
<th style="text-align: right;">z.outfit</th>
<th style="text-align: right;">infit</th>
<th style="text-align: right;">z.infit</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">V1</td>
<td style="text-align: right;">0.6386014</td>
<td style="text-align: right;">-2.365002</td>
<td style="text-align: right;">0.8743268</td>
<td style="text-align: right;">-1.7362580</td>
</tr>
<tr class="even">
<td style="text-align: left;">V2</td>
<td style="text-align: right;">0.8481622</td>
<td style="text-align: right;">-3.058789</td>
<td style="text-align: right;">0.8705611</td>
<td style="text-align: right;">-3.7125518</td>
</tr>
<tr class="odd">
<td style="text-align: left;">V3</td>
<td style="text-align: right;">0.8320215</td>
<td style="text-align: right;">-3.792355</td>
<td style="text-align: right;">0.8794736</td>
<td style="text-align: right;">-3.4444110</td>
</tr>
<tr class="even">
<td style="text-align: left;">V4</td>
<td style="text-align: right;">0.8362322</td>
<td style="text-align: right;">-2.532776</td>
<td style="text-align: right;">0.8729352</td>
<td style="text-align: right;">-2.5664209</td>
</tr>
<tr class="odd">
<td style="text-align: left;">V5</td>
<td style="text-align: right;">0.8035407</td>
<td style="text-align: right;">-3.634037</td>
<td style="text-align: right;">0.8727943</td>
<td style="text-align: right;">-3.1862993</td>
</tr>
<tr class="even">
<td style="text-align: left;">V6</td>
<td style="text-align: right;">0.8104688</td>
<td style="text-align: right;">-4.158982</td>
<td style="text-align: right;">0.8432826</td>
<td style="text-align: right;">-4.6042826</td>
</tr>
<tr class="odd">
<td style="text-align: left;">V7</td>
<td style="text-align: right;">0.7308953</td>
<td style="text-align: right;">-1.594022</td>
<td style="text-align: right;">0.9935245</td>
<td style="text-align: right;">-0.0331537</td>
</tr>
<tr class="even">
<td style="text-align: left;">V8</td>
<td style="text-align: right;">0.8929857</td>
<td style="text-align: right;">-3.419761</td>
<td style="text-align: right;">0.9110881</td>
<td style="text-align: right;">-3.2959229</td>
</tr>
<tr class="odd">
<td style="text-align: left;">V9</td>
<td style="text-align: right;">0.5264292</td>
<td style="text-align: right;">-1.359471</td>
<td style="text-align: right;">1.0527710</td>
<td style="text-align: right;">0.3834487</td>
</tr>
<tr class="even">
<td style="text-align: left;">V10</td>
<td style="text-align: right;">0.8713525</td>
<td style="text-align: right;">-3.689040</td>
<td style="text-align: right;">0.8945440</td>
<td style="text-align: right;">-3.6438640</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="cell" data-layout-align="center">
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">itemfitPlot</span>(fit3PL)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-9-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://philippmasur.de/blog/2022-05-13-irt-analyses-in-r/index_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Item fit plot for the 3PL model showing the outfit mean-square value of each of the ten items, with item V9 lying closest to the lower threshold of 0.5."></a></p>
</figure>
</div>
</div>
</div>
<p>We again see that item V9 has a lower fit (outfit value close to .5), but according to Linacre’s guidelines, this should not be concerning.</p>
</section>
<section id="assessing-person-fit" class="level2">
<h2 class="anchored" data-anchor-id="assessing-person-fit">Assessing person fit</h2>
<p>We can technically produce the exact same measures for each person to assess how well each person’s response patterns aligns with this model. Think about it this way: If a person with a high theta (that is high latent ability) answers does not answer an easy item correctly, this person does not fit the model. Conversely, if a person with a low ability answer a very difficult question correctly, it likewise doesn’t fit the model. In practice, there will most likely be a few people who do not fit the model well. But as long as the number of non-fitting respondents is low, we are good. We mostly look again at infit and outfit statistics. If less than 5% of the respondents have higher or lower infit and outfit values than 1.96 and -1.96, we are good.</p>
<div class="cell" data-layout-align="center">
<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">personfit</span>(fit3PL))</span></code></pre></div></div>
<div class="cell-output-display">
<div class="kable-table">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: right;">outfit</th>
<th style="text-align: right;">z.outfit</th>
<th style="text-align: right;">infit</th>
<th style="text-align: right;">z.infit</th>
<th style="text-align: right;">Zh</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">1.0161479</td>
<td style="text-align: right;">0.1749541</td>
<td style="text-align: right;">1.0183924</td>
<td style="text-align: right;">0.1584039</td>
<td style="text-align: right;">-0.0732308</td>
</tr>
<tr class="even">
<td style="text-align: right;">0.6331922</td>
<td style="text-align: right;">-0.1327510</td>
<td style="text-align: right;">0.8411215</td>
<td style="text-align: right;">-0.4976128</td>
<td style="text-align: right;">0.5615934</td>
</tr>
<tr class="odd">
<td style="text-align: right;">0.7456650</td>
<td style="text-align: right;">-0.1905736</td>
<td style="text-align: right;">0.8912849</td>
<td style="text-align: right;">-0.3611853</td>
<td style="text-align: right;">0.4390657</td>
</tr>
<tr class="even">
<td style="text-align: right;">0.7209540</td>
<td style="text-align: right;">-0.0392887</td>
<td style="text-align: right;">0.9571507</td>
<td style="text-align: right;">-0.0637470</td>
<td style="text-align: right;">0.2601261</td>
</tr>
<tr class="odd">
<td style="text-align: right;">2.1363674</td>
<td style="text-align: right;">2.3695393</td>
<td style="text-align: right;">1.7713727</td>
<td style="text-align: right;">2.1832656</td>
<td style="text-align: right;">-2.6948633</td>
</tr>
<tr class="even">
<td style="text-align: right;">0.7538809</td>
<td style="text-align: right;">0.0610415</td>
<td style="text-align: right;">1.0221023</td>
<td style="text-align: right;">0.1724740</td>
<td style="text-align: right;">0.0843561</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="cell" data-layout-align="center">
<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;">personfit</span>(fit3PL) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reframe</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">infit.outside =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">prop.table</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(z.infit <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.96</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> z.infit <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</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;">1.96</span>)),</span>
<span id="cb14-3">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outfit.outside =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">prop.table</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(z.outfit <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.96</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> z.outfit <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</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;">1.96</span>))) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># lower row = non-fitting people</span></span></code></pre></div></div>
<div class="cell-output-display">
<div class="kable-table">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: right;">infit.outside</th>
<th style="text-align: right;">outfit.outside</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">0.958</td>
<td style="text-align: right;">0.98</td>
</tr>
<tr class="even">
<td style="text-align: right;">0.042</td>
<td style="text-align: right;">0.02</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="cell" data-layout-align="center">
<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;">personfitPlot</span>(fit3PL)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-12-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-3"><img src="https://philippmasur.de/blog/2022-05-13-irt-analyses-in-r/index_files/figure-html/unnamed-chunk-12-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Person fit plot for the 3PL model showing the distributions of the standardized infit and outfit statistics of all 500 respondents, with most values falling between -1.96 and 1.96."></a></p>
</figure>
</div>
</div>
</div>
</section>
<section id="typical-irt-plots" class="level2">
<h2 class="anchored" data-anchor-id="typical-irt-plots">Typical IRT plots</h2>
<p>Next to overall model fit, item and person fit, we can evaluate many more things. Some typical questions include:</p>
<ol type="1">
<li>How well do the items cover the range of the latent ability?</li>
<li>Is there any redundancy in the items?</li>
<li>At what theta levels does the scale perform best?</li>
</ol>
<p>A lot of these questions can be answered by visualizing different aspects of the IRT models.</p>
</section>
<section id="item-person-map-wright-map" class="level2">
<h2 class="anchored" data-anchor-id="item-person-map-wright-map">Item Person Map (Wright Map)</h2>
<p>The first question can be assessed using a so-called “Item Person Map” (also known as Kernel Density Plots or Wright Maps). This visualization first plots the distribution of the latent ability in the studied sample. Next, we also plot each item’s difficulty on the same theta scale. Aligning both plots shows us how well the items cover the latent ability.</p>
<div class="cell" data-layout-align="center">
<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;">itempersonMap</span>(fit3PL)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-13-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-4"><img src="https://philippmasur.de/blog/2022-05-13-irt-analyses-in-r/index_files/figure-html/unnamed-chunk-13-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:70.0%" alt="Item person map for the 3PL model: the upper panel shows the distribution of the estimated latent ability, the lower panel shows the difficulty of each item on the same theta scale."></a></p>
</figure>
</div>
</div>
</div>
<section id="item-characteristics-curves-trace-plots" class="level3">
<h3 class="anchored" data-anchor-id="item-characteristics-curves-trace-plots">Item Characteristics Curves (Trace Plots)</h3>
<p>Item characteristic curves or trace plots (we already saw one earlier) visualize all three IRT parameters for all the items. This visualization is helpful in better understanding the unique properties of each item. It can also be helpful to identify gaps in assessment as well as differences in slope</p>
<div class="cell" data-layout-align="center">
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tracePlot</span>(fit3PL)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-14-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-5"><img src="https://philippmasur.de/blog/2022-05-13-irt-analyses-in-r/index_files/figure-html/unnamed-chunk-14-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Item characteristic curves of the 3PL model, one facet per item, showing the probability of a correct response across theta."></a></p>
</figure>
</div>
</div>
</div>
<div class="cell" data-layout-align="center">
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tracePlot</span>(fit3PL, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">facet =</span> F, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend =</span> T) <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_color_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set3"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-15-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-6"><img src="https://philippmasur.de/blog/2022-05-13-irt-analyses-in-r/index_files/figure-html/unnamed-chunk-15-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Item characteristic curves of all ten items of the 3PL model drawn on top of each other in different colours."></a></p>
</figure>
</div>
</div>
</div>
<div class="cell" data-layout-align="center">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plotting only individual items</span></span>
<span id="cb19-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tracePlot</span>(fit3PL,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">items =</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="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="at" style="color: #657422;
background-color: null;
font-style: inherit;">facet =</span> F, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend =</span> T) <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_color_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set2"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-16-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-7"><img src="https://philippmasur.de/blog/2022-05-13-irt-analyses-in-r/index_files/figure-html/unnamed-chunk-16-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Item characteristic curves of the first three items of the 3PL model drawn on top of each other."></a></p>
</figure>
</div>
</div>
</div>
<p>Particularly when plotted on top of each other, we can clearly see the differences in slope, difficulty, and guessing.</p>
</section>
<section id="item-information-curves" class="level3">
<h3 class="anchored" data-anchor-id="item-information-curves">Item Information Curves</h3>
<p>Another way of looking at the quality of each item is by plotting so call item information curves. Information is a statistical concept that refers to the ability of an item to accurately estimate scores on theta. Item level information clarifies how well each item contributes to score estimation precision with higher levels of information leading to more accurate score estimates.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">itemInfoPlot</span>(fit3PL) <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_color_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set3"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-17-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-8"><img src="https://philippmasur.de/blog/2022-05-13-irt-analyses-in-r/index_files/figure-html/unnamed-chunk-17-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Item information curves of all ten items of the 3PL model drawn on top of each other in different colours."></a></p>
</figure>
</div>
</div>
</div>
<div class="cell" data-layout-align="center">
<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;">itemInfoPlot</span>(fit3PL, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">facet =</span> T)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-18-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-9"><img src="https://philippmasur.de/blog/2022-05-13-irt-analyses-in-r/index_files/figure-html/unnamed-chunk-18-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Item information curves of the 3PL model, one facet per item, showing at which theta levels each item provides the most information."></a></p>
</figure>
</div>
</div>
</div>
<p>Here we, see clearly that some items offer most information on higher theta levels whereas others cover the entire range of theta.</p>
</section>
<section id="test-information-curves" class="level3">
<h3 class="anchored" data-anchor-id="test-information-curves">Test Information Curves</h3>
<p>The concept of “information” can also be applied to the entire scale. Here, we see that the scale is very good at estimating theta scores between -2 and 3, but has less precision at estimate theta scores at the extremes.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">testInfoPlot</span>(fit3PL, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">adj_factor =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-19-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-10"><img src="https://philippmasur.de/blog/2022-05-13-irt-analyses-in-r/index_files/figure-html/unnamed-chunk-19-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Test information curve of the 3PL model together with the standard error of measurement across theta."></a></p>
</figure>
</div>
</div>
</div>
</section>
<section id="scale-characteristic-curves" class="level3">
<h3 class="anchored" data-anchor-id="scale-characteristic-curves">Scale Characteristic Curves</h3>
<p>A last quality of a IRT model can be that the bare number-correct-score (sum score of correct responses) is a sufficiently good estimation of the underlying trait. A plot of the so-called scale characteristic curve allows to assess this visually by plotting the relationship between theta and the number-correct-score.</p>
<div class="cell" data-layout-align="center">
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scaleCharPlot</span>(fit3PL)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-20-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-11"><img src="https://philippmasur.de/blog/2022-05-13-irt-analyses-in-r/index_files/figure-html/unnamed-chunk-20-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="S-shaped scale characteristic curve of the 3PL model relating theta to the expected number-correct score."></a></p>
</figure>
</div>
</div>
</div>
<p>This curve usually takes the form of a S-shape as the relationship is stronger in the middle range of theta and worse at the extremes (as already seen in the test information curve). We can of course also test this with a simple correlation. First, we extract the latent IRT score using the function <code>fscores()</code>. We then correlate it with the simple number-correct-score.</p>
<div class="cell" data-layout-align="center">
<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">score <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;">fscores</span>(fit3PL)</span>
<span id="cb24-2">sumscore <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;">rowSums</span>(d)</span>
<span id="cb24-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor.test</span>(score, sumscore)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; 
#&gt;  Pearson's product-moment correlation
#&gt; 
#&gt; data:  score and sumscore
#&gt; t = 181.54, df = 498, p-value &lt; 2.2e-16
#&gt; alternative hypothesis: true correlation is not equal to 0
#&gt; 95 percent confidence interval:
#&gt;  0.9910997 0.9937302
#&gt; sample estimates:
#&gt;       cor 
#&gt; 0.9925295</code></pre>
</div>
</div>
<p>As we can see, they correlate almost perfectly.</p>
</section>
</section>
</section>
<section id="pl-model-1" class="level1">
<h1>2PL model</h1>
<p>The 2PL model only differs from the 3PL model in one regard: All items are assumed to have no guessing probability. So the model only takes item discrimination (a) and item difficulty into account. The mathematical form is hence (we simply delete the part before the fraction):</p>
<p><img src="https://latex.codecogs.com/png.latex?P(%5Ctheta_j)%20=%20%5Cfrac%7Be%5E%7B1.7a_i(%5Ctheta_j-b_i)%7D%7D%7B1+e%5E%7B1.7a_i(%5Ctheta_j-b_i)%7D%7D"></p>
<p>The general procedure to estimate and assess the model remains the same. I hence only pinpoint to differences and do not repeat all steps outlined for the 3PL model.</p>
<section id="fitting-the-model-1" class="level2">
<h2 class="anchored" data-anchor-id="fitting-the-model-1">Fitting the model</h2>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1">fit2PL <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;">mirt</span>(d, <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;">itemtype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2PL"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> F)</span>
<span id="cb26-2">fit2PL</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; 
#&gt; Call:
#&gt; mirt(data = d, model = 1, itemtype = "2PL", verbose = F)
#&gt; 
#&gt; Full-information item factor analysis with 1 factor(s).
#&gt; Converged within 1e-04 tolerance after 15 EM iterations.
#&gt; mirt version: 1.45.1 
#&gt; M-step optimizer: BFGS 
#&gt; EM acceleration: Ramsay 
#&gt; Number of rectangular quadrature: 61
#&gt; Latent density type: Gaussian 
#&gt; 
#&gt; Log-likelihood = -2739.232
#&gt; Estimated parameters: 20 
#&gt; AIC = 5518.465
#&gt; BIC = 5602.757; SABIC = 5539.276
#&gt; G2 (1003) = 498.96, p = 1
#&gt; RMSEA = 0, CFI = NaN, TLI = NaN</code></pre>
</div>
</div>
</section>
<section id="compare-fit-with-3pl-model" class="level2">
<h2 class="anchored" data-anchor-id="compare-fit-with-3pl-model">Compare fit with 3PL model</h2>
<p>We can always compare different models that are based on the same data. Using the function <code>anova()</code>, we can check whether the models differ based on various fit indices and a <img src="https://latex.codecogs.com/png.latex?%5Cchi%5E2"> test. In this case, the 2PL actually fits the data sightly better, but the difference is not significant.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(fit2PL, fit3PL)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="kable-table">
<table class="caption-top table table-sm table-striped small">
<colgroup>
<col style="width: 9%">
<col style="width: 12%">
<col style="width: 12%">
<col style="width: 12%">
<col style="width: 12%">
<col style="width: 13%">
<col style="width: 12%">
<col style="width: 4%">
<col style="width: 13%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;"></th>
<th style="text-align: right;">AIC</th>
<th style="text-align: right;">SABIC</th>
<th style="text-align: right;">HQ</th>
<th style="text-align: right;">BIC</th>
<th style="text-align: right;">logLik</th>
<th style="text-align: right;">X2</th>
<th style="text-align: right;">df</th>
<th style="text-align: right;">p</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">fit2PL</td>
<td style="text-align: right;">5518.465</td>
<td style="text-align: right;">5539.276</td>
<td style="text-align: right;">5551.541</td>
<td style="text-align: right;">5602.757</td>
<td style="text-align: right;">-2739.232</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">NA</td>
</tr>
<tr class="even">
<td style="text-align: left;">fit3PL</td>
<td style="text-align: right;">5537.377</td>
<td style="text-align: right;">5568.594</td>
<td style="text-align: right;">5586.991</td>
<td style="text-align: right;">5663.816</td>
<td style="text-align: right;">-2738.689</td>
<td style="text-align: right;">1.087351</td>
<td style="text-align: right;">10</td>
<td style="text-align: right;">0.9997476</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>If we inspect the IRT parameters, the third parameter (c, here denoted as g) is fixed to 0.</p>
<div class="cell" data-layout-align="center">
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(fit2PL, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">IRTpars =</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;">simplify =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; $items
#&gt;         a      b g u
#&gt; V1  1.699  1.168 0 1
#&gt; V2  1.018 -0.582 0 1
#&gt; V3  1.084  0.343 0 1
#&gt; V4  1.275  0.657 0 1
#&gt; V5  1.177  0.514 0 1
#&gt; V6  1.176 -0.146 0 1
#&gt; V7  1.286  1.868 0 1
#&gt; V8  0.838  0.024 0 1
#&gt; V9  1.684  2.162 0 1
#&gt; V10 0.919 -0.070 0 1
#&gt; 
#&gt; $means
#&gt; F1 
#&gt;  0 
#&gt; 
#&gt; $cov
#&gt;    F1
#&gt; F1  1</code></pre>
</div>
</div>
</section>
<section id="trace-plot" class="level2">
<h2 class="anchored" data-anchor-id="trace-plot">Trace plot</h2>
<p>The difference between the 3PL and the 2PL model is particularly visible in the trace plot.</p>
<div class="cell" data-layout-align="center">
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tracePlot</span>(fit2PL, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta_range =</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;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">facet =</span> F, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend =</span> T) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb31-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set3"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb31-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2PL - Traceplot"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-25-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-12"><img src="https://philippmasur.de/blog/2022-05-13-irt-analyses-in-r/index_files/figure-html/unnamed-chunk-25-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Item characteristic curves of all ten items of the 2PL model drawn on top of each other; the curves differ in slope but all have a lower asymptote of zero."></a></p>
</figure>
</div>
</div>
</div>
<p>The curves have different slopes, but they do not have different asymptotes (yintercepts = guessing probability).</p>
</section>
</section>
<section id="pl-or-rasch-model" class="level1">
<h1>1PL or Rasch model</h1>
<p>The 1PL model gets rid of yet another parameter: item discrimination. It basically constrains item discrimination to be equal across all items. Only item difficulty is allowed to vary. The mathematical form hence becomes:</p>
<p><img src="https://latex.codecogs.com/png.latex?P(%5Ctheta_j)%20=%20%5Cfrac%7Be%5E%7B1.7a(%5Ctheta_j-b_i)%7D%7D%7B1+e%5E%7B1.7a(%5Ctheta_j-b_i)%7D%7D"></p>
<p>Note that there is no subscript for the letter <img src="https://latex.codecogs.com/png.latex?a">, because it is constrained to be the same for all items. The Rasch model (stemming from a different scholarly tradition) is mathematically equivalent, but is often expressed slightly differently:</p>
<p><img src="https://latex.codecogs.com/png.latex?P(%5Cbeta)%20=%20%5Cfrac%7Be%5E%7B(%5Cbeta-%5Cdelta_i)%7D%7D%7B1+%20e%5E%7B(%5Cbeta-%5Cdelta_i)%7D%7D"></p>
<p>It is basically the same as the equation above, but the 1.7 constant is omitted. Further, the typical notational system uses <img src="https://latex.codecogs.com/png.latex?%5Cdelta"> instead of <img src="https://latex.codecogs.com/png.latex?b"> and <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> instead of <img src="https://latex.codecogs.com/png.latex?%5Ctheta">.</p>
<p>This constrained model is rather an “ideal” measurement model than a model that can be perfectly fitted to the data. Yet, if we find items that fit this model sufficiently, it actually has some mathematical properties that cannot be obtained with less constrained models (e.g., 2PL or 3PL models). For this reason, many scholars favor this measurement model (particularly those following the Rasch School of Measurement)</p>
<ol type="1">
<li>For this model, the number-correct score is particularly a sufficient estimation of theta.</li>
<li>Due to the fixed discrimination parameter, item characteristic curves cannot cross. Items thus do not differ in how well they differentiate respondents.</li>
<li>Such a model has a so-called specific objectivity: Because the Rasch model is based on the idea that items are designed to fit certain properties, instead of finding a model that fits the data, item as well as person parameter estimates are invariant across samples. Person (i.e., proficiency scores) and item parameters (i.e., item difficulty) thus will be similar even if only a subset of the item pool is used or a different population is studied. This makes it very easy to create smaller tests, parallel test or even adaptive testing pools.</li>
</ol>
<section id="fitting-the-model-2" class="level2">
<h2 class="anchored" data-anchor-id="fitting-the-model-2">Fitting the model</h2>
<div class="cell" data-layout-align="center">
<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">fitRasch <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;">mirt</span>(d, <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;">itemtype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rasch"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> T)</span></code></pre></div></div>
</div>
<div class="cell" data-layout-align="center">
<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">fitRasch</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; 
#&gt; Call:
#&gt; mirt(data = d, model = 1, itemtype = "Rasch", verbose = T)
#&gt; 
#&gt; Full-information item factor analysis with 1 factor(s).
#&gt; Converged within 1e-04 tolerance after 20 EM iterations.
#&gt; mirt version: 1.45.1 
#&gt; M-step optimizer: nlminb 
#&gt; EM acceleration: Ramsay 
#&gt; Number of rectangular quadrature: 61
#&gt; Latent density type: Gaussian 
#&gt; 
#&gt; Log-likelihood = -2746.893
#&gt; Estimated parameters: 11 
#&gt; AIC = 5515.786
#&gt; BIC = 5562.147; SABIC = 5527.232
#&gt; G2 (1012) = 514.28, p = 1
#&gt; RMSEA = 0, CFI = NaN, TLI = NaN</code></pre>
</div>
</div>
</section>
<section id="compare-models" class="level2">
<h2 class="anchored" data-anchor-id="compare-models">Compare models</h2>
<div class="cell" data-layout-align="center">
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anova</span>(fitRasch, fit2PL)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="kable-table">
<table class="caption-top table table-sm table-striped small">
<colgroup>
<col style="width: 11%">
<col style="width: 11%">
<col style="width: 11%">
<col style="width: 11%">
<col style="width: 11%">
<col style="width: 13%">
<col style="width: 11%">
<col style="width: 3%">
<col style="width: 11%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;"></th>
<th style="text-align: right;">AIC</th>
<th style="text-align: right;">SABIC</th>
<th style="text-align: right;">HQ</th>
<th style="text-align: right;">BIC</th>
<th style="text-align: right;">logLik</th>
<th style="text-align: right;">X2</th>
<th style="text-align: right;">df</th>
<th style="text-align: right;">p</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">fitRasch</td>
<td style="text-align: right;">5515.786</td>
<td style="text-align: right;">5527.232</td>
<td style="text-align: right;">5533.978</td>
<td style="text-align: right;">5562.147</td>
<td style="text-align: right;">-2746.893</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">NA</td>
</tr>
<tr class="even">
<td style="text-align: left;">fit2PL</td>
<td style="text-align: right;">5518.465</td>
<td style="text-align: right;">5539.276</td>
<td style="text-align: right;">5551.541</td>
<td style="text-align: right;">5602.757</td>
<td style="text-align: right;">-2739.232</td>
<td style="text-align: right;">15.32166</td>
<td style="text-align: right;">9</td>
<td style="text-align: right;">0.082471</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<p>If we inspect the IRT parameters, the first (a, item discrimination) is constrained to be equal and the third parameter (c, here denoted as g) is fixed to 0.</p>
<div class="cell" data-layout-align="center">
<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;">coef</span>(fitRasch, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">IRTpars =</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;">simplify =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; $items
#&gt;     a      b g u
#&gt; V1  1  1.673 0 1
#&gt; V2  1 -0.618 0 1
#&gt; V3  1  0.374 0 1
#&gt; V4  1  0.798 0 1
#&gt; V5  1  0.593 0 1
#&gt; V6  1 -0.174 0 1
#&gt; V7  1  2.304 0 1
#&gt; V8  1  0.018 0 1
#&gt; V9  1  3.138 0 1
#&gt; V10 1 -0.073 0 1
#&gt; 
#&gt; $means
#&gt; F1 
#&gt;  0 
#&gt; 
#&gt; $cov
#&gt;       F1
#&gt; F1 1.302</code></pre>
</div>
</div>
</section>
<section id="trace-plot-1" class="level2">
<h2 class="anchored" data-anchor-id="trace-plot-1">Trace plot</h2>
<p>Again, the difference between the 3PL, 2PL and the 1PL (Rasch) model is particularly visible in the trace plot:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tracePlot</span>(fitRasch, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">theta_range =</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;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">facet =</span> F, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend =</span> T) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb38-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set3"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb38-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1PL - Traceplot"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-30-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-13"><img src="https://philippmasur.de/blog/2022-05-13-irt-analyses-in-r/index_files/figure-html/unnamed-chunk-30-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Item characteristic curves of all ten items of the 1PL (Rasch) model drawn on top of each other; all curves have the same slope and differ only in their location."></a></p>
</figure>
</div>
</div>
</div>
<p>All items have the exact same slope.</p>
</section>
<section id="creating-smaller-tests-or-parallel-tests" class="level2">
<h2 class="anchored" data-anchor-id="creating-smaller-tests-or-parallel-tests">Creating smaller tests or parallel tests</h2>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb39-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">itempersonMap</span>(fitRasch)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-31-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-14"><img src="https://philippmasur.de/blog/2022-05-13-irt-analyses-in-r/index_files/figure-html/unnamed-chunk-31-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Item person map for the Rasch model: the distribution of the latent ability above and the difficulty of each item on the same theta scale below."></a></p>
</figure>
</div>
</div>
</div>
<p>We can create smaller, but parallel tests by distributing similar items into to subset pools. Here, I am simply inspecting the item person map and try to form to sets that equally cover the range of the latent ability.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb40" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1">subset1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> d <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb40-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(V2, V10, V3, V4, V7)</span>
<span id="cb40-3">subset2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> d <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb40-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(V6, V8, V5, V1, V9)</span>
<span id="cb40-5"></span>
<span id="cb40-6">test1 <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;">mirt</span>(subset1, <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;">itemtype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rasch"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> F)</span>
<span id="cb40-7">test2 <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;">mirt</span>(subset2, <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;">itemtype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rasch"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> F)</span></code></pre></div></div>
</div>
<p>We can assess their similarity be plotting two test information curves on top of each other:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb41" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb41-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">testInfoCompare</span>(test1, test2)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-33-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-15"><img src="https://philippmasur.de/blog/2022-05-13-irt-analyses-in-r/index_files/figure-html/unnamed-chunk-33-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Test information curves of the two five-item Rasch subtests drawn on top of each other; the two curves largely overlap."></a></p>
</figure>
</div>
</div>
</div>
<p>As we can see, both test sufficiently overlap in their information curve. So they are very likely to correlate highly with the original test</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb42" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb42-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor.test</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fscores</span>(test1), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fscores</span>(fitRasch))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; 
#&gt;  Pearson's product-moment correlation
#&gt; 
#&gt; data:  fscores(test1) and fscores(fitRasch)
#&gt; t = 41.294, df = 498, p-value &lt; 2.2e-16
#&gt; alternative hypothesis: true correlation is not equal to 0
#&gt; 95 percent confidence interval:
#&gt;  0.8582740 0.8981538
#&gt; sample estimates:
#&gt;       cor 
#&gt; 0.8797522</code></pre>
</div>
</div>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb44" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb44-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cor.test</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fscores</span>(test2), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fscores</span>(fitRasch))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; 
#&gt;  Pearson's product-moment correlation
#&gt; 
#&gt; data:  fscores(test2) and fscores(fitRasch)
#&gt; t = 38.389, df = 498, p-value &lt; 2.2e-16
#&gt; alternative hypothesis: true correlation is not equal to 0
#&gt; 95 percent confidence interval:
#&gt;  0.8405774 0.8851290
#&gt; sample estimates:
#&gt;      cor 
#&gt; 0.864542</code></pre>
</div>
</div>
</section>
</section>
<section id="where-to-go-next" class="level1">
<h1>Where to go next?</h1>
<p>IRT is a broad field and there are many more models (e.g., graded response models, multidimensional models, 4PL models…). A good starting point is the extended literature:</p>
<ul>
<li><p>DeMars, C. (2010). Item response theory. Oxford University Press.</p></li>
<li><p>Embretson, S. E., &amp; Reise, S. P. (2013). Item response theory (2nd ed.). Psychology Press.</p></li>
</ul>
<p>There are also good online resources:</p>
<ul>
<li>Website dedicated to Rasch modeling: <a href="https://www.rasch.org/" class="uri">https://www.rasch.org/</a></li>
</ul>
</section>
<section id="references" class="level1">
<h1>References</h1>
<ul>
<li><p>DeMars, C. (2010). Item response theory. Oxford University Press.</p></li>
<li><p>Orlando, M. &amp; Thissen, D. (2000). Likelihood-based item fit indices for dichotomous item response theory models. Applied Psychological Measurement, 24, 50-64.</p></li>
</ul>
<div class="callout callout-style-simple callout-note no-icon callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>30 comments from the original post
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>These comments were left on the earlier WordPress version of this post.</p>
<div class="archived-comment">
<p><strong>Mehrdad Vossoughi</strong> · 9 November 2022</p>
<p>Dear Sir’<br>
I really appreciate you for this valuable page. I found it very useful, and I want to know whether you have another course on IRT for Likert items?<br>
Sincerely yours,<br>
Mehrdad Vossoughi<br>
PhD in Biostatistics</p>
<div class="archived-comment reply">
<p><strong>Philipp Masur</strong> · 22 November 2022</p>
<p>Dear Mehrdad,</p>
<p>have a look at this tutorial for the graded response model: <a href="https://github.com/ccs-amsterdam/r-course-material/blob/master/tutorials/R_test-theory_3_irt_graded.md">https://github.com/ccs-amsterdam/r-course-material/blob/master/tutorials/R_test-theory_3_irt_graded.md</a></p>
<p>Might contain what you are looking for!</p>
<p>Best,<br>
Philipp</p>
</div>
</div>
<div class="archived-comment">
<p><strong>Bogdan</strong> · 25 January 2023</p>
<p>Hi Philipp,</p>
<p>Thank you for this very detailed and well-structured post.</p>
<p>I just wanted to let you know that this post made my life considerably easier. Hope you’ll keep up posting such great content!</p>
<p>Best,<br>
Bogdan</p>
</div>
<div class="archived-comment">
<p><strong>Robert</strong> · 20 March 2023</p>
<p>Dear Philipp,</p>
<p>I get the message:</p>
<p>Warning: The “ argument of `guides()` cannot be `FALSE`. Use “none” instead as<br>
of ggplot2 3.3.4.</p>
<p>Could you please assist with rectifying this?</p>
<div class="archived-comment reply">
<p><strong>Philipp Masur</strong> · 23 August 2023</p>
<p>This may be something I’ll have to update in the package. Sorry. ggplot2 has seen an update in this regard. Best!</p>
</div>
</div>
<div class="archived-comment">
<p><strong>Robert</strong> · 20 March 2023</p>
<p>Dear Philipp,</p>
<p>Thank you for an excellent introduction to IRT and making ggmirt available.</p>
<p>I am also receiving the following message when I run your code:</p>
<p>Warning: Returning more (or less) than 1 row per `summarise()` group was deprecated in<br>
dplyr 1.1.0.<br>
ℹ Please use `reframe()` instead.<br>
ℹ When switching from `summarise()` to `reframe()`, remember that `reframe()`<br>
always returns an ungrouped data frame and adjust accordingly.</p>
<p>Could you please assist with fixing this?</p>
<div class="archived-comment reply">
<p><strong>Jamie</strong> · 8 May 2025</p>
<p>This is not an error or an issue and doesn’t need fixing from Philipp, this is simply a general warning about a particular function being preferred over another by the package creator.</p>
</div>
</div>
<div class="archived-comment">
<p><strong>Michelle</strong> · 23 March 2023</p>
<p>Thank you so much – a very useful tool, and the ggmirt graphs are extremely useful.</p>
</div>
<div class="archived-comment">
<p><strong>Ana Carol</strong> · 27 April 2023</p>
<p>I find it interesting that IRT models offer advantages such as computer adaptive testing and can be used to develop smaller scales or parallel tests while still maintaining high validity and reliability. How do these advantages translate into practical applications in the social sciences?</p>
<div class="archived-comment reply">
<p><strong>Philipp Masur</strong> · 23 August 2023</p>
<p>Well, particularly if a scale is very large, we may often not have the space to put them fully into questionnaires. Adaptive testing may then allow to still assess the latent trait/ability/concept without sacrificing too much space.</p>
<p>Parallel testing on the other hand is particularly important for pre-post experimental designs (e.g., intervention studies). How would you otherwise minimize learning effects?</p>
<p>Just some thoughts! 😉</p>
</div>
</div>
<div class="archived-comment">
<p><strong>Fabian</strong> · 2 May 2023</p>
<p>Hello, thanks for this great resource. Everything works fine and is exactly what I was looking for.</p>
<p>I just have one question.<br>
Within the traceplot function, is there a way to relabel the items? E.g it says item 1, item 2, etc. Instead I would like to use ‘Q10’, ‘Q11″ as label. Or can it use the naming it uses in the coef(fit1PL) function? Is this possible?</p>
<p>Thanks for your help.</p>
<div class="archived-comment reply">
<p><strong>Philipp Masur</strong> · 23 August 2023</p>
<p>I have to think about this. I am just using the standard output from ‘mirt’ here. But renaming should not be a problem per se.</p>
</div>
</div>
<div class="archived-comment">
<p><strong>Mahdi</strong> · 10 May 2023</p>
<p>Hello, thanks for this clear and interesting manual. But I have a question: is it possible to recalibrate person latent scores into a specified range (like 0_10) in simple rasch model (and how)?</p>
</div>
<div class="archived-comment">
<p><strong>tanya</strong> · 18 May 2023</p>
<p>Very well organized overview. Can you suggest any resources for equating with ‘mirt’?</p>
</div>
<div class="archived-comment">
<p><strong>Ibnu Rafi</strong> · 12 June 2023</p>
<p>Really appreciate excellent works that you have made. Is it possible to arrange the item in the order form for the case we generate ICC and IIF through tracePlot(fit3PL) and itemInfoPlot(fit3PL, facet = T), respectively?</p>
</div>
<div class="archived-comment">
<p><strong>Ibnu Rafi</strong> · 12 June 2023</p>
<p>Really appreciate excellent works that you have made. Is it possible to arrange the item in the order form when we want to generate ICC and IIF by using tracePlot(fit3PL) and itemInfoPlot(fit3PL, facet = T), respectively?</p>
</div>
<div class="archived-comment">
<p><strong>Melinda Higgins</strong> · 23 June 2023</p>
<p>Thank you for such a helpful tutorial. And especially like that your functions are compatible with ggplot2! I was able to use your code today to easily customize my plots for my current project!</p>
</div>
<div class="archived-comment">
<p><strong>Alda</strong> · 27 June 2023</p>
<p>Hi Phillip,<br>
Any advice on choosing the optimizer in mirt()? There are many, how one can decide on using the right one? Pros and cons?<br>
Any advice is higly appreciated!<br>
Thank you!</p>
<div class="archived-comment reply">
<p><strong>Philipp Masur</strong> · 23 August 2023</p>
<p>Hi Alda,</p>
<p>this is really a too big of a question to simply point to some resource or summarize the pros and cons. I would suggest to start in the ‘mirt’ manual (there is a small section on what type of optimizers exists under the main function, including some recommendations for when to use which) and then start searching for the relevant literature. I would even say that the default will work in most cases, but changing to other optimizers is really very specific to the study and model in question.</p>
<p>Best,<br>
Philipp</p>
</div>
</div>
<div class="archived-comment">
<p><strong>Rainer</strong> · 5 September 2023</p>
<p>Philipp, thank you for this nice overview of IRT packages in R! Maybe you want to consider mentioning our new package RMX, which supports all these analyses and packages with a new diagram, PIccc, an largely extended PI-Map? See, <a href="https://www.mdpi.com/2624-8611/5/3/62">https://www.mdpi.com/2624-8611/5/3/62</a> for the article and <a href="https://osf.io/n9c5r/">https://osf.io/n9c5r/</a> for the package.</p>
<p>Best,<br>
Rainer</p>
<div class="archived-comment reply">
<p><strong>Rainer</strong> · 7 October 2024</p>
<p>Update: The RMX package is now available at CRAN! (Thanks Philipp for leaving my impudent self-advertisment!) 🙂</p>
</div>
</div>
<div class="archived-comment">
<p><strong>Pierre Varly</strong> · 4 November 2023</p>
<p>Hello, by far the most well explained page I saw on IRT and a powerful package. I am very grateful for this. to install ggmirt I had to run this command : install.packages(“remotes”)<br>
remotes::install_github(“masurp/ggmirt”)<br>
<a href="https://rdrr.io/github/masurp/ggmirt/">https://rdrr.io/github/masurp/ggmirt/</a></p>
</div>
<div class="archived-comment">
<p><strong>Abiy Kefyalew</strong> · 15 January 2024</p>
<p>I find this page intriguing and beneficial. Could you please provide an interpretation of the output from comparing models? This would enhance clarity for me.</p>
</div>
<div class="archived-comment">
<p><strong>Lyubo</strong> · 15 March 2024</p>
<p>&gt; subset1 %<br>
+ select(V2, V10, V3, V4, V7)<br>
Error in select(., V2, V10, V3, V4, V7) :<br>
unused arguments (V2, V10, V3, V4, V7)</p>
</div>
<div class="archived-comment">
<p><strong>Emma Mills</strong> · 25 March 2024</p>
<p>Dear Philipp, Great primer. Thanks loads. RStudio advised posting this warning message to you after running the simulation code:<br>
d &lt;- sim_irt(500, 10, discrimination = .25, seed = 42).</p>
<p>Warning message:<br>
The `x` argument of `as_tibble.matrix()` must have unique column names if `.name_repair` is omitted as of tibble<br>
2.0.0.<br>
ℹ Using compatibility `.name_repair`.<br>
ℹ The deprecated feature was likely used in the ggmirt package. Please report the issue to the authors.</p>
<p>so done.<br>
Many thanks, once more!<br>
Emma</p>
</div>
<div class="archived-comment">
<p><strong>Clarissa</strong> · 18 April 2024</p>
<p>Hi there, thank you for this tutorial. Can you give me some advice on what to do if I want to fit a model with a dataframe that has some likert items and some dichotomous items. Is it possible to have one MIRT model with some items graded and some as 3PL? If not, is it suitable to do two separate models and compare the F values, a-parameters etc? Thank you!</p>
</div>
<div class="archived-comment">
<p><strong>Maureen</strong> · 16 May 2024</p>
<p>Thank you very much.<br>
The information and R codes were quite helpful.<br>
I was however not able to load ggmirt. Is it related to a particular version of R?</p>
</div>
<div class="archived-comment">
<p><strong>George Hagi</strong> · 10 June 2024</p>
<p>Dear Sir;</p>
<p>Is it adequate to do F1=1-10 F2=11-20 when analyzing irt in multidimensional data?</p>
</div>
<div class="archived-comment">
<p><strong>Zoltan Jakab</strong> · 25 June 2024</p>
<p>Dear Dr.&nbsp;Masur,<br>
Thank you very much indeed, for this introduction, I find it really helpful.<br>
However, I have the following question: I have a dichotomous data set thta looks okay (no missing data, row sums and col sums all &gt;0, and there is a broad distribution of values in both cases), yet the mirt() function returns the same error message for any model:</p>
<p>Error in min(s, na.rm = TRUE):max(x, na.rm = TRUE) :<br>
result would be too long a vector</p>
<p>How can I find out what the problem is with an IRT dataset?<br>
Thanks very much!<br>
Zoltan</p>
</div>
<div class="archived-comment">
<p><strong>Abiy Kefyalew Aboret</strong> · 10 November 2024</p>
<p>The fit statistics indicate that the item aligns with the model, yet the item characteristic curve suggests it is behaving in the opposite manner. How can we interpret this?</p>
</div>
</div>
</div>
</div>


<template id="pm-footer-icons"><a href="#" data-pm-mail="p.k.masur|vu.nl" aria-label="Email" title="Email"><svg class="pm-icon" aria-hidden="true"><use href="#i-mail"></use></svg></a><a href="https://scholar.google.com/citations?user=XA_WthYAAAAJ" target="_blank" rel="noopener" aria-label="Google Scholar" title="Google Scholar"><svg class="pm-icon" aria-hidden="true"><use href="#i-scholar"></use></svg></a><a href="https://orcid.org/0000-0003-3065-7305" target="_blank" rel="noopener" aria-label="ORCID" title="ORCID"><svg class="pm-icon" aria-hidden="true"><use href="#i-orcid"></use></svg></a><a href="https://osf.io/yuqv5/" target="_blank" rel="noopener" aria-label="Open Science Framework" title="Open Science Framework"><svg class="pm-icon" aria-hidden="true"><use href="#i-osf"></use></svg></a><a href="https://github.com/masurp" target="_blank" rel="noopener" aria-label="GitHub" title="GitHub"><svg class="pm-icon" aria-hidden="true"><use href="#i-github"></use></svg></a><a href="https://bsky.app/profile/masurphil.bsky.social" target="_blank" rel="noopener" aria-label="Bluesky" title="Bluesky"><svg class="pm-icon" aria-hidden="true"><use href="#i-bluesky"></use></svg></a><a href="https://research.vu.nl/en/persons/philipp-k-masur" target="_blank" rel="noopener" aria-label="VU Amsterdam" title="VU Amsterdam"><svg class="pm-icon" aria-hidden="true"><use href="#i-landmark"></use></svg></a></template>
</section>

 ]]></description>
  <category>R &amp; Statistics</category>
  <guid>https://philippmasur.de/blog/2022-05-13-irt-analyses-in-r/</guid>
  <pubDate>Thu, 12 May 2022 22:00:00 GMT</pubDate>
  <media:content url="https://philippmasur.de/blog/2022-05-13-irt-analyses-in-r/featured.png" medium="image" type="image/png" height="115" width="144"/>
</item>
<item>
  <title>How to make most of your PhD</title>
  <link>https://philippmasur.de/blog/2022-04-03-make-most-of-your-phd/</link>
  <description><![CDATA[ 

<!-- Icon sprite: 24×24 line icons, 1.6 stroke. Every shape has pathLength="1"
     so CSS can animate stroke-dashoffset 1 → 0 ("draw" the icon).
     Use: <svg class="pm-icon"><use href="#i-mail"/></svg> -->





<p><a href="img/Picture3-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://philippmasur.de/blog/2022-04-03-make-most-of-your-phd/img/Picture3-1.png" class="img-fluid"></a></p>
<p>From my personal point of view, becoming a researcher and succeeding in a PhD program can be one of the most rewarding experiences you will have in your life. You will have a lot of freedom in deciding who you want become, what you want to learn and what exactly you want to investigate. At the same time, it can be stressful, a lot of work, and – at times – be outright frustrating. Yet, much of what causes sorrow in the beginning turns out to be less problematic in later years. Although it is difficult to “prepare” someone for a PhD program and you will definitely make your own experiences, there are a couple of things that I think can help to succeed and to make most of your PhD.</p>
<p>The following list of recommendations is obviously aligned with my own experiences. Some aspects may work well for you, others may simply not be right for you. That being said, I do believe that it is worth to think a bit about all of them (even if it simply leads to dismissing some of them). Being a quantitative communication scholar, some recommendations are very much tailored to this particular type of research (e.g., my suggestion to learn a programming language and improve statistical skills). Most points on this list, however, are of a more general nature and should be interesting to anyone who starts an academic career.</p>
<p><img src="https://philippmasur.de/blog/2022-04-03-make-most-of-your-phd/img/Picture1.png" class="img-fluid">Just as the PhD project itself, the following list can be a bit daunting. It is not my goal to make your life even more stressful. Instead, take these recommendations with a grain of salt and rather as an opportunity to reflect on your current way of working. Adapt those points that make sense to you, where you feel that they could help you. Feel free to discard others and definitely do not see them as criteria that you need to fulfil or succeed at.</p>
<section id="time-management-and-self-organization" class="level3">
<h3 class="anchored" data-anchor-id="time-management-and-self-organization">Time Management and Self-Organization</h3>
<p>One of the best things about being an academic scholar is that it never gets boring. One has the unique privilege to engage in diverse projects and activities. Doing a PhD often means teaching some classes, participating in larger research projects, writing several papers at the same time, presenting at conferences, participating in workshops, and – on top of everything – keeping up with your actual PhD project. Time management and organisation is hence an important basis to reduce stress and frustration. I believe it is important to note that one never gets perfect at this. So don’t strive for perfection, but constantly reflect on your workflow and aim to get better instead.</p>
<ol type="1">
<li><strong>Take notes and systematize your thoughts and projects.</strong> You will have a lot of meetings – whether with your supervisors, project partners, colleagues of fellow PhD students. Don’t think you will remember everything. Write everything down! Meetings notes, tasks, methods details, code annotations, thoughts that pop up at the most random times, summaries of important papers – you should create an archive of your thinking. Look into apps such as <a href="https://evernote.com/intl/de/">Evernote</a> (my personal favorite) or <a href="https://www.onenote.com/">OneNote</a>. They allow to tag, search, and organize all of these notes in a meaningful manner, make them searchable, and can run on several devices.</li>
<li><strong>Rethink your workspace and back-up your work.</strong> You think your computer files and folders are well organized? Think again. The more structure and consistency you put into organizing your digital files, the better you will find anything you want to find. Create separate folders for different aspects of your work, create further separate folders for projects and classes and find a good way to sort and browse through them. A consistent file and folder naming policy might sound a bit strict, but it can be really helpful in the long run. Most importantly, find a good way to back up your work and do it frequently (at best automatically). If your current work is not backed up, do it now!</li>
<li><strong>Prioritize your PhD project.</strong> In the beginning, it can feel like it takes forever until you get invited to collaborative projects or until the first paper is published. Yet, stick to <em>your</em> project for the time being. Invitations and collaborations will eventually come and you will have more to do than you originally thought you would. Particularly in later stages of your PhD and beyond, learn to politely say no. Time management and self-organization also means to protect yourself against too much work.</li>
<li><strong>Use the SMART approach to break down larger projects.</strong> A PhD project usually takes several years and involves several studies, which, in turn, consists of several stages. To not get overwhelmed, break your thesis down into specific, measurable, attainable, relevant and timely (SMART) goals. “Draft first paragraph” is much easier to complete than “Write chapter 1”.</li>
<li><strong>Take breaks.</strong> Finally, I would like to suggest something seemingly paradoxical: The more you have to do, the more breaks you should take. Breaks will make you more and not less productive. They are important to recharge, to get re-motivated and simply enjoy working more. When you plan your day, don’t forget to plan breaks.</li>
</ol>
</section>
<section id="staying-up-to-date-and-organizing-your-knowledge" class="level3">
<h3 class="anchored" data-anchor-id="staying-up-to-date-and-organizing-your-knowledge">Staying Up-to-date and Organizing Your Knowledge</h3>
<p>The following aspects are in a way still connected to self-organization, but focus a bit more on your actual scientific work. The scientific literature is growing at a fast pace. Even in subfields, one can hardly read all publications. It is hence very important to stay up-to-date and find ways to digest the large amount of information.</p>
<ol type="1">
<li><strong>Gain an overview of the most important theories and studies in your field.</strong> You may have studied a bachelor and master program in your field, but do you really know your field? Communication Science (as any other disciplines) has become a broad discipline and it doesn’t hurt to try to get at least an idea of all of its research areas and subfields. I am not saying that you should become an expert in all of them, rather get some knowledge of the broad themes, theories, and developments. A historical understanding of the discipline is also beneficial (see this <a href="https://us.sagepub.com/en-us/nam/the-handbook-of-communication-science/book227447#description">handbook</a> for example for communication science).</li>
<li><strong>Keep track of the literature and use a reference manager.</strong> You really need to make sure that you are aware of the latest publications in your field. Journals offer RSS feeds, google offers journal-specific alerts. Set up a system that keeps you updated with the most important journals in your field. And by all means,&nbsp; use a reference manager to systematize the literature and to simplify your citing practice (my personal preference: <a href="https://www.zotero.org/">zotero</a>) .</li>
<li><strong>Consider joining (some) social media platforms.</strong> This is not for everyone, but specifically <a href="https://twitter.com/">Twitter</a> (to a minor degree platform such as <a href="https://www.researchgate.net/">ResearchGate</a>) provide you with information about new developments, access to current debates, and simply a platform to engage with other scientists. Job offers, conference calls, the newest publications (and how they are perceived…) all of this is shared on Twitter. I personally benefitted a lot from joining Twitter.</li>
<li><strong>Engage in conversations.</strong> Don’t be shy to participate in conversations, whether in your department or on Twitter. Don’t brag, be respectful, and think before you speak, but otherwise, join the discussion! Science is about exchanging, defending and refuting ideas.</li>
<li><strong>Participate in workshops.</strong> Conferences, institutions, departments or simply your colleagues will offer workshops on various topics. These events are not only good opportunities to learn something new, they are likewise often a lot of fun and a great way to meet new people.</li>
</ol>
</section>
<section id="refreshing-old-and-learning-new-skills" class="level3">
<h3 class="anchored" data-anchor-id="refreshing-old-and-learning-new-skills">Refreshing Old and Learning New Skills</h3>
<p>You know already a lot, but obviously you don’t know everything. A PhD should also be your chance to refresh and improve some skills as well as learn entirely new methods. You will of course specialize in certain areas, but I strongly recommend to get a good overview of the existing methods as well.<img src="https://philippmasur.de/blog/2022-04-03-make-most-of-your-phd/img/Picture2.png" class="img-fluid"></p>
<ol type="1">
<li><strong>Gain an overview of the most important methods.</strong> You think of yourself as a computational scientist and hence don’t feel like you should learn traditional survey methodology? You consider yourself an experimental researcher and don’t bother about machine learning? I guarantee that you will become a better scientist if you have a least a good understanding of what methods and areas of research exist. Never heard of eye tracking? Find out what it is about. Puzzled by people talking about Bayesian statistics? Well, don’ttry to excel at everything, but do get a grasp of what people are talking about.</li>
<li><strong>Familiarize yourself with open science principles.</strong> Don’t wait until after your PhD to do science the right way. More and more journals adopt open science principles, tenure-track programs include open science criteria and the general science community moves towards more open and transparent research. Familiarize yourself with preregistration, data sharing possibilities, reproducibility, and open access. This <a href="https://academic.oup.com/joc/advance-article/doi/10.1093/joc/jqz052/5803422#200712879">paper</a> may be a good starting point to dive into the debate and definitely get acquainted with the <a href="https://osf.io/">Open Science Framework</a>.</li>
<li><strong>Learn a programming language.</strong> Learning a programming language is not easy, but it is hard to get around now-a-days. Our field moves towards more and more computational methods and advanced statistics. You will want to be at the forefront of this movement. Which language you will want to learn depends on the type of research that you plan to conduct. If you are primarily interested in statistics, R might be your choice. If you want to dive deeper into machine learning, you will not get around python. If you plan to build web applications, you should look into javascript and react. This <a href="https://github.com/ccs-amsterdam/r-course-material">repository</a>&nbsp;created by some colleagues and myself can be a great starting point for learning R for statistical analysis and text analysis. This <a href="https://cssbook.net/">open access book</a> by my colleague Wouter van Atteveldt is further a great source for computational methods in both R and Python.</li>
<li><strong>Refresh your statistic skills.</strong> Related to learning R, take the time to refresh your statistic skills. Really try to understand frequentist inferences, get to know more advanced analysis approaches such as multilevel modelling, SEM, machine learning, to name just a few. Andy Field’s <a href="https://www.discoveringstatistics.com/books/discovering-statistics-using-r/">book</a> – by now almost a classic – can be a good introduction, but consider taking online courses (e.g., this <a href="https://www.coursera.org/learn/statistical-inferences?ranMID=40328&amp;ranEAID=je6NUbpObpQ&amp;ranSiteID=je6NUbpObpQ-6MuuyPfOsl5RETIjY4r3iw&amp;siteID=je6NUbpObpQ-6MuuyPfOsl5RETIjY4r3iw&amp;utm_content=10&amp;utm_medium=partners&amp;utm_source=linkshare&amp;utm_campaign=je6NUbpObpQ">coursera course</a> by Daniel Lakens) and consult more comprehensive books (e.g., Richard McElreath’s <a href="https://xcelab.net/rm/statistical-rethinking/">Rethinking Statistics</a>).</li>
<li><strong>Explore alternatives to “standard” software.</strong> Although it might seem convenient to use a standard word processing software such as MS Word or the classic MS Powerpoint to create your presentations (and it often is!), be aware of alternatives. Latex is annoyingly techy, but has benefits in many cases (particularly for formatting a large PhD thesis). <a href="https://rmarkdown.rstudio.com/">RMarkdown</a> can offer an interesting approach to combine writing and analysis (in R) to make your work more reproducible (see <a href="https://www.amyorben.com/docs/thesis/index.html">this thesis</a> by Amy Orben as an example). The book by <a href="https://bookdown.org/yihui/rmarkdown/">Yihui</a> is a great resource in this regard. Check out whether it works for you.</li>
</ol>
</section>
<section id="doing-research" class="level3">
<h3 class="anchored" data-anchor-id="doing-research">Doing Research</h3>
<p>A major part of your work as PhD student is obviously doing research. You can of course simply start and see where it leads you, but I would suggest to think a bit more long-term. What type of researcher do you want to be? How do you want to be seen in the field? Don’t stress too much about these questions, but it can be beneficial to reflect a little bit on them at the start.</p>
<ol type="1">
<li><strong>Follow the highest standards, but don’t stress yourself if things don’t work out.</strong> Always value <em>quality over quantity</em>. This does not mean that you should not publish and also keep publishing, but rather continuously put out good work than publish a lot of rubbish. Try to make it right the first time, but then again don’t stress if things don’t work out.</li>
<li><strong>Get comfortable with rewriting, changing, and improving your work.</strong> It can be a bit frustrating if you just finished a draft, shared it with your colleague or supervisor, only to get it back with thousands of comments on how to improve it. Don’t be intimated or offended, instead get comfortable with rewriting and restructuring, sometimes even entirely overthrowing manuscripts, drafts or research project. The first thought is seldom gold, but nonetheless needs to be written down before one can think “the better thought”.</li>
<li><strong>Have a clear plan for each project.</strong> Plan ahead, define milestones, schedule meetings, and keep to your plan (remember the SMART approach). It will help you easily completing the most complex projects.</li>
<li><strong>Think about the argumentation first.</strong> Don’t just start to write. Develop a skeletal outline, sketch out your argumentation, change, restructure, fight with it, and get feedback. Only if you are happy with this outline, start to write your paper (obviously, you don’t have to be follow this rule every time, it can of course make sense to simply start writing and develop ideas simultaneously).</li>
<li><strong>Prepare yourself for failure and frustrating review processes.</strong> Things will go wrong, but the most frustrating thing can be a seemingly unfair review process. Don’t be offended, see the merit in a reviewer’s comments, get over with the nasty bit, and move on. Simple as that (I have to admit I am not particularly good at this).</li>
</ol>
</section>
<section id="socializing-networking-and-becoming-part-of-the-community" class="level3">
<h3 class="anchored" data-anchor-id="socializing-networking-and-becoming-part-of-the-community">Socializing, Networking, and Becoming Part of the Community</h3>
<p>Although some people do a PhD and do not have the goal of staying in academia, I am assuming that you want to become and stay a researcher. In order to achieve that goal, you need to grow a network. I like to emphasize that I don’t refer to networking here in the classical sense. Don’t try to meet or befriend “great scholars” for the sake of gaining advantages. Rather, I want you to have a great community of scholars that help you out if you need support, that provide you with a lively exchange of information and ideas, and generally provide the informational, instrumental, and emotional support that every person – no matter what job – needs from time to time.<img src="https://philippmasur.de/blog/2022-04-03-make-most-of-your-phd/img/Picture4.png" class="img-fluid"></p>
<ol type="1">
<li><strong>Socialize with your lab and other colleagues, but don’t compare yourself with them.</strong> Don’t isolate yourself, but also don’t do anything that doesn’t feel right to you. Talk to colleagues, find friends, but don’t feel intimated by their success, CV, number of publications or whatever you might compare yourself on. Every career is different. Social comparison can be very detrimental and simply is not worth it.</li>
<li><strong>Meet and exchange ideas with other PhD students.</strong> Having lively conversations and exchanges with fellow PhD students is perhaps the most important recommendation. They will understand you and provide the social environment for your academic journey. Some of you will literally grow old together.</li>
<li><strong>Don’t struggle on your own.</strong> Don’t think that you need to figure everything out by yourself. Ask your supervisor and colleagues, be honest if you don’t know something. Don’t be shy to ask for help. A good mentor will help you tremendously.</li>
<li><strong>Present your research in your department and at conferences.</strong> It will take some courage in the beginning, and perhaps will be a little bit scary too, but be confident in your work and present it to colleagues and other scholars on conferences. Their feedback will be valuable and they will get to know you and what you are working on.</li>
<li><strong>Enjoy the ride together.</strong> Don’t compete, celebrate your achievements together, and have a good time. This is what this is all about.</li>
</ol>
</section>
<section id="work-life-balance-and-mental-health" class="level3">
<h3 class="anchored" data-anchor-id="work-life-balance-and-mental-health">Work-Life Balance and Mental Health</h3>
<p>Just as any job, being an academic scholar can be very rewarding, but also be frustrating and overwhelming. Taking care of yourself is as important as having the feeling that you advance and progress. As a final advice, I would like to remind you that none of the recommendations above are requirements to succeed. You don’t need to fulfil all or any of them. Don’t be too hard on you. In the noisy confusion of life, keep true to yourself and don’t do anything that you don’t want to do.</p>
<ol type="1">
<li><strong>Recognize your own brilliance</strong>. Many early (as well as later) career researchers suffer from imposter syndrome. They believe they may not be as competent as others perceive them to be. Don’t make yourself smaller than you are. Of course, you don’t know as much as a senior scholar, and of course you are not as proficient in R as an older, more experienced colleague, and of course you may not be as fast in writing a section of a paper than your supervisors who have done this for years. But that does not mean that you are less bright, less smart, and less qualified for this job. Be patient and trust in yourself and trust in others’ belief in you.</li>
<li><strong>Success doesn’t come overnight</strong>. Know that you will get there eventually. Things take time and you don’t need to be faster than everybody else. In other words, be patient and beyond a healthy amount of self-discipline, be gentle with yourself.</li>
<li><strong>Supervisors and other seniors can be and often are wrong.</strong> If you feel you are right, tell them so and make them understand why you see things differently. A comment makes no sense to you? Explain why you think so. If they request unrealistic amounts of work from you in a short period of time, tell them that this doesn’t work and that it doesn’t fit your planned schedule. They should and will have respect for your plans and schedules.</li>
<li><strong>Enjoy and acknowledge your achievements.</strong> Take time to celebrate your achievements. You got accepted to your first conference? You should be proud! You finally got your first paper published? Celebrate with your colleagues and friends. Every step on the way is important and worth acknowledging. Take a day off and enjoy the results of your hard work.</li>
<li><strong>If things get crazy, talk to your supervisor(s).</strong> In any job, there will come a time where things get a bit overwhelming and where you may not see the light at the end of the tunnel. Don’t be shy to tell your supervisor that you are struggling. They are there to help you out, to reduce your stress and make sure that you enjoy work again.</li>
</ol>
<div class="callout callout-style-simple callout-note no-icon callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>1 comment from the original post
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>These comments were left on the earlier WordPress version of this post.</p>
<div class="archived-comment">
<p><strong>Ting Xu</strong> · 27 September 2026</p>
<p>🙏 Well-written and reader-friendly. Thanks for putting together this motivating and useful article for PhD students.</p>
</div>
</div>
</div>
</div>


<template id="pm-footer-icons"><a href="#" data-pm-mail="p.k.masur|vu.nl" aria-label="Email" title="Email"><svg class="pm-icon" aria-hidden="true"><use href="#i-mail"></use></svg></a><a href="https://scholar.google.com/citations?user=XA_WthYAAAAJ" target="_blank" rel="noopener" aria-label="Google Scholar" title="Google Scholar"><svg class="pm-icon" aria-hidden="true"><use href="#i-scholar"></use></svg></a><a href="https://orcid.org/0000-0003-3065-7305" target="_blank" rel="noopener" aria-label="ORCID" title="ORCID"><svg class="pm-icon" aria-hidden="true"><use href="#i-orcid"></use></svg></a><a href="https://osf.io/yuqv5/" target="_blank" rel="noopener" aria-label="Open Science Framework" title="Open Science Framework"><svg class="pm-icon" aria-hidden="true"><use href="#i-osf"></use></svg></a><a href="https://github.com/masurp" target="_blank" rel="noopener" aria-label="GitHub" title="GitHub"><svg class="pm-icon" aria-hidden="true"><use href="#i-github"></use></svg></a><a href="https://bsky.app/profile/masurphil.bsky.social" target="_blank" rel="noopener" aria-label="Bluesky" title="Bluesky"><svg class="pm-icon" aria-hidden="true"><use href="#i-bluesky"></use></svg></a><a href="https://research.vu.nl/en/persons/philipp-k-masur" target="_blank" rel="noopener" aria-label="VU Amsterdam" title="VU Amsterdam"><svg class="pm-icon" aria-hidden="true"><use href="#i-landmark"></use></svg></a></template>
</section>

 ]]></description>
  <category>Academia</category>
  <guid>https://philippmasur.de/blog/2022-04-03-make-most-of-your-phd/</guid>
  <pubDate>Sat, 02 Apr 2022 22:00:00 GMT</pubDate>
  <media:content url="https://philippmasur.de/blog/2022-04-03-make-most-of-your-phd/featured.png" medium="image" type="image/png" height="96" width="144"/>
</item>
<item>
  <title>ICA Preconference on “Comparative Privacy and the Literacies of a Networked Age”</title>
  <link>https://philippmasur.de/blog/2022-01-18-ica-preconference-comparative-privacy/</link>
  <description><![CDATA[ 

<!-- Icon sprite: 24×24 line icons, 1.6 stroke. Every shape has pathLength="1"
     so CSS can animate stroke-dashoffset 1 → 0 ("draw" the icon).
     Use: <svg class="pm-icon"><use href="#i-mail"/></svg> -->





<p>The <a href="https://www.comparativeprivacy.org">comparative privacy research network</a>&nbsp;is organizing a preconference before the annual conference of the International Communication Association on <strong>May, 25th (9:30 to 17:00)</strong>.&nbsp;Drawing on previous and ongoing conversations and collaborations, this preconference aims to attend to privacy literacy’s critical comparative nature by bringing together scholars that examine the cultural, political, and otherwise contextualized aspects of privacy literacy.</p>
<p>The ultimate goal is to enhance conversation in communication studies about the ways in which systematic comparative cross-cultural analyses of privacy literacy may be conceptualized, theorized, and operationalized in novel ways. This preconference will be organized in two parts: First, keynotes will provide inputs on the central issues and concepts involved such as privacy, comparative research, and media literacy. Secondly, interactive sessions will focus on three main aspects of comparative privacy literacy research, namely: conceptualization, operationalization, and collaboration. These sessions will bring together competitively selected presentations followed by a discussion on the challenges of conceptualizing and operationalizing critical privacy literacy from a cross-cultural perspective. The presentation sessions will be followed by group activities where participants will discuss solutions to particular challenges. This final session will take the form of a guided discussion in the larger group that will build on the diversity of the group in order to consider new, future-oriented research questions and forge future collaborations.</p>
<p>**We invited you to submit extended abstracts (~700 words) to participate in this preconference!**</p>
<p>**DEADLINE EXTENDED: 28th of February 2022!**<br>
For more information and a link to the call of paper can be find <a href="https://comparativeprivacy.org/2022-ica-preconference/">here</a>.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<blockquote class="blockquote">
<p>?<a href="https://twitter.com/hashtag/ICA22?src=hash&amp;ref_src=twsrc%5Etfw">#ICA22</a> pre-conference in Mulhouse!?<a href="https://twitter.com/Think_Macro?ref_src=twsrc%5Etfw">@Think_Macro</a>, <a href="https://twitter.com/_kquinn_?ref_src=twsrc%5Etfw">@_kquinn_</a>, <a href="https://twitter.com/charlyjr?ref_src=twsrc%5Etfw">@charlyjr</a>, <a href="https://twitter.com/lutzid?ref_src=twsrc%5Etfw">@lutzid</a>, <a href="https://twitter.com/lbaruh?ref_src=twsrc%5Etfw">@lbaruh</a>, and I are organizing a pre-conference on comparative research of privacy and associated literacies. More info &amp; details under the link: <a href="https://t.co/071YCPoeyR" class="uri">https://t.co/071YCPoeyR</a> <a href="https://t.co/slsC1zeH1F">pic.twitter.com/slsC1zeH1F</a></p>
<p>— Philipp Masur (<span class="citation" data-cites="MasurPhil">@MasurPhil</span>) <a href="https://twitter.com/MasurPhil/status/1464144217450635267?ref_src=twsrc%5Etfw">November 26, 2021</a></p>
</blockquote>


<template id="pm-footer-icons"><a href="#" data-pm-mail="p.k.masur|vu.nl" aria-label="Email" title="Email"><svg class="pm-icon" aria-hidden="true"><use href="#i-mail"></use></svg></a><a href="https://scholar.google.com/citations?user=XA_WthYAAAAJ" target="_blank" rel="noopener" aria-label="Google Scholar" title="Google Scholar"><svg class="pm-icon" aria-hidden="true"><use href="#i-scholar"></use></svg></a><a href="https://orcid.org/0000-0003-3065-7305" target="_blank" rel="noopener" aria-label="ORCID" title="ORCID"><svg class="pm-icon" aria-hidden="true"><use href="#i-orcid"></use></svg></a><a href="https://osf.io/yuqv5/" target="_blank" rel="noopener" aria-label="Open Science Framework" title="Open Science Framework"><svg class="pm-icon" aria-hidden="true"><use href="#i-osf"></use></svg></a><a href="https://github.com/masurp" target="_blank" rel="noopener" aria-label="GitHub" title="GitHub"><svg class="pm-icon" aria-hidden="true"><use href="#i-github"></use></svg></a><a href="https://bsky.app/profile/masurphil.bsky.social" target="_blank" rel="noopener" aria-label="Bluesky" title="Bluesky"><svg class="pm-icon" aria-hidden="true"><use href="#i-bluesky"></use></svg></a><a href="https://research.vu.nl/en/persons/philipp-k-masur" target="_blank" rel="noopener" aria-label="VU Amsterdam" title="VU Amsterdam"><svg class="pm-icon" aria-hidden="true"><use href="#i-landmark"></use></svg></a></template>

 ]]></description>
  <category>News</category>
  <category>Privacy</category>
  <guid>https://philippmasur.de/blog/2022-01-18-ica-preconference-comparative-privacy/</guid>
  <pubDate>Mon, 17 Jan 2022 23:00:00 GMT</pubDate>
  <media:content url="https://philippmasur.de/blog/2022-01-18-ica-preconference-comparative-privacy/featured.png" medium="image" type="image/png" height="104" width="144"/>
</item>
<item>
  <title>AoIR Satellite Event on Comparing Fuzzy Things</title>
  <link>https://philippmasur.de/blog/2021-09-30-aoir-comparing-fuzzy-things/</link>
  <description><![CDATA[ 

<!-- Icon sprite: 24×24 line icons, 1.6 stroke. Every shape has pathLength="1"
     so CSS can animate stroke-dashoffset 1 → 0 ("draw" the icon).
     Use: <svg class="pm-icon"><use href="#i-mail"/></svg> -->





<p>The&nbsp;<a href="https://comparativeprivacy.org/">Comparative Privacy Research Network</a>&nbsp;is organizing a workshop on issues related to comparing fuzzy concepts like love, trust, and privacy across various structural settings (including, but not limited to cultures). Below is the description of the workshop from the CPRN website:</p>
<blockquote class="blockquote">
<p>Internet researchers often engage in the study of complex, multidimensional, and culturally sensitive ideas. Deploying such concepts in comparative research settings is critically important to knowledge advancement, yet challenging to implement in practice. This workshop is designed to engage members of the AoIR community in exploring the conceptualization and study of fuzzy concepts, such as trust, love, sharing, and happiness, in a comparative fashion. It will provide an opportunity to exchange ideas about how such comparative work can be conducted across disciplines. The session will include brief framing presentations on a comparative research framework, with examples of how this might work when studying privacy. Breakout group sessions will augment these sessions and allow participants to explore the applicability of this framework to other, fuzzy concepts using a variety of methodological approaches.</p>
</blockquote>
<p>The workshop will take place online on the <strong>5th of October 2021</strong> at <strong>13.00 – 16.00 (UTC)</strong>.</p>
<p>If interested, you can register for the workshop by clicking&nbsp;<a href="https://docs.google.com/forms/d/e/1FAIpQLSeUBSVH1iLhunxoe-jc_M_RmPYqIeAk0SttFKq3fvXmad0I5Q/viewform?usp=sf_link">here</a>.</p>


<template id="pm-footer-icons"><a href="#" data-pm-mail="p.k.masur|vu.nl" aria-label="Email" title="Email"><svg class="pm-icon" aria-hidden="true"><use href="#i-mail"></use></svg></a><a href="https://scholar.google.com/citations?user=XA_WthYAAAAJ" target="_blank" rel="noopener" aria-label="Google Scholar" title="Google Scholar"><svg class="pm-icon" aria-hidden="true"><use href="#i-scholar"></use></svg></a><a href="https://orcid.org/0000-0003-3065-7305" target="_blank" rel="noopener" aria-label="ORCID" title="ORCID"><svg class="pm-icon" aria-hidden="true"><use href="#i-orcid"></use></svg></a><a href="https://osf.io/yuqv5/" target="_blank" rel="noopener" aria-label="Open Science Framework" title="Open Science Framework"><svg class="pm-icon" aria-hidden="true"><use href="#i-osf"></use></svg></a><a href="https://github.com/masurp" target="_blank" rel="noopener" aria-label="GitHub" title="GitHub"><svg class="pm-icon" aria-hidden="true"><use href="#i-github"></use></svg></a><a href="https://bsky.app/profile/masurphil.bsky.social" target="_blank" rel="noopener" aria-label="Bluesky" title="Bluesky"><svg class="pm-icon" aria-hidden="true"><use href="#i-bluesky"></use></svg></a><a href="https://research.vu.nl/en/persons/philipp-k-masur" target="_blank" rel="noopener" aria-label="VU Amsterdam" title="VU Amsterdam"><svg class="pm-icon" aria-hidden="true"><use href="#i-landmark"></use></svg></a></template>

 ]]></description>
  <category>News</category>
  <category>Privacy</category>
  <guid>https://philippmasur.de/blog/2021-09-30-aoir-comparing-fuzzy-things/</guid>
  <pubDate>Wed, 29 Sep 2021 22:00:00 GMT</pubDate>
  <media:content url="https://philippmasur.de/blog/2021-09-30-aoir-comparing-fuzzy-things/featured.png" medium="image" type="image/png" height="72" width="144"/>
</item>
<item>
  <title>Communication journals that adopted open science principles</title>
  <link>https://philippmasur.de/blog/2021-06-04-journals-open-science/</link>
  <description><![CDATA[ 

<!-- Icon sprite: 24×24 line icons, 1.6 stroke. Every shape has pathLength="1"
     so CSS can animate stroke-dashoffset 1 → 0 ("draw" the icon).
     Use: <svg class="pm-icon"><use href="#i-mail"/></svg> -->





<p>With more and more communication scholars adopting open science principles (e.g., preregistration, sharing of data, material, and code), also more and more media and communication journals adopt open science features and take first steps in adopting the <a href="https://www.cos.io/initiatives/top-guidelines">TOP guidelines</a>. I just quickly would like to point your attention to a very useful resource in this regard.&nbsp;<a href="https://moritzbuchi.com/">Moritz Büchi</a> and <a href="https://tobiasdienlin.com/">Tobias Dienlin</a> started a list with peer-reviewed journals that a) focus on media and communication generally or any subdiscipline (e.g., science communication, media psychology…) and b) implemented some form of open science-related feature (e.g., registered report format, open peer review, open science badges…). When planning your next study, why not consider submitting it as a registered report? Or explicitly targeting a journal that honors open data and materials? This overview can help you find the right journal. You can access the latest version of the list <a href="https://docs.google.com/spreadsheets/d/1WDTBIop2Eg9CKsvQ1BUgMXJdmrh1TAreyS02SiWEJDY/edit#gid=0">here</a> (a static version is also available <a href="https://moritzbuchi.files.wordpress.com/2020/10/openmediaandcommunicationresearchjournallistv1.pdf">here</a>). Hopefully, the number of journals adopting TOP guidelines will increase in the future. If a journal is missing or you have suggestions for updates, you can also leave comments in the list.</p>
<p>Thanks to Moritz and Tobias for creating this helpful resource!</p>
<p><a href="https://docs.google.com/spreadsheets/d/1WDTBIop2Eg9CKsvQ1BUgMXJdmrh1TAreyS02SiWEJDY/edit#gid=0"><img src="https://philippmasur.de/blog/2021-06-04-journals-open-science/img/Screenshot-2021-06-04-at-12.26.37.png" class="img-fluid"></a></p>


<template id="pm-footer-icons"><a href="#" data-pm-mail="p.k.masur|vu.nl" aria-label="Email" title="Email"><svg class="pm-icon" aria-hidden="true"><use href="#i-mail"></use></svg></a><a href="https://scholar.google.com/citations?user=XA_WthYAAAAJ" target="_blank" rel="noopener" aria-label="Google Scholar" title="Google Scholar"><svg class="pm-icon" aria-hidden="true"><use href="#i-scholar"></use></svg></a><a href="https://orcid.org/0000-0003-3065-7305" target="_blank" rel="noopener" aria-label="ORCID" title="ORCID"><svg class="pm-icon" aria-hidden="true"><use href="#i-orcid"></use></svg></a><a href="https://osf.io/yuqv5/" target="_blank" rel="noopener" aria-label="Open Science Framework" title="Open Science Framework"><svg class="pm-icon" aria-hidden="true"><use href="#i-osf"></use></svg></a><a href="https://github.com/masurp" target="_blank" rel="noopener" aria-label="GitHub" title="GitHub"><svg class="pm-icon" aria-hidden="true"><use href="#i-github"></use></svg></a><a href="https://bsky.app/profile/masurphil.bsky.social" target="_blank" rel="noopener" aria-label="Bluesky" title="Bluesky"><svg class="pm-icon" aria-hidden="true"><use href="#i-bluesky"></use></svg></a><a href="https://research.vu.nl/en/persons/philipp-k-masur" target="_blank" rel="noopener" aria-label="VU Amsterdam" title="VU Amsterdam"><svg class="pm-icon" aria-hidden="true"><use href="#i-landmark"></use></svg></a></template>

 ]]></description>
  <category>Open Science</category>
  <category>Communication</category>
  <guid>https://philippmasur.de/blog/2021-06-04-journals-open-science/</guid>
  <pubDate>Thu, 03 Jun 2021 22:00:00 GMT</pubDate>
  <media:content url="https://philippmasur.de/blog/2021-06-04-journals-open-science/featured.png" medium="image" type="image/png" height="80" width="144"/>
</item>
<item>
  <title>I’m joining the Department of Communication Science at the VU Amsterdam!</title>
  <link>https://philippmasur.de/blog/2020-08-09-joining-vu-amsterdam/</link>
  <description><![CDATA[ 

<!-- Icon sprite: 24×24 line icons, 1.6 stroke. Every shape has pathLength="1"
     so CSS can animate stroke-dashoffset 1 → 0 ("draw" the icon).
     Use: <svg class="pm-icon"><use href="#i-mail"/></svg> -->





<p><a href="img/nick-scheerbart-VID8rLzqRxs-unsplash-1024x683.jpg" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://philippmasur.de/blog/2020-08-09-joining-vu-amsterdam/img/nick-scheerbart-VID8rLzqRxs-unsplash-1024x683.jpg" class="img-fluid"></a></p>
<p>I am excited to share that I accepted a position as <strong>assistant professor</strong> at the Vrije Universiteit (VU) Amsterdam in the <a href="https://fsw.vu.nl/en/departments/communication-science/">Department of Communication Science</a>. I am thrilled to become a member of this great department and I am looking forward to work and learn together with an amazing group of scholars. At the VU, I will have colleagues whose work is characterized by the use of advanced and computational research methods and an extraordinary openness to interdisciplinary collaboration. I couldn’t be more excited about learning and advancing my academic career in this environment.</p>
<p>So starting on the&nbsp;<strong>1st September 2020</strong>, I will be teaching and researching in the field of persuasive communication with a particular focus on understanding persuasion processes on social media. Primarily, I will investigate how affordances, social norms, and interpersonal processes interact in shaping attitude formation and behavior.</p>
<p>As you might imagine, we are equally excited about moving to the beautiful city of Amsterdam. Although we will be missing Mainz and cherish every remaining moment here, we are looking forward to new adventures and opportunities in the Netherlands. Tot ziens in Amsterdam!</p>
<section id="on-a-personal-note" class="level3">
<h3 class="anchored" data-anchor-id="on-a-personal-note">On a personal note…</h3>
<p>I am immensely grateful to all the people who supported me over the last years and who helped me to get to this point in my academic career. These include teachers, mentors, colleagues, and friends in Mainz, Stuttgart, and Ithaca. I am particularly thankful to those people whose advice, mentorship, and friendship have helped me to become the researcher I am today and who helped me in planing and reflecting on my future as a communication scholar – especially in the last months:</p>
<ul>
<li>My parents who are and remain my most trusted mentors and advisers.</li>
<li>Sabine Trepte, Christian Schemer, and Michael Scharkow whose guidance, advice, and mentorship had a profound impact on my research as well as my future plans and who helped me think clearly about what it means to be a communication scholar.</li>
<li>Tobias Dienlin, Stefan Winter, German Neubaum who are not just great scholars, but true friends. Their support means all to me.</li>
<li>Natalie Bazarova who not only supported my visit to Cornell, but continues to inspire and support me. I am very grateful that we have met and I am looking forward to many more collaborations in the future.</li>
<li>Janis Whitlock who teaches me important life lessons in every of our conversations. I hope that our paths will cross often in the next years.</li>
<li>Samuel Taylor whose friendship and collegiality means a lot to me.</li>
<li>Dima Epstein and all my colleagues from the Comparative Privacy Research Network who taught me to be patient while remaining immensely supportive.</li>
<li>I also want to thank all my colleagues in Hohenheim, Ithaca, and Mainz for making my day-to-day job an inspiring and fun endeavor.</li>
<li>Maria, for her unconditional support and love.</li>
</ul>


<template id="pm-footer-icons"><a href="#" data-pm-mail="p.k.masur|vu.nl" aria-label="Email" title="Email"><svg class="pm-icon" aria-hidden="true"><use href="#i-mail"></use></svg></a><a href="https://scholar.google.com/citations?user=XA_WthYAAAAJ" target="_blank" rel="noopener" aria-label="Google Scholar" title="Google Scholar"><svg class="pm-icon" aria-hidden="true"><use href="#i-scholar"></use></svg></a><a href="https://orcid.org/0000-0003-3065-7305" target="_blank" rel="noopener" aria-label="ORCID" title="ORCID"><svg class="pm-icon" aria-hidden="true"><use href="#i-orcid"></use></svg></a><a href="https://osf.io/yuqv5/" target="_blank" rel="noopener" aria-label="Open Science Framework" title="Open Science Framework"><svg class="pm-icon" aria-hidden="true"><use href="#i-osf"></use></svg></a><a href="https://github.com/masurp" target="_blank" rel="noopener" aria-label="GitHub" title="GitHub"><svg class="pm-icon" aria-hidden="true"><use href="#i-github"></use></svg></a><a href="https://bsky.app/profile/masurphil.bsky.social" target="_blank" rel="noopener" aria-label="Bluesky" title="Bluesky"><svg class="pm-icon" aria-hidden="true"><use href="#i-bluesky"></use></svg></a><a href="https://research.vu.nl/en/persons/philipp-k-masur" target="_blank" rel="noopener" aria-label="VU Amsterdam" title="VU Amsterdam"><svg class="pm-icon" aria-hidden="true"><use href="#i-landmark"></use></svg></a></template>
</section>

 ]]></description>
  <category>News</category>
  <guid>https://philippmasur.de/blog/2020-08-09-joining-vu-amsterdam/</guid>
  <pubDate>Sat, 08 Aug 2020 22:00:00 GMT</pubDate>
  <media:content url="https://philippmasur.de/blog/2020-08-09-joining-vu-amsterdam/featured.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>New Publication: Can online privacy literacy support informational self-determination?</title>
  <link>https://philippmasur.de/blog/2020-07-01-online-privacy-literacy-self-determination/</link>
  <description><![CDATA[ 

<!-- Icon sprite: 24×24 line icons, 1.6 stroke. Every shape has pathLength="1"
     so CSS can animate stroke-dashoffset 1 → 0 ("draw" the icon).
     Use: <svg class="pm-icon"><use href="#i-mail"/></svg> -->





<p>Current debates on online privacy are often rooted in liberal theory. Privacy is hence often understood as a form of freedom from social, economic, and institutional influences.&nbsp;Such a negative perspective on privacy, however, focuses too much on how individuals can be protected or can protect themselves instead of challenging the necessity for protection itself. Similar to treating symptoms of a disease instead of its causes, providing protection fails to acknowledge that the necessity for such protection is a consequence of the social power relations that led to privacy intrusions in the first place.</p>
<p>A while back, I already wrote about my research on privacy literacy (→ see <a href="../../blog/2019-03-28-reconceptualizing-online-privacy-literacy/">this post</a>). In a new article, published in&nbsp;<a href="https://www.cogitatiopress.com/mediaandcommunication/article/view/2855">Media and Communication</a>,&nbsp;I further develop these ideas and argue that a comprehensive and multi-dimensional form of online privacy literacy may not only empower individuals to achieve (a necessarily limited) form of negative privacy, but might also have the potential to make individuals agents of social change and thereby create conditions of positive privacy in democratic societies. Such a comprehensive concept of online privacy literacy needs to encompass a) factual privacy knowledge, b) privacy-related reflection abilities, c) privacy and data protection skills, and d) critical privacy literacy (see Figure 1).</p>
<p>&nbsp;</p>
<p><img src="https://philippmasur.de/blog/2020-07-01-online-privacy-literacy-self-determination/img/EbM-KreXQAAactV-1024x586.jpeg" class="img-fluid"><em>Figure 1:</em> A comprehensive model of online privacy literacy.</p>
<p>&nbsp;</p>
<p>The article is part of a <a href="https://www.cogitatiopress.com/mediaandcommunication/issue/view/162">special issue</a> on “The Politics of Privacy” edited by&nbsp;Johanna E. Möller, Jakub Nowak, Sigrid Kannengießer, and Judith E. Möller.&nbsp;The diverse contributions focus on theoretical challenges of contemporary communication and media privacy research as well as on structural privacy conditions and people’s mundane communicative practices underlining inherent political aspects. They highlight how particular acts of doing privacy are grounded in citizen agency realized in datafied environments.</p>
<p>All articles are open access and can be downloaded <a href="https://www.cogitatiopress.com/mediaandcommunication/issue/view/162">here</a>.</p>


<template id="pm-footer-icons"><a href="#" data-pm-mail="p.k.masur|vu.nl" aria-label="Email" title="Email"><svg class="pm-icon" aria-hidden="true"><use href="#i-mail"></use></svg></a><a href="https://scholar.google.com/citations?user=XA_WthYAAAAJ" target="_blank" rel="noopener" aria-label="Google Scholar" title="Google Scholar"><svg class="pm-icon" aria-hidden="true"><use href="#i-scholar"></use></svg></a><a href="https://orcid.org/0000-0003-3065-7305" target="_blank" rel="noopener" aria-label="ORCID" title="ORCID"><svg class="pm-icon" aria-hidden="true"><use href="#i-orcid"></use></svg></a><a href="https://osf.io/yuqv5/" target="_blank" rel="noopener" aria-label="Open Science Framework" title="Open Science Framework"><svg class="pm-icon" aria-hidden="true"><use href="#i-osf"></use></svg></a><a href="https://github.com/masurp" target="_blank" rel="noopener" aria-label="GitHub" title="GitHub"><svg class="pm-icon" aria-hidden="true"><use href="#i-github"></use></svg></a><a href="https://bsky.app/profile/masurphil.bsky.social" target="_blank" rel="noopener" aria-label="Bluesky" title="Bluesky"><svg class="pm-icon" aria-hidden="true"><use href="#i-bluesky"></use></svg></a><a href="https://research.vu.nl/en/persons/philipp-k-masur" target="_blank" rel="noopener" aria-label="VU Amsterdam" title="VU Amsterdam"><svg class="pm-icon" aria-hidden="true"><use href="#i-landmark"></use></svg></a></template>

 ]]></description>
  <category>Privacy</category>
  <category>Communication</category>
  <guid>https://philippmasur.de/blog/2020-07-01-online-privacy-literacy-self-determination/</guid>
  <pubDate>Tue, 30 Jun 2020 22:00:00 GMT</pubDate>
  <media:content url="https://philippmasur.de/blog/2020-07-01-online-privacy-literacy-self-determination/featured.jpeg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>New Publication: An Agenda for Open Science in Communication</title>
  <link>https://philippmasur.de/blog/2020-04-14-agenda-open-science/</link>
  <description><![CDATA[ 

<!-- Icon sprite: 24×24 line icons, 1.6 stroke. Every shape has pathLength="1"
     so CSS can animate stroke-dashoffset 1 → 0 ("draw" the icon).
     Use: <svg class="pm-icon"><use href="#i-mail"/></svg> -->





<p>In the last 10 years, many canonical findings in the social sciences appear unreliable. This so-called “replication crisis” has spurred calls for open science practices, which aim to increase the reproducibility, replicability, and generalizability of findings. Communication research is subject to many of the same challenges that have caused low replicability in other fields. As a result, I recently wrote a paper with more than 30 authors in which we propose an agenda for adopting open science practices in Communication. After a discussion of the biggest problems (including low powered studies and various questionable research practices), we make the following seven suggestions:</p>
<ol type="1">
<li>publish materials, data, and code</li>
<li>preregister studies and submit registered reports</li>
<li>conduct replications</li>
<li>collaborate</li>
<li>foster open science skills</li>
<li>implement Transparency and Openness Promotion Guidelines</li>
<li>incentivize open science practices</li>
</ol>
<p>The article was published in the Journal of Communication. It is open access and can thus be downloaded <a href="https://academic.oup.com/joc/article/doi/10.1093/joc/jqz052/5803422#200712879">here</a>.&nbsp;We hope that this agenda will spark a constructive conversation in our field and that scholars will explore even more options and benefits of implementing open science practices in various subfields. On the one hand, we want to invite you to sign and endorse the agenda (which you can do <a href="https://docs.google.com/forms/d/e/1FAIpQLSfqf-PjIH1ymNc3YHqskVHfSrem9fLaDgDse-An4GkdsUFQDA/viewform">here</a>). On the other hand, the Journal of Communication is planning a special issue on open science. If you are interested, please find the call for papers <a href="https://academic.oup.com/joc/pages/open-comm-cfp">here</a>.</p>


<template id="pm-footer-icons"><a href="#" data-pm-mail="p.k.masur|vu.nl" aria-label="Email" title="Email"><svg class="pm-icon" aria-hidden="true"><use href="#i-mail"></use></svg></a><a href="https://scholar.google.com/citations?user=XA_WthYAAAAJ" target="_blank" rel="noopener" aria-label="Google Scholar" title="Google Scholar"><svg class="pm-icon" aria-hidden="true"><use href="#i-scholar"></use></svg></a><a href="https://orcid.org/0000-0003-3065-7305" target="_blank" rel="noopener" aria-label="ORCID" title="ORCID"><svg class="pm-icon" aria-hidden="true"><use href="#i-orcid"></use></svg></a><a href="https://osf.io/yuqv5/" target="_blank" rel="noopener" aria-label="Open Science Framework" title="Open Science Framework"><svg class="pm-icon" aria-hidden="true"><use href="#i-osf"></use></svg></a><a href="https://github.com/masurp" target="_blank" rel="noopener" aria-label="GitHub" title="GitHub"><svg class="pm-icon" aria-hidden="true"><use href="#i-github"></use></svg></a><a href="https://bsky.app/profile/masurphil.bsky.social" target="_blank" rel="noopener" aria-label="Bluesky" title="Bluesky"><svg class="pm-icon" aria-hidden="true"><use href="#i-bluesky"></use></svg></a><a href="https://research.vu.nl/en/persons/philipp-k-masur" target="_blank" rel="noopener" aria-label="VU Amsterdam" title="VU Amsterdam"><svg class="pm-icon" aria-hidden="true"><use href="#i-landmark"></use></svg></a></template>

 ]]></description>
  <category>Open Science</category>
  <category>Communication</category>
  <guid>https://philippmasur.de/blog/2020-04-14-agenda-open-science/</guid>
  <pubDate>Mon, 13 Apr 2020 22:00:00 GMT</pubDate>
  <media:content url="https://philippmasur.de/blog/2020-04-14-agenda-open-science/featured.png" medium="image" type="image/png" height="76" width="144"/>
</item>
<item>
  <title>The problem of false positives: Antibody tests in times of Corona</title>
  <link>https://philippmasur.de/blog/2020-04-14-false-positives-antibody-tests/</link>
  <description><![CDATA[ 

<!-- Icon sprite: 24×24 line icons, 1.6 stroke. Every shape has pathLength="1"
     so CSS can animate stroke-dashoffset 1 → 0 ("draw" the icon).
     Use: <svg class="pm-icon"><use href="#i-mail"/></svg> -->





<p>A few weeks or months from now, we could have a Covid-19 test kit sent to our home. Similar to a blood sugar test for diabetics, we would prick our finger, wait for a couple of minutes, and we will know whether we are immune or not. The general idea is that this would help in lessen the social distancing restrictions because those who are immune could in principle go back to a normal life.</p>
<p>But it is of course more complicated than that. In short: If an antibody test tells you that you are “positive” (i.e., you have developed antibodies and therefore <em>are</em> or should <em>have been</em> infected), the probability that you are actually immune is nonetheless very low because your test result is very likely a “false positive”.</p>
<p>Yesterday, I stumbled upon the following Twitter thread, which pinpoints at an important issue when it comes to antibody tests that should help to determine who was infected and who was not.</p>
<blockquote class="blockquote">
<p>Antibody tests are coming online. Never before have humans needed to understand Bayes rule more. Let’s talk about why it’s critical NOT to assume you are immune to covid-19 when you have a positive antibody test. Seriously, people need to understand this to prevent many deaths.</p>
<p>— Taal Levi (<span class="citation" data-cites="taaltree">@taaltree</span>) <a href="https://twitter.com/taaltree/status/1248467731545911296?ref_src=twsrc%5Etfw">April 10, 2020</a></p>
</blockquote>
<p>Intuitively, most of us would probably think that the accuracy of a (antibody) test is primarily related to its precision. A test’s precision is denoted by its sensitivity (probability that a person tests positive given that they are indeed positive) and its specificity (probability that a person tests negative given that they are indeed negative). Cellex, which is the first company to get a rapid covid-19 antibody test approved by the FDA, has a sensitivity of 93.8% and a specificity of 95.6%.<sup>1</sup> This sounds pretty good. Only 6.2% would be “false negatives” and only 4.4% would be “false positives”. But this oversimplifies the situation.</p>
<p>The problem is related to the known <em>low</em> probability of being or having been infected in the first place. From all (of course, biased) numbers that we know, the amount of people infected in a country is very low (e.g., in Germany, it would be 0.15% of the entire population at this moment). Even if unknown cases are ten times higher (e.g., cases without symptoms), we would still only have an infection rate of 1.5%. In Bayesian terms, this implies that our prior knowledge suggests that the probability of being “positive” is <em>very low</em>. Using Bayes theorem (which I won’t explain here), we can derive that it is very unlikely to be infected, <em>even</em> if the antibody test says that one is “positive”.<sup>2</sup></p>
<p>Before we simulate it in R, try it yourself: below are 1,000 people who take the test. Change how common the infection is and how good the test is, and watch who ends up with a positive result.</p>
<figure class="pm-explainer figure"><iframe src="../../explainers/base-rates/index.html" title="Interactive explainer: base-rates" loading="lazy" style="height:760px"></iframe><figcaption> Interactive: 1,000 people take an antibody test. Runs entirely in your browser.</figcaption></figure>
<p>Because this is a bit hard to grasp intuitively, let us simulate the problem. I therefore first define a function that:</p>
<p>1) creates a population with a certain percentage of infected people,<br>
2) samples one person randomly, and<br>
3) and conducts a test with a pre-defined sensitivity and specificity.</p>
<div class="cell" data-layout-align="center">
<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>(tidyverse)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for data wrangling</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(MonteCarlo)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for the simulation</span></span>
<span id="cb1-3"></span>
<span id="cb1-4">test_sim <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>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rate =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.001</span>, </span>
<span id="cb1-5">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pop =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100000</span>, </span>
<span id="cb1-6">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sensitivity =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</span>, </span>
<span id="cb1-7">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">specificity =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</span>) {</span>
<span id="cb1-8"></span>
<span id="cb1-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create population with an infected subpopulation</span></span>
<span id="cb1-10">  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;">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>pop, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">corona =</span> <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;">rep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, pop<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>rate), <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>, pop<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>rate<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>pop)))</span>
<span id="cb1-11"></span>
<span id="cb1-12">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sample individual person randomly</span></span>
<span id="cb1-13">  person <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_n</span>(data, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-14"></span>
<span id="cb1-15">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "test" the person</span></span>
<span id="cb1-16">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(person<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>corona <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="cb1-17"></span>
<span id="cb1-18">    prob_sens <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"correct"</span>, (sensitivity<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="cb1-19">                     <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"false"</span>, (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>sensitivity<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 class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-20"></span>
<span id="cb1-21">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(prob_sens <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;">"correct"</span>) {</span>
<span id="cb1-22">      person<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>test_result <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="cb1-23">    } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb1-24">      person<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>test_result <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="cb1-25">    }</span>
<span id="cb1-26"></span>
<span id="cb1-27">  } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb1-28"></span>
<span id="cb1-29">    prob_spec <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"correct"</span>, (specificity<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="cb1-30">                     <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"false"</span>, (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>specificity<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 class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-31"></span>
<span id="cb1-32">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span>(prob_spec <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;">"correct"</span>) {</span>
<span id="cb1-33">      person<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>test_result <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="cb1-34">    } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb1-35">      person<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>test_result <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="cb1-36">    }</span>
<span id="cb1-37">  }</span>
<span id="cb1-38"></span>
<span id="cb1-39">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># return "true" infection and test result</span></span>
<span id="cb1-40">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"infected"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> person<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>corona,</span>
<span id="cb1-41">              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"test"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> person<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>test_result))</span>
<span id="cb1-42">}</span></code></pre></div></div>
</div>
<p>We can shortly test the function and see that in this one instance, 1 person of the 100000 was tested correctly as <em>negative</em> (0 = <em>negative</em>).</p>
<div class="cell" data-layout-align="center">
<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;">42</span>)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">test_sim</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; $infected
#&gt; [1] 0
#&gt; 
#&gt; $test
#&gt; [1] 0</code></pre>
</div>
</div>
<p>Next, we set up the simulation. In the simulation, we will vary the sensitivity and specificity of the test as well as the amount of infected people in the population.</p>
<div class="cell" data-layout-align="center">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define simulation parameters</span></span>
<span id="cb4-2">rate_grid <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.001</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.010</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.050</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.100</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.200</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rate of infection </span></span>
<span id="cb4-3">pop_grid <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;">100000</span>)                             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Population size    </span></span>
<span id="cb4-4">sens_grid <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;">85</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">99</span>)                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sensitivity of the test</span></span>
<span id="cb4-5">spec_grid <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;">85</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">99</span>)                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Specificity of the test</span></span>
<span id="cb4-6"></span>
<span id="cb4-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Collect simulation parameters in list</span></span>
<span id="cb4-8">(param_list <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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rate"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> rate_grid, </span>
<span id="cb4-9">                   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pop"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> pop_grid,</span>
<span id="cb4-10">                   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sensitivity"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> sens_grid,</span>
<span id="cb4-11">                   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"specificity"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> spec_grid))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; $rate
#&gt; [1] 0.001 0.010 0.050 0.100 0.200
#&gt; 
#&gt; $pop
#&gt; [1] 1e+05
#&gt; 
#&gt; $sensitivity
#&gt; [1] 0.85 0.90 0.95 0.99
#&gt; 
#&gt; $specificity
#&gt; [1] 0.85 0.90 0.95 0.99</code></pre>
</div>
</div>
<p>In order to test how these parameters affect the false positive (and false negative) rate, we run 1,000 tests on each combination.</p>
<div class="cell" data-layout-align="center">
<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;"># Run simulation</span></span>
<span id="cb6-2">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;">MonteCarlo</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">func =</span> test_sim,            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pass test function</span></span>
<span id="cb6-3">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrep =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>,                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># number of tests</span></span>
<span id="cb6-4">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">param_list =</span> param_list)    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># provide parameters</span></span>
<span id="cb6-5"></span>
<span id="cb6-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create result data frame</span></span>
<span id="cb6-7">df <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;">MakeFrame</span>(result) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> as_tibble <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-8">  <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;">test_result =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(infected <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> test <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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"correct negative"</span>,</span>
<span id="cb6-9">                              <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(infected <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;">&amp;</span> test <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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"correct positive"</span>,</span>
<span id="cb6-10">                                     <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(infected <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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> test <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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"false positive"</span>, </span>
<span id="cb6-11">                                            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(infected <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;">&amp;</span> test <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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"false negative"</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>)))))</span></code></pre></div></div>
</div>
<div class="cell" data-layout-align="center">
<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;">head</span>(df)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; # A tibble: 6 × 7
#&gt;    rate    pop sensitivity specificity infected  test test_result     
#&gt;   &lt;dbl&gt;  &lt;dbl&gt;       &lt;dbl&gt;       &lt;dbl&gt;    &lt;dbl&gt; &lt;dbl&gt; &lt;chr&gt;           
#&gt; 1 0.001 100000        0.85        0.85        0     0 correct negative
#&gt; 2 0.01  100000        0.85        0.85        0     0 correct negative
#&gt; 3 0.05  100000        0.85        0.85        0     1 false positive  
#&gt; 4 0.1   100000        0.85        0.85        0     0 correct negative
#&gt; 5 0.2   100000        0.85        0.85        0     0 correct negative
#&gt; 6 0.001 100000        0.9         0.85        0     0 correct negative</code></pre>
</div>
</div>
<p>The resulting data frame now includes 1,000 tests per combination and a column that indicates whether the test was a false positive, false negative, correct positive or correct negative. We can see that the third case is already a false positive.<br>
The following figure visualizes the results (click on image to enlarge!). I deliberately excluded all “correct negative” results as they make up the majority of the test results.</p>
<div class="cell" data-layout-align="center">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># plot</span></span>
<span id="cb9-2">df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(test_result <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;">"correct negative"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(rate, sensitivity, specificity) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"correct positive"</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;">length</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(test_result <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;">"correct positive"</span>)),</span>
<span id="cb9-6">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"false negative"</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;">length</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(test_result <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;">"false negative"</span>)),</span>
<span id="cb9-7">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"false positive"</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;">length</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(test_result <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;">"false positive"</span>))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gather</span>(key, value, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>rate, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>sensitivity, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>specificity) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-9">  <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;">value =</span> value<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;">1000</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span>     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># share of the 1,000 simulated tests per condition</span></span>
<span id="cb9-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</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;">x =</span> specificity, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> key)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-12">  <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>
<span id="cb9-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dark2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_grid</span>(rate <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> sensitivity, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labeller =</span> label_both) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>percent) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"share of all tests"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"test result"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-6-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://philippmasur.de/blog/2020-04-14-false-positives-antibody-tests/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Grid of line plots with infection rate in rows and test sensitivity in columns, showing the share of correct positive, false negative and false positive results across test specificity. False positives fall as specificity rises and outnumber correct positives when the infection rate is low."></a></p>
</figure>
</div>
</div>
</div>
<p>We can clearly see that the false positive rate increases strongly if a test has a lower specificity (as we would expect). More importantly, however, is the effect of the actual infection rate in the population. As long as the number of actually infected people in a population is very low (&lt; 5%), a positive test results is more likely to be a “false positive” than a “correct positive”. Only with a very good specificity (&gt;99%), the probability of being correctly identified as positive is as likely as being wrongly identified as positive (which is still pretty bad!). In the current situation, a single antibody test can thus hardly tell us whether we are actually immune. So what can we do? First, understand the problem. If you know that a positive result is likely to be a false positive, you will probably be more cautious in going back to normal life. Second, the probability of a “false positive” result decreases substantially if one takes more than one test. For example, obtaining a positive test result three times in a row should give you a greater confidence that your body actually has antibodies.</p>
<section id="take-home-message" class="level3">
<h3 class="anchored" data-anchor-id="take-home-message">Take home message</h3>
<p>A highly specific antibody test (e.g., Pr(tests−|is−) = 0.95) does <em>not</em> mean that the probability of being correctly identified as “positive” (i.e, Pr(is+|tests+)) is also high. In simple words, a large amount of people (the majority) who will get a positive test results are in fact not immune.</p>
<p>&nbsp;</p>
</section>
<section id="footnotes" class="level3">
<h3 class="anchored" data-anchor-id="footnotes">Footnotes</h3>
<div class="callout callout-style-simple callout-note no-icon callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>3 comments from the original post
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>These comments were left on the earlier WordPress version of this post.</p>
<div class="archived-comment">
<p><strong>Keng Siak Lam</strong> · 5 July 2020</p>
<p>I refer to your conclusion “A highly sensitive antibody test (e.g., Pr(tests+|is+) = 0.95) does not mean that the probability of being correctly identified as “positive” (i.e, Pr(is+|tests+)) is also high”.</p>
<p>The correct statement should be “A highly specific antibody test…”.</p>
<div class="archived-comment reply">
<p><strong>Philipp Masur</strong> · 3 August 2020</p>
<p>Thanks, Keng! Absolutely right.</p>
</div>
</div>
<div class="archived-comment">
<p><strong>Mike Jaeger</strong> · 14 September 2020</p>
<p>Great insights, but now apply it to actual testing for COVID. You will see with a CDC postd value of 70% sensitivity and an infection rate of 7.5%, the testing has been a complete sham, and has been the major driver in all poor decision-making.</p>
</div>
</div>
</div>
</div>


<template id="pm-footer-icons"><a href="#" data-pm-mail="p.k.masur|vu.nl" aria-label="Email" title="Email"><svg class="pm-icon" aria-hidden="true"><use href="#i-mail"></use></svg></a><a href="https://scholar.google.com/citations?user=XA_WthYAAAAJ" target="_blank" rel="noopener" aria-label="Google Scholar" title="Google Scholar"><svg class="pm-icon" aria-hidden="true"><use href="#i-scholar"></use></svg></a><a href="https://orcid.org/0000-0003-3065-7305" target="_blank" rel="noopener" aria-label="ORCID" title="ORCID"><svg class="pm-icon" aria-hidden="true"><use href="#i-orcid"></use></svg></a><a href="https://osf.io/yuqv5/" target="_blank" rel="noopener" aria-label="Open Science Framework" title="Open Science Framework"><svg class="pm-icon" aria-hidden="true"><use href="#i-osf"></use></svg></a><a href="https://github.com/masurp" target="_blank" rel="noopener" aria-label="GitHub" title="GitHub"><svg class="pm-icon" aria-hidden="true"><use href="#i-github"></use></svg></a><a href="https://bsky.app/profile/masurphil.bsky.social" target="_blank" rel="noopener" aria-label="Bluesky" title="Bluesky"><svg class="pm-icon" aria-hidden="true"><use href="#i-bluesky"></use></svg></a><a href="https://research.vu.nl/en/persons/philipp-k-masur" target="_blank" rel="noopener" aria-label="VU Amsterdam" title="VU Amsterdam"><svg class="pm-icon" aria-hidden="true"><use href="#i-landmark"></use></svg></a></template>
</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>Cellex test description:&nbsp;<a href="https://www.fda.gov/media/136625/download" class="uri">https://www.fda.gov/media/136625/download</a>; Comment by the MIT Technology Review:&nbsp;<a href="https://www.technologyreview.com/2020/04/09/998974/immunity-passports-cornavirus-antibody-test-outside/" class="uri">https://www.technologyreview.com/2020/04/09/998974/immunity-passports-cornavirus-antibody-test-outside/</a>.↩︎</p></li>
<li id="fn2"><p>For a comprehensive discussion of this problem, see pp.&nbsp;103-105 in the great book “Doing Bayesian Data Analysis” by Kruschke (2015).↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>R &amp; Statistics</category>
  <guid>https://philippmasur.de/blog/2020-04-14-false-positives-antibody-tests/</guid>
  <pubDate>Mon, 13 Apr 2020 22:00:00 GMT</pubDate>
  <media:content url="https://philippmasur.de/blog/2020-04-14-false-positives-antibody-tests/featured.png" medium="image" type="image/png" height="113" width="144"/>
</item>
<item>
  <title>Understanding exponential growth: The corona pandemic</title>
  <link>https://philippmasur.de/blog/2020-03-13-exponential-growth/</link>
  <description><![CDATA[ 

<!-- Icon sprite: 24×24 line icons, 1.6 stroke. Every shape has pathLength="1"
     so CSS can animate stroke-dashoffset 1 → 0 ("draw" the icon).
     Use: <svg class="pm-icon"><use href="#i-mail"/></svg> -->





<p>With the news going crazy these days, I felt like there is one particularly thing that is often misunderstood. The corona virus spreads exponentially (without intervention or measures). The problem is that we – as human beings – are very bad at imagining what an exponential trend looks like. By now, many different graphics and figures appear everywhere that aim to visualize the amount of infections or mortality rates per country. One of the most prominent examples is the following one:</p>
<blockquote class="blockquote">
<p>NEW on coronavirus: many western countries may soon face Italy’s situation</p>
<p>Case numbers since outbreaks began in several countries have tracked a ~33% daily rise. This is as true for UK, France, Germany as Italy; the latter is simply further down the path <a href="https://t.co/VcSZISFxzF" class="uri">https://t.co/VcSZISFxzF</a> <a href="https://t.co/xM6wXuMk4n">pic.twitter.com/xM6wXuMk4n</a></p>
<p>— John Burn-Murdoch (<span class="citation" data-cites="jburnmurdoch">@jburnmurdoch</span>) <a href="https://twitter.com/jburnmurdoch/status/1237737352879112194?ref_src=twsrc%5Etfw">March 11, 2020</a></p>
</blockquote>
<p>Although this figure is not wrong, I would like to highlight one particular aspect. At first glance, the figure seems to suggest that the growth curves are “linear”. However, the devil is in the details. If you look closely, you realize that the y-axis is log-transformed. But what does that actually mean and why is it so important to understand such a procedure? In this post, I try to explain, show, and visualize exponential growth.</p>
<p><strong>Disclaimer:</strong><br>
I would like to highlight that I am no epidemiologist or expert on virus outbreaks. My goal with this post is to explain exponential growth. My goal is not to provide a better understanding of the corona pandemic in general. Virologist, epidemiologist, and health care institutions are the experts that should be consulted in this regard.</p>
<section id="how-does-exponential-growth-actually-look-like" class="level1">
<h1>How does exponential growth actually look like?</h1>
<p>In what follows, I define a small function that allows us to simulate an exponentially growing vector. This means that each value is the previous value multiplied by a certain factor:</p>
<p><img src="https://latex.codecogs.com/png.latex?x_i%20=%20x_%7Bi-1%7D%20%5Ctimes%20%5Ctext%7Bfactor%7D"></p>
<p>Before we simulate this in R, play with it: change the daily growth rate and press <em>Play</em>. Then switch to the log axis, which is the trick behind the famous FT chart.</p>
<figure class="pm-explainer figure"><iframe src="../../explainers/growth/index.html" title="Interactive explainer: growth" loading="lazy" style="height:700px"></iframe><figcaption> Interactive: exponential vs. linear growth, computed in your browser.</figcaption></figure>
<div class="cell" data-layout-align="center">
<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>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(papaja)</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Exponential function</span></span>
<span id="cb1-5">exp_growth <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>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, </span>
<span id="cb1-6">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">steps =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, </span>
<span id="cb1-7">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">factor =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) {</span>
<span id="cb1-8">  x <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;">vector</span>()</span>
<span id="cb1-9">  x[<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> start</span>
<span id="cb1-10">  <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;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>steps){</span>
<span id="cb1-11">    x[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> x[i<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>factor</span>
<span id="cb1-12">  }</span>
<span id="cb1-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(x)</span>
<span id="cb1-14">}</span>
<span id="cb1-15"></span>
<span id="cb1-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Where are we after only 10 steps?</span></span>
<span id="cb1-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp_growth</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt;  [1]   1   2   4   8  16  32  64 128 256 512</code></pre>
</div>
</div>
<p>If we run the function with the default values (starting value = 1, steps = 10, factor = 2), we reach the number 512 after only 10 steps. Impressive growth, right?</p>
<div class="cell" data-layout-align="center">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Where are we after 20 steps?</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp_growth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">steps =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt;  [1]      1      2      4      8     16     32     64    128    256    512
#&gt; [11]   1024   2048   4096   8192  16384  32768  65536 131072 262144 524288</code></pre>
</div>
</div>
<p>After only 20 steps, however, we already reach a number that is larger than 0.5 million.</p>
<p>To get a true understanding of exponential growth, let us visualize 30 steps (while still using a starting value of 1).</p>
<div class="cell" data-layout-align="center">
<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">(plot1 <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;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</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;">30</span>,</span>
<span id="cb5-2">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp_growth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">steps =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</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;">x =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-5">  <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>
<span id="cb5-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>())</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-3-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://philippmasur.de/blog/2020-03-13-exponential-growth/index_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Line chart of a value that doubles at every step over 30 steps. The curve stays almost flat near zero until about step 25 and then shoots up to more than 500 million at step 30."></a></p>
</figure>
</div>
</div>
</div>
<p>After 30 steps only, we have reached the crazy large number of 536,870,912 (more than 500 million!). At the same time, it seems that the growth only really takes off after 25 steps. But remember our previous calculation. We were already at 0.4 million after 20 steps. So let’s zoom in on the first 10 steps again:</p>
<div class="cell" data-layout-align="center">
<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">plot1 <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;">ylim</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;">750</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;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-4-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://philippmasur.de/blog/2020-03-13-exponential-growth/index_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="The same doubling curve zoomed in on the first 10 steps. The value climbs steeply from 1 to 512 at step 10."></a></p>
</figure>
</div>
</div>
</div>
<p>After only 10 steps, we reached a number higher than 500. This steep growth was buried in the previous figure because the growth afterwards was just sooo much steeper.</p>
<p>Now what happens if we logarithmize the y-axis (as it was done in the figure referenced above)?</p>
<div class="cell" data-layout-align="center">
<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">plot1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_log10</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-5-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-3"><img src="https://philippmasur.de/blog/2020-03-13-exponential-growth/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="The doubling curve over 30 steps with a log-scaled y-axis. The points now form a perfectly straight rising line."></a></p>
</figure>
</div>
</div>
</div>
<p>With only one short change in the code, it seems like we have plotted a linear growth. But again, check the y-axis closely. The distance between two points on the axis is not as equal as the figure suggest. From step 1 to step 10, the difference is 1:512 (an increase of 511). However, from step 10 to step 20, 512:524,288 (an increase of 523,776!).</p>
</section>
<section id="exponential-growth-based-on-percentages" class="level1">
<h1>Exponential growth based on percentages</h1>
<p>Now in the case of the Corona virus spreading, we often quantify growth in percentage. So let’s consider another example. I now define a function that let’s us investigate different growth rates (based on percentages). So we again simulate a vector that represent exponential growth. This time, however, each value is the previous value plus a certain percentage of the previous value:</p>
<p><img src="https://latex.codecogs.com/png.latex?x_i%20=%20x_%7Bi-1%7D%20%5Ctimes%20%5Ctext%7Bpercent%7D"></p>
<div class="cell" data-layout-align="center">
<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">perc_growth <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>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, steps, perc) {</span>
<span id="cb8-2">  x <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;">vector</span>()</span>
<span id="cb8-3">  x[<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> start</span>
<span id="cb8-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;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>steps){</span>
<span id="cb8-5">    x[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> x[i<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> perc<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>x[i<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">-1</span>]</span>
<span id="cb8-6">  }</span>
<span id="cb8-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(x)</span>
<span id="cb8-8">}</span>
<span id="cb8-9"></span>
<span id="cb8-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Recreating the exponential growth from above</span></span>
<span id="cb8-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">perc_growth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start =</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;">steps =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">perc =</span> <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>#&gt;  [1]   1   2   4   8  16  32  64 128 256 512</code></pre>
</div>
</div>
<p>We see that if we start with 1 case and expect a 100% growth each day, we end up with 512 cases after 10 steps (e.g., 10 days). This is the exact exponential growth that we modelled above.</p>
<p>Of course, growth is not always 100%. It can be less, of course (but also more, like 200%!). In the next example, we see the effect of 5%, 10% and 20% growth on the growth curve.</p>
<div class="cell" data-layout-align="center">
<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">(plot2 <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;">data.frame</span> (<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</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;">30</span>,</span>
<span id="cb10-2">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5"</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;">perc_growth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start =</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;">steps =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">perc =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>),</span>
<span id="cb10-3">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10"</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;">perc_growth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start =</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;">steps =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">perc =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span>),</span>
<span id="cb10-4">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"20"</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;">perc_growth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start =</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;">steps =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">perc =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.20</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb10-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gather</span>(key, value, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>n) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb10-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</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;">x =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> key)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-8">  <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>
<span id="cb10-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>())</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-7-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-4"><img src="https://philippmasur.de/blog/2020-03-13-exponential-growth/index_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Three growth curves over 30 steps, labelled X5, X10 and X20. The X5 curve, which actually uses a growth rate of 50%, rises steeply to more than 100,000, while the 10% and 20% curves stay close to zero at this scale."></a></p>
</figure>
</div>
</div>
</div>
<p>Again we see that the growth of 50% is a lot steeper compared to 5% or 10%. Let’s again zoom in.</p>
<div class="cell" data-layout-align="center">
<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">plot2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">xlim</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;">10</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylim</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;">50</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-8-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-5"><img src="https://philippmasur.de/blog/2020-03-13-exponential-growth/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="The three growth curves zoomed in on the first 10 steps and values up to 50. The 50% curve reaches about 40 after 10 steps; the 10% and 20% curves stay below 6."></a></p>
</figure>
</div>
</div>
</div>
<p>As we can see, a 50% growth leads to ~40 cases after 10 steps (bear in mind, we started with only 1).</p>
</section>
<section id="growth-curves-of-the-corona-virus" class="level1">
<h1>Growth curves of the corona virus</h1>
<p>Now let us visualize the growth curves of the corona virus infections per country. I therefore use the public data provided by the John Hopkins University (<a href="https://github.com/CSSEGISandData/COVID-19" class="uri">https://github.com/CSSEGISandData/COVID-19</a>). For this purpose, I am only looking at the data from Germany.</p>
<!-- The original post read a local copy ("data_new.csv") of the JHU CSSE time series. The chunk below reads the same file directly from the (archived) GitHub repository, pinned to the commit of 12 March 2020, so the post still shows the data as of its publication. -->
<div class="cell" data-layout-align="center">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># JHU CSSE data as of 12 March 2020 (pinned to that day's commit)</span></span>
<span id="cb12-2">url <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;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://raw.githubusercontent.com/CSSEGISandData/COVID-19/"</span>,</span>
<span id="cb12-3">              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"82e1397911272be823cb93cbc19b62551608b8dc/"</span>,</span>
<span id="cb12-4">              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv"</span>)</span>
<span id="cb12-5"></span>
<span id="cb12-6">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;">read.csv</span>(url) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>Lat, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>Long)</span>
<span id="cb12-8"></span>
<span id="cb12-9">d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> d <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">country =</span> Country.Region) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(country <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;">"Germany"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gather</span>(date, value, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>country) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">separate</span>(date, <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;">"x"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"date"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</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;">%&gt;%</span></span>
<span id="cb12-14">  <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;">cases =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(value),</span>
<span id="cb12-15">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date =</span> lubridate<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;">mdy</span>(date)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-16">  as_tibble</span>
<span id="cb12-17"></span>
<span id="cb12-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># plot</span></span>
<span id="cb12-19">(plot3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> d <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</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;">x =</span> date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> cases)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-22">  <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>
<span id="cb12-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"date"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cases"</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-9-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-6"><img src="https://philippmasur.de/blog/2020-03-13-exponential-growth/index_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Line chart of confirmed corona cases in Germany from 22 January to 11 March 2020. Cases stay close to zero until late February and then rise steeply to about 1,900 on 11 March."></a></p>
</figure>
</div>
</div>
</div>
<p>If we compare this figure to the exponential growth curves that we simulated above, it should become clear that the actual growth curve of the corona pandemic in Germany is likewise exponential.</p>
<p>Again, if we log-transformed the y-axis and stretch the width of the figure (as shown in the tweet above), it again looks very different.</p>
<div class="cell" data-layout-align="center">
<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">d <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb13-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</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;">x =</span> date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> cases <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></span>
<span id="cb13-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> F, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_log10</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"date"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cases"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-10-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-7"><img src="https://philippmasur.de/blog/2020-03-13-exponential-growth/index_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Wide chart of confirmed corona cases in Germany plus one, on a log-scaled y-axis, with a smoothed black line. On this scale the growth looks gradual: a rise in late January, a plateau through February and a roughly straight climb from late February to mid-March."></a></p>
</figure>
</div>
</div>
</div>
<p>But what is the actual growth rate (in percentage)? And what do we need to expect in the near future?</p>
<div class="cell" data-layout-align="center">
<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">d <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-2">  <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;">cases_day =</span> cases <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;">lag</span>(cases),</span>
<span id="cb14-3">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">percent =</span> cases_day<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>cases) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">growthrate =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(percent, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> T),</span>
<span id="cb14-5">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> psych<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;">describe</span>(percent)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>se,</span>
<span id="cb14-6">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower =</span> growthrate <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;">1.96</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>se,</span>
<span id="cb14-7">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">upper =</span> growthrate <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;">1.96</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>se)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; # A tibble: 1 × 4
#&gt;   growthrate     se  lower upper
#&gt;        &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt; &lt;dbl&gt;
#&gt; 1      0.154 0.0318 0.0918 0.216</code></pre>
</div>
</div>
<p>The growth curve is on average ~15% (of course, we can debate if simply averaging across the growth rates per day is a good approach. Actually the figure in the tweet referenced above features a dotted line that represents a daily growth rate of 33% and most countries (including Germany) seem to approach that line). If we nonetheless use a growth rate of 15%, how would the increase in cases look like in a simulation for the next weeks and months?</p>
<p>Our starting value is 3,156 (the number of registered infections in Germany today).</p>
<div class="cell" data-layout-align="center">
<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;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</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;">30</span>,</span>
<span id="cb16-2">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cases =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">perc_growth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3156</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">steps =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">perc =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.154</span>),</span>
<span id="cb16-3">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">perc_growth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3156</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">steps =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">perc =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0918</span>),</span>
<span id="cb16-4">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">upper =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">perc_growth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3156</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">steps =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">perc =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.216</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb16-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gather</span>(key, cases, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>n) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb16-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</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;">x =</span> n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> cases, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> key)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-7">  <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>
<span id="cb16-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ylim</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;">200000</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values=</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;">"black"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</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;">30</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"days"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Simulated growth in Germany"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-12-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-8"><img src="https://philippmasur.de/blog/2020-03-13-exponential-growth/index_files/figure-html/unnamed-chunk-12-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Simulated growth in Germany over 30 days, starting from 3,156 cases. The black curve for the average growth rate of 15.4% passes 100,000 cases around day 25; grey curves for the lower (9.2%) and upper (21.6%) growth rates fan out below and above it."></a></p>
</figure>
</div>
</div>
</div>
<p>The grey lines show the uncertainty in the simulation. The black curve shows the average growth rate based on the available data. It reveals that if the trend would continue, we would have more than 100,000 cases after only 25 days. Yet there are several things that have to be taken into account: First, the growth rate of 15% might be wrong (I admit that averaging across all days is a very crude way of estimating the growth rate) or may change over time. The large uncertainty (grey lines) shows already how largely the actual growth rate could vary based on my crude measure. For example, if the growth rate would be ~9%, we would have ~25,000 cases instead of ~100,000 after 25 days.</p>
<p>So how come that the news talk about a 33% growth curve now? Well, they only started to compute the growth rate after ~100 cases were identified. In the following figure, you see a simulated 33% growth curve plotted against the actual growth curve from the day when Germany had 100 identified cases.</p>
<div class="cell" data-layout-align="center">
<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">(plot4 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> d <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb17-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(cases <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb17-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cases of corona"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> cases,</span>
<span id="cb17-4">         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"33% growth curve"</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;">perc_growth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">130</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">steps =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">perc =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">33</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb17-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(date, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cases of corona"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"33% growth curve"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb17-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gather</span>(key, value, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>date) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb17-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</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;">x =</span> date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> key)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-9">  <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>
<span id="cb17-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</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;">"grey"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"date"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cases"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-13-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-9"><img src="https://philippmasur.de/blog/2020-03-13-exponential-growth/index_files/figure-html/unnamed-chunk-13-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Confirmed cases in Germany from 1 to 11 March 2020 (black) plotted against a simulated 33% daily growth curve starting at 130 cases (grey). Both curves rise from about 130 to roughly 2,000 and track each other closely."></a></p>
</figure>
</div>
</div>
</div>
<p>As you can see, the curves align pretty well. If the daily growth rate would indeed approach 33% and stay this high for the next weeks, we would have ~2,961,940 cases after 25 days.</p>
<p>This is why acting now (both individually and on a societal level) is so important. We need to slow down this growth considerably. But we not only have to lower the actual infections, but also have to reduce the number of people who are infected at the same time. The latter is particularly important as the health system would otherwise not be able to treat all infected with the same care or even at all. The following video illustrates the effect of less infections at the same time on the health system very well:</p>
<blockquote class="blockquote">
<p><a href="https://twitter.com/hashtag/Corona?src=hash&amp;ref_src=twsrc%5Etfw">#Corona</a> breitet sich aus. Durch unser Verhalten können wir alle dazu beitragen, seine Verbreitung einzudämmen. Es geht darum, die Älteren und Schwachen zu schützen und unser Gesundheitssystem nicht zu überlasten. <a href="https://twitter.com/hashtag/flattenthecurve?src=hash&amp;ref_src=twsrc%5Etfw">#flattenthecurve</a>! <a href="https://twitter.com/hashtag/einHerzf%C3%BCreinander?src=hash&amp;ref_src=twsrc%5Etfw">#einHerzfüreinander</a> <a href="https://t.co/SrtK3DSQnB">pic.twitter.com/SrtK3DSQnB</a></p>
<p>— Paul Ziemiak (<span class="citation" data-cites="PaulZiemiak">@PaulZiemiak</span>) <a href="https://twitter.com/PaulZiemiak/status/1238729001679687680?ref_src=twsrc%5Etfw">March 14, 2020</a></p>
</blockquote>
</section>
<section id="conclusion" class="level1">
<h1>Conclusion</h1>
<p>Understanding exponential growth is hard. The problem is that we constantly underestimate the actual growth and its implications. In case of infections, this is very worrisome. And I fear that figures that log-transformed the y-axis (such as the one presented above) might provide a false picture of the actual growth.</p>
<p>So what do we need to do? With no treatment and a viable vaccine yet available, the only effective way to keep the coronavirus pandemic at bay is to give the virus less chances of spreading. The following list of actions are recommended to serve as a set of guidelines:</p>
<ol type="1">
<li>Don’t panic, but be alert.</li>
<li>Wash your hands often and practice good cough and sneeze etiquette.</li>
<li>Try to touch your face as little as possible, including your mouth, nose, and eyes.</li>
<li>Practice social distancing, no hugs and kisses, no handshakes, no high fives. If you must, use safer alternatives.</li>
<li>Do not attend concerts, stage plays, sporting events, or any other mass entertainment events.</li>
<li>Refrain from visiting museums, exhibitions, movie theaters, night clubs, and other entertainment venues.</li>
<li>Stay away from social gatherings and events, like club meetings, religious services, and private parties.</li>
<li>Reduce your amount of travel to a minimum. Don’t travel long distances if not absolutely necessary.</li>
<li>Do not use public transportation if not absolutely necessary.</li>
<li>If you can work from home, work from home. Urge your employer to allow remote work if needed.</li>
<li>Replace as many social interactions as possible with remote alternatives like phone calls or video chat.</li>
<li>Do not leave your home if not absolutely necessary.</li>
</ol>
<p>Be safe!</p>
<section id="some-more-information-on-the-corona-growth" class="level3">
<h3 class="anchored" data-anchor-id="some-more-information-on-the-corona-growth">Some more information on the corona growth:</h3>
<ul>
<li>This article provides a very thoughtful and informative explanation of this problem and I would highly recommend to read it:<br>
<a href="https://medium.com/@tomaspueyo/coronavirus-act-today-or-people-will-die-f4d3d9cd99ca" class="uri">https://medium.com/@tomaspueyo/coronavirus-act-today-or-people-will-die-f4d3d9cd99ca</a></li>
<li>For German readers, there is a translation of the article too:<br>
<a href="https://perspective-daily.de/article/1181/2hWA1mB8" class="uri">https://perspective-daily.de/article/1181/2hWA1mB8</a></li>
<li>And here is another attempt to explain exponential growth (in German):<br>
<a href="https://projekte.sueddeutsche.de/artikel/wissen/coronavirus-die-wucht-der-grossen-zahl-e575082/" class="uri">https://projekte.sueddeutsche.de/artikel/wissen/coronavirus-die-wucht-der-grossen-zahl-e575082/</a></li>
<li>Right after I wrote this post, I realized that Felix Schönbrodt has created a shiny app that allows to visualize the corona growth in various countries worldwide using data either from the European Center for Disease Prevention and Control or the John Hopkins University. This app allows to customize the visualization in various ways. Most importantly, it lets you choose whether the y-axis is log-transformed or not. Refrain from logarithmizing it every once in a while to get an idea of the actual growth:<br>
<a href="http://shinyapps.org/apps/corona/" class="uri">http://shinyapps.org/apps/corona/</a></li>
</ul>
<div class="callout callout-style-simple callout-note no-icon callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>1 comment from the original post
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>These comments were left on the earlier WordPress version of this post.</p>
<div class="archived-comment">
<p><strong>Dennis M Tupick</strong> · 25 May 2020</p>
<p>Universal masking?</p>
</div>
</div>
</div>
</div>


<template id="pm-footer-icons"><a href="#" data-pm-mail="p.k.masur|vu.nl" aria-label="Email" title="Email"><svg class="pm-icon" aria-hidden="true"><use href="#i-mail"></use></svg></a><a href="https://scholar.google.com/citations?user=XA_WthYAAAAJ" target="_blank" rel="noopener" aria-label="Google Scholar" title="Google Scholar"><svg class="pm-icon" aria-hidden="true"><use href="#i-scholar"></use></svg></a><a href="https://orcid.org/0000-0003-3065-7305" target="_blank" rel="noopener" aria-label="ORCID" title="ORCID"><svg class="pm-icon" aria-hidden="true"><use href="#i-orcid"></use></svg></a><a href="https://osf.io/yuqv5/" target="_blank" rel="noopener" aria-label="Open Science Framework" title="Open Science Framework"><svg class="pm-icon" aria-hidden="true"><use href="#i-osf"></use></svg></a><a href="https://github.com/masurp" target="_blank" rel="noopener" aria-label="GitHub" title="GitHub"><svg class="pm-icon" aria-hidden="true"><use href="#i-github"></use></svg></a><a href="https://bsky.app/profile/masurphil.bsky.social" target="_blank" rel="noopener" aria-label="Bluesky" title="Bluesky"><svg class="pm-icon" aria-hidden="true"><use href="#i-bluesky"></use></svg></a><a href="https://research.vu.nl/en/persons/philipp-k-masur" target="_blank" rel="noopener" aria-label="VU Amsterdam" title="VU Amsterdam"><svg class="pm-icon" aria-hidden="true"><use href="#i-landmark"></use></svg></a></template>
</section>
</section>

 ]]></description>
  <category>R &amp; Statistics</category>
  <guid>https://philippmasur.de/blog/2020-03-13-exponential-growth/</guid>
  <pubDate>Thu, 12 Mar 2020 23:00:00 GMT</pubDate>
  <media:content url="https://philippmasur.de/blog/2020-03-13-exponential-growth/featured.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>How to do power simulations for structural equation models in R</title>
  <link>https://philippmasur.de/blog/2020-03-11-power-simulations-sem/</link>
  <description><![CDATA[ 

<!-- Icon sprite: 24×24 line icons, 1.6 stroke. Every shape has pathLength="1"
     so CSS can animate stroke-dashoffset 1 → 0 ("draw" the icon).
     Use: <svg class="pm-icon"><use href="#i-mail"/></svg> -->





<p>Computing a priori power analyses for simple statistical models can be done analytically (e.g., with G*Power or the <code>pwr</code> package in R). However, estimating the power for more complex models and in particular structural equation models (SEM) is not as straightforward and requires simulations. I recently came across the package <a href="https://github.com/jeff-hughes/paramtest"><code>paramtest</code></a> (Hughes, 2017) which provides a great framework for conducting more complex power simulations.</p>
<p>In what follows, I provide some examples of how to simulate power for SEMs with the ultimate goal of estimating the required sample size. On a side note, SEM are comparatively complex models. Sample size justifications are hence not only based on power calculations but should also be informed by general considerations of model complexity (see e.g., Kline, 2015).</p>
<p>For the following analyses, we only need two packages. I will use various functions from the <code>tidyverse</code> (primarily from <code>dplyr</code> and <code>ggplot2</code>) and the functions <code>gen_data()</code> and <code>grid_search()</code> from the <code>paramtest</code> package.</p>
<div class="cell" data-layout-align="center">
<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>(tidyverse)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(paramtest)</span></code></pre></div></div>
</div>
<div class="cell" data-layout-align="center">
<details class="code-fold">
<summary>Helper functions: caching, power summaries and plot styling</summary>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The simulations take a while. Their results are saved next to this post</span></span>
<span id="cb2-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># (folder sims/) and re-used; delete the files to run them again.</span></span>
<span id="cb2-3">cached <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>(file, expr) {</span>
<span id="cb2-4">  <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;">file.exists</span>(file)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readRDS</span>(file))</span>
<span id="cb2-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dir.create</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dirname</span>(file), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">showWarnings =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb2-6">  res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> expr</span>
<span id="cb2-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">saveRDS</span>(res, file)</span>
<span id="cb2-8">  res</span>
<span id="cb2-9">}</span>
<span id="cb2-10"></span>
<span id="cb2-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Power per sample size, with a 95% (Wilson) interval for the Monte Carlo error</span></span>
<span id="cb2-12">summarise_power <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>(data, significant) {</span>
<span id="cb2-13">  data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> n.test) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-15">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">k =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>({{ significant }}, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb2-16">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iter =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</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;">is.na</span>({{ significant }})),</span>
<span id="cb2-17">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-18">    <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;">power =</span> k <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> iter,</span>
<span id="cb2-19">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qnorm</span>(.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">975</span>),</span>
<span id="cb2-20">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">centre =</span> (power <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> z<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> iter)) <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> z<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> iter),</span>
<span id="cb2-21">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">halfw =</span> z <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;">sqrt</span>(power <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> power) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> iter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> z<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;">4</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> iter<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> z<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> iter),</span>
<span id="cb2-22">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower =</span> centre <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> halfw, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">upper =</span> centre <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> halfw) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-23">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(n, k, iter, power, lower, upper)</span>
<span id="cb2-24">}</span>
<span id="cb2-25"></span>
<span id="cb2-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># A smooth power curve: power is approximately Phi(a + b * sqrt(n))</span></span>
<span id="cb2-27">power_fit <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>(pw) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glm</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(k, iter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> k) <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;">sqrt</span>(n), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">binomial</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"probit"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> pw)</span>
<span id="cb2-28">n_for_power <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>(fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">target =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unname</span>(((<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qnorm</span>(target) <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;">coef</span>(fit)[<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;">coef</span>(fit)[<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>
<span id="cb2-29">power_curve <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>(fit, n) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</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;">power =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">predict</span>(fit, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</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;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"response"</span>))</span>
<span id="cb2-30">fmt_n <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>(n) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</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;">1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">big.mark =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">","</span>)</span>
<span id="cb2-31"></span>
<span id="cb2-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot style: transparent background, muted text (readable in light and dark mode)</span></span>
<span id="cb2-33">pal <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="at" style="color: #657422;
background-color: null;
font-style: inherit;">blue =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#1497d6"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">orange =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#d9772b"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">green =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#1baf7a"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">grey =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#8a8f98"</span>)</span>
<span id="cb2-34">theme_blog <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>() {</span>
<span id="cb2-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-36">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.background  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"transparent"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>),</span>
<span id="cb2-37">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"transparent"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>),</span>
<span id="cb2-38">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.minor =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb2-39">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.major =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#8a8f9840"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>),</span>
<span id="cb2-40">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#7a7f88"</span>),</span>
<span id="cb2-41">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#7a7f88"</span>),</span>
<span id="cb2-42">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>)</span>
<span id="cb2-43">}</span></code></pre></div></div>
</details>
</div>
<section id="power-for-a-bivariate-relationship-with-manifest-variables" class="level2">
<h2 class="anchored" data-anchor-id="power-for-a-bivariate-relationship-with-manifest-variables">Power for a bivariate relationship with manifest variables</h2>
<p>Let us first consider a simple bivariate relationship between two <em>manifest</em> variables. In other words, we investigate the power for a relationship between two variables with “no measurement error”. The approach that I am going to use throughout this post is always as follows:</p>
<ol type="1">
<li>Define a function that simulates data, draw samples, estimate the relevant models, and extract parameters of interest.</li>
<li>Use <code>grid_search()</code> to run the simulations.</li>
<li>Count the number of significant results to “get” power.</li>
<li>Visualize the power curve.</li>
</ol>
<p>The first step requires defining a function with the following arguments: a) simNum = the iteration number (do not change this argument, it is required by <code>grid_search()</code>), b) n = the sample sizes that should be tests, c) any other variables that should vary across the simulations (could be effect size(s) of the relationship(s) of interest or factor loadings of latent variables, etc.). Within this function, we use the function <code>gen_data()</code> that generates samples from the predefined data properties.<br>
These properties include a “factor matrix” and an “effect matrix”. Defining both matrixes requires a good knowledge about the model that one wants to estimate. All steps are outlined below.</p>
<div class="cell" data-layout-align="center">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define the function</span></span>
<span id="cb3-2">model_bivar <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>(simNum, n, b1) {</span>
<span id="cb3-3">  </span>
<span id="cb3-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># compute factor matrix </span></span>
<span id="cb3-5">  factor <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="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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># x (no measurement error)</span></span>
<span id="cb3-6">                     <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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># y (no measurement error)</span></span>
<span id="cb3-7">                   <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 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;">2</span>, </span>
<span id="cb3-8">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">byrow =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, </span>
<span id="cb3-9">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dimnames =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</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;">'x'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'y'</span>),  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># rows</span></span>
<span id="cb3-10">                                   <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;">'x'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'y'</span>))) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># cols </span></span>
<span id="cb3-11">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># compute effect matrix</span></span>
<span id="cb3-12">  y_res <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>(<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> b1<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="cb3-13">  effect <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="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>, b1,    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># b1 = effect size of the relationship</span></span>
<span id="cb3-14">                     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, y_res), </span>
<span id="cb3-15">                    <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 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;">2</span>, </span>
<span id="cb3-16">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">byrow =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, </span>
<span id="cb3-17">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dimnames =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</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;">'x'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'y'</span>),    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># rows</span></span>
<span id="cb3-18">                                    <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;">'x'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'y'</span>)))   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># columns</span></span>
<span id="cb3-19">  </span>
<span id="cb3-20">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># generate data based on both matrices</span></span>
<span id="cb3-21">  data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> paramtest<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;">gen_data</span>(factor, effect, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_cases =</span> n)</span>
<span id="cb3-22">  </span>
<span id="cb3-23">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># fit lavaan model</span></span>
<span id="cb3-24">  model <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span></span>
<span id="cb3-25"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    y ~ b1*x</span></span>
<span id="cb3-26"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  '</span></span>
<span id="cb3-27">  fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> lavaan<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;">sem</span>(model, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data)</span>
<span id="cb3-28">  </span>
<span id="cb3-29">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># extract relevant parameters</span></span>
<span id="cb3-30">  ests <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>(lavaan<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;">parameterEstimates</span>(fit), label <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;">"b1"</span>) </span>
<span id="cb3-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(ests)</span>
<span id="cb3-32">}</span></code></pre></div></div>
</div>
<p>Now, we use <code>grid_search()</code> to run the simulations. In this case, we investigate how many participants we need to sample in order to test a bivariate effect between manifest variables of <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> = .10 (small according to Cohen) to reach different levels of power.</p>
<p>We hence provide a list with the varying parameters (in this case only the sample sizes). It is important to run enough iterations. As always, results stabilize with higher iterations. Side note: We can profit from parallel computing by using <code>parallel = 'snow'</code> and <code>ncpus = [number of available cores]</code>. This increases the computation speed considerably.</p>
<div class="cell" data-layout-align="center">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Run simulations (L'Ecuyer-CMRG makes the parallel random numbers reproducible)</span></span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">RNGkind</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"L'Ecuyer-CMRG"</span>)</span>
<span id="cb4-3"><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;">2020</span>)</span>
<span id="cb4-4">power_bivar <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;">cached</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sims/power_bivar.rds"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid_search</span>(</span>
<span id="cb4-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">func =</span> model_bivar,</span>
<span id="cb4-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">params =</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;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1500</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>)),</span>
<span id="cb4-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b1 =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb4-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n.iter =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>,</span>
<span id="cb4-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">output =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'data.frame'</span>,</span>
<span id="cb4-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parallel =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'snow'</span>,</span>
<span id="cb4-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncpus =</span> parallel<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;">detectCores</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="cb4-12">  ))</span></code></pre></div></div>
</div>
<div class="cell" data-layout-align="center">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># result table</span></span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">results</span>(power_bivar) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> as_tibble</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; # A tibble: 29,000 × 12
#&gt;     iter n.test lhs   op    rhs   label     est     se      z pvalue ci.lower
#&gt;    &lt;int&gt;  &lt;dbl&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;   &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt;    &lt;dbl&gt;
#&gt;  1     1    100 y     ~     x     b1     0.0640 0.0990  0.646 0.518  -0.130  
#&gt;  2     2    100 y     ~     x     b1     0.160  0.0994  1.61  0.107  -0.0345 
#&gt;  3     3    100 y     ~     x     b1     0.0697 0.0970  0.718 0.473  -0.121  
#&gt;  4     4    100 y     ~     x     b1     0.175  0.106   1.65  0.0998 -0.0334 
#&gt;  5     5    100 y     ~     x     b1    -0.0444 0.0952 -0.467 0.641  -0.231  
#&gt;  6     6    100 y     ~     x     b1     0.182  0.106   1.72  0.0851 -0.0251 
#&gt;  7     7    100 y     ~     x     b1     0.184  0.0907  2.03  0.0425  0.00621
#&gt;  8     8    100 y     ~     x     b1     0.0676 0.110   0.616 0.538  -0.147  
#&gt;  9     9    100 y     ~     x     b1     0.115  0.102   1.12  0.261  -0.0856 
#&gt; 10    10    100 y     ~     x     b1    -0.0108 0.0941 -0.115 0.908  -0.195  
#&gt; # ℹ 28,990 more rows
#&gt; # ℹ 1 more variable: ci.upper &lt;dbl&gt;</code></pre>
</div>
</div>
<p>The resulting data frame includes relevant parameters for the relationship between x and y. The column <code>n.test</code> indicates the sample size of each iteration.</p>
<p>To compute the power, we only need to count the number of significant results per sample size group (n = 100, 150, 200, 250,…). We can then check the “needed” sample size for various power levels. It would also suggest plotting a power curve as it provides more information than a single value alone.</p>
<div class="cell" data-layout-align="center">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># compute power</span></span>
<span id="cb7-2">(result_bivar <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;">results</span>(power_bivar) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(n.test) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-4">  <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;">sig =</span> (pvalue <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">05</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">power =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(sig, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; # A tibble: 29 × 2
#&gt;    n.test power
#&gt;     &lt;dbl&gt; &lt;dbl&gt;
#&gt;  1    100 0.171
#&gt;  2    150 0.235
#&gt;  3    200 0.311
#&gt;  4    250 0.337
#&gt;  5    300 0.415
#&gt;  6    350 0.462
#&gt;  7    400 0.552
#&gt;  8    450 0.58 
#&gt;  9    500 0.614
#&gt; 10    550 0.664
#&gt; # ℹ 19 more rows</code></pre>
</div>
</div>
<div class="cell" data-layout-align="center">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract sample size for 80% power</span></span>
<span id="cb9-2">result_bivar <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(power <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.80</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; # A tibble: 15 × 2
#&gt;    n.test power
#&gt;     &lt;dbl&gt; &lt;dbl&gt;
#&gt;  1    800 0.802
#&gt;  2    850 0.826
#&gt;  3    900 0.866
#&gt;  4    950 0.872
#&gt;  5   1000 0.887
#&gt;  6   1050 0.895
#&gt;  7   1100 0.921
#&gt;  8   1150 0.941
#&gt;  9   1200 0.942
#&gt; 10   1250 0.944
#&gt; 11   1300 0.959
#&gt; 12   1350 0.963
#&gt; 13   1400 0.96 
#&gt; 14   1450 0.97 
#&gt; 15   1500 0.979</code></pre>
</div>
</div>
<p>To plot a power curve, I add two things to the raw proportions: a 95% confidence interval for each estimate (with 1,000 iterations, the Monte Carlo error is about ±3 percentage points) and a smooth curve. Because power is approximately <img src="https://latex.codecogs.com/png.latex?%5CPhi(a%20+%20b%5Csqrt%7Bn%7D)">, a probit regression on <img src="https://latex.codecogs.com/png.latex?%5Csqrt%7Bn%7D"> fits these curves very well, and we can solve it for the sample size that reaches 80% power.</p>
<div class="cell" data-layout-align="center">
<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;"># Power with Monte Carlo intervals, fitted curve and the n for 80% power</span></span>
<span id="cb11-2">pw_bivar  <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;">summarise_power</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">results</span>(power_bivar), pvalue <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">05</span>)</span>
<span id="cb11-3">fit_bivar <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;">power_fit</span>(pw_bivar)</span>
<span id="cb11-4">n80_bivar <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;">n_for_power</span>(fit_bivar)</span></code></pre></div></div>
</div>
<div class="cell" data-layout-align="center">
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(pw_bivar, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(n, power)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> pal[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey"</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"segment"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> n80_bivar, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> n80_bivar, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</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;">yend =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>,</span>
<span id="cb12-4">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dotted"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> pal[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey"</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">power_curve</span>(fit_bivar, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</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;">1500</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> pal[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</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>
<span id="cb12-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_pointrange</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;">ymin =</span> lower, <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;">colour =</span> pal[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</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>
<span id="cb12-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> n80_bivar <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;">30</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">74</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</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;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#7a7f88"</span>,</span>
<span id="cb12-8">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"80% power at n ≈ "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmt_n</span>(n80_bivar))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>percent, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sample size"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Power"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blog</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-10-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://philippmasur.de/blog/2020-03-11-power-simulations-sem/index_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Simulated power curve for a manifest correlation of .10, with 95% intervals around each estimate and a fitted curve. Power reaches 80% at about n = 770."></a></p>
</figure>
</div>
</div>
</div>
<p>In this example, we see that we need roughly 770 participants in order to test the small correlation between the two variables with a power of 80% (assuming an alpha-level of .05!).</p>
<p>For this simple example, this procedure is a bit complex as a solution can also be derived analytically (any remaining difference in the resulting sample size is explainable by the Monte Carlo error of our simulations. With more iterations, we would get even closer to the analytical solution):</p>
<div class="cell" data-layout-align="center">
<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(pwr)</span>
<span id="cb13-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pwr.r.test</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r =</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>
<span id="cb13-3">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sig.level =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">05</span>,</span>
<span id="cb13-4">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">power =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>,</span>
<span id="cb13-5">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alternative =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"two.sided"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; 
#&gt;      approximate correlation power calculation (arctangh transformation) 
#&gt; 
#&gt;               n = 781.7516
#&gt;               r = 0.1
#&gt;       sig.level = 0.05
#&gt;           power = 0.8
#&gt;     alternative = two.sided</code></pre>
</div>
</div>
</section>
<section id="power-for-a-bivariate-relationship-between-latent-variables" class="level2">
<h2 class="anchored" data-anchor-id="power-for-a-bivariate-relationship-between-latent-variables">Power for a bivariate relationship between latent variables</h2>
<p>The whole procedure becomes more complex if we try to simulate the power for a relationship between latent variables. Here, we need to make assumptions about the items’ reliability. In this first example, we assume that we have comparatively good scales whose items have factor loadings of .70 (i.e., each item’s reliability is .70² = .49, and the reliability of a three-item scale is ω ≈ .74). Defining the factor matrix is hence more complex and the model is now a structural equation model with latent variables. Otherwise, the procedure remains the same.</p>
<div class="cell" data-layout-align="center">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># define function</span></span>
<span id="cb15-2">model_sem <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>(simNum,</span>
<span id="cb15-3">                      n, </span>
<span id="cb15-4">                      b1, </span>
<span id="cb15-5">                      x_ind1, x_ind2, x_ind3, </span>
<span id="cb15-6">                      y_ind1, y_ind2, y_ind3) {</span>
<span id="cb15-7">  </span>
<span id="cb15-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># compute factor matrix (in this case with measurement error)</span></span>
<span id="cb15-9">  factor <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(x_ind1, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># y1</span></span>
<span id="cb15-10">                     x_ind2, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># y2</span></span>
<span id="cb15-11">                     x_ind3, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># y3</span></span>
<span id="cb15-12">                     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, y_ind1,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># y1</span></span>
<span id="cb15-13">                     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, y_ind2,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># y2</span></span>
<span id="cb15-14">                     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, y_ind3),  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># y3</span></span>
<span id="cb15-15">                   <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;">6</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;">2</span>, </span>
<span id="cb15-16">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">byrow=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, </span>
<span id="cb15-17">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dimnames=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</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;">'x1'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x2'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x3'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'y1'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'y2'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'y3'</span>),  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># rows (observed)</span></span>
<span id="cb15-18">                                 <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;">'x'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'y'</span>)))                           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># columns (latent)</span></span>
<span id="cb15-19">  </span>
<span id="cb15-20">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># matrix of the effects structure (variance-covariance)</span></span>
<span id="cb15-21">  y_res <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>(<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> b1<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="cb15-22">  effect <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="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>, b1,     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># x</span></span>
<span id="cb15-23">                     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, y_res), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># y</span></span>
<span id="cb15-24">                    <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 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;">2</span>, </span>
<span id="cb15-25">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">byrow=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, </span>
<span id="cb15-26">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dimnames=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</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;">'x'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'y'</span>),    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># rows</span></span>
<span id="cb15-27">                                  <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;">'x'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'y'</span>)))   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># columns</span></span>
<span id="cb15-28">  </span>
<span id="cb15-29">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># generate data based on both matrices</span></span>
<span id="cb15-30">  data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> paramtest<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;">gen_data</span>(factor, effect, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_cases =</span> n)</span>
<span id="cb15-31"></span>
<span id="cb15-32">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># fit lavaan model</span></span>
<span id="cb15-33">  model <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span></span>
<span id="cb15-34"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">   # measurement model</span></span>
<span id="cb15-35"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">   x =~ x1 + x2 + x3</span></span>
<span id="cb15-36"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">   y =~ y1 + y2 + y3</span></span>
<span id="cb15-37"></span>
<span id="cb15-38"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">   # structural regression model</span></span>
<span id="cb15-39"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">   y ~ b1*x</span></span>
<span id="cb15-40"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  '</span></span>
<span id="cb15-41">  fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> lavaan<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;">sem</span>(model, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data)</span>
<span id="cb15-42">  ests <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> lavaan<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;">parameterEstimates</span>(fit)</span>
<span id="cb15-43">  </span>
<span id="cb15-44">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># extract relevant parameters</span></span>
<span id="cb15-45">  est <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ests[ests<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>label <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;">'b1'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'est'</span>]</span>
<span id="cb15-46">  p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ests[ests<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>label <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;">'b1'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'pvalue'</span>]</span>
<span id="cb15-47">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<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;">est =</span> est, </span>
<span id="cb15-48">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">p =</span> p, </span>
<span id="cb15-49">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sig =</span> (p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">05</span>)))</span>
<span id="cb15-50">}</span>
<span id="cb15-51"></span>
<span id="cb15-52"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Measurement error specified by factor loading = .7</span></span>
<span id="cb15-53"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">RNGkind</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"L'Ecuyer-CMRG"</span>)</span>
<span id="cb15-54"><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;">2021</span>)</span>
<span id="cb15-55">power_sem1 <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;">cached</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sims/power_sem1.rds"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid_search</span>(</span>
<span id="cb15-56">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">func =</span> model_sem, </span>
<span id="cb15-57">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">params =</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;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)),</span>
<span id="cb15-58">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b1 =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb15-59">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n.iter =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, </span>
<span id="cb15-60">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">output =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'data.frame'</span>,</span>
<span id="cb15-61">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x_ind1 =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>,    </span>
<span id="cb15-62">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x_ind2 =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, </span>
<span id="cb15-63">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x_ind3 =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>,      </span>
<span id="cb15-64">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y_ind1 =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, </span>
<span id="cb15-65">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y_ind2 =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, </span>
<span id="cb15-66">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y_ind3 =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, </span>
<span id="cb15-67">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parallel =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'snow'</span>,</span>
<span id="cb15-68">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncpus =</span> parallel<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;">detectCores</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="cb15-69">  ))</span></code></pre></div></div>
</div>
<p>Let us quickly check the resulting data frame.</p>
<div class="cell" data-layout-align="center">
<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;">results</span>(power_sem1) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> as_tibble</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; # A tibble: 36,000 × 5
#&gt;     iter n.test     est       p   sig
#&gt;    &lt;int&gt;  &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt; &lt;dbl&gt;
#&gt;  1     1    500 0.00710 0.906       0
#&gt;  2     2    500 0.182   0.0111      1
#&gt;  3     3    500 0.165   0.00259     1
#&gt;  4     4    500 0.0111  0.866       0
#&gt;  5     5    500 0.142   0.00922     1
#&gt;  6     6    500 0.111   0.110       0
#&gt;  7     7    500 0.0879  0.168       0
#&gt;  8     8    500 0.169   0.0112      1
#&gt;  9     9    500 0.109   0.0469      1
#&gt; 10    10    500 0.126   0.0396      1
#&gt; # ℹ 35,990 more rows</code></pre>
</div>
</div>
<p>This time, we only extracted the estimate, the p-value, and already transformed the p-value into a binary variable (<em>p</em> &lt; .05). Replications in which the model does not converge return <code>NA</code> and are dropped when we compute power (<code>na.rm = TRUE</code>).</p>
<p>But before we estimate the power, let us quickly run the simulation again, but change the reliability of the items to more realistic values (at least in the social sciences, scales often have items with factor loadings around .50). This is very easy, as the defined function has arguments for each factor loading in the model.</p>
<p>We then combine the results from both simulations. After a bit of data wrangling, we can now plot both power curves.</p>
<div class="cell" data-layout-align="center">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Let's also test a more realistic example (factor loadings = .5)</span></span>
<span id="cb18-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;">2022</span>)</span>
<span id="cb18-3">power_sem2 <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;">cached</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sims/power_sem2.rds"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid_search</span>(</span>
<span id="cb18-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">func =</span> model_sem, </span>
<span id="cb18-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">params =</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;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)),</span>
<span id="cb18-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b1 =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb18-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n.iter =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, </span>
<span id="cb18-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">output =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'data.frame'</span>,</span>
<span id="cb18-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x_ind1 =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># changed to .5 </span></span>
<span id="cb18-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x_ind2 =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, </span>
<span id="cb18-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x_ind3 =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,      </span>
<span id="cb18-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y_ind1 =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, </span>
<span id="cb18-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y_ind2 =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, </span>
<span id="cb18-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y_ind3 =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, </span>
<span id="cb18-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parallel =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'snow'</span>,</span>
<span id="cb18-16">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncpus =</span> parallel<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;">detectCores</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="cb18-17">  ))</span></code></pre></div></div>
</div>
<div class="cell" data-layout-align="center">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract result table</span></span>
<span id="cb19-2">(result_sem1 <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;">results</span>(power_sem1) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-3">    <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;">rel =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_rows</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">results</span>(power_sem2) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-5">                <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;">rel =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(rel, n.test) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb19-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">power=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(sig, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; # A tibble: 72 × 3
#&gt; # Groups:   rel [2]
#&gt;      rel n.test power
#&gt;    &lt;dbl&gt;  &lt;dbl&gt; &lt;dbl&gt;
#&gt;  1   0.5    500 0.175
#&gt;  2   0.5    600 0.192
#&gt;  3   0.5    700 0.232
#&gt;  4   0.5    800 0.3  
#&gt;  5   0.5    900 0.301
#&gt;  6   0.5   1000 0.346
#&gt;  7   0.5   1100 0.363
#&gt;  8   0.5   1200 0.4  
#&gt;  9   0.5   1300 0.419
#&gt; 10   0.5   1400 0.441
#&gt; # ℹ 62 more rows</code></pre>
</div>
</div>
<div class="cell" data-layout-align="center">
<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">pw_sem <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;">bind_rows</span>(</span>
<span id="cb21-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise_power</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">results</span>(power_sem1), sig <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;">|&gt;</span> <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;">loadings =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".70"</span>),</span>
<span id="cb21-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise_power</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">results</span>(power_sem2), sig <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;">|&gt;</span> <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;">loadings =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".50"</span>))</span>
<span id="cb21-4">fits_sem <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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split</span>(pw_sem, pw_sem<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>loadings), power_fit)</span>
<span id="cb21-5">n80_sem  <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;">sapply</span>(fits_sem, n_for_power)</span>
<span id="cb21-6">curves_sem <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;">bind_rows</span>(<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>(fits_sem), \(l) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">power_curve</span>(fits_sem[[l]], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</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;">4000</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <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;">loadings =</span> l)))</span></code></pre></div></div>
</div>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(pw_sem, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(n, power, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> loadings)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> pal[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey"</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> curves_sem, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</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>
<span id="cb22-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_pointrange</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;">ymin =</span> lower, <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;">size =</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>
<span id="cb22-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">annotate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"text"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4000</span>, <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;">c</span>(.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</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;">colour =</span> pal[<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;">"blue"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"orange"</span>)],</span>
<span id="cb22-6">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</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;">"loadings .70 (ω ≈ .74)"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"loadings .50 (ω = .50)"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_colour_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</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;">".70"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> pal[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>]], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".50"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> pal[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"orange"</span>]])) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>percent, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sample size"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Power"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blog</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-18-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://philippmasur.de/blog/2020-03-11-power-simulations-sem/index_files/figure-html/unnamed-chunk-18-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Simulated power curves for a correlation of .10 between two latent variables, for item factor loadings of .70 and .50, with 95% intervals and fitted curves. 80% power is reached at about n = 1,420 with loadings of .70 and only at about n = 3,200 with loadings of .50."></a></p>
</figure>
</div>
</div>
</div>
<p>We can see two very interesting things in this plot: First, a small correlation between two latent variables requires a LOT more participants than the same correlation between manifest variables (of course: There is no measurement error in the model). But the difference is considerable depending on the items’ reliability. Whereas factor loadings of .70 already require more participants to reach a power of 80% (roughly 1,420 compared to 770 in the first example), reducing factor loadings to .50 (the more realistic scenario; the scale reliability then drops to ω = .50) requires about 3,200 (!!!) participants to reach a power of 80%.</p>
</section>
<section id="power-for-a-simple-mediation-model-with-latent-variables" class="level2">
<h2 class="anchored" data-anchor-id="power-for-a-simple-mediation-model-with-latent-variables">Power for a simple mediation model with latent variables</h2>
<p>At last, let us consider a simple mediation model with latent variables. Again, the procedure remains the same, only the factor and effect matrix needs to be defined correctly (I again assumed good scales with factor loadings of .70). Likewise, the SEM needs to be specified correctly and the parameters of interest need to be extracted. Interestingly, we can also compute the power to detect the indirect or total effect even though we may only have assumptions about the direct paths in the model!</p>
<div class="cell" data-layout-align="center">
<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">model_med <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>(simNum,</span>
<span id="cb23-2">                      n, </span>
<span id="cb23-3">                      b1, </span>
<span id="cb23-4">                      b2,</span>
<span id="cb23-5">                      b3,</span>
<span id="cb23-6">                      x_ind1, x_ind2, x_ind3,</span>
<span id="cb23-7">                      m_ind1, m_ind2, </span>
<span id="cb23-8">                      y_ind1, y_ind2, y_ind3) {</span>
<span id="cb23-9">  </span>
<span id="cb23-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># compute factor matrix (in this case with measurement error)</span></span>
<span id="cb23-11">  factor <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(x_ind1, <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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># x1</span></span>
<span id="cb23-12">                     x_ind2, <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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># x2</span></span>
<span id="cb23-13">                     x_ind3, <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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># x3</span></span>
<span id="cb23-14">                     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, m_ind1, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># m1</span></span>
<span id="cb23-15">                     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, m_ind2, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># m2</span></span>
<span id="cb23-16">                     <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>, y_ind1,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># y1</span></span>
<span id="cb23-17">                     <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>, y_ind2,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># y2</span></span>
<span id="cb23-18">                     <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>, y_ind3),  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># y3</span></span>
<span id="cb23-19">                   <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;">8</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;">3</span>, </span>
<span id="cb23-20">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">byrow=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, </span>
<span id="cb23-21">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dimnames=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</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;">'x1'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x2'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'x3'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'m1'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'m2'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'y1'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'y2'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'y3'</span>),  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># rows (observed)</span></span>
<span id="cb23-22">                                 <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;">'x'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'m'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'y'</span>)))                           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># columns (latent)</span></span>
<span id="cb23-23">  </span>
<span id="cb23-24">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># matrix of the effects structure: gen_data() computes latent = Z %*% effect,</span></span>
<span id="cb23-25">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># with Z independent standard normal. To get m = b1*x + e_m and</span></span>
<span id="cb23-26">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># y = b2*x + b3*m + e_y (all latent variances = 1), the weights must be:</span></span>
<span id="cb23-27">  m_res <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>(<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> b1<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># m's own (residual) part</span></span>
<span id="cb23-28">  y_x   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> b2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> b1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> b3            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># weight of x in y = direct + indirect effect</span></span>
<span id="cb23-29">  y_m   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> b3 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> m_res              <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># weight of m's residual part in y</span></span>
<span id="cb23-30">  y_res <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>(<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> y_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;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> y_m<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># y's own (residual) part</span></span>
<span id="cb23-31">  effect <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="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>, b1,    y_x,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># x</span></span>
<span id="cb23-32">                     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, m_res, y_m,   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># m</span></span>
<span id="cb23-33">                     <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>,     y_res),<span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># y</span></span>
<span id="cb23-34">                    <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;">3</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;">3</span>, </span>
<span id="cb23-35">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">byrow=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, </span>
<span id="cb23-36">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dimnames=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</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;">'x'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'m'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'y'</span>),    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># rows</span></span>
<span id="cb23-37">                                  <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;">'x'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'m'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'y'</span>)))   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># columns</span></span>
<span id="cb23-38">  </span>
<span id="cb23-39">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># generate data based on both matrices</span></span>
<span id="cb23-40">  data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> paramtest<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;">gen_data</span>(factor, effect, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_cases =</span> n)</span>
<span id="cb23-41"></span>
<span id="cb23-42">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># fit lavaan model</span></span>
<span id="cb23-43">  model <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span></span>
<span id="cb23-44"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">   # measurement model</span></span>
<span id="cb23-45"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">   x =~ x1 + x2 + x3</span></span>
<span id="cb23-46"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">   y =~ y1 + y2 + y3</span></span>
<span id="cb23-47"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">   m =~ m1 + m2</span></span>
<span id="cb23-48"></span>
<span id="cb23-49"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">   # structural regression model</span></span>
<span id="cb23-50"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">   m ~ b1*x</span></span>
<span id="cb23-51"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">   y ~ b2*x + b3*m</span></span>
<span id="cb23-52"></span>
<span id="cb23-53"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">   ind := b1*b3</span></span>
<span id="cb23-54"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">   total := b2 + ind</span></span>
<span id="cb23-55"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  '</span></span>
<span id="cb23-56">  fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> lavaan<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;">sem</span>(model, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data)</span>
<span id="cb23-57">  ests <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> lavaan<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;">parameterEstimates</span>(fit)</span>
<span id="cb23-58">  </span>
<span id="cb23-59">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># extract relevant parameters</span></span>
<span id="cb23-60">  b1_est     <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ests[ests<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>label <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;">'b1'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'est'</span>]</span>
<span id="cb23-61">  b1_p       <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ests[ests<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>label <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;">'b1'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'pvalue'</span>]</span>
<span id="cb23-62">  b2_est     <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ests[ests<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>label <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;">'b2'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'est'</span>]</span>
<span id="cb23-63">  b2_p       <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ests[ests<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>label <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;">'b2'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'pvalue'</span>]</span>
<span id="cb23-64">  b3_est     <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ests[ests<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>label <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;">'b3'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'est'</span>]</span>
<span id="cb23-65">  b3_p       <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ests[ests<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>label <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;">'b3'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'pvalue'</span>]</span>
<span id="cb23-66">  ind_est    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ests[ests<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>label <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;">'ind'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'est'</span>]</span>
<span id="cb23-67">  ind_p      <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ests[ests<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>label <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;">'ind'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'pvalue'</span>]</span>
<span id="cb23-68">  total_est  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ests[ests<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>label <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;">'total'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'est'</span>]</span>
<span id="cb23-69">  total_p    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ests[ests<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>label <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;">'total'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'pvalue'</span>]</span>
<span id="cb23-70"></span>
<span id="cb23-71">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<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;">b1_est   =</span> b1_est,    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b1_p    =</span> b1_p,</span>
<span id="cb23-72">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b2_est   =</span> b2_est,    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b2_p    =</span> b2_p,</span>
<span id="cb23-73">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b3_est   =</span> b3_est,    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b3_p    =</span> b3_p,</span>
<span id="cb23-74">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ind_est  =</span> ind_est,   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ind_p   =</span> ind_p,</span>
<span id="cb23-75">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">total_est=</span> total_est, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">total_p =</span> total_p))</span>
<span id="cb23-76">}</span>
<span id="cb23-77"></span>
<span id="cb23-78"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">RNGkind</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"L'Ecuyer-CMRG"</span>)</span>
<span id="cb23-79"><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;">2023</span>)</span>
<span id="cb23-80">power_med <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;">cached</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sims/power_med.rds"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid_search</span>(</span>
<span id="cb23-81">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">func =</span> model_med, </span>
<span id="cb23-82">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">params =</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;">n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">700</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4500</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)),</span>
<span id="cb23-83">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b1 =</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;"># assuming a small effect for all three paths       </span></span>
<span id="cb23-84">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b2 =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb23-85">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b3 =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb23-86">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n.iter =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, </span>
<span id="cb23-87">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">output =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'data.frame'</span>,</span>
<span id="cb23-88">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x_ind1 =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>,    </span>
<span id="cb23-89">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x_ind2 =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, </span>
<span id="cb23-90">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x_ind3 =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, </span>
<span id="cb23-91">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">m_ind1 =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>,    </span>
<span id="cb23-92">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">m_ind2 =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>,</span>
<span id="cb23-93">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y_ind1 =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, </span>
<span id="cb23-94">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y_ind2 =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, </span>
<span id="cb23-95">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y_ind3 =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, </span>
<span id="cb23-96">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">parallel =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'snow'</span>,</span>
<span id="cb23-97">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncpus =</span> parallel<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;">detectCores</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="cb23-98">  ))</span></code></pre></div></div>
</div>
<div class="cell" data-layout-align="center">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract result table</span></span>
<span id="cb24-2">(result_med <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;">results</span>(power_med) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb24-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(n.test) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb24-4">    <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;">b1_sig =</span> (b1_p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">05</span>),</span>
<span id="cb24-5">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b2_sig =</span> (b2_p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">05</span>),</span>
<span id="cb24-6">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b3_sig =</span> (b3_p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">05</span>),</span>
<span id="cb24-7">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ind_sig =</span> (ind_p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">05</span>),</span>
<span id="cb24-8">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">total_sig =</span> (total_p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">05</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb24-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b1_power =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(b1_sig, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb24-10">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b2_power =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(b2_sig, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb24-11">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b3_power =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(b3_sig, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb24-12">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ind_power =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(ind_sig, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb24-13">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">total_power =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(total_sig, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; # A tibble: 39 × 6
#&gt;    n.test b1_power b2_power b3_power ind_power total_power
#&gt;     &lt;dbl&gt;    &lt;dbl&gt;    &lt;dbl&gt;    &lt;dbl&gt;     &lt;dbl&gt;       &lt;dbl&gt;
#&gt;  1    700    0.401    0.505    0.405    0.0557       0.589
#&gt;  2    800    0.423    0.564    0.404    0.0652       0.637
#&gt;  3    900    0.485    0.592    0.489    0.0956       0.666
#&gt;  4   1000    0.550    0.662    0.530    0.145        0.756
#&gt;  5   1100    0.561    0.694    0.557    0.148        0.767
#&gt;  6   1200    0.599    0.727    0.598    0.195        0.819
#&gt;  7   1300    0.648    0.746    0.662    0.246        0.825
#&gt;  8   1400    0.693    0.781    0.690    0.287        0.851
#&gt;  9   1500    0.698    0.829    0.712    0.309        0.879
#&gt; 10   1600    0.731    0.811    0.736    0.378        0.882
#&gt; # ℹ 29 more rows</code></pre>
</div>
</div>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1">pw_med <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;">results</span>(power_med) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb26-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ends_with</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_p"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"effect"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb26-3">  <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;">effect =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sub</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_p$"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, effect)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb26-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(effect) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb26-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_modify</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;">summarise_power</span>(.x, p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">05</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb26-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>()</span>
<span id="cb26-7">fits_med <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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split</span>(pw_med, pw_med<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>effect), power_fit)</span>
<span id="cb26-8">n80_med  <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;">sapply</span>(fits_med, n_for_power)</span>
<span id="cb26-9">curves_med <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;">bind_rows</span>(<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>(fits_med), \(e) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">power_curve</span>(fits_med[[e]], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">700</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;">4500</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <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;">effect =</span> e)))</span></code></pre></div></div>
</div>
<div class="cell" data-layout-align="center">
<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;"># Three direct paths in one colour, the indirect and total effect highlighted</span></span>
<span id="cb27-2">type <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="at" style="color: #657422;
background-color: null;
font-style: inherit;">b1 =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"direct"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b2 =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"direct"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b3 =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"direct"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ind =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"indirect"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">total =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"total"</span>)</span>
<span id="cb27-3">cols <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="at" style="color: #657422;
background-color: null;
font-style: inherit;">direct =</span> pal[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>]], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">indirect =</span> pal[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"orange"</span>]], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">total =</span> pal[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"green"</span>]])</span>
<span id="cb27-4">labs_med <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;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</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;">"direct"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"total"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"indirect"</span>),</span>
<span id="cb27-5">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</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;">"direct paths a, b, c′"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"total effect c′ + a × b"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"indirect effect a × b"</span>),</span>
<span id="cb27-6">                   <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;">c</span>(.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>))</span>
<span id="cb27-7"></span>
<span id="cb27-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(pw_med, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> type[effect]), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(n, power, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> type, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> effect)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb27-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_hline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yintercept =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> pal[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey"</span>]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb27-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(curves_med, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> type[effect]), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb27-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_pointrange</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;">ymin =</span> lower, <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;">size =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb27-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> labs_med, <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> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4500</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;">label =</span> label, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> type),</span>
<span id="cb27-13">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inherit.aes =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</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>
<span id="cb27-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_colour_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> cols) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb27-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>percent, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb27-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sample size"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Power"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb27-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_blog</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="index_files/figure-html/unnamed-chunk-23-1.png" class="lightbox" data-gallery="quarto-lightbox-gallery-3"><img src="https://philippmasur.de/blog/2020-03-11-power-simulations-sem/index_files/figure-html/unnamed-chunk-23-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%" alt="Simulated power curves for the latent mediation model with 95% intervals and fitted curves. The three direct paths (all .10) reach 80% power at about n = 1,440 to 1,810, the total effect earlier, and the indirect effect only at about n = 2,730."></a></p>
</figure>
</div>
</div>
</div>
<p>The result shows (as expected) that we need roughly the same sample size (about 1,440 to 1,810) to test the three direct paths in the model (as we assumed the same effect size of .10 for all three relationships). The path from x to m reaches 80% a bit earlier than the two paths into y, because the latter are estimated simultaneously (x and m are correlated) and the mediator is measured with only two indicators. However, we need considerably more participants to test the indirect effect (.10 × .10 = .01) with the same power! Here, we would need roughly 2,730 participants! Note that lavaan tests the indirect effect with the delta method (a Sobel-type test), which is known to be conservative. Bootstrapped or Monte Carlo confidence intervals have somewhat more power, but they are too slow to compute in every replication of a simulation like this one.</p>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>Simulating power for complex models is always going to be challenging due to the large number of parameters that one needs to make assumptions about. That said, the <code>paramtest</code> package provides a framework for doing so. In the examples above, I only varied the sample sizes. But I could have varied the effect sizes (or any other parameter in the simulation), too. This would allow analyzing the interaction between sample size and effect size on power to test such effects – from my point of view, this should be an important step before any study that aims at implementing structural equation modeling.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sessionInfo</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#&gt; R version 4.4.0 (2024-04-24)
#&gt; Platform: aarch64-apple-darwin20
#&gt; Running under: macOS 26.6.2
#&gt; 
#&gt; Matrix products: default
#&gt; BLAS:   /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib 
#&gt; LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0
#&gt; 
#&gt; Random number generation:
#&gt;  RNG:     L'Ecuyer-CMRG 
#&gt;  Normal:  Inversion 
#&gt;  Sample:  Rejection 
#&gt;  
#&gt; locale:
#&gt; [1] C.UTF-8/C.UTF-8/C.UTF-8/C/C.UTF-8/C.UTF-8
#&gt; 
#&gt; time zone: Europe/Amsterdam
#&gt; tzcode source: internal
#&gt; 
#&gt; attached base packages:
#&gt; [1] stats     graphics  grDevices utils     datasets  methods   base     
#&gt; 
#&gt; other attached packages:
#&gt;  [1] pwr_1.3-0       paramtest_0.1.0 lubridate_1.9.3 forcats_1.0.0  
#&gt;  [5] stringr_1.6.0   dplyr_1.2.0     purrr_1.2.1     readr_2.1.5    
#&gt;  [9] tidyr_1.3.2     tibble_3.3.1    ggplot2_4.0.2   tidyverse_2.0.0
#&gt; 
#&gt; loaded via a namespace (and not attached):
#&gt;  [1] gtable_0.3.6       jsonlite_2.0.0     compiler_4.4.0     tidyselect_1.2.1  
#&gt;  [5] scales_1.4.0       yaml_2.3.12        fastmap_1.2.0      R6_2.6.1          
#&gt;  [9] labeling_0.4.3     generics_0.1.4     knitr_1.51         htmlwidgets_1.6.4 
#&gt; [13] pillar_1.11.1      RColorBrewer_1.1-3 tzdb_0.4.0         rlang_1.1.7       
#&gt; [17] utf8_1.2.6         stringi_1.8.7      xfun_0.57          S7_0.2.1          
#&gt; [21] timechange_0.3.0   cli_3.6.5          withr_3.0.2        magrittr_2.0.4    
#&gt; [25] digest_0.6.39      grid_4.4.0         hms_1.1.3          lifecycle_1.0.5   
#&gt; [29] vctrs_0.7.2        evaluate_1.0.5     glue_1.8.0         farver_2.1.2      
#&gt; [33] rmarkdown_2.30     tools_4.4.0        pkgconfig_2.0.3    htmltools_0.5.8.1</code></pre>
</div>
</div>
<section id="references" class="level3">
<h3 class="anchored" data-anchor-id="references">References</h3>
<p>Hughes, J. (2017). <em>paramtest: R package for varying parameters in simulations or other iterative processes.</em>&nbsp;https://github.com/jeff-hughes/paramtest</p>
<p>Kline, R. (2015).&nbsp;<em>Principles and Practice of Structural Equation Modeling.</em> The Guilford Press.</p>


<template id="pm-footer-icons"><a href="#" data-pm-mail="p.k.masur|vu.nl" aria-label="Email" title="Email"><svg class="pm-icon" aria-hidden="true"><use href="#i-mail"></use></svg></a><a href="https://scholar.google.com/citations?user=XA_WthYAAAAJ" target="_blank" rel="noopener" aria-label="Google Scholar" title="Google Scholar"><svg class="pm-icon" aria-hidden="true"><use href="#i-scholar"></use></svg></a><a href="https://orcid.org/0000-0003-3065-7305" target="_blank" rel="noopener" aria-label="ORCID" title="ORCID"><svg class="pm-icon" aria-hidden="true"><use href="#i-orcid"></use></svg></a><a href="https://osf.io/yuqv5/" target="_blank" rel="noopener" aria-label="Open Science Framework" title="Open Science Framework"><svg class="pm-icon" aria-hidden="true"><use href="#i-osf"></use></svg></a><a href="https://github.com/masurp" target="_blank" rel="noopener" aria-label="GitHub" title="GitHub"><svg class="pm-icon" aria-hidden="true"><use href="#i-github"></use></svg></a><a href="https://bsky.app/profile/masurphil.bsky.social" target="_blank" rel="noopener" aria-label="Bluesky" title="Bluesky"><svg class="pm-icon" aria-hidden="true"><use href="#i-bluesky"></use></svg></a><a href="https://research.vu.nl/en/persons/philipp-k-masur" target="_blank" rel="noopener" aria-label="VU Amsterdam" title="VU Amsterdam"><svg class="pm-icon" aria-hidden="true"><use href="#i-landmark"></use></svg></a></template>
</section>
</section>

 ]]></description>
  <category>R &amp; Statistics</category>
  <category>Open Science</category>
  <guid>https://philippmasur.de/blog/2020-03-11-power-simulations-sem/</guid>
  <pubDate>Tue, 10 Mar 2020 23:00:00 GMT</pubDate>
  <media:content url="https://philippmasur.de/blog/2020-03-11-power-simulations-sem/featured.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>How to do specification curve analyses in R: Introducing ‘specr’</title>
  <link>https://philippmasur.de/blog/2020-01-02-introducing-specr/</link>
  <description><![CDATA[ 

<!-- Icon sprite: 24×24 line icons, 1.6 stroke. Every shape has pathLength="1"
     so CSS can animate stroke-dashoffset 1 → 0 ("draw" the icon).
     Use: <svg class="pm-icon"><use href="#i-mail"/></svg> -->





<p><img src="https://masurp.github.io/specr/reference/figures/specr_logo.png" class="img-fluid">In the last month, <a href="https://underused.org/">Michael Scharkow</a> and I have worked on a new R-package called <code>specr</code>. The goal was to facilitate specification curve analyses (also called multiverse analyses). The idea behind a <em>specification curve analysis</em> stems from the observation that a researcher has many degrees of freedom when conducting a quantitative analysis of a data set and sometimes, we do not really know how different decisions may impact the results.</p>
<p>It starts with the question of how to operationalize the dependent and the independent variable, what model to estimate, which control variables to include, what transformation to conduct, and ends with which cases to exclude or include (obviously there are even many more researchers’ degrees of freedom!). The problem is that the options in many of these decisions are somewhat similarly defensible. For example, we often have good arguments for both excluding and including a certain variable when estimating a latent variable. Or we might find good reasons for both excluding as well as including outliers. According to Gelman and Loken<sup>1</sup>, researchers literally find themselves in a “garden of forking paths” in which a multitude of possibilities could yield a multiverse of results (see Fig 1).</p>
<p><a href="img/fig-01.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://philippmasur.de/blog/2020-01-02-introducing-specr/img/fig-01.png" class="img-fluid"></a></p>
<p><em>Fig 1.</em> A visualization of the garden of forking paths.</p>
<p>The proposed solution to this problem is to run all theoretically defensible combinations of analytical choices (specifications) in order to obtain all possible results from the data. Simonsohn, Simmons, and Nelson<sup>2</sup> thus proposed the specification curve analysis: a statistical approach that aims at running all reasonable specifications and investigating all obtained results in using a curve plot. So far, only a few papers have used such an approach<sup>3</sup>, revealing that in some research areas, almost any result is obtainable even within the limits of just one data set.</p>
<section id="section" class="level2">
<h2 class="anchored" data-anchor-id="section"></h2>
</section>
<section id="the-idea-behind-specr" class="level2">
<h2 class="anchored" data-anchor-id="the-idea-behind-specr">The idea behind <code>specr</code></h2>
<p>So far, most researchers used loop-based functions in <em>R</em> to estimate all the models. They set up a results frame which gets filled with the results from each specification through a sequential analysis of all models. Although this makes a lot of sense, it has some limitations such as slow computation (due to the sequential nature) or a somewhat complex coding procedure.</p>
<p>With<code>specr</code>, we wanted to create convenient functions that standardize this procedure and allow for faster computation. Although not implement yet, this approach will allow to use parallelization of the model estimations. We are currently investigating the implementation of the great package <code>furrr</code>&nbsp;(check our github for updates on this!). In what follows, I will exemplify the use of <code>specr</code> using a simple simulated data set (taken from the documentation of <code>specr</code>).</p>
<p>For more information about the various functions , visit the package <a href="https://masurp.github.io/specr/index.html">documentation</a>. &nbsp;You can find comprehensive examples and vignettes on how to use the various functions of the package on the following pages:</p>
<ul>
<li><a href="https://masurp.github.io/specr/articles/specr.html">Getting started</a>: A comprehensive example. This vignette illustrates the major functions of the package.</li>
<li><a href="https://masurp.github.io/specr/articles/custom-plot.html">Customizing specification curve plots</a>: This vignette exemplifies various ways to plot the specification curve.</li>
<li><a href="https://masurp.github.io/specr/articles/decompose_var.html">Decomposing the variance of the specification curve</a>: An example of how to investigate variance components of the specification curve.</li>
<li><a href="https://masurp.github.io/specr/articles/progress.html">Visualizing progress during estimation</a>: This vignette explains how to create a customizable progress bar for longer computations.</li>
</ul>
</section>
<section id="section-1" class="level2">
<h2 class="anchored" data-anchor-id="section-1"></h2>
</section>
<section id="a-simple-example" class="level2">
<h2 class="anchored" data-anchor-id="a-simple-example">A simple example</h2>
<section id="how-to-install-the-r-package" class="level3">
<h3 class="anchored" data-anchor-id="how-to-install-the-r-package">How to install the R-package</h3>
<p>You can install the package from CRAN like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># install.packages("specr")</span></span></code></pre></div></div>
<p>You can also install the most recent development version from the respective&nbsp;<a href="https://github.com/">GitHub</a>&nbsp;repository with:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># install.packages("devtools")</span></span>
<span id="cb2-2">devtools<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;">install_github</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"masurp/specr"</span>)</span></code></pre></div></div>
</section>
<section id="check-the-data" class="level3">
<h3 class="anchored" data-anchor-id="check-the-data">Check the data</h3>
<p>In order to understand what type of analytical choices exists, we need to understand our data set.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load library</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(specr)</span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The example data included in the package</span></span>
<span id="cb3-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(example_data)</span>
<span id="cb3-6"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##         x1         x2         c1        c2        y1         y2 group1</span></span>
<span id="cb3-7"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## 1 1.533913  1.3697122  0.5424902 3.8924435 23.500543 10.4269278      0</span></span>
<span id="cb3-8"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## 2 1.680639  1.5163745 -1.2415868 3.3377268 17.017955  0.5733467      1</span></span>
<span id="cb3-9"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## 3 1.223941 -0.2381044  0.1405891 0.8911959 -3.678272  4.2303190      0</span></span>
<span id="cb3-10"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## 4 1.765276  0.9524049  4.0397943 1.8567454 21.668684 14.8865252      1</span></span>
<span id="cb3-11"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## 5 1.907134  0.6282816  3.1002518 5.5840574 32.713106 20.5251920      0</span></span>
<span id="cb3-12"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## 6 1.710695  1.1898467  0.4648824 4.0239483 20.422171  4.3471236      1</span></span>
<span id="cb3-13"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##   group2</span></span>
<span id="cb3-14"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## 1      A</span></span>
<span id="cb3-15"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## 2      C</span></span>
<span id="cb3-16"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## 3      B</span></span>
<span id="cb3-17"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## 4      B</span></span>
<span id="cb3-18"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## 5      A</span></span>
<span id="cb3-19"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## 6      C</span></span></code></pre></div></div>
<p>In this example, we assume that <code>x</code> refers to independent variables, <code>y</code> represents dependent variables, <code>c</code> refers to control variables, and <code>group</code> denotes potential grouping variables.</p>
</section>
<section id="define-analytical-choices-and-run-the-analyses" class="level3">
<h3 class="anchored" data-anchor-id="define-analytical-choices-and-run-the-analyses">Define analytical choices and run the analyses</h3>
<p>The main function of the package is <code>run_specs()</code>. We need to provide the data set, and all analytical choices that we deem resonable. In this cases, we investigate the influence of different dependent and independent variables, various control variables, as well as how subsets of <code>group1</code> affect the results.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Run specification curve analysis</span></span>
<span id="cb4-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;">run_specs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df =</span> example_data, </span>
<span id="cb4-3">                     <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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y2"</span>), </span>
<span id="cb4-4">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</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;">"x1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x2"</span>), </span>
<span id="cb4-5">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</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;">"lm"</span>), </span>
<span id="cb4-6">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">controls =</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;">"c1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"c2"</span>), </span>
<span id="cb4-7">                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subsets =</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;">group1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(example_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>group1)))</span>
<span id="cb4-8"></span>
<span id="cb4-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check</span></span>
<span id="cb4-10">results</span>
<span id="cb4-11"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## # A tibble: 48 x 12</span></span>
<span id="cb4-12"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##    x     y     model controls estimate std.error statistic  p.value</span></span>
<span id="cb4-13"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##    &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;       &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;    &lt;dbl&gt;</span></span>
<span id="cb4-14"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##  1 x1    y1    lm    c1 + c2    4.95       0.525    9.43   3.11e-18</span></span>
<span id="cb4-15"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##  2 x2    y1    lm    c1 + c2    6.83       0.321   21.3    1.20e-57</span></span>
<span id="cb4-16"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##  3 x1    y2    lm    c1 + c2   -0.227      0.373   -0.607  5.44e- 1</span></span>
<span id="cb4-17"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##  4 x2    y2    lm    c1 + c2    0.985      0.324    3.04   2.62e- 3</span></span>
<span id="cb4-18"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##  5 x1    y1    lm    c1         5.53       0.794    6.97   2.95e-11</span></span>
<span id="cb4-19"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##  6 x2    y1    lm    c1         8.07       0.557   14.5    6.90e-35</span></span>
<span id="cb4-20"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##  7 x1    y2    lm    c1         0.0461     0.466    0.0989 9.21e- 1</span></span>
<span id="cb4-21"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##  8 x2    y2    lm    c1         1.61       0.394    4.10   5.72e- 5</span></span>
<span id="cb4-22"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##  9 x1    y1    lm    c2         5.15       0.625    8.24   9.95e-15</span></span>
<span id="cb4-23"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## 10 x2    y1    lm    c2         6.50       0.466   13.9    5.38e-33</span></span>
<span id="cb4-24"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## # … with 38 more rows, and 4 more variables: conf.low &lt;dbl&gt;,</span></span>
<span id="cb4-25"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## #   conf.high &lt;dbl&gt;, obs &lt;int&gt;, subsets &lt;chr&gt;</span></span></code></pre></div></div>
<p>The resulting data frame includes relevant statistics for each of the estiamted models and the number of observations that have been used in each specification.</p>
<p>We can plot a simple visualization of the garden of forking paths to understand how our analytical choices led to the 48 specifications.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" 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;">plot_decisiontree</span>(results, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend =</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;">label =</span> T)</span></code></pre></div></div>
<p><a href="img/fig-02.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://philippmasur.de/blog/2020-01-02-introducing-specr/img/fig-02.png" class="img-fluid"></a></p>
<p><em>Fig 2.</em> How the analytical choices lead to a large number of specifications.</p>
</section>
<section id="investigate-the-specifications" class="level3">
<h3 class="anchored" data-anchor-id="investigate-the-specifications">Investigate the specifications</h3>
<p>The package includes a simple function <code>summarise_specs()</code> that allows to get a first summary of the results.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" 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;"># basic summary of the entire specification curve</span></span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise_specs</span>(results)</span>
<span id="cb6-3"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## # A tibble: 1 x 7</span></span>
<span id="cb6-4"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##   median   mad    min   max   q25   q75   obs</span></span>
<span id="cb6-5"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##    &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;</span></span>
<span id="cb6-6"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## 1   3.55  4.73 -0.227  9.03 0.727  7.62   250</span></span>
<span id="cb6-7"></span>
<span id="cb6-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># summary by specific groups and  statistics</span></span>
<span id="cb6-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise_specs</span>(results, </span>
<span id="cb6-10">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stats =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lst</span>(mean, median, min, max), </span>
<span id="cb6-11">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</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;">"x"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>))</span>
<span id="cb6-12"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## # A tibble: 4 x 7</span></span>
<span id="cb6-13"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## # Groups:   x [2]</span></span>
<span id="cb6-14"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##   x     y      mean median    min   max   obs</span></span>
<span id="cb6-15"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##   &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;</span></span>
<span id="cb6-16"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## 1 x1    y1    6.63   6.52   4.95  8.27    250</span></span>
<span id="cb6-17"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## 2 x1    y2    0.237  0.157 -0.227 0.727   250</span></span>
<span id="cb6-18"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## 3 x2    y1    7.80   7.84   6.50  9.03    250</span></span>
<span id="cb6-19"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## 4 x2    y2    1.36   1.34   0.653 2.14    250</span></span></code></pre></div></div>
<p>However, in order to grasp how the different analytical choices affect the outcome of interest (in this case, the estimate refers to the unstandardized regression coefficient <em>b</em>), most researchers plot a specification curve. The function <code>plot_specs()</code> produces such a typical visualization.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot specification curve analysis</span></span>
<span id="cb7-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_specs</span>(results)</span></code></pre></div></div>
<p><a href="img/fig-03.png" class="lightbox" data-gallery="quarto-lightbox-gallery-3"><img src="https://philippmasur.de/blog/2020-01-02-introducing-specr/img/fig-03.png" class="img-fluid"></a></p>
<p><em>Fig 3.</em> Visualization of the specification curve analyis.</p>
<p>Here, we can clearly see that the different dependent variables exert a big influence on the obtained regression coefficient.</p>
</section>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>We do see a lot of value in investigating how analytical choices affect a statistical outcome of interest. However, we strongly caution against using <code>specr</code> as a tool to somehow arrive at a better estimate. Running a specification curve analysis does not make findings any more reliable, valid or generalizable than a single analyis. The method is only meant to inform about the effects of analytical choices on results, and not a better way to estimate a correlation or effect.</p>
<p>We hope that our package can help in this process. If you find any bugs or would like to propose changes, please do so via the respective Github page: <a href="https://github.com/masurp/specr/" class="uri">https://github.com/masurp/specr/</a>. We are looking forward to feedback and comments.</p>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<div class="callout callout-style-simple callout-note no-icon callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon no-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>1 comment from the original post
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>These comments were left on the earlier WordPress version of this post.</p>
<div class="archived-comment">
<p><strong>Anonymous Llama</strong> · 4 May 2024</p>
<p>I got an error for this part. Any suggestions for how to fix it?</p>
<p>&gt; summarise_specs(results,<br>
+ stats = lst(mean, median, min, max),<br>
+ group = c(“x”, “y”))<br>
Error in `dplyr::group_by()`:<br>
ℹ In argument: `group = c(“x”, “y”)`.<br>
Caused by error:<br>
! `group` must be size 64 or 1, not 2.<br>
Run `rlang::last_trace()` to see where the error occurred.</p>
<p>I’m running this version of R:</p>
<p>R version 4.3.2 (2023-10-31) — “Eye Holes”<br>
Copyright (C) 2023 The R Foundation for Statistical Computing<br>
Platform: aarch64-apple-darwin20 (64-bit)</p>
</div>
</div>
</div>
</div>


<template id="pm-footer-icons"><a href="#" data-pm-mail="p.k.masur|vu.nl" aria-label="Email" title="Email"><svg class="pm-icon" aria-hidden="true"><use href="#i-mail"></use></svg></a><a href="https://scholar.google.com/citations?user=XA_WthYAAAAJ" target="_blank" rel="noopener" aria-label="Google Scholar" title="Google Scholar"><svg class="pm-icon" aria-hidden="true"><use href="#i-scholar"></use></svg></a><a href="https://orcid.org/0000-0003-3065-7305" target="_blank" rel="noopener" aria-label="ORCID" title="ORCID"><svg class="pm-icon" aria-hidden="true"><use href="#i-orcid"></use></svg></a><a href="https://osf.io/yuqv5/" target="_blank" rel="noopener" aria-label="Open Science Framework" title="Open Science Framework"><svg class="pm-icon" aria-hidden="true"><use href="#i-osf"></use></svg></a><a href="https://github.com/masurp" target="_blank" rel="noopener" aria-label="GitHub" title="GitHub"><svg class="pm-icon" aria-hidden="true"><use href="#i-github"></use></svg></a><a href="https://bsky.app/profile/masurphil.bsky.social" target="_blank" rel="noopener" aria-label="Bluesky" title="Bluesky"><svg class="pm-icon" aria-hidden="true"><use href="#i-bluesky"></use></svg></a><a href="https://research.vu.nl/en/persons/philipp-k-masur" target="_blank" rel="noopener" aria-label="VU Amsterdam" title="VU Amsterdam"><svg class="pm-icon" aria-hidden="true"><use href="#i-landmark"></use></svg></a></template>
</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>Gelman, A. &amp; Loken, E. (2013). <em>The garden of forking paths: Why multiple comparisons can be a problem, even when there is no “fishing expedition” or “p-hacking” and the research hypothesis was posited ahead of time.</em> <a href="http://www.stat.columbia.edu/~gelman/research/unpublished/p_hacking.pdf" class="uri">http://www.stat.columbia.edu/~gelman/research/unpublished/p_hacking.pdf</a>↩︎</p></li>
<li id="fn2"><p>Simonsohn, U., Simmons, J. P., &amp; Nelson, L. D. (2019). Specification Curve: Descriptive and Inferential Statistics for all Plausible Specifications Available at: <a href="http://urisohn.com/sohn_files/wp/wordpress/wp-content/uploads/Paper-Specification-curve-2019-11-16.pdf" class="uri">http://urisohn.com/sohn_files/wp/wordpress/wp-content/uploads/Paper-Specification-curve-2019-11-16.pdf</a>↩︎</p></li>
<li id="fn3"><p>E.g., Steegen, S. Tuerlinckx, F., Gelman, A., &amp; Vanpaemel, W. (2016). Increasing Transparency Through a Multiverse Analysis. Perspectives on Psychological Science, 11(5), 702-712. <a href="https://doi.org/10.1177/1745691616658637" class="uri">https://doi.org/10.1177/1745691616658637</a>; Rohrer, J., Egloff, B., Schmuckle, C. (2017). Probing Birth-Order Effects on Narrow Traits Using Specification-Curve Analysis. Psychological Science, 28(12), 1821–1832. <a href="https://doi.org/10.1177/0956797617723726" class="uri">https://doi.org/10.1177/0956797617723726</a>; Orben, A., Przybylski, A. K. (2019). The association between adolescent well-being and digital technology use. Nature Human Behaviour, <a href="https://doi.org/10.1038/s41562-018-0506-1" class="uri">https://doi.org/10.1038/s41562-018-0506-1</a>; Orben, A., Dienlin, T., &amp; Przybylski, A. K. (2019). Social media’s enduring effect on adolescent life satisfaction. Proceedings of the National Academy of Sciences of the United States of America. <a href="https:///doi.org/10.1073/pnas.1902058116" class="uri">https:///doi.org/10.1073/pnas.1902058116</a>↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>R &amp; Statistics</category>
  <category>Open Science</category>
  <guid>https://philippmasur.de/blog/2020-01-02-introducing-specr/</guid>
  <pubDate>Wed, 01 Jan 2020 23:00:00 GMT</pubDate>
  <media:content url="https://philippmasur.de/blog/2020-01-02-introducing-specr/featured.png" medium="image" type="image/png" height="64" width="144"/>
</item>
<item>
  <title>I started a post-doc position at the Johannes Gutenberg University Mainz</title>
  <link>https://philippmasur.de/blog/2019-05-02-postdoc-mainz/</link>
  <description><![CDATA[ 

<!-- Icon sprite: 24×24 line icons, 1.6 stroke. Every shape has pathLength="1"
     so CSS can animate stroke-dashoffset 1 → 0 ("draw" the icon).
     Use: <svg class="pm-icon"><use href="#i-mail"/></svg> -->





<p>I am excited to share with you that I started a post-doc position at the <a href="https://www.ifp.uni-mainz.de/">Department of Communication</a> at the <a href="http://www.uni-mainz.de/">Johannes Gutenberg University</a> in Mainz. Moving back to Mainz is exciting and a bit nostalgic too since I started my academic career (as a young student, of course) at this very university and I have good memories of living and studying in this city. I will be working in the <a href="https://www.kom.ifp.uni-mainz.de/">Dynamics of Society and Communication Research Group</a>&nbsp;(led by Prof.&nbsp;Dr.&nbsp;Christian Schemer).&nbsp;I am eager to continue my academic path in this amazing community and contribute to the teaching and research of this great institution.</p>
<p><a href="img/IMG_6431kl-1024x683.jpg" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://philippmasur.de/blog/2019-05-02-postdoc-mainz/img/IMG_6431kl-1024x683.jpg" class="img-fluid"></a></p>
<p>Nonetheless, I am changing affiliations with a laughing and a crying eye because starting somewhere new means saying goodbye to my academic home for the last six years. I am grateful for these years at the <a href="https://medienpsychologie.uni-hohenheim.de/">Department of Media Psychology</a>&nbsp;at the University of Hohenheim and I am deeply indepted to Sabine Trepte for her continuous support and mentorship and for providing me with the space and the environment for academic growth. I also want to thank all my past colleagues and friends at the University of Hohenheim (Tobias Dienlin, Michael Scharkow, Thilo von Pape, Doris Teutsch, Laura Loy, Laura Loths, Max Braun, Cornelia Mothes, Josephine Schmitt, Alexandra Lux, and Susanne Braunschweiger) for six exciting and memorable years.</p>
<p>I am excited about the future, eager to advance my research, and I am very much looking forward to continuing my personal learning process that I believe “research” is about.</p>


<template id="pm-footer-icons"><a href="#" data-pm-mail="p.k.masur|vu.nl" aria-label="Email" title="Email"><svg class="pm-icon" aria-hidden="true"><use href="#i-mail"></use></svg></a><a href="https://scholar.google.com/citations?user=XA_WthYAAAAJ" target="_blank" rel="noopener" aria-label="Google Scholar" title="Google Scholar"><svg class="pm-icon" aria-hidden="true"><use href="#i-scholar"></use></svg></a><a href="https://orcid.org/0000-0003-3065-7305" target="_blank" rel="noopener" aria-label="ORCID" title="ORCID"><svg class="pm-icon" aria-hidden="true"><use href="#i-orcid"></use></svg></a><a href="https://osf.io/yuqv5/" target="_blank" rel="noopener" aria-label="Open Science Framework" title="Open Science Framework"><svg class="pm-icon" aria-hidden="true"><use href="#i-osf"></use></svg></a><a href="https://github.com/masurp" target="_blank" rel="noopener" aria-label="GitHub" title="GitHub"><svg class="pm-icon" aria-hidden="true"><use href="#i-github"></use></svg></a><a href="https://bsky.app/profile/masurphil.bsky.social" target="_blank" rel="noopener" aria-label="Bluesky" title="Bluesky"><svg class="pm-icon" aria-hidden="true"><use href="#i-bluesky"></use></svg></a><a href="https://research.vu.nl/en/persons/philipp-k-masur" target="_blank" rel="noopener" aria-label="VU Amsterdam" title="VU Amsterdam"><svg class="pm-icon" aria-hidden="true"><use href="#i-landmark"></use></svg></a></template>

 ]]></description>
  <category>News</category>
  <guid>https://philippmasur.de/blog/2019-05-02-postdoc-mainz/</guid>
  <pubDate>Wed, 01 May 2019 22:00:00 GMT</pubDate>
  <media:content url="https://philippmasur.de/blog/2019-05-02-postdoc-mainz/featured.jpg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Reconceptualizing online privacy literacy</title>
  <link>https://philippmasur.de/blog/2019-03-28-reconceptualizing-online-privacy-literacy/</link>
  <description><![CDATA[ 

<!-- Icon sprite: 24×24 line icons, 1.6 stroke. Every shape has pathLength="1"
     so CSS can animate stroke-dashoffset 1 → 0 ("draw" the icon).
     Use: <svg class="pm-icon"><use href="#i-mail"/></svg> -->





<p>In recent years, online privacy literacy has often been regarded as a potential solution to people’s seemingly paradoxical behaviors in online environments. Based on empirical findings that Internet users rarely implement privacy and data protection strategies,<sup>1</sup> it has been suggested that they are simply not literate enough to make informed decisions in online environments.&nbsp;Throughout the last years, we<sup>2</sup> have been working on reconceptualizing online privacy literacy and providing reliable and validated instruments to measure online privacy literacy objectively. In this post, I would like to summarize different projects and publications that I was involved in over the last years. I also would like to describe how they led to my latest publication called “It is more than just privacy risk awareness: A reconceptualization of online privacy literacy as a composition of knowledge, abilities, and skills”.</p>
<hr>
<p>Online privacy literacy has often been regarded as a requirement for <em>informational</em> self-determination. Without the knowledge and skills necessary to navigate online environments and to ensure the protection of one’s personal information as well as to limit other’s access to the self, people cannot be autonomous in communicating and using online services. Hence, fostering online privacy literacy through education or other types of interventions has often been discussed as a potential solution to data protection issues on the Internet – both in political and scientific circles. Yet, it seemed that the the discussions remained superficial. Blinded by the potential of education to stopgap people’s seemingly paradoxical behavior, <sup>3</sup> people seemed all to ready to believe in an individual’s ability to learn the skills and knowledge to navigate the Internet safely and responsibly.</p>
<p>But I think that also the research lacked behind. First studies in recent years only <em>started</em> to investigate people’s privacy literacy. Their initial findings suggest that the level of literacy was rather low<sup>4</sup> and that people often overestimate their knowledge<sup>5</sup> A more recent study which investigated three dimensions of online privacy literacy found that higher levels in all three dimensions predicted data protection.<sup>6</sup> Although prior work on online privacy literacy existed, we realized that no comprehensive concept of, and no validated and reliable instrument to measure online privacy literacy existed. Prior concepts were often fragmented (focusing on one particular knowledge area, e.g., knowledge about data protection law or institutional practices of data collection and usage) or were focusing on particular platforms (e.g., Facebook). Additionally, such instruments were either self-reports or random, unvalidated knowledge items. In light of this,&nbsp;we set out to define this concept more comprehensively.</p>
<section id="conceptualizing-online-privacy-literacy-as-a-multi-dimensional-knowledge-concept" class="level3">
<h3 class="anchored" data-anchor-id="conceptualizing-online-privacy-literacy-as-a-multi-dimensional-knowledge-concept">Conceptualizing online privacy literacy as a multi-dimensional knowledge concept</h3>
<p>So far, online privacy literacy had mostly been operationalized as a pure knowledge concept. A few studies had shown that such a privacy knowledge is related to more self-data-protection. In a first step, we conducted&nbsp;an exhaustive qualitative content analysis of prior literature on privacy literacy and a profound content analysis of different sources (privacy policies, juridical documents, EU deliverables…) capturing a variety of aspects relevant to online privacy. The aim was to capture any aspect or area that we deemed related to online privacy literacy. What knowledge should people have? Which skills should they possess?</p>
<p>In the end, we came up with four knowledge dimensions:&nbsp;(1) knowledge about practices of organizations, institutions and online service providers (e.g., data collection, sharing, usage, and analysis); (2) knowledge about technical aspects of online privacy and data protection (e.g., understanding concepts such as cache, cookies, and how they relate to one’s privacy); (3) knowledge about laws and legal aspects of online data protection as well as (4) knowledge about European directives on privacy and data protection, and (5) knowledge about specific strategies for individual privacy regulation (e.g., what type of password to use, how to manage up privacy setting, etc). Based on these dimensions, we defined online privacy literacy as</p>
<blockquote class="blockquote">
<p>a combination of factual or declarative (“knowing that”) and procedural (“knowing how”) knowledge about online privacy. In terms of declarative knowledge, online privacy literacy refers to the users’ knowledge about technical aspects of online data protection and about laws and directives as well as institutional practices. In terms of procedural knowledge, online privacy literacy refers to the users’ ability to apply strategies for individual privacy regulation and data protection (p.&nbsp;339).</p>
</blockquote>
<p>Based on the content analysis, we then developed a first item pool that we believed captured all aspects of online privacy literacy.&nbsp;We summarized the findings of&nbsp;the content analysis in the following publication:</p>
<ul>
<li>Trepte, S., Teutsch, D., Masur, P. K., Eicher, C., Fischer, M., Hennhöfer, A., Lind, F. (2015). Do people know about privacy and data protection strategies? Towards the “Online Privacy Literacy Scale” (OPLIS). In. S. Gutwirth, R. Leenes &amp; P. de Hert (Eds.).&nbsp;<em>Reforming European Data Protection Law.</em>(pp.&nbsp;333-365). Springer: Netherlands. https://doi.org/10.1007/978-94-017-9385-8 [<a href="https://link.springer.com/chapter/10.1007/978-94-017-9385-8_14">Link</a> | <a href="https://www.researchgate.net/profile/Sabine_Trepte/publication/270889484_Do_People_Know_About_Privacy_and_Data_Protection_Strategies_Towards_the_Online_Privacy_Literacy_Scale_%28OPLIS%29/links/54bd22590cf218d4a16a07b8.pdf?origin=publication_detail_rebranded&amp;ev=pub_int_prw_xdl&amp;msrp=3bPSjtqpidQsltIJBv6F83J57WojjHXtTuLsq8BBKIfImO6sHBHx52De6h9%2FhhHDgRzqCUs29ogYJRZctoLdFg%3D%3D_ZK9gcIbvVS7W969CAYr6GKqG4S9qyslCLDXcFjLXIMA5%2Bg5Pt1jyg6JsHiGky8E09acZzlQ1qNadBb6tBiN3Ng%3D%3D">Preprint</a>]</li>
</ul>
</section>
<section id="development-and-validation-of-the-online-privacy-literacy-scale-oplis" class="level3">
<h3 class="anchored" data-anchor-id="development-and-validation-of-the-online-privacy-literacy-scale-oplis">Development and validation of the online privacy literacy scale (OPLIS)</h3>
<p>In a second step, our aim was to reduce the large item pool and develop a comprehensive, yet useable scale to measure online privacy literacy. Based on the initial item pool with 113 knowledge questions, we conducted three consecutive studies with which we tested the items for overall fit with the proposed theoretical concept. We measured all five dimensions identified in the qualitative content analysis. However, the analyses have revealed that the “legal” dimensions 3 and 4 were not distinct. The final scale hence includes only four dimensions of privacy literacy and consists of 20 items (five items per dimension).</p>
<p><a href="img/Screen-Shot-2019-03-27-at-7.37.33-PM.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://philippmasur.de/blog/2019-03-28-reconceptualizing-online-privacy-literacy/img/Screen-Shot-2019-03-27-at-7.37.33-PM.png" class="img-fluid"></a></p>
<p>Figure 1. The estimated bifactor model (Masur, Teutsch &amp; Trepte, 2017).</p>
<p>The psychometric quality of the scale was tested using structural equation modeling. The results of the studies show that the multidimensional concept of online privacy literacy can be supported with empirical data. Yet, we found that the dimensions identified in the literature do not represent distinct knowledge areas. Instead, the common variance in all items accounted for a strong global factor that represented actual online privacy literacy. Accordingly, we suggested to model online privacy literacy as a bifactor model which accounts for both the global variance and domain specific variances.<sup>7</sup></p>
<p>The model was validated with a quota sample of German internet users (N = 1.932). Based on different criteria the scale’s quality can be regarded as good. Specifically the global factor predicted different types of data protection behavior. The results of the three consecutive studies were published the the journal “Diagnostica”:</p>
<ul>
<li>Masur, P. K., Teutsch, D. &amp; Trepte, S. (2017). Entwicklung und Validierung der Online-Privatheitskompetenzskala (OPLIS) [engl. Development and validation of the online privacy literacy scale].&nbsp;<em>Diagnostica, 63</em>, 256-268. https://doi.org/10.1026/0012-1924/a000179 [<a href="http://econtent.hogrefe.com/doi/abs/10.1026/0012-1924/a000179">Link</a>&nbsp;| <a href="http://www.oplis.de/docs/OPLIS_pre-print.pdf">Preprint</a>]</li>
</ul>
<p>To make the scale available to the public, we also set up a website that features not only all items of the scale, but also instructions and tutorials on how to use them in different scenarios and norm tables for comparing individual results with the German population:</p>
<ul>
<li><a href="http://www.oplis.de">www.oplis.de</a></li>
</ul>
</section>
<section id="a-normative-perspective-on-privacy-literacy-and-do-it-yourself-data-protection" class="level3">
<h3 class="anchored" data-anchor-id="a-normative-perspective-on-privacy-literacy-and-do-it-yourself-data-protection">A normative perspective on privacy literacy and “do it yourself”-data protection</h3>
<p>Since its publication, the scale has been used in several studies – both nationally and internationally. Although higher literacy seemed promising in predicting people’s engagement in data and privacy protection, we started to question in how far fostering online privacy literacy was the right way to tackle the challenges of online privacy.&nbsp;In a chapter that was published as proceedings of the CPDP (a large conference on computers, privacy and data protrection), Tobias Matzner, Carsten Ochs, Thilo von Pape and I dove more deeply into the question of whether individual citizens should be responsible for their own data protection. Again, do-it-yourself (DIY) data protection has often been considered as an important part of comprehensive data protection. Fostering DIY privacy protection and providing the respective tools was seen both as important policy aim and as a developing market. Individuals are meant to be empowered in a world where an increasing amount of actors is interested in their data. The results of our scale development paper somewhat seemed to support this claim.</p>
<p>We took a step back and analyzed the preconditions of this view empirically and normatively: Thus, we asked first: Can individuals protect data efficiently? And second, should individuals be responsible for data protection. We found that individuals barely engage in sophisticated data protection that would allow them to restrict access of companies or institutions to their data and further argue that for normative reasons, a wider social perspective on data protection is required.&nbsp;Failing to see the larger, structural reasons behind individual lacks in privacy protection, does not attribute responsibility to the government as the actor who might actually be able to address structural problems. Seeing online privacy literacy as the solution thereby removes the responsibility of the government to ensure its citizens’ self-determination. The chapter has been published here:</p>
<ul>
<li>Matzner, T., Masur, P. K., Ochs, C. &amp; von Pape, T. (2015). Self-Data-Protection – Empowerment or burden? In: S. Gutwirth, R. Leenes &amp; P. de Hert (Eds.).&nbsp;<em>Data Protection on the Move</em>. (pp.&nbsp;277-305). Springer: Netherlands. https://doi.org/10.1007/978-94-017-7376-8_11 [<a href="../../documents/pubs/Matzner Masur et al._2016_Do-it-yourself data protection.pdf">Preprint</a>]</li>
</ul>
</section>
<section id="the-role-of-online-privacy-literacy-in-democratic-societies" class="level3">
<h3 class="anchored" data-anchor-id="the-role-of-online-privacy-literacy-in-democratic-societies">The role of online privacy literacy in democratic societies</h3>
<p>In light of this, we started to question the role of online privacy literacy. By investigating the more recent literature on interventions and potential outcomes, we found that often enough, increasing people’s knowledge (even in all areas that were captured by OPLIS) did not necessarily result in a behavior change (e.g., less self-disclosure). It seemed even possible that people gained more knowledge through using certain privacy protection strategies and not vice-versa.</p>
<p>It was hence unclear whether a respective intervention actually contributes to more self-determination on the Internet and whether it makes people behave more deliberate and more in accordance with their privacy needs. Through continued discussions and reviews of the literature, we came to the conclusions that&nbsp;online privacy literacy should be conceptualized as a combination of knowledge <em>and</em> specific abilities and skills. For this purpose, a reconceptualization is proposed that includes privacy-related reflection and critical abilities as well as practical privacy and data protection skills next to factorial knowledge about economic, technical, and legal aspects of online privacy.</p>
<p>We argued that in order to be a true enabler for self-determination (including political responsibility), privacy literacy has to be viewed as a more complex set of skills, abilities and knowledge sets.&nbsp;The first installment of this model was published in the following paper:</p>
<ul>
<li>Masur, P. K., Teutsch, D., Dienlin, T. &amp; Trepte, S. (2017). Online-Privatheitskompetenz und deren Bedeutung für demokratische Gesellschaften [engl. Online privacy literacy and its significance for democratic societies].&nbsp;<em>Forschungsjournal Soziale Bewegungen</em>, 30 (2), 180-189. https://doi.org/10.1515/fjsb-2017-0039 [<a href="http://forschungsjournal.de/jahrgaenge/2017heft2">Link</a>]</li>
</ul>
</section>
<section id="a-reconceptualization-of-online-privacy-literacy" class="level3">
<h3 class="anchored" data-anchor-id="a-reconceptualization-of-online-privacy-literacy">A reconceptualization of online privacy literacy</h3>
<p>In my most recent publication, I developed this idea further and proposed a comprehensive model of online privacy literacy that outlines several factual privacy knowledge areas, privacy-related reflection abilities, privacy and data protection skills, and privacy-related critical thinking abilities (see Figure 2).</p>
<p>&nbsp;</p>
<p><a href="img/Abbildung_german.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://philippmasur.de/blog/2019-03-28-reconceptualizing-online-privacy-literacy/img/Abbildung_german.png" class="img-fluid"></a></p>
<p>Figure 2: A comprehensive framework of online privacy literacy (Figure taken and translated from Masur, 2018).</p>
<p>With this model, I aim to provide a novel perspective on how to conceptualize privacy literacy. I agree that one of challenges today is that user are not literate enough to navigate online environments in a self-determined manner. But in contrast to prior literature, I do not think that is only a lack of knowledge that prevents them from becoming critical media users. Instead, I suggest that&nbsp;they (1) lack the knowledge to identify the risks involved in using online media, (2)&nbsp;do not have the necessary self-reflection abilities to questions&nbsp;and reflect their own behavior,&nbsp;(3)&nbsp;do not have the procedural skills to implemented increasingly more sophisticated privacy protection measures, and (4) lack the critical thinking ability to criticize the status quo and realize their own potential in actively changing their social and structural environments.</p>
<p>I argue that only by acknowledging reflection and critical thinking abilities, online privacy literacy may enable true self-determination. Particularly, individuals need to become critical in that they develop the ability to critically question and criticize institutional practices, the lack of usable data protection tools, and thereupon decide to change this status quote (e.g., by causing societal change through a certain voting behavior).<sup>8</sup></p>
<p>With this paper, I am drawing from classical media literacy theory to arrive at a more comprehensive definition of what a literate Internet users and particularly a privacy literate user may look like. The theoretical paper was published in the German journal “Medien &amp; Kommunikationswissenschaft”:</p>
<ul>
<li>Masur, P. K. (2018). Mehr als Bewusstsein für Privatheitsrisiken: Eine Rekonzeptualisierung der Online-Privatheitskompetenz als Kombination aus Wissen, Fähig- und Fertigkeiten [engl. It is more than just privacy risk awareness. A reconceptualization of online privacy literacy as a composition of knowledge, abilities, and skills].&nbsp;<em>Medien &amp; Kommunikationswissenschaft, 66</em>(4)<em>,&nbsp;</em>446 – 465.&nbsp;https://doi.org/10.5771/1615-634X-2018-4-446 [<a href="https://www.nomos-elibrary.de/10.5771/1615-634X-2018-4-446/mehr-als-bewusstsein-fuer-privatheitsrisiken-eine-rekonzeptualisierung-der-online-privatheitskompetenz-als-kombination-aus-wissen-faehig-und-fertigkeiten-jahrgang-66-2018-heft-4?page=1">Link</a>]</li>
</ul>
</section>
<section id="conclusion-and-future-perspectives" class="level3">
<h3 class="anchored" data-anchor-id="conclusion-and-future-perspectives">Conclusion and future perspectives</h3>
<p>It has been questioned before why we need online privacy literacy a distinct concept – particularly as it (and even more so my latest conceptualization) is so close to classical media literacy concept. In the paper, I answered the following (translated from German):</p>
<blockquote class="blockquote">
<p>Despite overlaps with some dimensions of media and digital literacy, I argue that the independent consideration of online privacy literacy makes sense both from a theoretical and an empirical point of view. Firstly, the focus on privacy-related knowledge dimensions, abilities and skills allows to explicit address necessary prerequisites for informationally self-determined behavior in online environments. Such a concept of literacy makes it possible to investigate particularly those person characteristics that actually allow the individual to determine for himself&nbsp;how his personal information and data is collected and used by potential third parties. Secondly, the concept allows for a conceptual condensation that is absolutely necessary for empirical assessments and imperative for potential educational programs designed to prevent irrational handling of one’s own privacy in online environments. Only the clear focus on privacy allows for reasonable operationalizations that are directly related to privacy-related behavior.</p>
</blockquote>
<p>With this in mind, I believe that investigating online privacy literacy as well as its antecedents and consequences is an important goal. At this moment, several international project use OPLIS to investigate online privacy literacy in different cultures and contexts. Given that privacy must often be understood form a situational <sup>9</sup> (or at least a contextual perspective<sup>10</sup> such studies are of utmost importance to understand the circumstances under which Internet users make use of their knowledge and skills and under which they don’t.</p>
<p>I will continue this research program in the next years aiming at understanding the connection between media and privacy literacy (is it a distinct concept or rather a subdimensions of a modern concept of media literacy?) and develop approaches toward assessing the various dimensions that I outlined in the latest paper. It is my hope that this research inspires other privacy scholars but also provides some valuable insights for practitioners and policy makers.</p>
<section id="footnotes" class="level4">
<h4 class="anchored" data-anchor-id="footnotes">Footnotes</h4>


<template id="pm-footer-icons"><a href="#" data-pm-mail="p.k.masur|vu.nl" aria-label="Email" title="Email"><svg class="pm-icon" aria-hidden="true"><use href="#i-mail"></use></svg></a><a href="https://scholar.google.com/citations?user=XA_WthYAAAAJ" target="_blank" rel="noopener" aria-label="Google Scholar" title="Google Scholar"><svg class="pm-icon" aria-hidden="true"><use href="#i-scholar"></use></svg></a><a href="https://orcid.org/0000-0003-3065-7305" target="_blank" rel="noopener" aria-label="ORCID" title="ORCID"><svg class="pm-icon" aria-hidden="true"><use href="#i-orcid"></use></svg></a><a href="https://osf.io/yuqv5/" target="_blank" rel="noopener" aria-label="Open Science Framework" title="Open Science Framework"><svg class="pm-icon" aria-hidden="true"><use href="#i-osf"></use></svg></a><a href="https://github.com/masurp" target="_blank" rel="noopener" aria-label="GitHub" title="GitHub"><svg class="pm-icon" aria-hidden="true"><use href="#i-github"></use></svg></a><a href="https://bsky.app/profile/masurphil.bsky.social" target="_blank" rel="noopener" aria-label="Bluesky" title="Bluesky"><svg class="pm-icon" aria-hidden="true"><use href="#i-bluesky"></use></svg></a><a href="https://research.vu.nl/en/persons/philipp-k-masur" target="_blank" rel="noopener" aria-label="VU Amsterdam" title="VU Amsterdam"><svg class="pm-icon" aria-hidden="true"><use href="#i-landmark"></use></svg></a></template>
</section>
</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>Matzner, T., Masur, P. K., Ochs, C. &amp; von Pape, T. (2015). Self-Data-Protection – Empowerment or burden? In: S. Gutwirth, R. Leenes &amp; P. de Hert (Eds.).&nbsp;<em>Data Protection on the Move</em>. (pp.&nbsp;277-305). Springer: Netherlands. https://doi.org/10.1007/978-94-017-7376-8_11 [<a href="http://www.philippmasur.de/documents/pubs/Matzner%20Masur%20et%20al._2016_Do-it-yourself%20data%20protection.pdf">PDF</a>]↩︎</p></li>
<li id="fn2"><p>The research program was started by Sabine Trepte, Doris Teutsch and me. Throughout the years, I further worked on this project with other scholars such as Tobias Matzner, Thilo von Pape, Carsten Ochs, Carolin Eicher, Mona Fischer, Alisa Hennhöfer, and Fabienne Lind who all contributed immensely to my current understanding of online privacy literacy.↩︎</p></li>
<li id="fn3"><p>Since 2005, empirical work on people’s disclosure behavior in online environments (particularly social media such as Facebook) often found that people disclose a large amount of information despite strong privacy concerns. This observation has been termed the “privacy paradox”. Yet, more recent studies with larger samples and more sophisticated methods as well as meta-analyses have shown that concerns do relate to privacy behavior suggesting that people’s online behavior is not as paradoxical as it seems.↩︎</p></li>
<li id="fn4"><p>Hoofnagle, C. J., King, J., Li, S. &amp; Turow, J. (2010). How different are young adults from older adults when it comes to information privacy attitudes and policies? Retrieved October 7, 2016, from http://papers.ssrn.com/sol3/Delivery.cfm/SSRN_ID1589864_code364326.pdf?abstractid=1589864&amp;mirid=1↩︎</p></li>
<li id="fn5"><p>e.g.,&nbsp;Morrison, B. (2013). Do we know what we think we know? An exploration of online social network users’ privacy literacy.&nbsp;<em>Workplace Review</em>, April 2013 Issue, 58–79. doi: 10.13140/RG.2.1.4898.0567↩︎</p></li>
<li id="fn6"><p>Park, Y. J. (2013). Digital literacy and privacy behavior online. Communication Research, 40, 215–236.↩︎</p></li>
<li id="fn7"><p>Reise, S. P. (2012). The Rediscovery of bifactor measurement models. <em>Multivariate Behavioral Research, 47</em>, 667–696; Reise, S. P., Moore, T. M. &amp; Haviland, M. G. (2010). Bifactor models and rotations: Exploring the extent to which multidimensional data yield univocal scale scores. <em>Journal of Personality Assessment, 92</em>, 544–559. doi:10.1080/00223891.2010.496477↩︎</p></li>
<li id="fn8"><p>The recent demonstrations against the new European copyright directive is an interesting example of such an critical thinking ability↩︎</p></li>
<li id="fn9"><p>See e.g., <a href="http://philippmasur.de/blog/research/dissertation/">this post</a>.↩︎</p></li>
<li id="fn10"><p>Nissenbaum, H. F. (2010). Privacy in context: Technology, policy, and the integrity of social life. Stanford: Stanford Law Books.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>Privacy</category>
  <category>Communication</category>
  <guid>https://philippmasur.de/blog/2019-03-28-reconceptualizing-online-privacy-literacy/</guid>
  <pubDate>Wed, 27 Mar 2019 23:00:00 GMT</pubDate>
  <media:content url="https://philippmasur.de/blog/2019-03-28-reconceptualizing-online-privacy-literacy/featured.png" medium="image" type="image/png" height="106" width="144"/>
</item>
</channel>
</rss>
