title: Find the CI for a given interval of HDI in R?
body: <p>I'm looking for a method to calculate the value of the CI for a given a range around the mode.  For example, if my mode (or value with highest KDE) is 50 and I establish an acceptable range of 10 around the mode, what is my equivalent CI?</p>
  
  <p>I've been successful with following the HDI content from <a href="https://rdrr.io/cran/HDInterval/man/hdi.html" rel="nofollow noreferrer">https://rdrr.io/cran/HDInterval/man/hdi.html</a> to find the range based on a given CI, but I'm attempting to the reverse the process.  Sample code copied from site to this post for convenience of testing with data:</p>
  
  <pre><code># for a vector:
  tst &lt;- rgamma(1e5, 2.5, 2)
  hdi(tst)
  hdi(tst, credMass=0.8)
  # For comparison, the symmetrical 80% CrI:
  quantile(tst, c(0.1,0.9))
  
  # for a density:
  dens &lt;- density(tst)
  hdi(dens, credMass=0.8)
  </code></pre>
  
  <p>My brute-force method would consider something like calculating the difference between the upper/lower values across different CI input until I find a the maximum CI for a given delta between upper and lower.  I'm sure there's a better way?</p>
  
  <p>The goal of this is to compare how the CI changes across different datasets following the same range of values.  Data is often multimodal and trying to avoid constraints of fitting to a distribution.</p>