<!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, shrink-to-fit=no"><meta name="description" content='Parses simple string distribution specifications, like "normal(0, 1)", into two columns of
a data frame, suitable for use with the dist and args aesthetics of stat_slabinterval()
and its shortcut stats (like stat_halfeye()). This format is output
by brms::get_prior, making it particularly useful for visualizing priors from
brms models.'><title>Parse distribution specifications into columns of a data frame — parse_dist • ggdist</title><!-- favicons --><link rel="icon" type="image/png" sizes="16x16" href="../favicon-16x16.png"><link rel="icon" type="image/png" sizes="32x32" href="../favicon-32x32.png"><link rel="apple-touch-icon" type="image/png" sizes="180x180" href="../apple-touch-icon.png"><link rel="apple-touch-icon" type="image/png" sizes="120x120" href="../apple-touch-icon-120x120.png"><link rel="apple-touch-icon" type="image/png" sizes="76x76" href="../apple-touch-icon-76x76.png"><link rel="apple-touch-icon" type="image/png" sizes="60x60" href="../apple-touch-icon-60x60.png"><script src="../deps/jquery-3.6.0/jquery-3.6.0.min.js"></script><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><link href="../deps/bootstrap-5.3.1/bootstrap.min.css" rel="stylesheet"><script src="../deps/bootstrap-5.3.1/bootstrap.bundle.min.js"></script><link href="../deps/Source_Sans_Pro-0.4.8/font.css" rel="stylesheet"><link href="../deps/Source_Code_Pro-0.4.8/font.css" rel="stylesheet"><!-- 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"><!-- bootstrap-toc --><script src="https://cdn.jsdelivr.net/gh/afeld/bootstrap-toc@v1.0.1/dist/bootstrap-toc.min.js" integrity="sha256-4veVQbu7//Lk5TSmc7YV48MxtMy98e26cf5MrgZYnwo=" 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><!-- 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><!-- search --><script src="https://cdnjs.cloudflare.com/ajax/libs/fuse.js/6.4.6/fuse.js" integrity="sha512-zv6Ywkjyktsohkbp9bb45V6tEMoWhzFzXis+LrMehmJZZSys19Yxf1dopHx7WzIKxr5tK2dVcYmaCk2uqdjF4A==" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/autocomplete.js/0.38.0/autocomplete.jquery.min.js" integrity="sha512-GU9ayf+66Xx2TmpxqJpliWbT5PiGYxpaG8rfnBEk1LL8l1KGkRShhngwdXK1UgqhAzWpZHSiYPc09/NwDQIGyg==" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mark.js/8.11.1/mark.min.js" integrity="sha512-5CYOlHXGh6QpOFA/TeTylKLWfB3ftPsde7AnmhuitiTX4K5SqCLBeKro6sPS8ilsz1Q4NRx3v8Ko2IBiszzdww==" crossorigin="anonymous"></script><!-- pkgdown --><script src="../pkgdown.js"></script><meta property="og:title" content="Parse distribution specifications into columns of a data frame — parse_dist"><meta property="og:description" content='Parses simple string distribution specifications, like "normal(0, 1)", into two columns of
a data frame, suitable for use with the dist and args aesthetics of stat_slabinterval()
and its shortcut stats (like stat_halfeye()). This format is output
by brms::get_prior, making it particularly useful for visualizing priors from
brms models.'><meta property="og:image" content="http://mjskay.github.io/ggdist/logo.svg"><!-- 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]--><!-- Global site tag (gtag.js) - Google Analytics --><script async src="https://www.googletagmanager.com/gtag/js?id=UA-93322-5"></script><script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-93322-5');
</script></head><body>
    <a href="#main" class="visually-hidden-focusable">Skip to contents</a>
    

    <nav class="navbar fixed-top navbar-light navbar-expand-lg bg-light"><div class="container">
    
    <a class="navbar-brand me-2" href="../index.html">ggdist</a>

    <small class="nav-text text-muted me-auto" data-bs-toggle="tooltip" data-bs-placement="bottom" title="">3.3.2</small>

    
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbar" aria-controls="navbar" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>

    <div id="navbar" class="collapse navbar-collapse ms-3">
      <ul class="navbar-nav me-auto"><li class="active nav-item">
  <a class="nav-link" href="../reference/index.html">Reference</a>
</li>
<li class="nav-item dropdown">
  <a href="#" class="nav-link dropdown-toggle" data-bs-toggle="dropdown" role="button" aria-expanded="false" aria-haspopup="true" id="dropdown-articles">Articles</a>
  <div class="dropdown-menu" aria-labelledby="dropdown-articles">
    <a class="dropdown-item" href="../articles/dotsinterval.html">Dots + interval stats and geoms</a>
    <a class="dropdown-item" href="../articles/freq-uncertainty-vis.html">Frequentist uncertainty visualization</a>
    <a class="dropdown-item" href="../articles/lineribbon.html">Lineribbon stats and geoms</a>
    <a class="dropdown-item" href="../articles/slabinterval.html">Slab + interval stats and geoms</a>
    <a class="dropdown-item" href="../articles/thickness.html">Thickness scale</a>
  </div>
