<!DOCTYPE html>
<!-- Generated by pkgdown: do not edit by hand --><html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Setting up different types of specifications • specr</title>
<!-- jquery --><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script><!-- Bootstrap --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha256-bZLfwXAP04zRMK2BjiO8iu9pf4FbLqX6zitd+tIvLhE=" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha256-nuL8/2cJ5NDSSwnKD8VqreErSWHtnEP9E7AySL+1ev4=" crossorigin="anonymous"></script><!-- bootstrap-toc --><link rel="stylesheet" href="../bootstrap-toc.css">
<script src="../bootstrap-toc.js"></script><!-- Font Awesome icons --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css" integrity="sha256-mmgLkCYLUQbXn0B1SRqzHar6dCnv9oZFPEC1g1cwlkk=" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/v4-shims.min.css" integrity="sha256-wZjR52fzng1pJHwx4aV2AO3yyTOXrcDW7jBpJtTwVxw=" crossorigin="anonymous">
<!-- clipboard.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><!-- headroom.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/headroom.min.js" integrity="sha256-AsUX4SJE1+yuDu5+mAVzJbuYNPHj/WroHuZ8Ir/CkE0=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/jQuery.headroom.min.js" integrity="sha256-ZX/yNShbjqsohH1k95liqY9Gd8uOiE1S4vZc+9KQ1K4=" crossorigin="anonymous"></script><!-- pkgdown --><link href="../pkgdown.css" rel="stylesheet">
<script src="../pkgdown.js"></script><meta property="og:title" content="Setting up different types of specifications">
<meta property="og:description" content="specr">
<!-- mathjax --><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js" integrity="sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA=" crossorigin="anonymous"></script><!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body data-spy="scroll" data-target="#toc">
    

    <div class="container template-article">
      <header><div class="navbar navbar-default navbar-fixed-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <span class="navbar-brand">
        <a class="navbar-link" href="../index.html">specr</a>
        <span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="">1.0.1</span>
      </span>
    </div>

    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
<li>
  <a href="../articles/getting-started.html">Getting started</a>
</li>
<li>
  <a href="../reference/index.html">Reference</a>
</li>
<li class="dropdown">
  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
    Articles
     
    <span class="caret"></span>
  </a>
  <ul class="dropdown-menu" role="menu">
<li class="dropdown-header">Basics</li>
    <li>
      <a href="../articles/different-specifications.html">Setting up different types of specifications</a>
    </li>
    <li>
      <a href="../articles/custom-plot.html">Visualizing specification curve analyses</a>
    </li>
    <li>
      <a href="../articles/invest-spec.html">Investigating specific specifications</a>
    </li>
    <li>
      <a href="../articles/inferences.html">Inferences with specification curve analysis</a>
    </li>
    <li>
      <a href="../articles/parallelization.html">Parallelization</a>
    </li>
    <li class="divider">
    </li>
<li class="dropdown-header">Incorporating specific models</li>
    <li>
      <a href="../articles/measurement-models.html">Structural equation models</a>
    </li>
    <li>
      <a href="../articles/multilevel-models.html">Multilevel models</a>
    </li>
    <li>
      <a href="../articles/parallel-bayesian-models.html">Bayesian models</a>
    </li>
  </ul>
</li>
<li>
  <a href="../news/index.html">News</a>
</li>
      </ul>
<ul class="nav navbar-nav navbar-right">
<li>
  <a href="https://github.com/masurp/specr" class="external-link">
    <span class="fa fa-github fa-lg"></span>
     
  </a>
</li>
      </ul>
</div>
<!--/.nav-collapse -->
  </div>
<!--/.container -->
</div>
<!--/.navbar -->

      

      </header><div class="row">
  <div class="col-md-9 contents">
    <div class="page-header toc-ignore">
      <h1 data-toc-skip>Setting up different types of
specifications</h1>
            
      
      <small class="dont-index">Source: <a href="https://github.com/masurp/specr/blob/HEAD/vignettes/different-specifications.Rmd" class="external-link"><code>vignettes/different-specifications.Rmd</code></a></small>
      <div class="hidden name"><code>different-specifications.Rmd</code></div>

    </div>

    
    