</li>
<li class="nav-item">
  <a class="nav-link" href="../news/index.html">Changelog</a>
</li>
      </ul><form class="form-inline my-2 my-lg-0" role="search">
        <input type="search" class="form-control me-sm-2" aria-label="Toggle navigation" name="search-input" data-search-index="../search.json" id="search-input" placeholder="Search for" autocomplete="off"></form>

      <ul class="navbar-nav"><li class="nav-item">
  <a class="external-link nav-link" href="https://github.com/mjskay/ggdist/" aria-label="github">
    <span class="fab fa fab fa-github fa-lg"></span>
     
  </a>
</li>
      </ul></div>

    
  </div>
</nav><div class="container template-reference-topic">
<div class="row">
  <main id="main" class="col-md-9"><div class="page-header">
      <img src="../logo.svg" class="logo" alt=""><h1>Parse distribution specifications into columns of a data frame</h1>
      <small class="dont-index">Source: <a href="https://github.com/mjskay/ggdist/blob/HEAD/R/parse_dist.R" class="external-link"><code>R/parse_dist.R</code></a></small>
      <div class="d-none name"><code>parse_dist.Rd</code></div>
    </div>

    <div class="ref-description section level2">
    <p>Parses simple string distribution specifications, like <code>"normal(0, 1)"</code>, into two columns of