<p>The function <code><a href="../reference/setup.html">setup()</a></code> seems rather simple, but it is in
fact very versatile and allows to incorporate (almost) any analytic
choice that one might engage in during data analysis. In this vignette,
I am going to exemplify how this function can be used to create several
analytical choices.</p>
<div class="sourceCode" id="cb1"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://tidyverse.tidyverse.org" class="external-link">tidyverse</a></span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://masurp.github.io/specr/" class="external-link">specr</a></span><span class="op">)</span></span></code></pre></div>
<div class="section level2">
<h2 id="different-independent-and-dependent-variables">Different independent and dependent variables<a class="anchor" aria-label="anchor" href="#different-independent-and-dependent-variables"></a>
</h2>
<p>One analytic choice may refer to choosing different independent or
dependent variables (e.g., different scales that measure the same
construct) or different ways in how these variables are compute (e.g.,
mean vs. latent measurement, log-transformation, etc).</p>
<div class="section level3">
<h3 id="simply-providing-different-variable-as-vectors">Simply providing different variable as vectors<a class="anchor" aria-label="anchor" href="#simply-providing-different-variable-as-vectors"></a>
</h3>
<p>In most cases, this simply means adding such variables to the data
set and provide them as alternative choices in setup.</p>
<div class="sourceCode" id="cb2"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># Setup specs</span></span>
<span><span class="va">specs</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/setup.html">setup</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">example_data</span>,</span>
<span>               x <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"x1"</span>, <span class="st">"x2"</span>, <span class="st">"x3"</span>, <span class="st">"x4"</span><span class="op">)</span>,</span>
<span>               y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"y1"</span>, <span class="st">"y2"</span>, <span class="st">"y3"</span><span class="op">)</span>,</span>
<span>               model <span class="op">=</span> <span class="st">"lm"</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Summary of specifications</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/summary.html" class="external-link">summary</a></span><span class="op">(</span><span class="va">specs</span>, rows <span class="op">=</span> <span class="fl">12</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## Setup for the Specification Curve Analysis</span></span>
<span><span class="co">## -------------------------------------------</span></span>
<span><span class="co">## Class:                      specr.setup -- version: 1.0.1 </span></span>
<span><span class="co">## Number of specifications:   12 </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## Specifications:</span></span>
<span><span class="co">## </span></span>
<span><span class="co">##   Independent variable:     x1, x2, x3, x4 </span></span>
<span><span class="co">##   Dependent variable:       y1, y2, y3 </span></span>
<span><span class="co">##   Models:                   lm </span></span>
<span><span class="co">##   Covariates:               no covariates </span></span>
<span><span class="co">##   Subsets analyses:         all </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## Function used to extract parameters:</span></span>
<span><span class="co">## </span></span>
<span><span class="co">##   function (x) </span></span>
<span><span class="co">## broom::tidy(x, conf.int = TRUE)</span></span>
<span><span class="co">## &lt;environment: 0x7f9c37823040&gt;</span></span>
<span><span class="co">## </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## Head of specifications table (first 12 rows):</span></span></code></pre>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 12 × 6</span></span></span>
<span><span class="co">##    x     y     model controls      subsets formula    </span></span>
<span><span class="co">##    <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>         <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>   <span style="color: #949494; font-style: italic;">&lt;glue&gt;</span>     </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 1</span> x1    y1    lm    no covariates all     y1 ~ x1 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 2</span> x1    y2    lm    no covariates all     y2 ~ x1 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span> x1    y3    lm    no covariates all     y3 ~ x1 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span> x2    y1    lm    no covariates all     y1 ~ x2 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span> x2    y2    lm    no covariates all     y2 ~ x2 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 6</span> x2    y3    lm    no covariates all     y3 ~ x2 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 7</span> x3    y1    lm    no covariates all     y1 ~ x3 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span> x3    y2    lm    no covariates all     y2 ~ x3 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span> x3    y3    lm    no covariates all     y3 ~ x3 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span> x4    y1    lm    no covariates all     y1 ~ x4 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">11</span> x4    y2    lm    no covariates all     y2 ~ x4 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">12</span> x4    y3    lm    no covariates all     y3 ~ x4 + 1</span></span></code></pre>
<div class="sourceCode" id="cb5"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># Run analysis and plot results</span></span>
<span><span class="va">results</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/specr.html">specr</a></span><span class="op">(</span><span class="va">specs</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/utils/head.html" class="external-link">head</a></span><span class="op">(</span><span class="va">results</span><span class="op">$</span><span class="va">data</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 6 × 26</span></span></span>
<span><span class="co">##   x     y     model controls      subsets formula  model_function term  estimate</span></span>
<span><span class="co">##   <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>         <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>   <span style="color: #949494; font-style: italic;">&lt;glue&gt;</span>   <span style="color: #949494; font-style: italic;">&lt;list&gt;</span>         <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>    <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span> x1    y1    lm    no covariates all     y1 ~ x1… <span style="color: #949494;">&lt;fn&gt;</span>           x1      0.620 </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span> x1    y2    lm    no covariates all     y2 ~ x1… <span style="color: #949494;">&lt;fn&gt;</span>           x1     -<span style="color: #BB0000;">0.328</span> </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">3</span> x1    y3    lm    no covariates all     y3 ~ x1… <span style="color: #949494;">&lt;fn&gt;</span>           x1     -<span style="color: #BB0000;">0.082</span><span style="color: #BB0000; text-decoration: underline;">1</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">4</span> x2    y1    lm    no covariates all     y1 ~ x2… <span style="color: #949494;">&lt;fn&gt;</span>           x2      0.272 </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">5</span> x2    y2    lm    no covariates all     y2 ~ x2… <span style="color: #949494;">&lt;fn&gt;</span>           x2     -<span style="color: #BB0000;">0.102</span> </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">6</span> x2    y3    lm    no covariates all     y3 ~ x2… <span style="color: #949494;">&lt;fn&gt;</span>           x2     -<span style="color: #BB0000;">0.073</span><span style="color: #BB0000; text-decoration: underline;">5</span></span></span>
<span><span class="co">## <span style="color: #949494;"># ℹ 17 more variables: std.error &lt;dbl&gt;, statistic &lt;dbl&gt;, p.value &lt;dbl&gt;,</span></span></span>
<span><span class="co">## <span style="color: #949494;">#   conf.low &lt;dbl&gt;, conf.high &lt;dbl&gt;, fit_r.squared &lt;dbl&gt;,</span></span></span>
<span><span class="co">## <span style="color: #949494;">#   fit_adj.r.squared &lt;dbl&gt;, fit_sigma &lt;dbl&gt;, fit_statistic &lt;dbl&gt;,</span></span></span>
<span><span class="co">## <span style="color: #949494;">#   fit_p.value &lt;dbl&gt;, fit_df &lt;dbl&gt;, fit_logLik &lt;dbl&gt;, fit_AIC &lt;dbl&gt;,</span></span></span>
<span><span class="co">## <span style="color: #949494;">#   fit_BIC &lt;dbl&gt;, fit_deviance &lt;dbl&gt;, fit_df.residual &lt;int&gt;, fit_nobs &lt;int&gt;</span></span></span></code></pre>
</div>
<div class="section level3">
<h3 id="specific-variablemodel-combinations">Specific variable/model combinations<a class="anchor" aria-label="anchor" href="#specific-variablemodel-combinations"></a>
</h3>
<p>However, in some cases, we may have different type of variables
(e.g., a mean vs. a dichotomous variable). In some cases, for example
when this refers to a dependent variable, such a choice of variables
requires also different model estimation functions. So in our setup, we
want to acknowledge for specific variable/model combinations. In the
following, let’s imagine that next to our standard dependent variables
(<code>y1</code>-<code>y3</code>), we also have a dichotomous item
(<code>y_dich</code>; here only recoded from <code>y1</code>). In this
case, we want to specify and add a custom function - here a logistic
model (<code>glm(formula, data, family = binomial()</code>)) - to the
setup.</p>
<div class="sourceCode" id="cb7"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># Dichotomous dependent variable</span></span>
<span><span class="va">data</span> <span class="op">&lt;-</span> <span class="va">example_data</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/mutate.html" class="external-link">mutate</a></span><span class="op">(</span>y_dich <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/ifelse.html" class="external-link">ifelse</a></span><span class="op">(</span><span class="va">y1</span> <span class="op">&gt;</span> <span class="fu"><a href="https://rdrr.io/r/base/mean.html" class="external-link">mean</a></span><span class="op">(</span><span class="va">y1</span><span class="op">)</span>, <span class="fl">1</span>, <span class="fl">0</span><span class="op">)</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Specific function</span></span>
<span><span class="va">log_glm</span> <span class="op">&lt;-</span> <span class="kw">function</span><span class="op">(</span><span class="va">formula</span>, <span class="va">data</span><span class="op">)</span> <span class="op">{</span></span>
<span>  <span class="fu"><a href="https://rdrr.io/r/stats/glm.html" class="external-link">glm</a></span><span class="op">(</span><span class="va">formula</span>, <span class="va">data</span>, family <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/stats/family.html" class="external-link">binomial</a></span><span class="op">(</span><span class="op">)</span><span class="op">)</span></span>
<span><span class="op">}</span></span>
<span></span>
<span><span class="co"># Setup specs</span></span>
<span><span class="va">specs</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/setup.html">setup</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">data</span>,</span>
<span>               x <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"x1"</span>, <span class="st">"x2"</span><span class="op">)</span>,</span>
<span>               y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"y1"</span>, <span class="st">"y2"</span>, <span class="st">"y3"</span>, <span class="st">"y_dich"</span><span class="op">)</span>,</span>
<span>               model <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"lm"</span>, <span class="st">"log_glm"</span><span class="op">)</span><span class="op">)</span></span>
<span><span class="co"># Check</span></span>
<span><span class="va">specs</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="va">as_tibble</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 16 × 7</span></span></span>
<span><span class="co">##    x     y      model   controls      subsets formula         model_function</span></span>
<span><span class="co">##    <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>   <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>         <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>   <span style="color: #949494; font-style: italic;">&lt;glue&gt;</span>          <span style="color: #949494; font-style: italic;">&lt;list&gt;</span>        </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 1</span> x1    y1     lm      no covariates all     y1 ~ x1 + 1     <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 2</span> x1    y1     log_glm no covariates all     y1 ~ x1 + 1     <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span> x1    y2     lm      no covariates all     y2 ~ x1 + 1     <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span> x1    y2     log_glm no covariates all     y2 ~ x1 + 1     <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span> x1    y3     lm      no covariates all     y3 ~ x1 + 1     <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 6</span> x1    y3     log_glm no covariates all     y3 ~ x1 + 1     <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 7</span> x1    y_dich lm      no covariates all     y_dich ~ x1 + 1 <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span> x1    y_dich log_glm no covariates all     y_dich ~ x1 + 1 <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span> x2    y1     lm      no covariates all     y1 ~ x2 + 1     <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span> x2    y1     log_glm no covariates all     y1 ~ x2 + 1     <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">11</span> x2    y2     lm      no covariates all     y2 ~ x2 + 1     <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">12</span> x2    y2     log_glm no covariates all     y2 ~ x2 + 1     <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">13</span> x2    y3     lm      no covariates all     y3 ~ x2 + 1     <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">14</span> x2    y3     log_glm no covariates all     y3 ~ x2 + 1     <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">15</span> x2    y_dich lm      no covariates all     y_dich ~ x2 + 1 <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">16</span> x2    y_dich log_glm no covariates all     y_dich ~ x2 + 1 <span style="color: #949494;">&lt;fn&gt;</span></span></span></code></pre>
<p>As we can see, the <code><a href="../reference/setup.html">setup()</a></code> function produces all
combinations between the variables and the model functions. This is not
meaningful as the logistic model should only be used if the dependent
variable is <code>y_dich</code>. In comparison to specr version 0.2.1,
this is were the strength of version 1.0.0 comes in. Because we setup
all specifications beforehand, we can simply remove those that we are
not interested in or that we deem non-meaningful. Note: You won’t be
able to filter or subset the <code>specr.setup</code> object
(essentially a list). Instead, you want to filter/subset the first
position of the list: the data frame that includes the specifications
(here <code>specs$specs</code> or <code>specs[[1]]</code>).</p>
<div class="sourceCode" id="cb9"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># Filter out models that are not meaningful (here only keep log_glm, when y == "y4")</span></span>
<span><span class="va">specs</span><span class="op">$</span><span class="va">specs</span> <span class="op">&lt;-</span> <span class="va">specs</span><span class="op">$</span><span class="va">specs</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/filter.html" class="external-link">filter</a></span><span class="op">(</span><span class="op">!</span><span class="op">(</span><span class="va">model</span> <span class="op">==</span> <span class="st">"log_glm"</span> <span class="op">&amp;</span> <span class="va">y</span> <span class="op">!=</span> <span class="st">"y_dich"</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/filter.html" class="external-link">filter</a></span><span class="op">(</span><span class="op">!</span><span class="op">(</span><span class="va">model</span> <span class="op">==</span> <span class="st">"lm"</span> <span class="op">&amp;</span> <span class="va">y</span> <span class="op">==</span> <span class="st">"y_dich"</span><span class="op">)</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Check results (only meaningful specifications remain)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/summary.html" class="external-link">summary</a></span><span class="op">(</span><span class="va">specs</span>, rows <span class="op">=</span> <span class="fl">8</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## Setup for the Specification Curve Analysis</span></span>
<span><span class="co">## -------------------------------------------</span></span>
<span><span class="co">## Class:                      specr.setup -- version: 1.0.1 </span></span>
<span><span class="co">## Number of specifications:   16 </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## Specifications:</span></span>
<span><span class="co">## </span></span>
<span><span class="co">##   Independent variable:     x1, x2 </span></span>
<span><span class="co">##   Dependent variable:       y1, y2, y3, y_dich </span></span>
<span><span class="co">##   Models:                   lm, log_glm </span></span>
<span><span class="co">##   Covariates:               no covariates </span></span>
<span><span class="co">##   Subsets analyses:         all </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## Function used to extract parameters:</span></span>
<span><span class="co">## </span></span>
<span><span class="co">##   function (x) </span></span>
<span><span class="co">## broom::tidy(x, conf.int = TRUE)</span></span>
<span><span class="co">## &lt;environment: 0x7f9c32e50ba8&gt;</span></span>
<span><span class="co">## </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## Head of specifications table (first 8 rows):</span></span></code></pre>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 8 × 6</span></span></span>
<span><span class="co">##   x     y      model   controls      subsets formula        </span></span>
<span><span class="co">##   <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>   <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>         <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>   <span style="color: #949494; font-style: italic;">&lt;glue&gt;</span>         </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span> x1    y1     lm      no covariates all     y1 ~ x1 + 1    </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span> x1    y2     lm      no covariates all     y2 ~ x1 + 1    </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">3</span> x1    y3     lm      no covariates all     y3 ~ x1 + 1    </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">4</span> x1    y_dich log_glm no covariates all     y_dich ~ x1 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">5</span> x2    y1     lm      no covariates all     y1 ~ x2 + 1    </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">6</span> x2    y2     lm      no covariates all     y2 ~ x2 + 1    </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">7</span> x2    y3     lm      no covariates all     y3 ~ x2 + 1    </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">8</span> x2    y_dich log_glm no covariates all     y_dich ~ x2 + 1</span></span></code></pre>
<div class="sourceCode" id="cb12"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># Run analysis and plot results</span></span>
<span><span class="va">results</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/specr.html">specr</a></span><span class="op">(</span><span class="va">specs</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/utils/head.html" class="external-link">head</a></span><span class="op">(</span><span class="va">results</span><span class="op">$</span><span class="va">data</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 6 × 28</span></span></span>
<span><span class="co">##   x     y      model   controls    subsets formula model_function term  estimate</span></span>
<span><span class="co">##   <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>   <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>       <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>   <span style="color: #949494; font-style: italic;">&lt;glue&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;list&gt;</span>         <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>    <span style="color: #949494; font-style: italic;">&lt;dbl&gt;</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span> x1    y1     lm      no covaria… all     y1 ~ x… <span style="color: #949494;">&lt;fn&gt;</span>           x1      0.620 </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span> x1    y2     lm      no covaria… all     y2 ~ x… <span style="color: #949494;">&lt;fn&gt;</span>           x1     -<span style="color: #BB0000;">0.328</span> </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">3</span> x1    y3     lm      no covaria… all     y3 ~ x… <span style="color: #949494;">&lt;fn&gt;</span>           x1     -<span style="color: #BB0000;">0.082</span><span style="color: #BB0000; text-decoration: underline;">1</span></span></span>
<span><span class="co">## <span style="color: #BCBCBC;">4</span> x1    y_dich log_glm no covaria… all     y_dich… <span style="color: #949494;">&lt;fn&gt;</span>           x1      0.753 </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">5</span> x2    y1     lm      no covaria… all     y1 ~ x… <span style="color: #949494;">&lt;fn&gt;</span>           x2      0.272 </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">6</span> x2    y2     lm      no covaria… all     y2 ~ x… <span style="color: #949494;">&lt;fn&gt;</span>           x2     -<span style="color: #BB0000;">0.102</span> </span></span>
<span><span class="co">## <span style="color: #949494;"># ℹ 19 more variables: std.error &lt;dbl&gt;, statistic &lt;dbl&gt;, p.value &lt;dbl&gt;,</span></span></span>
<span><span class="co">## <span style="color: #949494;">#   conf.low &lt;dbl&gt;, conf.high &lt;dbl&gt;, fit_r.squared &lt;dbl&gt;,</span></span></span>
<span><span class="co">## <span style="color: #949494;">#   fit_adj.r.squared &lt;dbl&gt;, fit_sigma &lt;dbl&gt;, fit_statistic &lt;dbl&gt;,</span></span></span>
<span><span class="co">## <span style="color: #949494;">#   fit_p.value &lt;dbl&gt;, fit_df &lt;dbl&gt;, fit_logLik &lt;dbl&gt;, fit_AIC &lt;dbl&gt;,</span></span></span>
<span><span class="co">## <span style="color: #949494;">#   fit_BIC &lt;dbl&gt;, fit_deviance &lt;dbl&gt;, fit_df.residual &lt;int&gt;, fit_nobs &lt;int&gt;,</span></span></span>
<span><span class="co">## <span style="color: #949494;">#   fit_null.deviance &lt;dbl&gt;, fit_df.null &lt;int&gt;</span></span></span></code></pre>
</div>
<div class="section level3">
<h3 id="adding-composite-and-latent-variables">Adding composite and latent variables<a class="anchor" aria-label="anchor" href="#adding-composite-and-latent-variables"></a>
</h3>
<p>In some cases, we may want to add mean indices or even latent
measurement models of our variables of interest to explore how latent
vs. manifest measurement affects our results. This requires a bit more
upfront work as we need to work with <a href="https://lavaan.ugent.be/" class="external-link">lavaan</a> for the latent measurement.
Furthermore, we need to adjust the extract functions (<code>fun1</code>
and <code>fun2</code> in <code><a href="../reference/setup.html">setup()</a></code>) so that the extraction of
parameters aligns across different type of models. The beauty of
<code><a href="https://generics.r-lib.org/reference/tidy.html" class="external-link">broom::tidy()</a></code> and <code><a href="https://generics.r-lib.org/reference/glance.html" class="external-link">broom::glance()</a></code> (which are
used by default) is that they adapt to the different model types that
are passed to <code>setup</code>. Although the same parameters are
extracted for most models, they sometimes differ for specific models
(e.g., structural equation models resulting from
<code><a href="https://rdrr.io/pkg/lavaan/man/sem.html" class="external-link">lavaan::sem()</a></code>). This means we need to adjust them to
extract the same parameters across different models.</p>
<div class="sourceCode" id="cb14"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># Add mean (one choice)</span></span>
<span><span class="va">data</span> <span class="op">&lt;-</span> <span class="va">data</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="va">rowwise</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/mutate.html" class="external-link">mutate</a></span><span class="op">(</span>x_mean <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/mean.html" class="external-link">mean</a></span><span class="op">(</span><span class="va">x1</span>, <span class="va">x2</span>, <span class="va">x3</span>, <span class="va">x4</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="va">ungroup</span></span>
<span></span>
<span><span class="co"># Add custom function with latent measurement models to pass to "models" (another choice)</span></span>
<span><span class="va">custom_sem</span> <span class="op">&lt;-</span> <span class="kw">function</span><span class="op">(</span><span class="va">formula</span>, <span class="va">data</span><span class="op">)</span> <span class="op">{</span></span>
<span>  </span>
<span>  <span class="co"># Make sure lavaan is loaded</span></span>
<span>  <span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">require</a></span><span class="op">(</span><span class="va"><a href="https://lavaan.ugent.be" class="external-link">lavaan</a></span><span class="op">)</span></span>
<span>  </span>
<span>  <span class="co"># Add latent measurement as list</span></span>
<span>  <span class="va">latent</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/list.html" class="external-link">list</a></span><span class="op">(</span>latent_x12 <span class="op">=</span> <span class="st">"latent_x12 =~ x1 + x2"</span><span class="op">)</span></span>
<span>  </span>
<span>  <span class="co"># Remove +1 from formula as lavaan doesn't know how to process it </span></span>
<span>  <span class="va">semformula</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://stringr.tidyverse.org/reference/str_remove.html" class="external-link">str_remove_all</a></span><span class="op">(</span><span class="va">formula</span>, <span class="st">"\\+ 1"</span><span class="op">)</span></span>
<span>  </span>
<span>  <span class="co"># remove non-used latent measurement models from list by checking the formula</span></span>
<span>  <span class="va">valid</span> <span class="op">&lt;-</span> <span class="fu">purrr</span><span class="fu">::</span><span class="fu"><a href="https://purrr.tidyverse.org/reference/keep.html" class="external-link">keep</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/names.html" class="external-link">names</a></span><span class="op">(</span><span class="va">latent</span><span class="op">)</span>, <span class="op">~</span> <span class="fu">stringr</span><span class="fu">::</span><span class="fu"><a href="https://stringr.tidyverse.org/reference/str_detect.html" class="external-link">str_detect</a></span><span class="op">(</span><span class="va">formula</span>, <span class="va">.x</span><span class="op">)</span><span class="op">)</span></span>
<span>  </span>
<span>  <span class="co"># Create new formula that includes latent measurement models</span></span>
<span>  <span class="va">formula</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/paste.html" class="external-link">paste</a></span><span class="op">(</span><span class="va">formula</span>, <span class="st">"\n"</span>, <span class="fu"><a href="https://rdrr.io/r/base/paste.html" class="external-link">paste</a></span><span class="op">(</span><span class="va">latent</span><span class="op">[</span><span class="va">valid</span><span class="op">]</span>, collapse <span class="op">=</span> <span class="st">" \n "</span><span class="op">)</span><span class="op">)</span></span>
<span>  </span>
<span>  <span class="co"># Pass formula to `sem()`</span></span>
<span>  <span class="fu"><a href="https://rdrr.io/pkg/lavaan/man/sem.html" class="external-link">sem</a></span><span class="op">(</span><span class="va">formula</span>, <span class="va">data</span><span class="op">)</span></span>
<span><span class="op">}</span></span>
<span></span>
<span><span class="co"># Create custom tidy function that extracts the same parameters from different models!</span></span>
<span><span class="va">tidy_new</span> <span class="op">&lt;-</span> <span class="kw">function</span><span class="op">(</span><span class="va">x</span><span class="op">)</span> <span class="op">{</span></span>
<span>  <span class="kw">if</span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/class.html" class="external-link">class</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span> <span class="op">==</span> <span class="st">"lavaan"</span><span class="op">)</span> <span class="op">{</span></span>
<span>    <span class="fu">broom</span><span class="fu">::</span><span class="fu"><a href="https://generics.r-lib.org/reference/tidy.html" class="external-link">tidy</a></span><span class="op">(</span><span class="va">x</span>, conf.int <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span> </span>
<span>    <span class="fu"><a href="https://dplyr.tidyverse.org/reference/select.html" class="external-link">select</a></span><span class="op">(</span><span class="va">term</span>, <span class="va">estimate</span>, <span class="va">conf.low</span>, <span class="va">conf.high</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span>   <span class="co"># select parameters you want to keep</span></span>
<span>    <span class="fu"><a href="https://dplyr.tidyverse.org/reference/filter.html" class="external-link">filter</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/grep.html" class="external-link">grepl</a></span><span class="op">(</span><span class="st">" ~ "</span>, <span class="va">term</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span>                    <span class="co"># term needs to be adjusted</span></span>
<span>    <span class="fu"><a href="https://tidyr.tidyverse.org/reference/separate.html" class="external-link">separate</a></span><span class="op">(</span><span class="va">term</span>, <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"dv"</span>, <span class="st">"term"</span><span class="op">)</span>, sep <span class="op">=</span> <span class="st">" ~ "</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span>  <span class="co"># extract independent variable</span></span>
<span>    <span class="fu"><a href="https://dplyr.tidyverse.org/reference/select.html" class="external-link">select</a></span><span class="op">(</span><span class="op">-</span><span class="va">dv</span><span class="op">)</span>                                       <span class="co"># remove dependent variable</span></span>
<span>  <span class="op">}</span> <span class="kw">else</span> <span class="op">{</span></span>
<span>    <span class="fu">broom</span><span class="fu">::</span><span class="fu"><a href="https://generics.r-lib.org/reference/tidy.html" class="external-link">tidy</a></span><span class="op">(</span><span class="va">x</span>, conf.int <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span> </span>
<span>    <span class="fu"><a href="https://dplyr.tidyverse.org/reference/select.html" class="external-link">select</a></span><span class="op">(</span><span class="va">term</span>, <span class="va">estimate</span>, <span class="va">conf.low</span>, <span class="va">conf.high</span><span class="op">)</span>      <span class="co"># same parameters as above</span></span>
<span>  <span class="op">}</span></span>
<span><span class="op">}</span></span>
<span></span>
<span><span class="co"># Setup specs with new custom function</span></span>
<span><span class="va">specs</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/setup.html">setup</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">data</span>,</span>
<span>               x <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"x1"</span>, <span class="st">"x2"</span>, <span class="st">"x3"</span>, <span class="st">"x4"</span>, <span class="st">"x_mean"</span>, <span class="st">"latent_x12"</span><span class="op">)</span>,</span>
<span>               y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"y1"</span>, <span class="st">"y2"</span><span class="op">)</span>,</span>
<span>               model <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"lm"</span>, <span class="st">"custom_sem"</span><span class="op">)</span>,</span>
<span>               fun1 <span class="op">=</span> <span class="va">tidy_new</span>, <span class="co"># We pass the new extract function</span></span>
<span>               fun2 <span class="op">=</span> <span class="cn">NULL</span><span class="op">)</span>     <span class="co"># switch off "glance" as it produces different fit indices and wouldn't work</span></span>
<span></span>
<span><span class="co"># Quick check (still includes non-meaningful specifications)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/summary.html" class="external-link">summary</a></span><span class="op">(</span><span class="va">specs</span>, rows <span class="op">=</span> <span class="fl">12</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## Setup for the Specification Curve Analysis</span></span>
<span><span class="co">## -------------------------------------------</span></span>
<span><span class="co">## Class:                      specr.setup -- version: 1.0.1 </span></span>
<span><span class="co">## Number of specifications:   24 </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## Specifications:</span></span>
<span><span class="co">## </span></span>
<span><span class="co">##   Independent variable:     x1, x2, x3, x4, x_mean, latent_x12 </span></span>
<span><span class="co">##   Dependent variable:       y1, y2 </span></span>
<span><span class="co">##   Models:                   lm, custom_sem </span></span>
<span><span class="co">##   Covariates:               no covariates </span></span>
<span><span class="co">##   Subsets analyses:         all </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## Function used to extract parameters:</span></span>
<span><span class="co">## </span></span>
<span><span class="co">##   function(x) {</span></span>
<span><span class="co">##   if(class(x) == "lavaan") {</span></span>
<span><span class="co">##     broom::tidy(x, conf.int = TRUE) %&gt;% </span></span>
<span><span class="co">##     select(term, estimate, conf.low, conf.high) %&gt;%   # select parameters you want to keep</span></span>
<span><span class="co">##     filter(grepl(" ~ ", term)) %&gt;%                    # term needs to be adjusted</span></span>
<span><span class="co">##     separate(term, c("dv", "term"), sep = " ~ ") %&gt;%  # extract independent variable</span></span>
<span><span class="co">##     select(-dv)                                       # remove dependent variable</span></span>
<span><span class="co">##   } else {</span></span>
<span><span class="co">##     broom::tidy(x, conf.int = TRUE) %&gt;% </span></span>
<span><span class="co">##     select(term, estimate, conf.low, conf.high)      # same parameters as above</span></span>
<span><span class="co">##   }</span></span>
<span><span class="co">## }</span></span>
<span><span class="co">## </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## Head of specifications table (first 12 rows):</span></span></code></pre>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 12 × 6</span></span></span>
<span><span class="co">##    x     y     model      controls      subsets formula    </span></span>
<span><span class="co">##    <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>      <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>         <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>   <span style="color: #949494; font-style: italic;">&lt;glue&gt;</span>     </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 1</span> x1    y1    lm         no covariates all     y1 ~ x1 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 2</span> x1    y1    custom_sem no covariates all     y1 ~ x1 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span> x1    y2    lm         no covariates all     y2 ~ x1 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span> x1    y2    custom_sem no covariates all     y2 ~ x1 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span> x2    y1    lm         no covariates all     y1 ~ x2 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 6</span> x2    y1    custom_sem no covariates all     y1 ~ x2 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 7</span> x2    y2    lm         no covariates all     y2 ~ x2 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span> x2    y2    custom_sem no covariates all     y2 ~ x2 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span> x3    y1    lm         no covariates all     y1 ~ x3 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span> x3    y1    custom_sem no covariates all     y1 ~ x3 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">11</span> x3    y2    lm         no covariates all     y2 ~ x3 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">12</span> x3    y2    custom_sem no covariates all     y2 ~ x3 + 1</span></span></code></pre>
<div class="sourceCode" id="cb17"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># Filter out non-meaningful specifications</span></span>
<span><span class="va">specs</span><span class="op">$</span><span class="va">specs</span> <span class="op">&lt;-</span> <span class="va">specs</span><span class="op">$</span><span class="va">specs</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/filter.html" class="external-link">filter</a></span><span class="op">(</span><span class="op">!</span><span class="op">(</span><span class="va">model</span> <span class="op">==</span> <span class="st">"custom_sem"</span> <span class="op">&amp;</span> <span class="op">!</span><span class="fu"><a href="https://rdrr.io/r/base/grep.html" class="external-link">grepl</a></span><span class="op">(</span><span class="st">"latent"</span>, <span class="va">x</span><span class="op">)</span><span class="op">)</span><span class="op">)</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/filter.html" class="external-link">filter</a></span><span class="op">(</span><span class="op">!</span><span class="op">(</span><span class="va">model</span> <span class="op">==</span> <span class="st">"lm"</span> <span class="op">&amp;</span> <span class="fu"><a href="https://rdrr.io/r/base/grep.html" class="external-link">grepl</a></span><span class="op">(</span><span class="st">"latent"</span>, <span class="va">x</span><span class="op">)</span><span class="op">)</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Check again</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/summary.html" class="external-link">summary</a></span><span class="op">(</span><span class="va">specs</span>, rows <span class="op">=</span> <span class="fl">12</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## Setup for the Specification Curve Analysis</span></span>
<span><span class="co">## -------------------------------------------</span></span>
<span><span class="co">## Class:                      specr.setup -- version: 1.0.1 </span></span>
<span><span class="co">## Number of specifications:   24 </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## Specifications:</span></span>
<span><span class="co">## </span></span>
<span><span class="co">##   Independent variable:     x1, x2, x3, x4, x_mean, latent_x12 </span></span>
<span><span class="co">##   Dependent variable:       y1, y2 </span></span>
<span><span class="co">##   Models:                   lm, custom_sem </span></span>
<span><span class="co">##   Covariates:               no covariates </span></span>
<span><span class="co">##   Subsets analyses:         all </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## Function used to extract parameters:</span></span>
<span><span class="co">## </span></span>
<span><span class="co">##   function(x) {</span></span>
<span><span class="co">##   if(class(x) == "lavaan") {</span></span>
<span><span class="co">##     broom::tidy(x, conf.int = TRUE) %&gt;% </span></span>
<span><span class="co">##     select(term, estimate, conf.low, conf.high) %&gt;%   # select parameters you want to keep</span></span>
<span><span class="co">##     filter(grepl(" ~ ", term)) %&gt;%                    # term needs to be adjusted</span></span>
<span><span class="co">##     separate(term, c("dv", "term"), sep = " ~ ") %&gt;%  # extract independent variable</span></span>
<span><span class="co">##     select(-dv)                                       # remove dependent variable</span></span>
<span><span class="co">##   } else {</span></span>
<span><span class="co">##     broom::tidy(x, conf.int = TRUE) %&gt;% </span></span>
<span><span class="co">##     select(term, estimate, conf.low, conf.high)      # same parameters as above</span></span>
<span><span class="co">##   }</span></span>
<span><span class="co">## }</span></span>
<span><span class="co">## </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## Head of specifications table (first 12 rows):</span></span></code></pre>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 12 × 6</span></span></span>
<span><span class="co">##    x          y     model      controls      subsets formula            </span></span>
<span><span class="co">##    <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>      <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>      <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>         <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>   <span style="color: #949494; font-style: italic;">&lt;glue&gt;</span>             </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 1</span> x1         y1    lm         no covariates all     y1 ~ x1 + 1        </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 2</span> x1         y2    lm         no covariates all     y2 ~ x1 + 1        </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span> x2         y1    lm         no covariates all     y1 ~ x2 + 1        </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span> x2         y2    lm         no covariates all     y2 ~ x2 + 1        </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span> x3         y1    lm         no covariates all     y1 ~ x3 + 1        </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 6</span> x3         y2    lm         no covariates all     y2 ~ x3 + 1        </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 7</span> x4         y1    lm         no covariates all     y1 ~ x4 + 1        </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span> x4         y2    lm         no covariates all     y2 ~ x4 + 1        </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span> x_mean     y1    lm         no covariates all     y1 ~ x_mean + 1    </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span> x_mean     y2    lm         no covariates all     y2 ~ x_mean + 1    </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">11</span> latent_x12 y1    custom_sem no covariates all     y1 ~ latent_x12 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">12</span> latent_x12 y2    custom_sem no covariates all     y2 ~ latent_x12 + 1</span></span></code></pre>
<div class="sourceCode" id="cb20"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># Run analysis and plot results</span></span>
<span><span class="va">results</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/specr.html">specr</a></span><span class="op">(</span><span class="va">specs</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/graphics/plot.default.html" class="external-link">plot</a></span><span class="op">(</span><span class="va">results</span>, choices <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"x"</span>, <span class="st">"y"</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="different-specifications_files/figure-html/unnamed-chunk-5-1.png" width="700"></p>
<p>As we can see, we can now investigate the manifest and latent
measures next to the individual items.</p>
</div>
</div>
<div class="section level2">
<h2 id="control-variables">Control variables<a class="anchor" aria-label="anchor" href="#control-variables"></a>
</h2>
<p>Another analytical choice may refer to the inclusion of covariates.
Here, the <code><a href="../reference/setup.html">setup()</a></code> function is again very versatile and
allows to include different combinations of covariates.</p>
<div class="section level3">
<h3 id="adding-a-bunch-of-control-variables">Adding a bunch of control variables<a class="anchor" aria-label="anchor" href="#adding-a-bunch-of-control-variables"></a>
</h3>
<p>By providing just a vector of covariates, <code><a href="../reference/setup.html">setup()</a></code>
produces all combinations of these covariates plus a specification
without any covariates. If we add the argument
<code>simplify = TRUE</code>, not all combinations between covariates
are created. Instead, only no covariates, each individually, and all
together are included.</p>
<div class="sourceCode" id="cb21"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># Setup specification that include all combinations of covariates</span></span>
<span><span class="va">specs1</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/setup.html">setup</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">example_data</span>,</span>
<span>               x <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"x1"</span>, <span class="st">"x2"</span><span class="op">)</span>,</span>
<span>               y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"y1"</span>, <span class="st">"y2"</span><span class="op">)</span>,</span>
<span>               model <span class="op">=</span> <span class="st">"lm"</span>,</span>
<span>               controls <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"c1"</span>, <span class="st">"c2"</span>, <span class="st">"c3"</span>, <span class="st">"c4"</span><span class="op">)</span><span class="op">)</span>  <span class="co"># simply providing a vector of control variables</span></span>
<span></span>
<span><span class="co"># Setup secifications that include only no covariates, each individually, and all together</span></span>
<span><span class="va">specs2</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/setup.html">setup</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">example_data</span>,</span>
<span>               x <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"x1"</span>, <span class="st">"x2"</span><span class="op">)</span>,</span>
<span>               y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"y1"</span>, <span class="st">"y2"</span><span class="op">)</span>,</span>
<span>               model <span class="op">=</span> <span class="st">"lm"</span>,</span>
<span>               controls <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"c1"</span>, <span class="st">"c2"</span>, <span class="st">"c3"</span>, <span class="st">"c4"</span><span class="op">)</span>,</span>
<span>               simplify <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span>   <span class="co"># Difference to specs1!</span></span>
<span></span>
<span><span class="co"># Check</span></span>
<span><span class="fu"><a href="https://dplyr.tidyverse.org/reference/distinct.html" class="external-link">distinct</a></span><span class="op">(</span><span class="va">specs1</span><span class="op">$</span><span class="va">specs</span>, <span class="va">controls</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 16 × 1</span></span></span>
<span><span class="co">##    controls         </span></span>
<span><span class="co">##    <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>            </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 1</span> no covariates    </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 2</span> c1               </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span> c2               </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span> c3               </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span> c4               </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 6</span> c1 + c2          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 7</span> c1 + c3          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span> c1 + c4          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span> c2 + c3          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span> c2 + c4          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">11</span> c3 + c4          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">12</span> c1 + c2 + c3     </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">13</span> c1 + c2 + c4     </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">14</span> c1 + c3 + c4     </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">15</span> c2 + c3 + c4     </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">16</span> c1 + c2 + c3 + c4</span></span></code></pre>
<div class="sourceCode" id="cb23"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://dplyr.tidyverse.org/reference/distinct.html" class="external-link">distinct</a></span><span class="op">(</span><span class="va">specs2</span><span class="op">$</span><span class="va">specs</span>, <span class="va">controls</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 6 × 1</span></span></span>
<span><span class="co">##   controls      </span></span>
<span><span class="co">##   <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>         </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span> no covariates </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span> c1            </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">3</span> c2            </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">4</span> c3            </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">5</span> c4            </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">6</span> all covariates</span></span></code></pre>
</div>
<div class="section level3">
<h3 id="sets-of-covariates">Sets of covariates<a class="anchor" aria-label="anchor" href="#sets-of-covariates"></a>
</h3>
<p>We can also add groups of covariates. The way setup creates
combinations between such groups remains the same.</p>
<div class="sourceCode" id="cb25"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># Add groups of covariates</span></span>
<span><span class="va">specs3</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/setup.html">setup</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">example_data</span>,</span>
<span>               x <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"x1"</span>, <span class="st">"x2"</span><span class="op">)</span>,</span>
<span>               y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"y1"</span>, <span class="st">"y2"</span><span class="op">)</span>,</span>
<span>               model <span class="op">=</span> <span class="st">"lm"</span>,</span>
<span>               controls <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"c1 + c2"</span>, <span class="st">"c3 + c4"</span><span class="op">)</span><span class="op">)</span></span>
<span><span class="co"># Check</span></span>
<span><span class="fu"><a href="https://dplyr.tidyverse.org/reference/distinct.html" class="external-link">distinct</a></span><span class="op">(</span><span class="va">specs3</span><span class="op">$</span><span class="va">specs</span>, <span class="va">controls</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 4 × 1</span></span></span>
<span><span class="co">##   controls         </span></span>
<span><span class="co">##   <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>            </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span> no covariates    </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span> c1 + c2          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">3</span> c3 + c4          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">4</span> c1 + c2 + c3 + c4</span></span></code></pre>
<p>The <code><a href="../reference/setup.html">setup()</a></code> function further has an argument
<code>add_to_formula</code>, which allows to add covariates (or any
other formula-relevant aspects, e.g., random effect structures) to all
model functions.</p>
<div class="sourceCode" id="cb27"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># Add some control variables to all models</span></span>
<span><span class="va">specs4</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/setup.html">setup</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">example_data</span>,</span>
<span>               x <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"x1"</span>, <span class="st">"x2"</span><span class="op">)</span>,</span>
<span>               y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"y1"</span>, <span class="st">"y2"</span><span class="op">)</span>,</span>
<span>               model <span class="op">=</span> <span class="st">"lm"</span>,</span>
<span>               controls <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"c1"</span>, <span class="st">"c2"</span><span class="op">)</span>,</span>
<span>               add_to_formula <span class="op">=</span> <span class="st">"c3"</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Check (see how `c3` is added to each formula, but is not part of controls)</span></span>
<span><span class="va">specs4</span><span class="op">$</span><span class="va">specs</span><span class="op">[</span><span class="fl">1</span><span class="op">:</span><span class="fl">6</span>,<span class="op">]</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 6 × 7</span></span></span>
<span><span class="co">##   x     y     model controls      subsets formula                model_function</span></span>
<span><span class="co">##   <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>         <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>   <span style="color: #949494; font-style: italic;">&lt;glue&gt;</span>                 <span style="color: #949494; font-style: italic;">&lt;list&gt;</span>        </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span> x1    y1    lm    no covariates all     y1 ~ x1 + 1 + c3       <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span> x1    y1    lm    c1            all     y1 ~ x1 + c1 + c3      <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">3</span> x1    y1    lm    c2            all     y1 ~ x1 + c2 + c3      <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">4</span> x1    y1    lm    c1 + c2       all     y1 ~ x1 + c1 + c2 + c3 <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">5</span> x1    y2    lm    no covariates all     y2 ~ x1 + 1 + c3       <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">6</span> x1    y2    lm    c1            all     y2 ~ x1 + c1 + c3      <span style="color: #949494;">&lt;fn&gt;</span></span></span></code></pre>
</div>
<div class="section level3">
<h3 id="dealing-with-duplicates-in-specifications">Dealing with duplicates in specifications<a class="anchor" aria-label="anchor" href="#dealing-with-duplicates-in-specifications"></a>
</h3>
<p>Finally, we may sometimes decide to add one of the independent or
dependent variables as covariates as well. In this case, it would not
make sense to add e.g., <code>x1</code> as covariate if it is already
the independent variable. In this new version of specr, the
<code><a href="../reference/setup.html">setup()</a></code> function automatically detects such duplications
and deletes such specifications.</p>
<div class="sourceCode" id="cb29"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># Adding a covariate that is also a independent or dependent variable</span></span>
<span><span class="va">specs5</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/setup.html">setup</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">example_data</span>,</span>
<span>               x <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"x1"</span>, <span class="st">"x2"</span><span class="op">)</span>,</span>
<span>               y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"y1"</span>, <span class="st">"y2"</span><span class="op">)</span>,</span>
<span>               model <span class="op">=</span> <span class="st">"lm"</span>,</span>
<span>               controls <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"x1"</span>, <span class="st">"y1"</span><span class="op">)</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Check (see how only 9 specifications are kept)</span></span>
<span><span class="va">specs5</span><span class="op">$</span><span class="va">specs</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 9 × 7</span></span></span>
<span><span class="co">##   x     y     model controls      subsets formula           model_function</span></span>
<span><span class="co">##   <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>         <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>   <span style="color: #949494; font-style: italic;">&lt;glue&gt;</span>            <span style="color: #949494; font-style: italic;">&lt;list&gt;</span>        </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span> x1    y1    lm    no covariates all     y1 ~ x1 + 1       <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span> x1    y2    lm    no covariates all     y2 ~ x1 + 1       <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">3</span> x1    y2    lm    y1            all     y2 ~ x1 + y1      <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">4</span> x2    y1    lm    no covariates all     y1 ~ x2 + 1       <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">5</span> x2    y1    lm    x1            all     y1 ~ x2 + x1      <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">6</span> x2    y2    lm    no covariates all     y2 ~ x2 + 1       <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">7</span> x2    y2    lm    x1            all     y2 ~ x2 + x1      <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">8</span> x2    y2    lm    y1            all     y2 ~ x2 + y1      <span style="color: #949494;">&lt;fn&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">9</span> x2    y2    lm    x1 + y1       all     y2 ~ x2 + x1 + y1 <span style="color: #949494;">&lt;fn&gt;</span></span></span></code></pre>
</div>
</div>
<div class="section level2">
<h2 id="subset-analyses">Subset analyses<a class="anchor" aria-label="anchor" href="#subset-analyses"></a>
</h2>
<p>One of the most powerful arguments within the <code><a href="../reference/setup.html">setup()</a></code>
framework is <code>subsets</code>, which can be used for so-called
“subset analyses”. With this argument, a variety of analytical choices
can be included.</p>
<div class="section level3">
<h3 id="simple-subset-analysis-based-on-grouping-variables">Simple subset analysis based on grouping variables<a class="anchor" aria-label="anchor" href="#simple-subset-analysis-based-on-grouping-variables"></a>
</h3>
<p>In simple cases, we may want to investigate whether a relationship
differs across certain subgroups. In the example data, we can for
example investigate whether the relationship between <code>x</code> and
<code>y</code> differs across age groups (<code>group1</code>) and
gender (<code>group2</code>). We need to can add such subset analyses by
specifying a list in two alternative ways. The easiest way refers to
simply providing named vectors that refer to the respective variables in
the data set and their unique values:</p>
<pre><code>list(group1 = c("young", "middle", "old),
      group2 = c("female", "male"))</code></pre>
<p>Alternatively, we can achieve the same thing by using the function
<code><a href="https://rdrr.io/r/base/unique.html" class="external-link">unique()</a></code>, which extracts the unique values from each
variable in the data set:</p>
<pre><code><span><span class="fu"><a href="https://rdrr.io/r/base/list.html" class="external-link">list</a></span><span class="op">(</span>group1 <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/unique.html" class="external-link">unique</a></span><span class="op">(</span><span class="va">example_data</span><span class="op">$</span><span class="va">group1</span><span class="op">)</span>,</span>
<span>     group2 <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/unique.html" class="external-link">unique</a></span><span class="op">(</span><span class="va">example_data</span><span class="op">$</span><span class="va">group2</span><span class="op">)</span><span class="op">)</span></span></code></pre>
<p>These lists are then passed as combinatorial factors to the expand
function within <code><a href="../reference/setup.html">setup()</a></code>. If two or more grouping variables
are provided in this way (you can provide as many as you want, but it
quickly becomes messy), <code><a href="../reference/setup.html">setup()</a></code> automatically adds them as
“choice” columns to the specification setup data frame. Note how it
automatically adds the value <code>NA</code> to make sure that also the
simple subsets and not only the combinations between the two groups are
included in the specifications. It further creates a new column
“subsets”, which represents the combination of the subsetting
factors.</p>
<div class="sourceCode" id="cb33"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># Setup specifications</span></span>
<span><span class="va">specs</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/setup.html">setup</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">example_data</span>,</span>
<span>               x <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"x1"</span>, <span class="st">"x2"</span><span class="op">)</span>,</span>
<span>               y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"y1"</span>, <span class="st">"y2"</span><span class="op">)</span>,</span>
<span>               model <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"lm"</span><span class="op">)</span>,</span>
<span>               controls <span class="op">=</span> <span class="st">"c1"</span>,</span>
<span>               subsets <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/list.html" class="external-link">list</a></span><span class="op">(</span>group1 <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/unique.html" class="external-link">unique</a></span><span class="op">(</span><span class="va">example_data</span><span class="op">$</span><span class="va">group1</span><span class="op">)</span>,</span>
<span>                              group2 <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/unique.html" class="external-link">unique</a></span><span class="op">(</span><span class="va">example_data</span><span class="op">$</span><span class="va">group2</span><span class="op">)</span><span class="op">)</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Summary of specifications</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/summary.html" class="external-link">summary</a></span><span class="op">(</span><span class="va">specs</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## Setup for the Specification Curve Analysis</span></span>
<span><span class="co">## -------------------------------------------</span></span>
<span><span class="co">## Class:                      specr.setup -- version: 1.0.1 </span></span>
<span><span class="co">## Number of specifications:   96 </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## Specifications:</span></span>
<span><span class="co">## </span></span>
<span><span class="co">##   Independent variable:     x1, x2 </span></span>
<span><span class="co">##   Dependent variable:       y1, y2 </span></span>
<span><span class="co">##   Models:                   lm </span></span>
<span><span class="co">##   Covariates:               no covariates, c1 </span></span>
<span><span class="co">##   Subsets analyses:         middle &amp; female, old &amp; female, young &amp; female, female, middle &amp; male, old &amp; male, young &amp; male, male, middle, old, young, all </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## Function used to extract parameters:</span></span>
<span><span class="co">## </span></span>
<span><span class="co">##   function (x) </span></span>
<span><span class="co">## broom::tidy(x, conf.int = TRUE)</span></span>
<span><span class="co">## &lt;environment: 0x7f9c34e41358&gt;</span></span>
<span><span class="co">## </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## Head of specifications table (first 6 rows):</span></span></code></pre>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 6 × 8</span></span></span>
<span><span class="co">##   x     y     model controls      subsets         group1 group2 formula    </span></span>
<span><span class="co">##   <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>         <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>           <span style="color: #949494; font-style: italic;">&lt;fct&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;fct&gt;</span>  <span style="color: #949494; font-style: italic;">&lt;glue&gt;</span>     </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span> x1    y1    lm    no covariates middle &amp; female middle female y1 ~ x1 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span> x1    y1    lm    no covariates old &amp; female    old    female y1 ~ x1 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">3</span> x1    y1    lm    no covariates young &amp; female  young  female y1 ~ x1 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">4</span> x1    y1    lm    no covariates female          <span style="color: #BB0000;">NA</span>     female y1 ~ x1 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">5</span> x1    y1    lm    no covariates middle &amp; male   middle male   y1 ~ x1 + 1</span></span>
<span><span class="co">## <span style="color: #BCBCBC;">6</span> x1    y1    lm    no covariates old &amp; male      old    male   y1 ~ x1 + 1</span></span></code></pre>
<div class="sourceCode" id="cb36"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># Check subsets (in this case, 12 different types of subset analysis,</span></span>
<span><span class="co"># including using "all" subjects)</span></span>
<span><span class="fu"><a href="https://dplyr.tidyverse.org/reference/distinct.html" class="external-link">distinct</a></span><span class="op">(</span><span class="va">specs</span><span class="op">$</span><span class="va">specs</span>, <span class="va">subsets</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 12 × 1</span></span></span>
<span><span class="co">##    subsets        </span></span>
<span><span class="co">##    <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 1</span> middle &amp; female</span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 2</span> old &amp; female   </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 3</span> young &amp; female </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 4</span> female         </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 5</span> middle &amp; male  </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 6</span> old &amp; male     </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 7</span> young &amp; male   </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 8</span> male           </span></span>
<span><span class="co">## <span style="color: #BCBCBC;"> 9</span> middle         </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">10</span> old            </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">11</span> young          </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">12</span> all</span></span></code></pre>
<div class="sourceCode" id="cb38"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># Run analysis and plot results</span></span>
<span><span class="va">results</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/specr.html">specr</a></span><span class="op">(</span><span class="va">specs</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/graphics/plot.default.html" class="external-link">plot</a></span><span class="op">(</span><span class="va">results</span>, choices <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"x"</span>, <span class="st">"y"</span>, <span class="st">"subsets"</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="different-specifications_files/figure-html/unnamed-chunk-10-1.png" width="700"></p>
</div>
<div class="section level3">
<h3 id="more-complex-analysis-using-the-subsets-functionality">More complex analysis using the subsets functionality<a class="anchor" aria-label="anchor" href="#more-complex-analysis-using-the-subsets-functionality"></a>
</h3>
<p>The true potential of this type of subsetting can only be grasped
with another example. Let’s imagine we want to include the removal of
outliers as an analytical choice in our specification curve analysis.
Perhaps we are unsure about the cut-off value and want to investigate
different (arbitrary) ones. This can again be seen as a subset
analysis.</p>
<p>We first create variables that denote who is an outlier and who is
not according to the rule we want to use. We then pass these variables
as subsets to the <code><a href="../reference/setup.html">setup()</a></code> function. This again produces
non-meaningful specifications (e.g., a subset that includes outliers, a
subset that includes people who align with different outlier rules,
etc.). So we want to keep only the meaningful specifications. In this
case, these are just 7 different types of specifications.</p>
<div class="sourceCode" id="cb39"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># Create variables that denote outliers (here with a range of arbitrary thresholds)</span></span>
<span><span class="va">data</span> <span class="op">&lt;-</span> <span class="va">data</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/mutate.html" class="external-link">mutate</a></span><span class="op">(</span>outlier1 <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/ifelse.html" class="external-link">ifelse</a></span><span class="op">(</span><span class="va">y1</span> <span class="op">&lt;</span> <span class="fu"><a href="https://rdrr.io/r/base/mean.html" class="external-link">mean</a></span><span class="op">(</span><span class="va">y1</span><span class="op">)</span> <span class="op">-</span> <span class="fl">2</span><span class="op">*</span><span class="fu"><a href="https://rdrr.io/r/stats/sd.html" class="external-link">sd</a></span><span class="op">(</span><span class="va">y1</span><span class="op">)</span> <span class="op">|</span> <span class="va">y1</span> <span class="op">&gt;</span> <span class="fu"><a href="https://rdrr.io/r/base/mean.html" class="external-link">mean</a></span><span class="op">(</span><span class="va">y1</span><span class="op">)</span> <span class="op">+</span> <span class="fl">2</span><span class="op">*</span><span class="fu"><a href="https://rdrr.io/r/stats/sd.html" class="external-link">sd</a></span><span class="op">(</span><span class="va">y1</span><span class="op">)</span>, <span class="st">"outlier"</span>, <span class="st">"2.0*SD"</span><span class="op">)</span>,</span>
<span>         outlier2 <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/ifelse.html" class="external-link">ifelse</a></span><span class="op">(</span><span class="va">y1</span> <span class="op">&lt;</span> <span class="fu"><a href="https://rdrr.io/r/base/mean.html" class="external-link">mean</a></span><span class="op">(</span><span class="va">y1</span><span class="op">)</span> <span class="op">-</span> <span class="fl">2.1</span><span class="op">*</span><span class="fu"><a href="https://rdrr.io/r/stats/sd.html" class="external-link">sd</a></span><span class="op">(</span><span class="va">y1</span><span class="op">)</span> <span class="op">|</span> <span class="va">y1</span> <span class="op">&gt;</span> <span class="fu"><a href="https://rdrr.io/r/base/mean.html" class="external-link">mean</a></span><span class="op">(</span><span class="va">y1</span><span class="op">)</span> <span class="op">+</span> <span class="fl">2.1</span><span class="op">*</span><span class="fu"><a href="https://rdrr.io/r/stats/sd.html" class="external-link">sd</a></span><span class="op">(</span><span class="va">y1</span><span class="op">)</span>, <span class="st">"outlier"</span>, <span class="st">"2.1*SD"</span><span class="op">)</span>,</span>
<span>         outlier3 <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/ifelse.html" class="external-link">ifelse</a></span><span class="op">(</span><span class="va">y1</span> <span class="op">&lt;</span> <span class="fu"><a href="https://rdrr.io/r/base/mean.html" class="external-link">mean</a></span><span class="op">(</span><span class="va">y1</span><span class="op">)</span> <span class="op">-</span> <span class="fl">2.2</span><span class="op">*</span><span class="fu"><a href="https://rdrr.io/r/stats/sd.html" class="external-link">sd</a></span><span class="op">(</span><span class="va">y1</span><span class="op">)</span> <span class="op">|</span> <span class="va">y1</span> <span class="op">&gt;</span> <span class="fu"><a href="https://rdrr.io/r/base/mean.html" class="external-link">mean</a></span><span class="op">(</span><span class="va">y1</span><span class="op">)</span> <span class="op">+</span> <span class="fl">2.2</span><span class="op">*</span><span class="fu"><a href="https://rdrr.io/r/stats/sd.html" class="external-link">sd</a></span><span class="op">(</span><span class="va">y1</span><span class="op">)</span>, <span class="st">"outlier"</span>, <span class="st">"2.2*SD"</span><span class="op">)</span>,</span>
<span>         outlier4 <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/ifelse.html" class="external-link">ifelse</a></span><span class="op">(</span><span class="va">y1</span> <span class="op">&lt;</span> <span class="fu"><a href="https://rdrr.io/r/base/mean.html" class="external-link">mean</a></span><span class="op">(</span><span class="va">y1</span><span class="op">)</span> <span class="op">-</span> <span class="fl">2.3</span><span class="op">*</span><span class="fu"><a href="https://rdrr.io/r/stats/sd.html" class="external-link">sd</a></span><span class="op">(</span><span class="va">y1</span><span class="op">)</span> <span class="op">|</span> <span class="va">y1</span> <span class="op">&gt;</span> <span class="fu"><a href="https://rdrr.io/r/base/mean.html" class="external-link">mean</a></span><span class="op">(</span><span class="va">y1</span><span class="op">)</span> <span class="op">+</span> <span class="fl">2.3</span><span class="op">*</span><span class="fu"><a href="https://rdrr.io/r/stats/sd.html" class="external-link">sd</a></span><span class="op">(</span><span class="va">y1</span><span class="op">)</span>, <span class="st">"outlier"</span>, <span class="st">"2.3*SD"</span><span class="op">)</span>,</span>
<span>         outlier5 <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/ifelse.html" class="external-link">ifelse</a></span><span class="op">(</span><span class="va">y1</span> <span class="op">&lt;</span> <span class="fu"><a href="https://rdrr.io/r/base/mean.html" class="external-link">mean</a></span><span class="op">(</span><span class="va">y1</span><span class="op">)</span> <span class="op">-</span> <span class="fl">2.4</span><span class="op">*</span><span class="fu"><a href="https://rdrr.io/r/stats/sd.html" class="external-link">sd</a></span><span class="op">(</span><span class="va">y1</span><span class="op">)</span> <span class="op">|</span> <span class="va">y1</span> <span class="op">&gt;</span> <span class="fu"><a href="https://rdrr.io/r/base/mean.html" class="external-link">mean</a></span><span class="op">(</span><span class="va">y1</span><span class="op">)</span> <span class="op">+</span> <span class="fl">2.4</span><span class="op">*</span><span class="fu"><a href="https://rdrr.io/r/stats/sd.html" class="external-link">sd</a></span><span class="op">(</span><span class="va">y1</span><span class="op">)</span>, <span class="st">"outlier"</span>, <span class="st">"2.4*SD"</span><span class="op">)</span>,</span>
<span>         outlier6 <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/ifelse.html" class="external-link">ifelse</a></span><span class="op">(</span><span class="va">y1</span> <span class="op">&lt;</span> <span class="fu"><a href="https://rdrr.io/r/base/mean.html" class="external-link">mean</a></span><span class="op">(</span><span class="va">y1</span><span class="op">)</span> <span class="op">-</span> <span class="fl">2.5</span><span class="op">*</span><span class="fu"><a href="https://rdrr.io/r/stats/sd.html" class="external-link">sd</a></span><span class="op">(</span><span class="va">y1</span><span class="op">)</span> <span class="op">|</span> <span class="va">y1</span> <span class="op">&gt;</span> <span class="fu"><a href="https://rdrr.io/r/base/mean.html" class="external-link">mean</a></span><span class="op">(</span><span class="va">y1</span><span class="op">)</span> <span class="op">+</span> <span class="fl">2.5</span><span class="op">*</span><span class="fu"><a href="https://rdrr.io/r/stats/sd.html" class="external-link">sd</a></span><span class="op">(</span><span class="va">y1</span><span class="op">)</span>, <span class="st">"outlier"</span>, <span class="st">"2.5*SD"</span><span class="op">)</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Setup specs</span></span>
<span><span class="va">specs</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/setup.html">setup</a></span><span class="op">(</span>data <span class="op">=</span> <span class="va">data</span>,</span>
<span>               x <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"x1"</span>, <span class="st">"x2"</span><span class="op">)</span>,</span>
<span>               y <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"y1"</span>, <span class="st">"y2"</span><span class="op">)</span>,</span>
<span>               model <span class="op">=</span> <span class="st">"lm"</span>,</span>
<span>               controls <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"c1"</span>, <span class="st">"c2"</span><span class="op">)</span>,</span>
<span>               subsets <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/list.html" class="external-link">list</a></span><span class="op">(</span>outlier1 <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"2.0*SD"</span><span class="op">)</span>,</span>
<span>                              outlier2 <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"2.1*SD"</span><span class="op">)</span>,</span>
<span>                              outlier3 <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"2.2*SD"</span><span class="op">)</span>,</span>
<span>                              outlier4 <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"2.3*SD"</span><span class="op">)</span>,</span>
<span>                              outlier5 <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"2.4*SD"</span><span class="op">)</span>,</span>
<span>                              outlier6 <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"2.5*SD"</span><span class="op">)</span><span class="op">)</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Remove unnecessary combinations</span></span>
<span><span class="va">specs</span><span class="op">$</span><span class="va">specs</span> <span class="op">&lt;-</span> <span class="va">specs</span><span class="op">$</span><span class="va">specs</span> <span class="op"><a href="https://magrittr.tidyverse.org/reference/pipe.html" class="external-link">%&gt;%</a></span></span>
<span>  <span class="fu"><a href="https://dplyr.tidyverse.org/reference/filter.html" class="external-link">filter</a></span><span class="op">(</span><span class="va">subsets</span> <span class="op">==</span> <span class="st">"2.0*SD"</span> <span class="op">|</span> <span class="va">subsets</span> <span class="op">==</span> <span class="st">"2.1*SD"</span> <span class="op">|</span> </span>
<span>         <span class="va">subsets</span> <span class="op">==</span> <span class="st">"2.2*SD"</span> <span class="op">|</span> <span class="va">subsets</span> <span class="op">==</span> <span class="st">"2.3*SD"</span> <span class="op">|</span></span>
<span>         <span class="va">subsets</span> <span class="op">==</span> <span class="st">"2.4*SD"</span> <span class="op">|</span> <span class="va">subsets</span> <span class="op">==</span> <span class="st">"2.5*SD"</span> <span class="op">|</span>  </span>
<span>         <span class="va">subsets</span> <span class="op">==</span> <span class="st">"all"</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Check specifications (see how it contains only meaningful subsets?)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/summary.html" class="external-link">summary</a></span><span class="op">(</span><span class="va">specs</span>, rows <span class="op">=</span> <span class="fl">7</span><span class="op">)</span></span></code></pre></div>
<pre><code><span><span class="co">## Setup for the Specification Curve Analysis</span></span>
<span><span class="co">## -------------------------------------------</span></span>
<span><span class="co">## Class:                      specr.setup -- version: 1.0.1 </span></span>
<span><span class="co">## Number of specifications:   1024 </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## Specifications:</span></span>
<span><span class="co">## </span></span>
<span><span class="co">##   Independent variable:     x1, x2 </span></span>
<span><span class="co">##   Dependent variable:       y1, y2 </span></span>
<span><span class="co">##   Models:                   lm </span></span>
<span><span class="co">##   Covariates:               no covariates, c1, c2, c1 + c2 </span></span>
<span><span class="co">##   Subsets analyses:         2.5*SD, 2.4*SD, 2.3*SD, 2.2*SD, 2.1*SD, 2.0*SD, all </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## Function used to extract parameters:</span></span>
<span><span class="co">## </span></span>
<span><span class="co">##   function (x) </span></span>
<span><span class="co">## broom::tidy(x, conf.int = TRUE)</span></span>
<span><span class="co">## &lt;environment: 0x7f9c34649be8&gt;</span></span>
<span><span class="co">## </span></span>
<span><span class="co">## </span></span>
<span><span class="co">## Head of specifications table (first 7 rows):</span></span></code></pre>
<pre><code><span><span class="co">## <span style="color: #949494;"># A tibble: 7 × 12</span></span></span>
<span><span class="co">##   x     y     model controls      subsets outlier1 outlier2 outlier3 outlier4</span></span>
<span><span class="co">##   <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span> <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>         <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>   <span style="color: #949494; font-style: italic;">&lt;fct&gt;</span>    <span style="color: #949494; font-style: italic;">&lt;fct&gt;</span>    <span style="color: #949494; font-style: italic;">&lt;fct&gt;</span>    <span style="color: #949494; font-style: italic;">&lt;fct&gt;</span>   </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">1</span> x1    y1    lm    no covariates 2.5*SD  <span style="color: #BB0000;">NA</span>       <span style="color: #BB0000;">NA</span>       <span style="color: #BB0000;">NA</span>       <span style="color: #BB0000;">NA</span>      </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">2</span> x1    y1    lm    no covariates 2.4*SD  <span style="color: #BB0000;">NA</span>       <span style="color: #BB0000;">NA</span>       <span style="color: #BB0000;">NA</span>       <span style="color: #BB0000;">NA</span>      </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">3</span> x1    y1    lm    no covariates 2.3*SD  <span style="color: #BB0000;">NA</span>       <span style="color: #BB0000;">NA</span>       <span style="color: #BB0000;">NA</span>       2.3*SD  </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">4</span> x1    y1    lm    no covariates 2.2*SD  <span style="color: #BB0000;">NA</span>       <span style="color: #BB0000;">NA</span>       2.2*SD   <span style="color: #BB0000;">NA</span>      </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">5</span> x1    y1    lm    no covariates 2.1*SD  <span style="color: #BB0000;">NA</span>       2.1*SD   <span style="color: #BB0000;">NA</span>       <span style="color: #BB0000;">NA</span>      </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">6</span> x1    y1    lm    no covariates 2.0*SD  2.0*SD   <span style="color: #BB0000;">NA</span>       <span style="color: #BB0000;">NA</span>       <span style="color: #BB0000;">NA</span>      </span></span>
<span><span class="co">## <span style="color: #BCBCBC;">7</span> x1    y1    lm    no covariates all     <span style="color: #BB0000;">NA</span>       <span style="color: #BB0000;">NA</span>       <span style="color: #BB0000;">NA</span>       <span style="color: #BB0000;">NA</span>      </span></span>
<span><span class="co">## <span style="color: #949494;"># ℹ 3 more variables: outlier5 &lt;fct&gt;, outlier6 &lt;fct&gt;, formula &lt;glue&gt;</span></span></span></code></pre>
<div class="sourceCode" id="cb42"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># Run analysis and plot results</span></span>
<span><span class="va">results</span> <span class="op">&lt;-</span> <span class="fu"><a href="../reference/specr.html">specr</a></span><span class="op">(</span><span class="va">specs</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/graphics/plot.default.html" class="external-link">plot</a></span><span class="op">(</span><span class="va">results</span>, choices <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"x"</span>, <span class="st">"y"</span>, <span class="st">"subsets"</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p><img src="different-specifications_files/figure-html/unnamed-chunk-11-1.png" width="700"></p>
<p>There are of course many more types of analytical decisions. For some
more examples, see some of the other vignettes. If you have a specific
type of analytical decision that you don’t know how to include, feel
free to open an issue on github.</p>
</div>
</div>
  </div>

  <div class="col-md-3 hidden-xs hidden-sm" id="pkgdown-sidebar">

        <nav id="toc" data-toggle="toc"><h2 data-toc-skip>Contents</h2>
    </nav>
</div>

</div>



      <footer><div class="copyright">
  <p></p>
<p>Developed by Philipp K. Masur, Michael Scharkow.</p>
</div>

<div class="pkgdown">
  <p></p>
<p>Site built with <a href="https://pkgdown.r-lib.org/" class="external-link">pkgdown</a> 2.0.7.</p>
</div>

      </footer>
</div>

  


  

  </body>
</html>