a data frame, suitable for use with the <code>dist</code> and <code>args</code> aesthetics of <code><a href="stat_slabinterval.html">stat_slabinterval()</a></code>
and its shortcut stats (like <code><a href="stat_halfeye.html">stat_halfeye()</a></code>). This format is output
by <code><a href="https://paul-buerkner.github.io/brms/reference/get_prior.html" class="external-link">brms::get_prior</a></code>, making it particularly useful for visualizing priors from
brms models.</p>
    </div>

    <div class="section level2">
    <h2 id="ref-usage">Usage<a class="anchor" aria-label="anchor" href="#ref-usage"></a></h2>
    <div class="sourceCode"><pre class="sourceCode r"><code><span><span class="fu">parse_dist</span><span class="op">(</span></span>
<span>  <span class="va">object</span>,</span>
<span>  <span class="va">...</span>,</span>
<span>  dist <span class="op">=</span> <span class="st">".dist"</span>,</span>
<span>  args <span class="op">=</span> <span class="st">".args"</span>,</span>
<span>  dist_obj <span class="op">=</span> <span class="st">".dist_obj"</span>,</span>
<span>  package <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  to_r_names <span class="op">=</span> <span class="cn">TRUE</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="co"># S3 method for default</span></span>
<span><span class="fu">parse_dist</span><span class="op">(</span><span class="va">object</span>, <span class="va">...</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># S3 method for data.frame</span></span>
<span><span class="fu">parse_dist</span><span class="op">(</span></span>
<span>  <span class="va">object</span>,</span>
<span>  <span class="va">dist_col</span>,</span>
<span>  <span class="va">...</span>,</span>
<span>  dist <span class="op">=</span> <span class="st">".dist"</span>,</span>
<span>  args <span class="op">=</span> <span class="st">".args"</span>,</span>
<span>  dist_obj <span class="op">=</span> <span class="st">".dist_obj"</span>,</span>
<span>  package <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  lb <span class="op">=</span> <span class="st">"lb"</span>,</span>
<span>  ub <span class="op">=</span> <span class="st">"ub"</span>,</span>
<span>  to_r_names <span class="op">=</span> <span class="cn">TRUE</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="co"># S3 method for character</span></span>
<span><span class="fu">parse_dist</span><span class="op">(</span></span>
<span>  <span class="va">object</span>,</span>
<span>  <span class="va">...</span>,</span>
<span>  dist <span class="op">=</span> <span class="st">".dist"</span>,</span>
<span>  args <span class="op">=</span> <span class="st">".args"</span>,</span>
<span>  dist_obj <span class="op">=</span> <span class="st">".dist_obj"</span>,</span>
<span>  package <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  to_r_names <span class="op">=</span> <span class="cn">TRUE</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="co"># S3 method for factor</span></span>
<span><span class="fu">parse_dist</span><span class="op">(</span></span>
<span>  <span class="va">object</span>,</span>
<span>  <span class="va">...</span>,</span>
<span>  dist <span class="op">=</span> <span class="st">".dist"</span>,</span>
<span>  args <span class="op">=</span> <span class="st">".args"</span>,</span>
<span>  dist_obj <span class="op">=</span> <span class="st">".dist_obj"</span>,</span>
<span>  package <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  to_r_names <span class="op">=</span> <span class="cn">TRUE</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="co"># S3 method for brmsprior</span></span>
<span><span class="fu">parse_dist</span><span class="op">(</span></span>
<span>  <span class="va">object</span>,</span>
<span>  dist_col <span class="op">=</span> <span class="va">prior</span>,</span>
<span>  <span class="va">...</span>,</span>
<span>  dist <span class="op">=</span> <span class="st">".dist"</span>,</span>
<span>  args <span class="op">=</span> <span class="st">".args"</span>,</span>
<span>  dist_obj <span class="op">=</span> <span class="st">".dist_obj"</span>,</span>
<span>  package <span class="op">=</span> <span class="cn">NULL</span>,</span>
<span>  to_r_names <span class="op">=</span> <span class="cn">TRUE</span></span>
<span><span class="op">)</span></span>
<span></span>
<span><span class="fu">r_dist_name</span><span class="op">(</span><span class="va">dist_name</span><span class="op">)</span></span></code></pre></div>
    </div>

    <div class="section level2">
    <h2 id="arguments">Arguments<a class="anchor" aria-label="anchor" href="#arguments"></a></h2>
    <dl><dt>object</dt>
<dd><p>A character vector containing distribution specifications or a data frame with a column
containing distribution specifications.</p></dd>


<dt>...</dt>
<dd><p>Arguments passed to other implementations of <code>parse_dist</code>.</p></dd>


<dt>dist</dt>
<dd><p>The name of the output column to contain the distribution name</p></dd>


<dt>args</dt>
<dd><p>The name of the output column to contain the arguments to the distribution</p></dd>


<dt>dist_obj</dt>
<dd><p>The name of the output column to contain a <span class="pkg">distributional</span> object representing the distribution</p></dd>


<dt>package</dt>
<dd><p>The package or environment to search for distribution functions in.
Passed to <code><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_wrap.html" class="external-link">distributional::dist_wrap()</a></code>. One of:</p><ul><li><p><code>NULL</code>: use the calling environment</p></li>
<li><p>a string: use the environment for the package with the given name</p></li>
<li><p>an <a href="https://rdrr.io/r/base/environment.html" class="external-link">environment</a>: use the given environment</p></li>
</ul></dd>


<dt>to_r_names</dt>
<dd><p>If <code>TRUE</code> (the default), certain common aliases for distribution names are
automatically translated into names that R can recognize (i.e., names which have functions starting
with <code>r</code>, <code>p</code>, <code>q</code>, and <code>d</code> representing random number generators, distribution
functions, etc. for that distribution), using the <code>r_dist_name</code> function. For example,
<code>"normal"</code> is translated into <code>"norm"</code> and <code>"lognormal"</code> is translated into <code>"lnorm"</code>.</p></dd>


<dt>dist_col</dt>
<dd><p>A bare (unquoted) column or column expression that resolves to a character vector
of distribution specifications.</p></dd>


<dt>lb</dt>
<dd><p>The name of an input column (for <code>data.frame</code> and <code><a href="https://paul-buerkner.github.io/brms/reference/set_prior.html" class="external-link">brms::prior</a></code> objects) that contains
the lower bound of the distribution, which if present will produce a truncated distribution using
<code><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_truncated.html" class="external-link">dist_truncated()</a></code>. Ignored if <code>lb</code> is <code>NULL</code> or if <code>object[[lb]]</code> is <code>NA</code> for the corresponding
input row.</p></dd>


<dt>ub</dt>
<dd><p>The name of an input column (for <code>data.frame</code> and <code><a href="https://paul-buerkner.github.io/brms/reference/set_prior.html" class="external-link">brms::prior</a></code> objects) that contains
the upper bound of the distribution, which if present will produce a truncated distribution using
<code><a href="https://pkg.mitchelloharawild.com/distributional/reference/dist_truncated.html" class="external-link">dist_truncated()</a></code>. Ignored if <code>ub</code> is <code>NULL</code> or if <code>object[[ub]]</code> is <code>NA</code> for the corresponding
input row.</p></dd>


<dt>dist_name</dt>
<dd><p>For <code>r_dist_name</code>, a character vector of distribution names to be translated into
distribution names R recognizes. Unrecognized names are left as-is.</p></dd>

</dl></div>
    <div class="section level2">
    <h2 id="value">Value<a class="anchor" aria-label="anchor" href="#value"></a></h2>
    

<ul><li><p><code>parse_dist</code> returns a data frame containing at least two columns named after the <code>dist</code> and <code>args</code>
parameters. If the input is a data frame, the output is a data frame of the same length with those
two columns added. If the input is a character vector or factor, the output is a two-column data frame
with the same number of rows as the length of the input.</p></li>
<li><p><code>r_dist_name</code> returns a character vector the same length as the input containing translations of the
input names into distribution names R can recognize.</p></li>
</ul></div>
    <div class="section level2">
    <h2 id="details">Details<a class="anchor" aria-label="anchor" href="#details"></a></h2>
    <p><code>parse_dist()</code> can be applied to character vectors or to a data frame + bare column name of the
column to parse, and returns a data frame with <code>".dist"</code> and <code>".args"</code> columns added.
<code>parse_dist()</code> uses <code>r_dist_name()</code> to translate distribution names into names recognized
by R.</p>
<p><code>r_dist_name()</code> takes a character vector of names and translates common names into R
distribution names. Names are first made into valid R names using <code><a href="https://rdrr.io/r/base/make.names.html" class="external-link">make.names()</a></code>,
then translated (ignoring character case, <code>"."</code>, and <code>"_"</code>). Thus, <code>"lognormal"</code>,
<code>"LogNormal"</code>, <code>"log_normal"</code>, <code>"log-Normal"</code>, and any number of other variants
all get translated into <code>"lnorm"</code>.</p>
    </div>
    <div class="section level2">
    <h2 id="see-also">See also<a class="anchor" aria-label="anchor" href="#see-also"></a></h2>
    <div class="dont-index"><p>See <code><a href="stat_slabinterval.html">stat_slabinterval()</a></code> and its shortcut stats, which can easily make use of
the output of this function using the <code>dist</code> and <code>args</code> aesthetics.</p></div>
    </div>

    <div class="section level2">
    <h2 id="ref-examples">Examples<a class="anchor" aria-label="anchor" href="#ref-examples"></a></h2>
    <div class="sourceCode"><pre class="sourceCode r"><code><span class="r-in"><span></span></span>
<span class="r-in"><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://dplyr.tidyverse.org" class="external-link">dplyr</a></span><span class="op">)</span></span></span>
<span class="r-in"><span></span></span>
<span class="r-in"><span><span class="co"># parse dist can operate on strings directly...</span></span></span>
<span class="r-in"><span><span class="fu">parse_dist</span><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">"normal(0,1)"</span>, <span class="st">"student_t(3,0,1)"</span><span class="op">)</span><span class="op">)</span></span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span> <span style="color: #949494;"># A tibble: 2 × 3</span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span>   .dist     .args               .dist_obj</span>
<span class="r-out co"><span class="r-pr">#&gt;</span>   <span style="color: #949494; font-style: italic;">&lt;chr&gt;</span>     <span style="color: #949494; font-style: italic;">&lt;list&gt;</span>                 <span style="color: #949494; font-style: italic;">&lt;dist&gt;</span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span> <span style="color: #BCBCBC;">1</span> norm      <span style="color: #949494;">&lt;list [2]&gt;</span>         norm(0, 1)</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> <span style="color: #BCBCBC;">2</span> student_t <span style="color: #949494;">&lt;list [3]&gt;</span> student_t(3, 0, 1)</span>
<span class="r-in"><span></span></span>
<span class="r-in"><span><span class="co"># ... or on columns of a data frame, where it adds the</span></span></span>
<span class="r-in"><span><span class="co"># parsed specs back on as columns</span></span></span>
<span class="r-in"><span><span class="fu"><a href="https://rdrr.io/r/base/data.frame.html" class="external-link">data.frame</a></span><span class="op">(</span>prior <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">"normal(0,1)"</span>, <span class="st">"student_t(3,0,1)"</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="r-in"><span>  <span class="fu">parse_dist</span><span class="op">(</span><span class="va">prior</span><span class="op">)</span></span></span>
<span class="r-out co"><span class="r-pr">#&gt;</span>              prior     .dist   .args          .dist_obj</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> 1      normal(0,1)      norm    0, 1         norm(0, 1)</span>
<span class="r-out co"><span class="r-pr">#&gt;</span> 2 student_t(3,0,1) student_t 3, 0, 1 student_t(3, 0, 1)</span>
<span class="r-in"><span></span></span>
<span class="r-in"><span><span class="co"># parse_dist is particularly useful with the output of brms::prior(),</span></span></span>
<span class="r-in"><span><span class="co"># which follows the same format as above</span></span></span>
<span class="r-in"><span></span></span>
</code></pre></div>
    </div>
  </main><aside class="col-md-3"><nav id="toc"><h2>On this page</h2>
    </nav></aside></div>


    <footer><div class="pkgdown-footer-left">
  <p></p><p>Developed by <a href="https://www.mjskay.com" class="external-link">Matthew Kay</a>.</p>
</div>

<div class="pkgdown-footer-right">
  <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>

