<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Andrew M. Raim</title>
<link>https://andrewraim.github.io/post.html</link>
<atom:link href="https://andrewraim.github.io/post.xml" rel="self" type="application/rss+xml"/>
<description></description>
<generator>quarto-1.9.38</generator>
<lastBuildDate>Fri, 09 Jan 2026 05:00:00 GMT</lastBuildDate>
<item>
  <title>Vim as a Simple (R) IDE without Plugins II</title>
  <link>https://andrewraim.github.io/post/2026-01-09-vim-ide-II.html</link>
  <description><![CDATA[ 




<section id="introduction" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> Introduction</h1>
<p>This post is a sequel to <a href="../post/2026-01-03-vim-ide.html">Vim as a Simple IDE without Plugins</a>. There we showed how to send text from an open file in Vim to a console running another buffer in the same Vim session. This lets us step through scripts in R, Python, Julia, etc interactively without having to start a full IDE like <a href="https://posit.co">Rstudio</a>, or even necessarily be in a graphical environment - we just need a terminal session.</p>
<p>In this post, we make a simple popup menu to work with R packages. If our current working directory is within an R package, the menu will allow us to build &amp; install the package, generate its documentation, run a CRAN check, etc. That will take place in a terminal buffer running in the same Vim session. This post is specific to R, but the pattern might extend to other languages.</p>
<p>The complete code is given in a <a href="../downloads/2026-01-09-vim-ide-II/vimrc">vimrc</a> file; this also includes the material from the <a href="../post/2026-01-03-vim-ide.html">prequel</a>. As before, you can copy or import the contents into your own <a href="https://vimhelp.org/usr_05.txt.html#05.1">vimrc</a> file to make use of them.</p>
<p>The final result is demonstrated in Figure&nbsp;1, where the following sequence is shown.</p>
<ol type="1">
<li>Initially start Vim in a folder which is not an R package and see an alert when we try to bring up the menu.</li>
<li>Exit Vim and change to another folder that is part of a package.</li>
<li>Start Vim, try to build the package documentation, but get informed that no R terminal is running.</li>
<li>Start an R terminal in Vim and exchange windows so that terminal is on the right side of the screen.</li>
<li>Build package documentation and PDF manual using the menu.</li>
<li>Run a line from the script window to view a manual page from the package.</li>
<li>Switch focus to the terminal, close the manual page, and exit the session.</li>
</ol>
<div id="fig-demo" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-demo-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://andrewraim.github.io/downloads/2026-01-09-vim-ide-II/demo.svg" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-demo-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Demonstration.
</figcaption>
</figure>
</div>
</section>
<section id="are-we-in-an-r-package" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> Are we in an R Package?</h1>
<p>Suppose our current working directory is <code>/path/to/folder</code>. To determine whether we are in an R package, we check for the existence of a <a href="https://cran.r-project.org/doc/manuals/r-release/R-exts.html#The-DESCRIPTION-file">DESCRIPTION</a> file in the current directory; if found, that will be considered the root of the project. Otherwise, we repeat the check in the parent directory <code>/path/to</code>. We proceed this way until we reach <code>/</code> where there are no more parents to check. This follows the idea from the <code>find_package_root</code> function in the <a href="https://cran.r-project.org/package=desc">desc</a> R package.</p>
<p>The following Vimscript function carries this out.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode numberSource vim number-lines code-with-copy"><code class="sourceCode"><span id="cb1-1">function! RProjectRoot(path) abort</span>
<span id="cb1-2">    let path = a:path</span>
<span id="cb1-3">    let done = v:false</span>
<span id="cb1-4"></span>
<span id="cb1-5">    while done == v:false</span>
<span id="cb1-6">        let ff = expand(path) . "/" . 'DESCRIPTION'</span>
<span id="cb1-7"></span>
<span id="cb1-8">        if filereadable(ff)</span>
<span id="cb1-9">            return path</span>
<span id="cb1-10">        endif</span>
<span id="cb1-11"></span>
<span id="cb1-12">        let newpath = fnamemodify(path, ':h')</span>
<span id="cb1-13">        let done = (newpath == path)</span>
<span id="cb1-14">        let path = newpath</span>
<span id="cb1-15">    endwhile</span>
<span id="cb1-16"></span>
<span id="cb1-17">    throw "Not within an R project: " . a:path</span>
<span id="cb1-18">endfunction</span></code></pre></div></div>
<p>The <code>fnamemodify</code> function is used to obtain the parent directory of the current path. If the argument <code>path</code> is the root, the result <code>newpath</code> will also be the root, and we have nothing else to check. An exception is thrown if we cannot find any <code>DESCRIPTION</code> file when we reach the root directory.</p>
</section>
<section id="menu-operations-and-r-functions" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> Menu Operations and R Functions</h1>
<p>If we determine that we are in an R package, our menu will carry out selected actions via corresponding calls to the R <a href="https://CRAN.R-project.org/package=devtools">devtools</a> package. This mapping is shown in Table&nbsp;1.</p>
<div id="tbl-devtools" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-tbl figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-tbl" id="tbl-devtools-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;1: Menu operations and corresponding <code>devtools</code> functions.
</figcaption>
<div aria-describedby="tbl-devtools-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<table class="caption-top table">
<thead>
<tr class="header">
<th style="text-align: left;">Action</th>
<th style="text-align: left;">Function</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><code>Load</code></td>
<td style="text-align: left;"><code>load_all</code></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>Install</code></td>
<td style="text-align: left;"><code>install_local</code></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>Document</code></td>
<td style="text-align: left;"><code>document</code></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>Test</code></td>
<td style="text-align: left;"><code>test</code></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>Clean</code></td>
<td style="text-align: left;"><code>clean_dll</code></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>Manual</code></td>
<td style="text-align: left;"><code>build_manual</code></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>Vignettes</code></td>
<td style="text-align: left;"><code>build_vignettes</code></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>Build Source</code></td>
<td style="text-align: left;"><code>build</code> with <code>binary = FALSE</code></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>Build Binary</code></td>
<td style="text-align: left;"><code>build</code> with <code>binary = TRUE</code></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>Check</code></td>
<td style="text-align: left;"><code>check</code></td>
</tr>
</tbody>
</table>
</div>
</figure>
</div>
</section>
<section id="menu-in-vim" class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> Menu in Vim</h1>
<p>For the Vim menu, we will use the <code>popup_menu</code> function. It takes an array of menu options and a dictionary of additional named arguments. We will set two named arguments: the title of the menu and a handler. The handler is another function that is called when an item is selected in the menu; its job is to take the selection and carry out the desired action. Both of these functions are defined within an <code>RProjectMenu</code> function, is invoked to bring up the menu.</p>
<p>The following snippet is an abridged version of <code>RProjectMenu</code>, which can be seen in <a href="../downloads/2026-01-09-vim-ide-II/vimrc">vimrc</a>. The <code>MenuHandler</code> function is somewhat long, so the contents are summarized as comments.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode numberSource vim number-lines code-with-copy"><code class="sourceCode"><span id="cb2-1">function! RProjectMenu() abort</span>
<span id="cb2-2">    function! MenuHandler(id, idx)</span>
<span id="cb2-3">        " 1. Get the selected menu item</span>
<span id="cb2-4">        " 2. Call RProjectRoot to see if we are in an R project; if so,</span>
<span id="cb2-5">        "    get the path to the root. If not, print a message to the</span>
<span id="cb2-6">        "    user and return.</span>
<span id="cb2-7">        " 3. Form a string with the corresponding devtools command.</span>
<span id="cb2-8">        " 4. See if there is a terminal running R. If not, alert the</span>
<span id="cb2-9">        "    user and return.</span>
<span id="cb2-10">        " 5. If we made it this far, send command to the terminal</span>
<span id="cb2-11">        "    running R.</span>
<span id="cb2-12">    endfunction</span>
<span id="cb2-13"></span>
<span id="cb2-14">    try</span>
<span id="cb2-15">        let projroot = RProjectRoot(getcwd())</span>
<span id="cb2-16">    catch /.*/</span>
<span id="cb2-17">        echom v:exception</span>
<span id="cb2-18">        return</span>
<span id="cb2-19">    endtry</span>
<span id="cb2-20"></span>
<span id="cb2-21">    let items = [ 'Load', 'Install', 'Document', 'Test', 'Clean', 'Manual',</span>
<span id="cb2-22">    \ 'Vignettes', 'Build Source', 'Build Binary', 'Check' ]</span>
<span id="cb2-23"></span>
<span id="cb2-24">    let args = #{</span>
<span id="cb2-25">    \ title: 'R Project: ' . pathshorten(expand(projroot)),</span>
<span id="cb2-26">    \ callback: 'MenuHandler'</span>
<span id="cb2-27">    \ }</span>
<span id="cb2-28"></span>
<span id="cb2-29">    call popup_menu(items, args)</span>
<span id="cb2-30">endfunction</span></code></pre></div></div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>In our implementation, we look for a terminal by name using the function call <code>bufnr('R')</code>. The <code>'R'</code> name will be set if we start the terminal using the method developed in the <a href="../post/2026-01-03-vim-ide.html">prequel</a> post, or if we start it manually via <code>:term('R')</code> or <code>:vert term('R')</code>. These actions start a dedicated terminal for R which will exit when the R session ends.</p>
<p>It is also possible to repurpose a running terminal by changing its buffer name. To do this, take the following steps.</p>
<ol type="1">
<li>Change focus to the terminal window.</li>
<li>Enter terminal-normal mode using keystrokes <code>&lt;c-\&gt;&lt;c-n&gt;</code> (<code>Ctrl-\</code> then <code>Ctrl-n</code>).</li>
<li>Enter the command <code>:file R'</code>.</li>
<li>Type <code>i</code> to return to terminal-insert mode</li>
</ol>
</div>
</div>
</section>
<section id="keybinding" class="level1" data-number="5">
<h1 data-number="5"><span class="header-section-number">5</span> Keybinding</h1>
<p>We can set a keybinding to easily call the menu. In the <a href="../post/2026-01-03-vim-ide.html">prequel</a> post, we set the <code>&lt;leader&gt;</code> key to space bar. Here we set <code>&lt;leader&gt;r</code> (i.e., <code>&lt;space&gt;</code> then <code>r</code>) as the binding to invoke the menu.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode numberSource vim number-lines code-with-copy"><code class="sourceCode"><span id="cb3-1">nnoremap &lt;silent&gt; &lt;leader&gt;r :call RProjectMenu()&lt;cr&gt;</span></code></pre></div></div>
<p>We should now be able to use the menu as in Figure&nbsp;1.</p>


</section>

 ]]></description>
  <category>linux</category>
  <guid>https://andrewraim.github.io/post/2026-01-09-vim-ide-II.html</guid>
  <pubDate>Fri, 09 Jan 2026 05:00:00 GMT</pubDate>
</item>
<item>
  <title>Customized Bash Prompt</title>
  <link>https://andrewraim.github.io/post/2026-01-06-bash-prompt.html</link>
  <description><![CDATA[ 




<section id="introduction" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> Introduction</h1>
<p>I have used <a href="https://www.gnu.org/software/bash">Bash</a> for the vast majority of time as a Unix / Linux user. It is always the default shell and sometimes I don’t have access to change it. Modern shells like <a href="https://www.zsh.org">Zsh</a> and <a href="https://fishshell.com">Fish</a> appear to offer better aesthetics and more information; however, we can also customize the Bash prompt. Here I will show some preferred customizations; Figure&nbsp;1 displays the results after implementing them.</p>
<div id="fig-result" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-result-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://andrewraim.github.io/downloads/2026-01-06-bash-prompt/term-result.svg" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-result-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Prompt after customizations.
</figcaption>
</figure>
</div>
<p>The complete script is <a href="../downloads/2026-01-06-bash-prompt/prompt">here</a>. To activate it in a Bash session, use the <code>source</code> command as follows.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">source</span> /path/to/prompt</span></code></pre></div></div>
<p>To apply it to new sessions, include the line above in your <code>~/.bashrc</code> file.</p>
</section>
<section id="environment-variables-for-the-prompt" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> Environment Variables for the Prompt</h1>
<p>The prompt can be set by manipulating the <code>PS1</code> and <code>PS2</code> environment variables. <code>PS1</code> is the primary prompt and <code>PS2</code> is the “continuation” prompt which is shown when a command spans more than one line. There are also <code>PS0</code>, <code>PS3</code> and <code>PS4</code> prompts used in other particular situations, discussed in the <code>bash</code> manual page.</p>
<p>The defaults for <code>PS1</code> and <code>PS2</code> are usually something like the following. There may also be codes added to make the display colorful, but we will omit them for now.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[\u@\h:\w]</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\$</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> "</span></span>
<span id="cb2-2"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&gt; "</span></span>
<span id="cb2-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[araim@localhost:~/.config/systemd/user]$</span></span></code></pre></div></div>
<p>The prompts appear as follows.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[araim@localhost:~/.config/systemd/user]$</span> echo <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hello world"</span></span>
<span id="cb3-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">hello</span> world</span>
<span id="cb3-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[araim@localhost:~/.config/systemd/user]$</span> for i in 1 2 3</span>
<span id="cb3-4"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> do</span>
<span id="cb3-5"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> echo <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hello world </span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$i</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb3-6"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> done</span>
<span id="cb3-7"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">hello</span> world 1</span>
<span id="cb3-8"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">hello</span> world 2</span>
<span id="cb3-9"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">hello</span> world 3</span>
<span id="cb3-10"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[araim@localhost:~/.config/systemd/user]$</span></span></code></pre></div></div>
</section>
<section id="placeholders-and-aesthetic-prompt-symbol" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> Placeholders and Aesthetic Prompt Symbol</h1>
<p>The strings <code>\u</code>, <code>\h</code>, and <code>\w</code> are placeholders for the username, hostname, and current working directory. See the <code>PROMPTING</code> section of the Bash manual page for the list of available placeholders.</p>
<p>The typing area can start to feel cramped if there is a lot of information in front of it, so let’s include a newline after the information. For aesthetics, let’s also replace the ascii <code>&gt;</code> with the unicode glyph <code>❱</code> “Heavy right-pointing angle bracket ornament” which has hex code <code>2771</code>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb4-1"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'\u@\h'</span></span>
<span id="cb4-2"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">':'</span></span>
<span id="cb4-3"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'\w'</span></span>
<span id="cb4-4"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'\$'</span></span>
<span id="cb4-5"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'\n'</span></span>
<span id="cb4-6"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'❱'</span></span>
<span id="cb4-7"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">' '</span></span>
<span id="cb4-8"></span>
<span id="cb4-9"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"❱ "</span></span></code></pre></div></div>
<p>This is how it looks now.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">araim@localhost:~/.config/systemd/user$</span></span>
<span id="cb5-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">❱</span> echo <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hello world"</span></span>
<span id="cb5-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">hello</span> world</span>
<span id="cb5-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">araim@localhost:~/.config/systemd/user$</span></span>
<span id="cb5-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">❱</span> for i in 1 2 3</span>
<span id="cb5-6"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">❱</span> do</span>
<span id="cb5-7"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">❱</span> echo <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hello world </span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$i</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb5-8"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">❱</span> done</span>
<span id="cb5-9"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">hello</span> world 1</span>
<span id="cb5-10"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">hello</span> world 2</span>
<span id="cb5-11"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">hello</span> world 3</span>
<span id="cb5-12"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">araim@localhost:~/.config/systemd/user$</span></span></code></pre></div></div>
<p>The <code>\h</code> element can become very long for deeply nested paths. We can limit the number of parent directories in the display using the <code>PROMPT_DIRTRIM</code> environment variable.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb6-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">araim@localhost:~/.config/systemd/user$</span></span>
<span id="cb6-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">❱</span> mkdir <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span> very/long/path</span>
<span id="cb6-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">araim@localhost:~/.config/systemd/user$</span></span>
<span id="cb6-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">❱</span> cd very/long/path</span>
<span id="cb6-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">araim@localhost:~/.config/systemd/user/very/long/path$</span></span>
<span id="cb6-6"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">❱</span> PROMPT_DIRTRIM=3</span>
<span id="cb6-7"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">araim@localhost:~/.../very/long/path$</span></span>
<span id="cb6-8"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">❱</span></span></code></pre></div></div>
</section>
<section id="custom-information-with-function-calls" class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> Custom Information with Function Calls</h1>
<p>If our current working directory is within a Git repo, it could be useful to have the repo name and current branch displayed on the prompt. Let’s define a Bash function to gather that information.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">prompt_git()</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">{</span></span>
<span id="cb7-2">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">basedir</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> rev-parse <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--show-toplevel</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> /dev/null<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">)</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">||</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span></span>
<span id="cb7-3">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">repo</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">basename</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${basedir})</span></span>
<span id="cb7-4">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">branch</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> rev-parse <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--abbrev-ref</span> HEAD<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">)</span></span>
<span id="cb7-5">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-ne</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" ➜</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${repo}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">:</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${branch}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb7-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>The arrow glyph <code>➜</code> is the unicode “Heavy round-tipped rightward arrow” character with hex code <code>279C</code>. Include a call to <code>prompt_git</code> in our primary prompt.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb8-1"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'\u@\h'</span></span>
<span id="cb8-2"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">':'</span></span>
<span id="cb8-3"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'\w'</span></span>
<span id="cb8-4"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'\$'</span></span>
<span id="cb8-5"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'$(prompt_git)'</span></span>
<span id="cb8-6"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'\n'</span></span>
<span id="cb8-7"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'❱'</span></span>
<span id="cb8-8"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">' '</span></span></code></pre></div></div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb9-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">❱</span> cd ~/.config/systemd/user</span>
<span id="cb9-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">araim@localhost:~/.config/systemd/user$</span>                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## Not a git repo</span></span>
<span id="cb9-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">❱</span> cd vimrc/</span>
<span id="cb9-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">araim@localhost:~/.../systemd/user/vimrc$</span> ➜vimrc:main  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">## A git repo</span></span>
<span id="cb9-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">❱</span></span></code></pre></div></div>
<p>Another useful piece of information would be the return code <code>$?</code> which was set by from the previous command. But let’s only display if is something other than zero.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">prompt_ecode()</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">{</span></span>
<span id="cb10-2">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">[[</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$1</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0"</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">]]</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">||</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-ne</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$1</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">] "</span></span>
<span id="cb10-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$1</span></span>
<span id="cb10-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>The function <code>prompt_ecode</code> takes one argument which is expected to be the value of <code>$?</code> set by the previous command. The caller must ensure we get this information before <code>$?</code> is reset by another operation. We return <code>$1</code> to put the variable <code>$?</code> back to the same state, perhaps for another function to use it.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>There is also a <code>PROMPT_COMMAND</code> environment variable that can be used to take some action before <code>PS1</code> is displayed. This can also be used to check the last command’s return code. However, if <code>PROMPT_COMMAND</code> is doing something to set the return code in <code>$?</code>, it may interfere with <code>prompt_ecode</code>.</p>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb11-1"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'$(prompt_ecode $?)'</span></span>
<span id="cb11-2"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'\u@\h'</span></span>
<span id="cb11-3"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">':'</span></span>
<span id="cb11-4"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'\w'</span></span>
<span id="cb11-5"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'\$'</span></span>
<span id="cb11-6"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'$(prompt_git)'</span></span>
<span id="cb11-7"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'\n'</span></span>
<span id="cb11-8"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'❱'</span></span>
<span id="cb11-9"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">' '</span></span></code></pre></div></div>
<p>Here is how it looks.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb12-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">araim@localhost:~/.config/systemd/user$</span></span>
<span id="cb12-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">❱</span> cd vimrc</span>
<span id="cb12-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">araim@localhost:~/.../systemd/user/vimrc$</span> ➜vimrc:main</span>
<span id="cb12-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">❱</span> ech <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hello world"</span></span>
<span id="cb12-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">bash:</span> ech: command not found</span>
<span id="cb12-6"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">[127]</span> araim@localhost:~/.../systemd/user/vimrc$ ➜vimrc:main</span>
<span id="cb12-7"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">araim@localhost:~/.../systemd/user/vimrc$</span> ➜vimrc:main</span>
<span id="cb12-8"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">❱</span> echo <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hello world"</span></span>
<span id="cb12-9"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">hello</span> world</span>
<span id="cb12-10"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">❱</span> </span></code></pre></div></div>
</section>
<section id="colors" class="level1" data-number="5">
<h1 data-number="5"><span class="header-section-number">5</span> Colors</h1>
<p>Let’s apply some basic colors. This will help to distinguish the prompt from other activity in the terminal. Define environment variables for three colors and insert them into the primary prompt.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb13-1"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">COL_FG</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'\033[38;5;016m'</span></span>
<span id="cb13-2"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">COL_BG</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'\033[48;5;002m'</span></span>
<span id="cb13-3"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">COL_OFF</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'\033[0m'</span></span>
<span id="cb13-4"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"\[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${COL_FG}${COL_BG}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">\]"</span></span>
<span id="cb13-5"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'$(prompt_ecode $?)'</span></span>
<span id="cb13-6"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'\u@\h'</span></span>
<span id="cb13-7"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">':'</span></span>
<span id="cb13-8"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'\w'</span></span>
<span id="cb13-9"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'\$'</span></span>
<span id="cb13-10"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'$(prompt_git)'</span></span>
<span id="cb13-11"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"\[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${COL_OFF}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">\]"</span></span>
<span id="cb13-12"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'\n'</span></span>
<span id="cb13-13"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'❱'</span></span>
<span id="cb13-14"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">' '</span></span></code></pre></div></div>
<p>Here, <code>\033[</code> is an ASCII control sequence, <code>38;5;016m</code> sets the foreground color (<code>38;5</code>) to black (<code>016</code>), and <code>48;5;002m</code> sets the background color (<code>48;5</code>) to green (<code>002</code>). The charater <code>m</code> signifies the end of the sequence. Color codes are wrapped in <code>\[ ... \]</code> to mark them as nonprintable characters: if this is omitted, the terminal will be glitchy when we are editing commands and traversing the command history. The website <a href="https://colors.sh">colors.sh</a> provides a nice interface to browse terminal colors find the corresponding codes. Figure&nbsp;2 shows the result so far.</p>
<div id="fig-colors" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-colors-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://andrewraim.github.io/downloads/2026-01-06-bash-prompt/term-colors.svg" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-colors-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Prompt with colors.
</figcaption>
</figure>
</div>
</section>
<section id="informative-colors" class="level1" data-number="6">
<h1 data-number="6"><span class="header-section-number">6</span> Informative Colors</h1>
<p>It may be useful to change the color when there is a non-zero return code from the previous command, to serve as a visual indicator. We add another function to handle the colors as follows. Again, argument <code>$1</code> should be the return code from the previous command; we return it in case we need it subsequently.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">prompt_color()</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">{</span></span>
<span id="cb14-2">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">COL_FG</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"\033[38;5;016m"</span></span>
<span id="cb14-3">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">COL_CLR_BG</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"\033[48;5;002m"</span></span>
<span id="cb14-4">    <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">COL_SET_BG</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"\033[48;5;003m"</span></span>
<span id="cb14-5"></span>
<span id="cb14-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">[</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$1</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"0"</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">]</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">;</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">then</span></span>
<span id="cb14-7">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-ne</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${COL_FG}${COL_CLR_BG}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb14-8">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span></span>
<span id="cb14-9">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">echo</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-ne</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${COL_FG}${COL_SET_BG}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb14-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">fi</span></span>
<span id="cb14-11"></span>
<span id="cb14-12">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$1</span></span>
<span id="cb14-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Now the prompt background changes to olive / yellow if a nonzero status code was set by the last command.</p>
<p>Remove the hard-coded colors from the beginning of <code>PS1</code> and call <code>prompt_color</code> instead.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb15-1"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'\[$(prompt_color $?)\]'</span></span>
<span id="cb15-2"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'$(prompt_ecode $?)'</span></span>
<span id="cb15-3"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'\u@\h'</span></span>
<span id="cb15-4"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">':'</span></span>
<span id="cb15-5"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'\w'</span></span>
<span id="cb15-6"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'\$'</span></span>
<span id="cb15-7"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'$(prompt_git)'</span></span>
<span id="cb15-8"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"\[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">${COL_OFF}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">\]"</span></span>
<span id="cb15-9"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'\n'</span></span>
<span id="cb15-10"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'❱'</span></span>
<span id="cb15-11"><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PS1</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">' '</span></span></code></pre></div></div>
<p>This should result in the prompt from Figure&nbsp;1.</p>


</section>

 ]]></description>
  <category>linux</category>
  <guid>https://andrewraim.github.io/post/2026-01-06-bash-prompt.html</guid>
  <pubDate>Tue, 06 Jan 2026 05:00:00 GMT</pubDate>
</item>
<item>
  <title>Vim as a Simple IDE without Plugins</title>
  <link>https://andrewraim.github.io/post/2026-01-03-vim-ide.html</link>
  <description><![CDATA[ 




<section id="introduction" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> Introduction</h1>
<p>There are a vast number of options for text editors, but one compelling reason to stick with the classic Vim is that it comes packaged with many Unix-based operating systems such as Linux. It is free, open source, and available almost everywhere. Despite its humble appearance, Vim is a powerful tool with extensive features and can be heavily customized.</p>
<p>This post demonstrates some configuration changes that will let us use Vim as a simple integrated development environment (IDE), without the need for any additional plugins or a graphical environment. Here, one Vim window can be used to edit and run code and a second Vim window can be running a console. While editing the code, we can use a few keystrokes to run snippets in the console. We also make it easy to launch the console when we are editing a file. A small demonstration of the result is given in Figure&nbsp;1; here we launch an R console using one of our keybindings, issue some R commands from the script using several other bindings, and then close Vim.</p>
<div id="fig-demo" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-demo-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://andrewraim.github.io/downloads/2026-01-03-vim-ide/demo.svg" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-demo-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: A quick demonstration of Vim as an IDE.
</figcaption>
</figure>
</div>
<p>A file with the all of the functions and keybindings is given <a href="../downloads/2026-01-03-vim-ide/vimrc">here</a>. The easiest way to use it is to paste the contents into your own <a href="https://vimhelp.org/usr_05.txt.html#05.1">vimrc</a> file; however, it is also possible to use a more modular configuration with multiple files.</p>
<p>This material was developed using version 9.1.1882 of Vim. The embedded terminal that we will need was introduced in version 8.1.</p>
<p>If you would prefer more sophisticated IDE capabilities than DIYing, see <a href="https://github.com/jpalardy/vim-slime">vim-slime</a>, <a href="https://github.com/R-nvim/R.nvim">R.nvim</a> for R within <a href="https://neovim.io">Neovim</a>, <a href="https://github.com/andreypopp/julia-repl-vim">julia-repl-vim</a> for Julia. There are probably also some good options for tmux if you’d like persistant terminal sessions or are not primarily focused on Vim.</p>
<div class="callout callout-style-default callout-caution callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Caution
</div>
</div>
<div class="callout-body-container callout-body">
<p>The code shown in this post is written in the Vimscript scripting language. I am not very familiar with Vimscript, but was able to get things working with some help from AI and various forum posts. There are certainly opportunities for improvement in this code.</p>
<p>Notice, for example in Figure&nbsp;1, some of the content appears out of order as it is sent to the console. This is only a visual artifact: the code actually runs correctly, but we would prefer the display to be correct as well.</p>
</div>
</div>
</section>
<section id="embedded-terminal" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> Embedded Terminal</h1>
<p>We first note that Vim has its own internal terminal. We will make use of this to run an intrepreter for our language(s) of choice. To start a terminal from Vim, enter <code>:term</code> from normal mode. This starts the terminal in a horizontal split. See the documentation on <a href="https://vimhelp.org/windows.txt.html">windows</a> on how to change focus between windows, change their positions, resize them, etc. Exiting the terminal closes the split. We can also start a terminal with a vertical split using <code>:vert term</code> from normal mode.</p>
<p>Suppose we have a vertical split with an R script on the left and a terminal running the R console on the right. We would like to be able to send text from the editor to be execute in the R console without too many keystrokes. We would also like the ability to quickly spawn that R console when we are working on an R script.</p>
<p>We will now define two functions.</p>
</section>
<section id="send-text-to-the-terminal" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> Send Text to the Terminal</h1>
<p>Our first function takes a string <code>content</code> and sends it to the terminal. There may be multiple terminals; in this simple implementation - and because I have never used more than one terminal in the same session - we will always send our content to the first terminal in the list.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode numberSource vim number-lines code-with-copy"><code class="sourceCode"><span id="cb1-1">function! SendToTerm(content)</span>
<span id="cb1-2">    let tl = term_list()</span>
<span id="cb1-3"></span>
<span id="cb1-4">    if len(tl) == 0</span>
<span id="cb1-5">        echom "No terminal buffer found"</span>
<span id="cb1-6">    else</span>
<span id="cb1-7">        call term_sendkeys(tl[0], a:content)</span>
<span id="cb1-8">    endif</span>
<span id="cb1-9">endfunction</span></code></pre></div></div>
<p>The function <code>term_list()</code> returns an array of terminal buffer numbers. If there are no terminal buffers, a notification to the user is printed with <code>echom</code>. Otherwise, the argument <code>content</code> is sent to the selected terminal using <code>term_sendkeys</code>.</p>
</section>
<section id="spawn-a-terminal-with-a-console-program" class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> Spawn a Terminal with a Console Program</h1>
<p>The second function spawns a terminal with a suitable program to handle the type of file we are currently editing. If we don’t have a specific program in mind, we will spawn the terminal and just leave it at the command line.</p>
<p>The function takes a boolean argument <code>vert</code>; the terminal will be started with a vertical split if <code>vert</code> is true, otherwise a horizontal split will be used. After the terminal is spawned, we exchange positions with the previous window. In vertical mode, this puts the script on the left and the terminal on the right. In horizontal mode, the script will be on top and the terminal will be underneath. This will also shift focus back to the script.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode numberSource vim number-lines code-with-copy"><code class="sourceCode"><span id="cb2-1">function! FileTypeToTerm(vert)</span>
<span id="cb2-2">    if &amp;filetype == 'r'</span>
<span id="cb2-3">        let cmd = "R\n"</span>
<span id="cb2-4">    elseif &amp;filetype == 'julia'</span>
<span id="cb2-5">        let cmd = "julia\n"</span>
<span id="cb2-6">    elseif &amp;filetype == 'python'</span>
<span id="cb2-7">        let cmd = "python\n"</span>
<span id="cb2-8">    else</span>
<span id="cb2-9">        let cmd = ""</span>
<span id="cb2-10">    endif</span>
<span id="cb2-11"></span>
<span id="cb2-12">    if a:vert</span>
<span id="cb2-13">        execute "vertical terminal! " . cmd</span>
<span id="cb2-14">    else</span>
<span id="cb2-15">        execute "terminal! " . cmd</span>
<span id="cb2-16">    endif</span>
<span id="cb2-17"></span>
<span id="cb2-18">    execute "normal! \&lt;c-w&gt;x"</span>
<span id="cb2-19">endfunction</span></code></pre></div></div>
<p>I have defined programs for three specific languages. Of course, you can modify this for your own cases.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>The strings <code>'r'</code>, <code>'julia'</code>, and <code>'python'</code> represent designated file types in Vim. To display the file type for the currently open buffer, use the command <code>:echo(&amp;filetype)</code> in normal mode.</p>
</div>
</div>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>When <code>cmd</code> is a non-empty command, the terminal will halt when the command exits. However, the Vim window will remain open in the halted state until we exit (e.g., via <code>:q</code>). This allows us to note any error messages from our code, in case the console crashed or the exit was otherwise not intentional.</p>
</div>
</div>
</section>
<section id="keybindings" class="level1" data-number="5">
<h1 data-number="5"><span class="header-section-number">5</span> Keybindings</h1>
<p>We will set several keybindings to interact with our functions. First, let us define the leader key, assuming it has not been defined already. A space is commonly used for this purpose.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode numberSource vim number-lines code-with-copy"><code class="sourceCode"><span id="cb3-1">let mapleader = " "</span></code></pre></div></div>
<p>The following to send content from a script to the console.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode numberSource vim number-lines code-with-copy"><code class="sourceCode"><span id="cb4-1">nnoremap &lt;silent&gt; &lt;leader&gt;&lt;leader&gt; :call SendToTerm(getline('.')."\n")&lt;cr&gt;g$</span>
<span id="cb4-2">\:call search('\S', 'W')&lt;cr&gt;:nohl&lt;cr&gt;</span>
<span id="cb4-3">xnoremap &lt;expr&gt; &lt;silent&gt; &lt;leader&gt;&lt;leader&gt; mode() ==# 'V' ?</span>
<span id="cb4-4">\ 'y :&lt;c-u&gt;call SendToTerm(@")&lt;cr&gt;`&gt;' :</span>
<span id="cb4-5">\ 'y :&lt;c-u&gt;call SendToTerm(@" . "\n")&lt;cr&gt;`&gt;'</span></code></pre></div></div>
<p>The <code>nnoremap</code> binding is used in normal mode. It sends the current line under the cursor to the console and moves cursor to the next non-whitespace character after the line. The move is accomplished by pattern-matching for <code>\S</code>, with option <code>W</code> to avoid wrapping back to the beginning when we have reached the last line of non-whitespace. The command <code>:nohl</code> is used to suppress highlighting during pattern searching.</p>
<p>The <code>xnoremap</code> binding handles two distinct cases. The first case handles <a href="https://vimhelp.org/visual.txt.html#visual-start">visual line mode</a>, where entire lines are selected at a time. Here we send the entire selection - which already includes a newline at the end - to the console, then move the cursor at the end of the selection using <code>&lt;backtick&gt;</code> then <code>&gt;</code>.</p>
<p>The second case handles regular visual mode which is character-by-character. Here the selection ends with a non-newline character, so we explicitly send a newline at the end.</p>
<p>The normal mode binding allows us to type <code>&lt;space&gt;&lt;space&gt;</code> repeatedly to step through the script and run commands line-by-line. The visual mode bindings allow us to highlight text in visual mode and type <code>&lt;space&gt;&lt;space&gt;</code> to send it to the console.</p>
<p>The next set of keybindings initializes our console: <code>&lt;space&gt;th</code> opens a console with a horizontal split and <code>&lt;space&gt;tv</code> opens a console with a vertical split.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode numberSource vim number-lines code-with-copy"><code class="sourceCode"><span id="cb5-1">nnoremap &lt;silent&gt; &lt;leader&gt;th :call FileTypeToTerm(v:false)&lt;cr&gt;</span>
<span id="cb5-2">nnoremap &lt;silent&gt; &lt;leader&gt;tv :call FileTypeToTerm(v:true)&lt;cr&gt;</span></code></pre></div></div>


</section>

 ]]></description>
  <category>linux</category>
  <guid>https://andrewraim.github.io/post/2026-01-03-vim-ide.html</guid>
  <pubDate>Sat, 03 Jan 2026 05:00:00 GMT</pubDate>
</item>
<item>
  <title>vws: Vertical Weighted Strips in R using C++</title>
  <link>https://andrewraim.github.io/post/2025-11-05-vws-R.html</link>
  <description><![CDATA[ 




<p><span class="citation" data-cites="vws-2024">Raim et al. (2024+)</span> consider a method of proposal construction for rejection sampling referred to as vertical weighted strips (VWS). Here, the target density is regarded as a weighted density; i.e., the product of a weight function and a simpler (“base”) density function. The practitioner majorizes the weight function by providing another function that bounds it from above. The majorizing function is combined with the base density to form a proposal distribution. In many cases, it is useful (and also more convenient) to partition the support of the target and use a piecewise majorizer. In this case, the proposal distribution can be regarded as a finite mixture whose components are correspond to the regions in the partition.</p>
<p>The <code>vws</code> package has been developed to support development of such proposals and their use in rejection sampling. Coding is primarily done in C++ using the provided API. Sampling functions may be then exposed in R via <a href="https://cran.r-project.org/package=Rcpp">Rcpp</a> for use in applications. A detailed guide to <code>vws</code> programming - including several worked examples - is provided in the package vignette.</p>
<p><code>vws</code> package</p>
<ul>
<li>Deployed on <a href="https://cran.r-project.org/package=vws">CRAN</a></li>
<li>Source on <a href="https://github.com/andrewraim/vws">Github</a></li>
</ul>




<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-vws-2024" class="csl-entry">
Raim, Andrew M., James A. Livsey, and Kyle M. Irimata. 2024+. <span>“Rejection Sampling with Vertical Weighted Strips.”</span> 2024+. <a href="https://arxiv.org/abs/2401.09696">https://arxiv.org/abs/2401.09696</a>.
</div>
</div></section></div> ]]></description>
  <category>article</category>
  <category>programming</category>
  <guid>https://andrewraim.github.io/post/2025-11-05-vws-R.html</guid>
  <pubDate>Wed, 05 Nov 2025 05:00:00 GMT</pubDate>
</item>
<item>
  <title>Self-Tuned VWS within Gibbs and Small Area Estimation</title>
  <link>https://andrewraim.github.io/post/2025-09-23-saevws.html</link>
  <description><![CDATA[ 




<p><span class="citation" data-cites="saevws-2025">Raim et al. (2025+)</span> consider an application of the vertical weighted strips (VWS) method <span class="citation" data-cites="vws-2024">(Raim et al. 2024+)</span> to small area estimation (SAE). SAE is used in official statistics to augment estimates from sample surveys (“direct estimates”) with a model. “Small areas” are cross-sections of a population - based on geography or other characteristics - where the sample size from the survey is small.</p>
<p>In particular, <span class="citation" data-cites="You-2021">You (2021)</span> presents an SAE model with regressions on both direct point estimates and corresponding variance estimates. A Gibbs sampler is proposed for Bayesian analysis, with one family of conditionals being an unfamiliar distribution that arises from assuming a lognormal regression in the variance model. VWS may be used to generate from this family for each small area. We find that some of the latent variances mix poorly using the independent Metropolis-Hastings step considered in <span class="citation" data-cites="You-2021">You (2021)</span>, which is a routine choice for use within Gibbs samplers. In these cases, mixing is seen to be greatly improved by taking exact draws from the conditionals with rejection sampling.</p>
<p>It is computationally burdensome to construct new VWS proposals for each small area over the course of many Gibbs sampling iterations. To address this, we consider a rule-of-thumb to adjust the proposals as the chain evolves. In particular, knots are added using rejected draws when the probability of rejection is too large. Knots are removed when the probability of rejection is sufficiently small and their corresponding regions are found to have a low contribution. The number of adjustments is seen to diminish as the chain moves to the target posterior distribution.</p>




<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-saevws-2025" class="csl-entry">
Raim, Andrew M., Kyle M. Irimata, and James A. Livsey. 2025+. <span>“Self-Tuned Rejection Sampling Within <span>G</span>ibbs and a Case Study in Small Area Estimation.”</span> 2025+. <a href="https://arxiv.org/abs/2509.17155">https://arxiv.org/abs/2509.17155</a>.
</div>
<div id="ref-vws-2024" class="csl-entry">
Raim, Andrew M., James A. Livsey, and Kyle M. Irimata. 2024+. <span>“Rejection Sampling with Vertical Weighted Strips.”</span> 2024+. <a href="https://arxiv.org/abs/2401.09696">https://arxiv.org/abs/2401.09696</a>.
</div>
<div id="ref-You-2021" class="csl-entry">
You, Yong. 2021. <span>“Small Area Estimation Using <span>F</span>ay-<span>H</span>erriot Area Level Model with Sampling Variance Smoothing and Modeling.”</span> <em>Survey Methodology</em> 47 (2). <a href="http://www.statcan.gc.ca/pub/12-001-x/2021002/article/00007-eng.htm">http://www.statcan.gc.ca/pub/12-001-x/2021002/article/00007-eng.htm</a>.
</div>
</div></section></div> ]]></description>
  <category>article</category>
  <guid>https://andrewraim.github.io/post/2025-09-23-saevws.html</guid>
  <pubDate>Tue, 23 Sep 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Numerical Maximum Likelihood with the stats4 Package</title>
  <link>https://andrewraim.github.io/post/2024-12-11-stats4.html</link>
  <description><![CDATA[ 




<section id="introduction" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> Introduction</h1>
<p>The <a href="https://stat.ethz.ch/R-manual/R-devel/library/stats4/html/00Index.html">stats4</a> package included in R provides a layer of usability on top of <code>optim</code> for numerial maximum likelihood (ML). For example, the user can request an estimate of the covariance matrix associated with the ML estimates rather than computing it manually. According to <span class="citation" data-cites="Henningsen-Toomet-2011">Henningsen and Toomet (2011)</span>, <code>stats4</code> has been bundled into R since 2003, but I only found out about it recently. In this post, we will present some brief examples using <code>stats4</code>. Note that the <a href="https://cran.r-project.org/package=maxLik">maxLik</a> package presented by <span class="citation" data-cites="Henningsen-Toomet-2011">Henningsen and Toomet (2011)</span> covers similar ground with additional functionality; therefore it may also be of interest.</p>
</section>
<section id="poisson-regression-example" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> Poisson Regression Example</h1>
<p>Consider a Poisson regression setting with <img src="https://latex.codecogs.com/png.latex?Y_i%20%5Csim%20%5Ctext%7BPoisson%7D(%5Clambda_i)">, independently distributed for <img src="https://latex.codecogs.com/png.latex?i%20=%201,%20%5Cldots,%20n"> where <img src="https://latex.codecogs.com/png.latex?%5Clambda_i%20=%20%5Cexp(x_i%5E%5Ctop%20%5Cbeta)"> for covariate <img src="https://latex.codecogs.com/png.latex?x_i%20%5Cin%20%5Cmathbb%7BR%7D%5Ed"> and unknown coefficient <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20%5Cin%20%5Cmathbb%7BR%7D%5Ed">.</p>
<p>Let us simulate a dataset from this setting.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1235</span>)</span>
<span id="cb1-2">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span></span>
<span id="cb1-3">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(n)</span>
<span id="cb1-4">X <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cbind</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, x)</span>
<span id="cb1-5">d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ncol</span>(X)</span>
<span id="cb1-6">beta_true <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>)</span>
<span id="cb1-7">lambda_true <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> beta_true)</span>
<span id="cb1-8">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rpois</span>(n, lambda_true)</span></code></pre></div></div>
</div>
<p>Of course we can use the <code>glm</code> function to obtain the MLE <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cbeta%7D"> in this setting. Let us do that as a point of comparison.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">glm_out <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glm</span>(y <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">family =</span> poisson)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(glm_out)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
glm(formula = y ~ x, family = poisson)

Coefficients:
            Estimate Std. Error z value Pr(&gt;|z|)    
(Intercept)  0.98109    0.04392  22.338  &lt; 2e-16 ***
x           -0.25893    0.03908  -6.626 3.44e-11 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for poisson family taken to be 1)

    Null deviance: 272.04  on 199  degrees of freedom
Residual deviance: 228.85  on 198  degrees of freedom
AIC: 749.28

Number of Fisher Scoring iterations: 5</code></pre>
</div>
</div>
</section>
<section id="using-the-mle-function-in-stats4" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> Using the <code>mle</code> Function in <code>stats4</code></h1>
<p>Recall that the loglikelihood for this model is <!-- --> <img src="https://latex.codecogs.com/png.latex?%0A%5Clog%20%5Cmathcal%7BL%7D(%5Cbeta)%20=%20%5Csum_%7Bi=1%7D%5En%20%5CBig%5C%7B%20y_i%20%5Clog%20%5Clambda_i%20-%20%5Clambda_i%20-%20%5Clog%20%5CGamma(y_i%20+%201)%20%5CBig%5C%7D.%0A"> <!-- --> If this were a nonstandard likelihood, we would need to be able to code and optimize it ourselves. The <code>stats4</code> package allows us to do this. Here are the arguments of the <code>stats4::mle</code> function which may be used to fit the MLE.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str</span>(mle, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">give.attr =</span> F)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>function (minuslogl, start, optim = stats::optim, method = if (!useLim) "BFGS" else "L-BFGS-B", 
    fixed = list(), nobs, lower, upper, ...)  </code></pre>
</div>
</div>
<p>Note that the first argument expects the negative loglikelihood; the optimization is then carried out as a minimization. Let us code it.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">nloglik <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">beta =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(d)) {</span>
<span id="cb6-2">    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dpois</span>(y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lambda =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> beta), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">log =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span>
<span id="cb6-3">}</span>
<span id="cb6-4"></span>
<span id="cb6-5">mle_out <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mle</span>(nloglik, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"L-BFGS-B"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nobs =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(y))</span></code></pre></div></div>
</div>
<p>A variety of included accessors can be used to work with the result.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(mle_out)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Maximum likelihood estimation

Call:
mle(minuslogl = nloglik, method = "L-BFGS-B", nobs = length(y))

Coefficients:
        Estimate Std. Error
beta1  0.9810935 0.04392105
beta2 -0.2589335 0.03907591

-2 log L: 745.2773 </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(mle_out)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>     beta1      beta2 
 0.9810935 -0.2589335 </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logLik</span>(mle_out)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>'log Lik.' -372.6387 (df=2)</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">AIC</span>(mle_out)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 749.2773</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">BIC</span>(mle_out)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 755.874</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">confint</span>(mle_out, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">level =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.90</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Profiling...</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>             5 %      95 %
beta1  0.9079239  1.052437
beta2 -0.3230551 -0.194514</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vcov</span>(mle_out)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>             beta1        beta2
beta1 0.0019290582 0.0004114616
beta2 0.0004114616 0.0015269270</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">profile</span>(mle_out)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>An object of class "profile.mle"
Slot "profile":
$beta1
            z par.vals.beta1 par.vals.beta2
1  -3.0192356      0.8453337     -0.2894697
2  -2.5258203      0.8679603     -0.2841584
3  -2.0285449      0.8905870     -0.2789367
4  -1.5273678      0.9132136     -0.2738039
5  -1.0222468      0.9358402     -0.2687596
6  -0.5131396      0.9584668     -0.2638030
7   0.0000000      0.9810935     -0.2589335
8   0.5172066      1.0037201     -0.2541504
9   1.0385329      1.0263467     -0.2494528
10  1.5640206      1.0489733     -0.2448400
11  2.0937147      1.0716000     -0.2403111
12  2.6276606      1.0942266     -0.2358653

$beta2
            z par.vals.beta1 par.vals.beta2
1  -2.5855783      0.9478930     -0.3595864
2  -2.0668516      0.9555116     -0.3394558
3  -1.5489482      0.9626386     -0.3193253
4  -1.0318503      0.9692764     -0.2991947
5  -0.5155395      0.9754273     -0.2790641
6   0.0000000      0.9810935     -0.2589335
7   0.5147983      0.9862771     -0.2388029
8   1.0288654      0.9909803     -0.2186724
9   1.5422257      0.9952050     -0.1985418
10  2.0549007      0.9989531     -0.1784112
11  2.5669123      1.0022264     -0.1582806
12  3.0782832      1.0050269     -0.1381501


Slot "summary":
Maximum likelihood estimation

Call:
mle(minuslogl = nloglik, method = "L-BFGS-B", nobs = length(y))

Coefficients:
        Estimate Std. Error
beta1  0.9810935 0.04392105
beta2 -0.2589335 0.03907591

-2 log L: 745.2773 </code></pre>
</div>
</div>
</section>
<section id="fixed-parameters" class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> Fixed Parameters</h1>
<p>An argument of <code>mle</code> that can be useful is <code>fixed</code>, where we can specify some parameters to be held fixed during the optimization. In our example with <img src="https://latex.codecogs.com/png.latex?%5Cbeta%20=%20(%5Cbeta_1,%20%5Cbeta_2)"> let us consider another fit with the slope coefficient fixed at <img src="https://latex.codecogs.com/png.latex?%5Cbeta_2%20=%200"> .</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1">mle0_out <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mle</span>(nloglik, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"L-BFGS-B"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nobs =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(y),</span>
<span id="cb24-2">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fixed =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">beta =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)))</span>
<span id="cb24-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(mle0_out)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
mle(minuslogl = nloglik, method = "L-BFGS-B", fixed = list(beta = c(NA, 
    0)), nobs = length(y))

Coefficients:
   beta1    beta2 
1.011601 0.000000 </code></pre>
</div>
</div>
<p>Note that <code>NA</code> was associated with <img src="https://latex.codecogs.com/png.latex?%5Cbeta_1"> to indicate that it should not remain fixed. We can compute a likelihood ratio test of the hypothesis <img src="https://latex.codecogs.com/png.latex?H_0:%20%5Cbeta_2%20=%200"> versus <img src="https://latex.codecogs.com/png.latex?H_1:%20%5Cbeta_2%20%5Cneq%200"> using the two fits <code>mle_out</code> and <code>mle0_out</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1">lrt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logLik</span>(mle_out) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">logLik</span>(mle0_out))</span>
<span id="cb26-2">pval <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pchisq</span>(lrt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lower.tail =</span> F)</span>
<span id="cb26-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"LRT was computed with test statistic"</span>, lrt, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"and p-value"</span>, pval, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>LRT was computed with test statistic 43.19441 and p-value 4.956214e-11 .</code></pre>
</div>
</div>
</section>
<section id="transforming-parameters" class="level1" data-number="5">
<h1 data-number="5"><span class="header-section-number">5</span> Transforming Parameters</h1>
<p>A convenient property of ML estimates (MLEs) is the invariance property: suppose the MLE of <img src="https://latex.codecogs.com/png.latex?%5Cbeta"> is <img src="https://latex.codecogs.com/png.latex?%5Chat%7B%5Cbeta%7D">, then the MLE of a function <img src="https://latex.codecogs.com/png.latex?g(%5Cbeta)"> is <img src="https://latex.codecogs.com/png.latex?g(%5Chat%7B%5Cbeta%7D)">. An associated variance estimate is given by <!-- --> <img src="https://latex.codecogs.com/png.latex?%0A%5Cwidehat%7B%5Ctext%7BVar%7D%7D(g(%5Chat%7B%5Cbeta%7D))%20=%0A%5BJ(%5Chat%7B%5Cbeta%7D)%5D%0A%5Cwidehat%7B%5Ctext%7BVar%7D%7D(%5Chat%7B%5Cbeta%7D)%0A%5BJ(%5Chat%7B%5Cbeta%7D)%5D%5E%5Ctop%0A"> <!-- --> where <img src="https://latex.codecogs.com/png.latex?J(%5Cbeta)%20=%20%5Cpartial%20g(%5Cbeta)%20/%20%5Cpartial%20%5Cbeta"> is the Jacobian of the transformation. Here is an R function to compute the transformed variance. Note that we use the <code>numDeriv</code> package.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1">vcov_tx <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(object, tx) {</span>
<span id="cb28-2">    J <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> numDeriv<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">jacobian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">func =</span> tx, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(object))</span>
<span id="cb28-3">    V <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vcov</span>(object)</span>
<span id="cb28-4">    J <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tcrossprod</span>(V, J)</span>
<span id="cb28-5">}</span></code></pre></div></div>
</div>
<p>As an example, let us estimate each <img src="https://latex.codecogs.com/png.latex?%5Clambda_i%20=%20%5Cexp(x_i%5E%5Ctop%20%5Cbeta)"> and its variance, and construct an associated confidence interval with level 90% nominal coverage level.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1">alpha <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.10</span></span>
<span id="cb29-2">tx <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(beta) { <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exp</span>(X <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%*%</span> beta) }</span>
<span id="cb29-3">tx_hat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tx</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coef</span>(mle_out))</span>
<span id="cb29-4">sd_tx_hat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">diag</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vcov_tx</span>(mle_out, tx)))</span>
<span id="cb29-5">tx_lo <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> tx_hat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> alpha<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> sd_tx_hat</span>
<span id="cb29-6">tx_hi <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> tx_hat <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">qnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> alpha<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> sd_tx_hat</span></code></pre></div></div>
</div>
<p>Here are the results in a data frame.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb30-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(kableExtra)</span>
<span id="cb30-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">EST =</span> tx_hat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">SD =</span> sd_tx_hat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">LO =</span> tx_lo, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">HI =</span> tx_hi) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb30-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TRUTH =</span> lambda_true) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb30-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb30-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kbl</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">booktabs =</span> T)</span></code></pre></div></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: right;" data-quarto-table-cell-role="th">EST</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">SD</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">LO</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">HI</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">TRUTH</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">3.195763</td>
<td style="text-align: right;">0.1463983</td>
<td style="text-align: right;">2.954960</td>
<td style="text-align: right;">3.436567</td>
<td style="text-align: right;">3.236515</td>
</tr>
<tr class="even">
<td style="text-align: right;">3.720229</td>
<td style="text-align: right;">0.2166837</td>
<td style="text-align: right;">3.363816</td>
<td style="text-align: right;">4.076642</td>
<td style="text-align: right;">3.747967</td>
</tr>
<tr class="odd">
<td style="text-align: right;">2.064235</td>
<td style="text-align: right;">0.1344154</td>
<td style="text-align: right;">1.843142</td>
<td style="text-align: right;">2.285329</td>
<td style="text-align: right;">2.122321</td>
</tr>
<tr class="even">
<td style="text-align: right;">2.591277</td>
<td style="text-align: right;">0.1170420</td>
<td style="text-align: right;">2.398760</td>
<td style="text-align: right;">2.783794</td>
<td style="text-align: right;">2.643374</td>
</tr>
<tr class="odd">
<td style="text-align: right;">2.589646</td>
<td style="text-align: right;">0.1170497</td>
<td style="text-align: right;">2.397117</td>
<td style="text-align: right;">2.782176</td>
<td style="text-align: right;">2.641767</td>
</tr>
<tr class="even">
<td style="text-align: right;">1.718365</td>
<td style="text-align: right;">0.1510794</td>
<td style="text-align: right;">1.469862</td>
<td style="text-align: right;">1.966869</td>
<td style="text-align: right;">1.777932</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
<section id="references" class="level1" data-number="6">




</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">6 References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-Henningsen-Toomet-2011" class="csl-entry">
Henningsen, Arne, and Ott Toomet. 2011. <span>“maxLik: A Package for Maximum Likelihood Estimation in <span>R</span>.”</span> <em>Computational Statistics</em> 26 (3): 443–58. <a href="https://doi.org/10.1007/s00180-010-0217-1">https://doi.org/10.1007/s00180-010-0217-1</a>.
</div>
</div></section></div> ]]></description>
  <category>programming</category>
  <guid>https://andrewraim.github.io/post/2024-12-11-stats4.html</guid>
  <pubDate>Wed, 11 Dec 2024 05:00:00 GMT</pubDate>
</item>
<item>
  <title>fntl: Numerical Tools for Rcpp and Lambda Functions</title>
  <link>https://andrewraim.github.io/post/2024-07-16-fntl.html</link>
  <description><![CDATA[ 




<p><a href="https://en.cppreference.com/w/cpp/language/lambda">Lambda functions</a> were added to C++ in the C++11 specification. They are functions which can be defined on the fly in the course of a program, can make use of variables in the environment within their body, and be passed as arguments to other functions. Use of lambdas may be especially appealing for Rcpp programmers who are accustomed to working with function objects in R but occasionally need C++ when performance becomes a concern.</p>
<p>The objective of the R package <code>fntl</code> <span class="citation" data-cites="fntl">(Raim 2024a)</span> is to facilitate programming in Rcpp with lambda functions by providing an API to routinely needed numerical tools such as integration, root-finding, and optimization. Such functions require one or more functions as a primary argument; these are supplied as lambdas to <code>fntl</code>. Where possible, the <code>fntl</code> API utilizes methods exposed from the <a href="https://cran.r-project.org/doc/manuals/r-release/R-exts.html#The-R-API">R API</a> so that the same numerical methods are used as in R. In cases where methods are not exposed from the R API, <code>fntl</code> implements methods that are intended to be comparable. A detailed guide to the <code>fntl</code> API is provided in the package vignette <span class="citation" data-cites="fntl-2024">(Raim 2024b)</span>.</p>
<p><code>fntl</code> package</p>
<ul>
<li>Deployed on <a href="https://cran.r-project.org/package=fntl">CRAN</a></li>
<li>Source on <a href="https://github.com/andrewraim/fntl">Github</a></li>
</ul>




<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-fntl" class="csl-entry">
Raim, Andrew M. 2024a. <em><span class="nocase">fntl</span>: Numerical Tools for <span>Rcpp</span> and Lambda Functions</em>. <a href="https://github.com/andrewraim/fntl">https://github.com/andrewraim/fntl</a>.
</div>
<div id="ref-fntl-2024" class="csl-entry">
Raim, Andrew M. 2024b. <em><span class="nocase">fntl</span>: Numerical Tools for <span>Rcpp</span> and Lambda Functions</em>. Research Report Series: Computing #2024-01. Center for Statistical Research; Methodology, U.S.&nbsp;Census Bureau. <a href="https://www.census.gov/library/working-papers/2024/adrm/RRC2024-01.html">https://www.census.gov/library/working-papers/2024/adrm/RRC2024-01.html</a>.
</div>
</div></section></div> ]]></description>
  <category>article</category>
  <category>programming</category>
  <guid>https://andrewraim.github.io/post/2024-07-16-fntl.html</guid>
  <pubDate>Tue, 16 Jul 2024 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Rejection Sampling with Vertical Weighted Strips</title>
  <link>https://andrewraim.github.io/post/2024-01-19-vws.html</link>
  <description><![CDATA[ 




<p>Rejection sampling is a classical algorithm <span class="citation" data-cites="vonNeumann-1951">(Neumann 1951)</span> to generate variates from a target distribution where a constructive sampling method—e.g., by composing other random variables—may not be apparent. A benefit of rejection sampling is that accepted draws follow the target distribution exactly. However, coming up with a good proposal for rejection sampling can take some creativity. A poor choice can result in extremely low acceptance rates where practically no draws are accepted from a very large number of candidates.</p>
<p><span class="citation" data-cites="vws-2024">Raim et al. (2024+)</span> explore an approach to construct proposals by regarding the target as a weighted density and majorizing the weight function. This yields another weighted density whose unnormalized form can be used as an envelope in the acceptance ratio. If designed consciously, the normalized form can also be used to draw candidate variates to complete the rejection sampler. An improved proposal can be obtained by partitioning the support and majorizing within each region. Here the proposal is a finite mixture. The method is referred to as “vertical weighted strips” because it can be regarded as an extension of the vertical strips method <span class="citation" data-cites="Devroye-1986 Martino-Luengo-Miguez-2018">(Devroye 1986, VIII; Martino et al. 2018, sec. 3.6)</span>, with the weighted form introducing an additional degree of flexibility for algorithm development.</p>




<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-Devroye-1986" class="csl-entry">
Devroye, Luc. 1986. <em>Non-Uniform Random Variate Generation</em>. Springer.
</div>
<div id="ref-Martino-Luengo-Miguez-2018" class="csl-entry">
Martino, Luca, David Luengo, and Joaquín Míguez. 2018. <em>Independent Random Sampling Methods</em>. Springer. https://doi.org/<a href="https://dx.doi.org/10.1007/978-3-319-72634-2">https://dx.doi.org/10.1007/978-3-319-72634-2</a>.
</div>
<div id="ref-vonNeumann-1951" class="csl-entry">
Neumann, John von. 1951. <span>“Various Techniques in Connection with Random Digits.”</span> In <em>Monte Carlo Methods</em>, edited by A. S. Householder, G. E. Forsythe, and H. H. Germond. National Bureau of Standards Applied Mathematics Series. U.S. Government Printing Office, Washington, DC.
</div>
<div id="ref-vws-2024" class="csl-entry">
Raim, Andrew M., James A. Livsey, and Kyle M. Irimata. 2024+. <span>“Rejection Sampling with Vertical Weighted Strips.”</span> 2024+. <a href="https://arxiv.org/abs/2401.09696">https://arxiv.org/abs/2401.09696</a>.
</div>
</div></section></div> ]]></description>
  <category>article</category>
  <guid>https://andrewraim.github.io/post/2024-01-19-vws.html</guid>
  <pubDate>Fri, 19 Jan 2024 05:00:00 GMT</pubDate>
</item>
<item>
  <title>Sending Mail from R</title>
  <link>https://andrewraim.github.io/post/2023-05-01-sendmail-R.html</link>
  <description><![CDATA[ 




<p>Here is a quick note on sending mail from R. For example, this could be useful in a long running job to alert yourself when it completes or halts because of an error. The <a href="https://cran.r-project.org/package=sendmailR">sendmailR</a> package provides a general interface to mail.</p>
<p>Here is an example where we use sendmailR to send mail to the local mailbox of our user (araim) on the host machine. This assumes we have a sendmail server set up to relay local mail and that araim has a mailbox.</p>
<p>The following call in R sends a message.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">result <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sendmail</span>(</span>
<span id="cb1-2">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sendmailR"</span>,</span>
<span id="cb1-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"araim"</span>,</span>
<span id="cb1-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subject =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hello from R"</span>,</span>
<span id="cb1-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">msg =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"This is an email from R using the sendmailR package."</span>,</span>
<span id="cb1-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">control =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">smtpServer =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"localhost"</span>)</span>
<span id="cb1-7">)</span></code></pre></div></div>
</div>
<p>Here is how the message appears when viewed with <code>mutt</code>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb2-1">i:Exit  -:PrevPg  &lt;Space&gt;:NextPg  v:View Attachm.  d:Del  r:Reply  j:Next  ?:Help</span>
<span id="cb2-2">Date: Tue, 02 May 2023 09:24:31 -0000</span>
<span id="cb2-3">From: sendmailR@localhost</span>
<span id="cb2-4">To: araim@localhost</span>
<span id="cb2-5">Subject: Hello from R</span>
<span id="cb2-6"></span>
<span id="cb2-7">[-- Attachment #1 --]</span>
<span id="cb2-8">[-- Type: text/plain; charset=us-ascii, Encoding: 7bit, Size: 0.1K --]</span>
<span id="cb2-9"></span>
<span id="cb2-10">This is an email from R using the sendmailR package.</span>
<span id="cb2-11"></span>
<span id="cb2-12">-N +- 1155/1155: sendmailR@localhost   Hello from R                      -- (all)</span></code></pre></div></div>
<p>Here is an example of sending mail on an exception using <code>withCallingHandlers</code>. Here we use <code>sys.calls</code> to give some context about the call that led to the error.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">f <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) { <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"An exception!"</span>) }</span>
<span id="cb3-2">g <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) { <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">f</span>(x) }</span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">withCallingHandlers</span>({</span>
<span id="cb3-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">g</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb3-6">}, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) {</span>
<span id="cb3-7">    result <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sendmail</span>(</span>
<span id="cb3-8">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sendmailR"</span>,</span>
<span id="cb3-9">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"araim"</span>,</span>
<span id="cb3-10">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subject =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Exception from R"</span>,</span>
<span id="cb3-11">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">msg =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%d: %s"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sys.calls</span>()), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sys.calls</span>()),</span>
<span id="cb3-12">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">control =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">smtpServer =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"localhost"</span>)</span>
<span id="cb3-13">    )</span>
<span id="cb3-14">})</span></code></pre></div></div>
</div>
<p>And here is how the message appears when viewed with <code>mutt</code>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode default code-with-copy"><code class="sourceCode default"><span id="cb4-1">i:Exit  -:PrevPg  &lt;Space&gt;:NextPg  v:View Attachm.  d:Del  r:Reply  j:Next  ?:Help</span>
<span id="cb4-2">Date: Tue, 02 May 2023 10:06:56 -0000</span>
<span id="cb4-3">From: sendmailR@localhost</span>
<span id="cb4-4">To: araim@localhost</span>
<span id="cb4-5">Subject: Exception from R</span>
<span id="cb4-6"></span>
<span id="cb4-7">[-- Attachment #1 --]</span>
<span id="cb4-8">[-- Type: text/plain; charset=us-ascii, Encoding: 7bit, Size: 0.6K --]</span>
<span id="cb4-9"></span>
<span id="cb4-10">1: withCallingHandlers({</span>
<span id="cb4-11">   g(1:10)</span>
<span id="cb4-12">}, error = function(e) {</span>
<span id="cb4-13">   result = sendmail(from = "sendmailR", to = "araim", subject = "Exception from</span>
<span id="cb4-14">+R", msg = sprintf("%d: %s", seq_along(sys.calls()), sys.calls()), control =</span>
<span id="cb4-15">+list(smtpServer = "localhost"))</span>
<span id="cb4-16">})</span>
<span id="cb4-17">2: g(1:10)</span>
<span id="cb4-18">3: f(x)</span>
<span id="cb4-19">4: stop("An exception!")</span>
<span id="cb4-20">5: .handleSimpleError(function (e) {</span>
<span id="cb4-21">   result = sendmail(from = "sendmailR", to = "araim", subject = "Exception from</span>
<span id="cb4-22">+R", msg = sprintf("%d: %s", seq_along(sys.calls()), sys.calls()), control =</span>
<span id="cb4-23">+list(smtpServer = "localhost"))</span>
<span id="cb4-24">}, "An exception!", base::quote(f(x)))</span>
<span id="cb4-25">6: h(simpleError(msg, call))</span>
<span id="cb4-26"></span>
<span id="cb4-27">-N +- 1162/1162: sendmailR@localhost   Exception from R                  -- (all)</span></code></pre></div></div>



 ]]></description>
  <category>programming</category>
  <guid>https://andrewraim.github.io/post/2023-05-01-sendmail-R.html</guid>
  <pubDate>Mon, 01 May 2023 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Comparison of Usability Preferences with Bivariate Ordinal Regression</title>
  <link>https://andrewraim.github.io/post/2023-04-20-bivariate-ordinal-maps.html</link>
  <description><![CDATA[ 




<p>Suppose <img src="https://latex.codecogs.com/png.latex?n"> respondents are asked to rate several alternatives on a scale from, say, 1 to 5, with 1 being least preferred and 5 being most preferred. How can we determine whether one alternative is significantly more preferred to the others? The data may include covariates which are associated with the preference of interest.</p>
<p>Elizabeth Nichols and I carry out this kind of analysis on map usability in a recent report <span class="citation" data-cites="bivariate-ordinal-maps-2023">(Raim and Nichols 2023)</span>. With ordinal data recorded in a survey setting, we compare preferences for satellite maps - which display details such as landmarks and terrain - to those of simplified road maps. We make use of a bivariate ordinal regression model to carry out the analysis. Computations are carried out using the <a href="https://cran.r-project.org/package=mvord">mvord</a> R package by <span class="citation" data-cites="Hirk-Hornik-Vana-2020">Hirk et al. (2020)</span>, which can also support multivariate models with more than two alternatives.</p>




<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-bibliography"><h2 class="anchored quarto-appendix-heading">References</h2><div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-Hirk-Hornik-Vana-2020" class="csl-entry">
Hirk, Rainer, Kurt Hornik, and Laura Vana. 2020. <span>“<span class="nocase">mvord</span>: An <span>R</span> Package for Fitting Multivariate Ordinal Regression Models.”</span> <em>Journal of Statistical Software</em> 93 (4): 1–41. <a href="https://doi.org/10.18637/jss.v093.i04">https://doi.org/10.18637/jss.v093.i04</a>.
</div>
<div id="ref-bivariate-ordinal-maps-2023" class="csl-entry">
Raim, Andrew M., and Elizabeth Nichols. 2023. <em>A Comparison of Map Usability via Bivariate Ordinal Analysis</em>. Study Series: Statistics #2023-01. Center for Statistical Research; Methodology, U.S.&nbsp;Census Bureau. <a href="https://www.census.gov/library/working-papers/2023/adrm/SSS2023-01.html">https://www.census.gov/library/working-papers/2023/adrm/SSS2023-01.html</a>.
</div>
</div></section></div> ]]></description>
  <category>article</category>
  <guid>https://andrewraim.github.io/post/2023-04-20-bivariate-ordinal-maps.html</guid>
  <pubDate>Thu, 20 Apr 2023 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Interactive Map with Shiny and Leaflet</title>
  <link>https://andrewraim.github.io/post/2023-02-27-shiny-leaflet.html</link>
  <description><![CDATA[ 




<p>There are powerful tools for working with geographical data in R. For example, the <a href="https://cran.r-project.org/package=ggplot2">ggplot</a> and <a href="https://cran.r-project.org/package=sf">sf</a> packages can be used together to display such data. But what about interactive maps which support panning, zooming, and clicks to trigger actions (e.g., highlighting an area)?</p>
<p>One way to accomplish this is through <a href="https://shiny.rstudio.com">Shiny</a> and <a href="https://rstudio.github.io/leaflet">Leaflet</a>. With this combination of tools, it is relatively easy to stand up an graphical interface. Shiny supports constructs beyond routine R programming which allow variables, displays, and computations to react to user inputs. Leaflet provides widgets for interactive maps.</p>
<p>There are already a number of interesting examples on the web, but <a href="https://github.com/andrewraim/shiny-leaflet-example">here</a> is something I put together.</p>
<p><a href="../image/shiny-leaflet-example.png"><img src="https://andrewraim.github.io/image/shiny-leaflet-example-thumb.png" class="img-fluid"></a></p>
<p>It displays counties in Maryland along with some randomly placed markers, and displays the count of markers within each county as a choropleth. Users can remove existing markers or add new ones, which in turn triggers the counts to be recomputed and the choropleth to be updated.</p>



 ]]></description>
  <category>programming</category>
  <guid>https://andrewraim.github.io/post/2023-02-27-shiny-leaflet.html</guid>
  <pubDate>Mon, 27 Feb 2023 05:00:00 GMT</pubDate>
</item>
<item>
  <title>Worker Tool for Computational Studies</title>
  <link>https://andrewraim.github.io/post/2023-02-12-worker.html</link>
  <description><![CDATA[ 




<p>Large statistical simulations and other computational studies often consist of a collection of independent tasks to be processed. For example, to study properties of an estimator, we may wish to vary the parameters of an assumed data-generating mechanism along with the sample size. The estimator requires substantial amount of computation because it makes use of a Markov chain Monte Carlo (MCMC) procedure. We generate 500 datasets in each setting, evaluate the estimator, and save the results to be summarized and later. From a parallel computing perspective, such tasks are described as <a href="https://en.wikipedia.org/wiki/Embarrassingly_parallel">embarrassingly parallel</a> because they do not need to interact while they run.</p>
<p>There are usually many more tasks than available processors. Suppose we are limited to the use of <img src="https://latex.codecogs.com/png.latex?k"> processors; we would like to keep these <img src="https://latex.codecogs.com/png.latex?k"> processors busy with our workload without using additional processors. Ideally, we would like to do this without the need for manual intervention. <a href="https://github.com/andrewraim/worker">Worker</a> is a lightweight tool to help automate this process.</p>
<p>Suppose our study consists of a one parameter with 1000 levels. Suppose that we have set up 1000 corresponding directories, named as follows.</p>
<pre><code>$ ls -1
run000
run001
...
run999</code></pre>
<p>In each folder, suppose there is a script <code>launch.R</code> that runs the corresponding level of the simulation. An output file <code>output.csv</code> is placed in the directory upon successful completion of <code>launch.R</code>.</p>
<pre><code>$ ls run000
launch.R    output.csv</code></pre>
<p>We would like to run <img src="https://latex.codecogs.com/png.latex?k"> of the <code>launch.R</code> scripts at a time until the collection of 1000 tasks is complete. The worker script attempts to automate this in a way that is agnostic of the underlying simulation. It loops through tasks and runs them sequentially, but multiple workers can be run simultaneously on the same study to achieve “embarrassingly parallel” parallel computing. Workers make use of lock files to avoid stepping on each other’s toes: each task is taken up by only one worker. This paradigm can be useful in shared computing environments where users may be asked to limit the number of processors requested at any given time. We can start, say, ten workers to ensure ten simultaneous tasks rather than having to continually monitor a scheduler’s queue and submit more tasks.</p>
<p>Here is an example of the syntax used to invoke each worker in this example.</p>
<pre><code>$ worker.sh -p 'run???' -c 'Rscript launch.R'</code></pre>
<p>The worker looks for folders whose names match pattern <code>run???</code>. When such a folder is found, it enters the folder and first checks to see if the task has previously been claimed by a worker (including itself in a previous iteration). If not, it claims the task and launches it via the specified command <code>Rscript launch.R</code>. The task is run in the worker’s foreground until completion. The worker terminates when it is no longer actively engaged and there are no unclaimed jobs remaining.</p>
<p>After all of the runs are completed, we will likely want to load each of the <code>output.csv</code> files and construct tables and plots to summarize the results. This <a href="../post/2023-02-11-latex-tables-from-R.html">post</a> may be helpful if you are looking to construct Latex tables from a computational study.</p>



 ]]></description>
  <category>programming</category>
  <guid>https://andrewraim.github.io/post/2023-02-12-worker.html</guid>
  <pubDate>Sun, 12 Feb 2023 05:00:00 GMT</pubDate>
</item>
<item>
  <title>Generating Latex Tables in R</title>
  <link>https://andrewraim.github.io/post/2023-02-11-latex-tables-from-R.html</link>
  <description><![CDATA[ 




<section id="introduction" class="level1">
<h1>Introduction</h1>
<p>Preparing Latex tables from results of a simulation study or other program can be tedious and time-consuming when done by hand. Especially when the process must be repeated many tables as results are updated to correct mistakes or to investigate unexpected findings. Hours spent manually adjusting formatting of table entries and typing ampersands between them could be spent in better ways. Fortunately, there are some excellent tools in R to help. Taking time to learn the tools and automate your table generation may be worth the investment. We will give a brief example in this post. This is only based on my experience; there are likely even better tools and methods that I have yet to learn.</p>
</section>
<section id="objective" class="level1">
<h1>Objective</h1>
<p>Our objective will be to generate a Latex table for the first six rows of the <code>airquality</code> dataset.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(airquality, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)</span>
<span id="cb1-2">  Ozone Solar.R Wind Temp Month Day</span>
<span id="cb1-3"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">41</span>     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">190</span>  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.4</span>   <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">67</span>     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>   <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb1-4"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">36</span>     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">118</span>  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8.0</span>   <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">72</span>     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>   <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb1-5"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">149</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">12.6</span>   <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">74</span>     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>   <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span></span>
<span id="cb1-6"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">313</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">11.5</span>   <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">62</span>     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>   <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span></span>
<span id="cb1-7"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>    <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>      <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">14.3</span>   <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">56</span>     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>   <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span></span>
<span id="cb1-8"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">28</span>      <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">14.9</span>   <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">66</span>     <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>   <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span></span></code></pre></div></div>
</div>
<p>We would also like to meet the following criteria.</p>
<ol type="1">
<li>Criteria for formatting of the rendered Latex table.
<ol type="a">
<li>Use <a href="https://ctan.org/pkg/booktabs">booktabs</a> and make all separators horizontal lines.</li>
<li>Use multiline columns to render column names.</li>
<li>Add several headers which group together columns in the display.</li>
<li>Specify a caption and a label which can be used in Latex references.</li>
<li>Request the table to be placed “here”.</li>
</ol></li>
<li>Criteria for table content.
<ol type="a">
<li>Include row labels (observation number) in the table as a column.</li>
<li>Format numbers in a purposeful way.</li>
<li>Display the <code>Month</code> and <code>Day</code> columns together as a date.</li>
<li>Customize column names to be more descriptive.</li>
</ol></li>
<li>Ensure that the generated Latex code is legible.
<ol type="a">
<li>Pad strings in a given column so that they are nicely aligned in the code.</li>
</ol></li>
</ol>
<p>To do this, we will make use of the following tools.</p>
<ol type="1">
<li>The <a href="https://www.tidyverse.org">Tidyverse</a> framework to manipulate tables.</li>
<li>The <a href="https://yihui.org/knitr/">knitr</a> package for reproducible research in R. In particular, we will use the <a href="https://bookdown.org/yihui/rmarkdown-cookbook/kable.html">kable</a> function. to generate Latex from data frames.</li>
<li>The <a href="https://cran.r-project.org/package=kableExtra">kableExtra</a> package to handle some additional Latex work.</li>
<li>The built-in <code>sprintf</code> function to format table entries.</li>
</ol>
</section>
<section id="preparing-the-data-frame" class="level1">
<h1>Preparing the Data Frame</h1>
<p>First let us load the packages we will use.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(knitr)</span>
<span id="cb2-2">R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb2-3">R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tibble)</span>
<span id="cb2-4">R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(stringr)</span>
<span id="cb2-5">R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(kableExtra)</span></code></pre></div></div>
</div>
<p>Before converting to Latex, let us format the entries and column names as we would like them to appear.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">tbl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(airquality, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames_to_column</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">var =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Num"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Date =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%04d-%02d-%02d"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1973</span>, Month, Day)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Solar.R =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%0.2e"</span>, Solar.R)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TempC =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%0.2f"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (Temp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(Num, Date, Ozone, Solar.R, Wind, TempC)</span></code></pre></div></div>
</div>
<p>We have done the following.</p>
<ol type="1">
<li>Assemble <code>Month</code> and <code>Day</code> into <code>Date</code>, where 1973 is the year which all observations were taken (according to the manual page for the dataset).</li>
<li>Convert <code>Solar.R</code> to a string with the original value in scientific notation.</li>
<li>Convert temperature <code>Temp</code> from Farenheit to Celcius and call the result <code>TempC</code>.</li>
<li>Use <code>tibble::rownames_to_column</code> to include row labels in the table as the <code>Obs</code> column.</li>
</ol>
<p>This produces the following table.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(tbl)</span>
<span id="cb4-2">  Num       Date Ozone  Solar.R Wind TempC</span>
<span id="cb4-3"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>   <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1973-05-01</span>    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">41</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.90e+02</span>  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.4</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">19.44</span></span>
<span id="cb4-4"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>   <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1973-05-02</span>    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">36</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.18e+02</span>  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">8.0</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">22.22</span></span>
<span id="cb4-5"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>   <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1973-05-03</span>    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.49e+02</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">12.6</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">23.33</span></span>
<span id="cb4-6"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>   <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1973-05-04</span>    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.13e+02</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">11.5</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">16.67</span></span>
<span id="cb4-7"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>   <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1973-05-05</span>    <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>       <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">14.3</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">13.33</span></span>
<span id="cb4-8"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>   <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1973-05-06</span>    <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">28</span>       <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">14.9</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">18.89</span></span></code></pre></div></div>
</div>
</section>
<section id="generating-latex" class="level1">
<h1>Generating Latex</h1>
<p>We can now generate code to display out formatted table as Latex code.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">out <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Solar.R =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_pad</span>(Solar.R, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">side =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pad =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"\u00A0"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Wind =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_pad</span>(Wind, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">side =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pad =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"\u00A0"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"latex"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">booktabs =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linesep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rlrrrr"</span>),</span>
<span id="cb5-5">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"My formatted airquality table."</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"airquality"</span>,</span>
<span id="cb5-6">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col.names =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Number"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Date"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ozone (PPB)"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Radiation (Ly)"</span>,</span>
<span id="cb5-7">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wind (MPH)"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Temp (C)"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable_styling</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">latex_options =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hold_position"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_header_above</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Observation"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Solar"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Weather"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span></code></pre></div></div>
</div>
<p>We have done the following.</p>
<ol type="1">
<li><p>Left-pad the <code>Solar.R</code> and <code>Wind</code> fields with the unicode “nbsp” character, which will render as a space in our resulting code rather than be ignored, as a regular space would.</p></li>
<li><p>Use <code>kable</code> to generate a Latex table from our data frame. We have specified options such as cell alignments, the caption, and the label, which should be familiar to Latex users. We have also specified descriptive column names here.</p></li>
<li><p>The option <code>hold_position</code> specifies the option <code>!h</code> for how Latex should place the table.</p></li>
<li><p>The function <code>add_header_above</code> specifies a layer of headers above the column names. These respectively have text “Observation”, “Solar” and “Weather”, and are each two columns wide.</p></li>
</ol>
<p>Printing the result yields the following Latex code.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(out)</span>
<span id="cb6-2">\begin{table}[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>h]</span>
<span id="cb6-3">\centering</span>
<span id="cb6-4">\caption{\label{tab<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>airquality}My formatted airquality table.}</span>
<span id="cb6-5">\centering</span>
<span id="cb6-6">\begin{tabular}[t]{rlrrrr}</span>
<span id="cb6-7">\toprule</span>
<span id="cb6-8">\multicolumn{<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>}{c}{Observation} <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> \multicolumn{<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>}{c}{Solar} <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> \multicolumn{<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>}{c}{Weather} \\</span>
<span id="cb6-9">\<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cmidrule</span>(l{<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>pt}r{<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>pt}){<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1-2</span>} \<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cmidrule</span>(l{<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>pt}r{<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>pt}){<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3-4</span>} \<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cmidrule</span>(l{<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>pt}r{<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>pt}){<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5-6</span>}</span>
<span id="cb6-10">Number <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> Date <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Ozone</span> (PPB) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Radiation</span> (Ly) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Wind</span> (MPH) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Temp</span> (C)\\</span>
<span id="cb6-11">\midrule</span>
<span id="cb6-12"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1973-05-01</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">41</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> &nbsp;&nbsp;<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.90e+02</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> &nbsp;&nbsp;&nbsp;<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.4</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">19.44</span>\\</span>
<span id="cb6-13"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1973-05-02</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">36</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> &nbsp;&nbsp;<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.18e+02</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">22.22</span>\\</span>
<span id="cb6-14"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1973-05-03</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> &nbsp;&nbsp;<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.49e+02</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> &nbsp;&nbsp;<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">12.6</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">23.33</span>\\</span>
<span id="cb6-15"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1973-05-04</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> &nbsp;&nbsp;<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.13e+02</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> &nbsp;&nbsp;<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">11.5</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">16.67</span>\\</span>
<span id="cb6-16"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1973-05-05</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NA <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> &nbsp;&nbsp;<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">14.3</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">13.33</span>\\</span>
<span id="cb6-17"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1973-05-06</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">28</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NA <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> &nbsp;&nbsp;<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">14.9</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">18.89</span>\\</span>
<span id="cb6-18">\bottomrule</span>
<span id="cb6-19">\end{tabular}</span>
<span id="cb6-20">\end{table}</span></code></pre></div></div>
</div>
<p>We now have a Latex <code>table</code> environment - which is fairly human-readable - that can be copy/pasted into a Latex document, or even included programmatically using <a href="../post/2022-06-27-knitr-latex.html">Sweave</a>.</p>


</section>

 ]]></description>
  <category>programming</category>
  <guid>https://andrewraim.github.io/post/2023-02-11-latex-tables-from-R.html</guid>
  <pubDate>Sat, 11 Feb 2023 05:00:00 GMT</pubDate>
</item>
<item>
  <title>COMPoissonReg: Usage, the Normalizing Constant, and Other Computational Details</title>
  <link>https://andrewraim.github.io/post/2022-11-15-compoissonreg.html</link>
  <description><![CDATA[ 




<p>I have been maintaining the <a href="https://CRAN.R-project.org/package=COMPoissonReg">COMPoissonReg</a> package, which facilitates use of the Conway-Maxwell Poisson (CMP) distribution. The normalizing constant of the CMP distribution is central to many computations in the package. Users have occasionally reported strange results that can be traced back to problems computing the normalizing constant. Early versions computed the constant by truncating the series to an finite sum with a fixed number of terms; however, this may yield inaccurate results. We addressed this to some degree in more recent versions in an ad hoc way. In the latest version (0.8.0) of the package, these computations have been revisited in a more principled way. Kimberly Sellers and I recently completed a <a href="https://www.census.gov/library/working-papers/2022/adrm/RRC2022-01.html">report</a> describing the updated method, which also demonstrates general use of the package. A vignette version of this report will be included in the package and updated along with any changes in the software.</p>



 ]]></description>
  <category>article</category>
  <category>programming</category>
  <guid>https://andrewraim.github.io/post/2022-11-15-compoissonreg.html</guid>
  <pubDate>Tue, 15 Nov 2022 05:00:00 GMT</pubDate>
</item>
<item>
  <title>Browsing summary files from the decennial census with R</title>
  <link>https://andrewraim.github.io/post/2022-08-15-sfreader.html</link>
  <description><![CDATA[ 




<p>If you have needed to use decennial census data without access to the internet, you may have encountered the raw data form of the summary files. These may be downloaded from the Census Bureau website for offline use; e.g., <a href="https://www.census.gov/data/datasets/2010/dec/summary-file-2.html">here</a>. The format of the files is compact but can be difficult to navigate. James Livsey, Kyle Irimata, and I prepared a <a href="https://www.census.gov/library/working-papers/2022/adrm/SSC2022-01.html">technical report</a> and a <a href="https://github.com/andrewraim/sfreader">package</a> to assist R users with this via the tidyverse. These materials focus on the 2010 SF2 summary file, but support for others may be added in the future as needed.</p>



 ]]></description>
  <category>article</category>
  <category>programming</category>
  <guid>https://andrewraim.github.io/post/2022-08-15-sfreader.html</guid>
  <pubDate>Mon, 15 Aug 2022 04:00:00 GMT</pubDate>
</item>
<item>
  <title>knitr and Latex Documents</title>
  <link>https://andrewraim.github.io/post/2022-06-27-knitr-latex.html</link>
  <description><![CDATA[ 




<p>Rmarkdown seems to be the most standard way to embed R code and results into a document. Markdown has many benefits compared to Latex: it is much easier to get started with and the source code is closer to a plain readable text document. It is also possible to use Latex within Markdown when an occasional equation is needed. But sometimes you really want to work in Latex without going through Markdown.</p>
<p>I have been aware that Sweave allows embedded R for Latex documents, but it seemed archaic compared to more modern R tools for reproducible research, and I avoided using it. Recently, I was made aware that <a href="https://www.overleaf.com/learn/latex/Knitr">Overleaf</a> supports authoring Sweave documents which require very little special markup and are almost like working in regular Latex. After some additional searching, I found that this is also possible without Overleaf. <a href="https://CRAN.R-project.org/package=knitr">knitr</a> has <a href="https://yihui.org/knitr/demo/sweave/">long been able</a> to do this.</p>
<p>Examples of Sweave with knitr can be found online, but I thought it would be worthwhile to post several more. See the repo <a href="https://github.com/andrewraim/sweave-templates" class="uri">https://github.com/andrewraim/sweave-templates</a> for an example in article format <a href="https://github.com/andrewraim/sweave-templates/raw/master/article/article-knitr.pdf">(pdf)</a> and one in Beamer slide format <a href="https://github.com/andrewraim/sweave-templates/raw/master/slides/slides-knitr.pdf">(pdf)</a>.</p>



 ]]></description>
  <category>programming</category>
  <guid>https://andrewraim.github.io/post/2022-06-27-knitr-latex.html</guid>
  <pubDate>Mon, 27 Jun 2022 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Presentation for 2020 Joint Statistical Meetings</title>
  <link>https://andrewraim.github.io/post/2020-08-03-jsm2020.html</link>
  <description><![CDATA[ 




<p>For this year’s virtual JSM, I am presenting in the session listed <a href="https://ww2.amstat.org/meetings/jsm/2020/onlineprogram/AbstractDetails.cfm?abstractid=309805">here</a>. Here are links to download the poster <a href="https://drive.google.com/uc?export=view&amp;id=1m4NIMm0kfrVRwsfigFHJDSUqOkCEHObA">PDF</a> and voiceover <a href="https://drive.google.com/uc?export=view&amp;id=1keGcR_2ZJ87nFicBbrPnvPh6nyGsvOLk">MP4</a>. Note that the files are hosted on Google Drive: if this is not accessible to you, email me for copies of the files.</p>



 ]]></description>
  <category>conference</category>
  <guid>https://andrewraim.github.io/post/2020-08-03-jsm2020.html</guid>
  <pubDate>Mon, 27 Jul 2020 04:00:00 GMT</pubDate>
</item>
<item>
  <title>R Workshop in Kerala</title>
  <link>https://andrewraim.github.io/post/2017-12-20-kerala-workshop.html</link>
  <description><![CDATA[ 




<section id="about-the-workshop" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> About the Workshop</h1>
<p><a href="http://www.csm.ornl.gov/~ost/">George Ostrouchov</a>, <a href="http://www.math.umbc.edu/people/neerchal.htm">Nagaraj Neerchal</a> and I are giving a two-day workshop <strong>High Performance Statistical Computing using R</strong> at the <em>International Conference on Recent Advances in Statistical Methodology with Applications in Clinical and Official Statistics</em> <a href="https://icsa2018.wordpress.com/">(ICSA 2018)</a>, Department of Statistics, St.&nbsp;Thomas College, Pala, Kerala, India. An abstract and brief outline for the workshop are given <a href="https://icsa2018.wordpress.com/workshop/">here</a>.</p>
<p>The workshop builds up to parallel and distributed computing in R, but begins by introducing basics of working in R. Previous experience with R and Rstudio will be helpful, but much of the material should be accessible to new R users with experience in another technical computing language such as Matlab, Python, or Julia. Applications involving statistical and machine learning methods will be presented throughout the workshop. A bachelors degree in a discipline such as statistics, computer science, or mathematics - OR equivalent work experience - should be sufficient to understand the methodology.</p>
<p>Attendees are encouraged to bring their laptops. Many example codes will be provided, and there will be opportunities at some points during the workshop for users to follow along. If you will be bringing your laptop, the remainder of this page discusses how to prepare it for the workshop.</p>
<ul>
<li><p>It is possible to manually install R, Rstudio, and all packages that will be used in the workshop. But this is quite impractical, especially for the material on parallel and distributed programming which varies somewhat based on the computing platform (Windows, Mac, Linux). Therefore, we highly suggest a second option…</p></li>
<li><p>Instead, we have prepared a <a href="https://www.docker.com">Docker</a> container, which is like a virtual machine. This will provide all attendees with the same programming environment which contains all the software needed for the workshop.</p></li>
</ul>
<p>Instructions to obtain and run the Docker container are given below. For attendees who cannot install Docker, alternative instructions are provided further below.</p>
<p>Workshop contents such as slides, programs, and dataset will be provided at the start of the workshop.</p>
</section>
<section id="installing-docker" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> Installing Docker</h1>
<p>Follow instructions to install Docker based on your computing platform (Windows, Mac, or Linux). Look for the Community Edition, for which there is no cost to use. Also, we recommend the Stable release rather than the Edge release. Here are some brief notes specific to platform.</p>
<section id="windows" class="level2" data-number="2.1">
<h2 data-number="2.1" class="anchored" data-anchor-id="windows"><span class="header-section-number">2.1</span> Windows</h2>
<p>Note that Windows users who do not have Windows 10 Pro or Enterprise - which will probably be most of us - will need a version of Docker called <a href="https://docs.docker.com/toolbox/overview/">Docker Toolbox</a>. Once it is installed, open the Docker Quickstart Terminal to issue commands, which should appear as follows.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://andrewraim.github.io/image/Kerala2018/win-docker-prompt.png" class="img-fluid figure-img"></p>
<figcaption>Logging into Rstudio Server</figcaption>
</figure>
</div>
<p>Take note of the IP address listed in the output; we will use it later when connecting to Rstudio.</p>
<p>I initially had some trouble installing Docker Toolbox on Windows 10 Home. I believe it was due to low disk space - I suggest having at least a 5-10 GB available before installing. For me, a solution was to remove Docker Toolbox, and the Oracle VirtualBox program installed with it, remove the <code>.docker</code> and <code>.virtualbox</code> subdirectories in <code>C:\Users\Andrew</code>, and rerun the Docker Toolbox installer. It may also be helpful to temporarily turn off Windows Firewall, or your antivirus program’s firewall if you have one, during the installation.</p>
</section>
<section id="linux" class="level2" data-number="2.2">
<h2 data-number="2.2" class="anchored" data-anchor-id="linux"><span class="header-section-number">2.2</span> Linux</h2>
<p>Linux users should follow the <a href="https://docs.docker.com/engine/installation/">installation instructions</a> specific to their Linux distribution (Ubuntu, CentOS, etc). Once it is installed, Docker can be controlled through the system terminal.</p>
<p>Note that Docker commands on Linux need administrator-level access, and must be prefixed with <code>sudo</code>. Because we anticipate mostly Windows users in attendance, the <code>sudo</code> has been left out of the commands shown on the remainder of this page.</p>
</section>
<section id="mac" class="level2" data-number="2.3">
<h2 data-number="2.3" class="anchored" data-anchor-id="mac"><span class="header-section-number">2.3</span> Mac</h2>
<p>Mac users should follow their set of <a href="https://docs.docker.com/engine/installation/">installation instructions</a>. As with Linux, once Docker is installed on a Mac, it can be controlled through the Terminal application. <code>sudo</code> is also needed for Mac when executing Docker commands.</p>
</section>
<section id="a-quick-test-of-your-docker-setup" class="level2" data-number="2.4">
<h2 data-number="2.4" class="anchored" data-anchor-id="a-quick-test-of-your-docker-setup"><span class="header-section-number">2.4</span> A Quick Test of your Docker Setup</h2>
<p>In your terminal, issue the following command.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> docker run hello-world</span></code></pre></div></div>
<p>Here, <code>$</code> represents the prompt, which is not part of the command. If your Docker setup is working, you should get the following result.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> docker run hello-world</span>
<span id="cb2-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Hello</span> from Docker!</span>
<span id="cb2-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">This</span> message shows that your installation appears to be working correctly.</span>
<span id="cb2-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">To</span> generate this message, Docker took the following steps:</span>
<span id="cb2-5"> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">1.</span> The Docker client contacted the Docker daemon.</span>
<span id="cb2-6"> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">2.</span> The Docker daemon pulled the <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hello-world"</span> image from the Docker Hub.</span>
<span id="cb2-7">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">(</span><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">amd64</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span></span>
<span id="cb2-8"> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">3.</span> The Docker daemon created a new container from that image which runs the</span>
<span id="cb2-9">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">executable</span> that produces the output you are currently reading.</span>
<span id="cb2-10"> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">4.</span> The Docker daemon streamed that output to the Docker client, which sent it</span>
<span id="cb2-11">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">to</span> your terminal.</span>
<span id="cb2-12"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">To</span> try something more ambitious, you can run an Ubuntu container with:</span>
<span id="cb2-13"> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> docker run <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-it</span> ubuntu bash</span>
<span id="cb2-14"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Share</span> images, automate workflows, and more with a free Docker ID:</span>
<span id="cb2-15"> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">https://cloud.docker.com/</span></span>
<span id="cb2-16"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">For</span> more examples and ideas, visit:</span>
<span id="cb2-17"> <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">https://docs.docker.com/engine/userguide/</span></span></code></pre></div></div>
</section>
</section>
<section id="downloading-workshop-config-files" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> Downloading Workshop Config Files</h1>
<p>Download the workshop’s <a href="https://drive.google.com/uc?export=view&amp;id=1CEnhcye1ifSdQHJKBalf20ZiVdeIyZII">Dockerfile</a> and accompanying <a href="https://drive.google.com/uc?export=view&amp;id=15hFA_kSpGmUddMa8tSyqN574h-SIB_zA">start.sh</a>. Save the files to a folder where you will keep workshop materials. Let us call this directory <code>/path/to/workshop</code>.</p>
<p>In Windows, my web browser renamed <code>Dockerfile</code> to <code>Dockerfile.txt</code> when it was downloaded. If this happens, rename the file and remove the <code>.txt</code> extension.</p>
<p>After downloading <code>start.sh</code>, Linux and Mac users may need to modify its permissions before the Docker container will run. Ensure that the script can be executed via the following command.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> chmod +x start.sh</span></code></pre></div></div>
</section>
<section id="preparing-and-running-the-workshop-container" class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> Preparing and Running the Workshop Container</h1>
<p>The following “build” command downloads and builds all of the prerequisites used in the container. It may take a while to run, especially on a slower network or a slower computer. For me it typically takes 20 or 30 minutes (roughly).</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb4-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> docker build <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-t</span> rworkshop /path/to/workshop</span></code></pre></div></div>
<p>The build command does not need to be run again unless the Dockerfile is changed, or unless your deployment changes. If the build was successful, the last few lines of output should be something like the following.</p>
<pre><code>Successfully built f5055a2787e5
Successfully tagged rworkshop:latest</code></pre>
<p>The container can now be run using the following “run” command (which is proceeded by defining two environment variables for our convenience).</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb6-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> export XSOCK=/tmp/.X11-unix/X0</span>
<span id="cb6-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> export EXT=/path/to/workshop</span>
<span id="cb6-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> docker run <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-v</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$XSOCK</span>:<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$XSOCK</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-v</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$EXT</span>:/home/rstudio/ext <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span></span>
<span id="cb6-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-p</span> 8787:8787 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-i</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-t</span> rworkshop</span></code></pre></div></div>
<p>Note that: * The option <code>-v $XSOCK:$XSOCK</code> allows the container to display graphics. This is useful for packages such as <code>Rcmdr</code>. * The option <code>-v $EXT:/home/rstudio/ext</code> makes the directory <code>$EXT</code> on your computer available inside the container as <code>/home/rstudio/ext</code>. This will allow you to load files from your local machine into the container, and to save work from the container back to your local machine. * The option <code>-p 8787:8787</code> exposes port 8787 from the container on your computer. This is needed to use Rstudio in the container. * The flags <code>-i</code> and <code>-t</code> are specified because the container is interactive. * The <code>\</code> character is used to break up a long command into multiple lines.</p>
</section>
<section id="using-r-in-the-container" class="level1" data-number="5">
<h1 data-number="5"><span class="header-section-number">5</span> Using R in the Container</h1>
<p>Once the container is successfully started, you will encounter a Linux command prompt like the following.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb7-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">rstudio@a8f9a900c791:~$</span></span></code></pre></div></div>
<section id="r-on-the-command-line" class="level2" data-number="5.1">
<h2 data-number="5.1" class="anchored" data-anchor-id="r-on-the-command-line"><span class="header-section-number">5.1</span> R on the Command Line</h2>
<p>To start R on the command line, issue the <code>R</code> command.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb8-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">rstudio@a8f9a900c791:~$</span> R</span>
<span id="cb8-2"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> cat<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hello world\n"</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">)</span></span>
<span id="cb8-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Hello</span> world</span></code></pre></div></div>
</section>
<section id="rstudio" class="level2" data-number="5.2">
<h2 data-number="5.2" class="anchored" data-anchor-id="rstudio"><span class="header-section-number">5.2</span> Rstudio</h2>
<p>To connect to Rstudio server, open a web browser on your laptop.</p>
<ul>
<li><p><strong>Mac</strong> and <strong>Linux</strong> users should navigate to the URL <a href="http://localhost:8787" class="uri">http://localhost:8787</a>.</p></li>
<li><p><strong>Windows</strong> users should recall the IP address they noted back in section 2.1. Suppose your assigned IP was <code>192.168.99.100</code> (which is the one I have in the screenshot); navigate to the URL <a href="http://192.168.99.100:8787" class="uri">http://192.168.99.100:8787</a>. If you forgot the IP address, start up another Docker Terminal and run the following.</p></li>
</ul>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb9-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> docker-machine ls</span></code></pre></div></div>
<p>A login prompt should appear in your browser. Enter <code>rstudio</code> as both the username and the password.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://andrewraim.github.io/image/Kerala2018/rstudio-server-login.png" class="img-fluid figure-img"></p>
<figcaption>Logging into Rstudio Server</figcaption>
</figure>
</div>
<p>Now you should be ready to use Rstudio in your web browser.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://andrewraim.github.io/image/Kerala2018/rstudio-server-screen.png" class="img-fluid figure-img"></p>
<figcaption>Rstudio Server in browser</figcaption>
</figure>
</div>
</section>
<section id="r-commander" class="level2" data-number="5.3">
<h2 data-number="5.3" class="anchored" data-anchor-id="r-commander"><span class="header-section-number">5.3</span> R Commander</h2>
<p>To launch the R Commander GUI, first start R via the command line or Rstudio. Then issue the following command.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(Rcmdr)</span></code></pre></div></div>
</section>
<section id="running-mpi-jobs" class="level2" data-number="5.4">
<h2 data-number="5.4" class="anchored" data-anchor-id="running-mpi-jobs"><span class="header-section-number">5.4</span> Running MPI Jobs</h2>
<p>To demonstrate running an MPI job, let us use a simple Hello World example. Open a text editor on your laptop and save the following code to the file <code>/path/to/workshop/hello.R</code></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(pbdMPI, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">quiet =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb11-2"></span>
<span id="cb11-3">msg <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hello world from process %d</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comm.rank</span>(), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comm.size</span>())</span>
<span id="cb11-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comm.cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Say hello:</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">quiet =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb11-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comm.cat</span>(msg, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">all.rank =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb11-6"></span>
<span id="cb11-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">finalize</span>()</span></code></pre></div></div>
<p>Recall that this file will be accessible inside the container via the path <code>/home/rstudio/ext/hello.R</code>. Inside the container, you should be able to run the script in parallel via the <code>mpirun</code> command.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb12-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">rstudio@a8f9a900c791:~$</span> mpirun <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-np</span> 4 Rscript ~/ext/hello.R</span>
<span id="cb12-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Say</span> hello:</span>
<span id="cb12-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">COMM.RANK</span> = 0</span>
<span id="cb12-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Hello</span> world from process 0</span>
<span id="cb12-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">COMM.RANK</span> = 1</span>
<span id="cb12-6"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">COMM.RANK</span> = 2</span>
<span id="cb12-7"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Hello</span> world from process 1</span>
<span id="cb12-8"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Hello</span> world from process 2</span>
<span id="cb12-9"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">COMM.RANK</span> = 3</span>
<span id="cb12-10"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Hello</span> world from process 3</span></code></pre></div></div>
<p>Outputs from the container should be saved to <code>/home/rstudio/ext</code>. This will allow you to view images, PDFs, etc using the tools already installed on your laptop.</p>
<p>If you made it this far, congratulations - your laptop is ready!</p>
</section>
</section>
<section id="more-on-docker" class="level1" data-number="6">
<h1 data-number="6"><span class="header-section-number">6</span> More on Docker</h1>
<p>For a list of Docker commands, see <a href="https://docs.docker.com/engine/reference/commandline/docker" class="uri">https://docs.docker.com/engine/reference/commandline/docker</a>. Here we will mention a few specific ones. If you would like a video tutorial, there appear to be several good ones on YouTube such as <a href="https://youtu.be/pa2wHWQ6WA8">this one</a>.</p>
<p>List active Docker containers.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb13-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> [araim@araim-inspiron docker]$ docker ps</span>
<span id="cb13-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">CONTAINER</span> ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES</span>
<span id="cb13-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">f117630f47b8</span>        rworkshop           <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/etc/start.sh"</span>     8 seconds ago       Up 7 seconds        0.0.0.0:8787-<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span>8787/tcp   hopeful_elion</span></code></pre></div></div>
<p>Normally, our rworkshop container will stop running when you log out of its shell. In case something went wrong with the container listed above and we wanted to kill it, we could use the following command from another terminal window.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb14-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> docker kill f117630f47b8</span>
<span id="cb14-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">f117630f47b8</span></span></code></pre></div></div>
<p>List Docker images that have been deployed onto your machine.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb15-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> docker images</span>
<span id="cb15-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">REPOSITORY</span>          TAG                 IMAGE ID            CREATED             SIZE</span>
<span id="cb15-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">rworkshop</span>           latest              d658ef03dc24        13 hours ago        1.98GB</span>
<span id="cb15-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">rocker/rstudio</span>      3.4.3               f206114fe549        3 weeks ago         1.08GB</span>
<span id="cb15-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">hello-world</span>         latest              1815c82652c0        6 months ago        1.84kB</span></code></pre></div></div>
<p>The <code>rworkshop</code> and <code>rocker/rstudio</code> images are rather large, so you may need to remove them after the workshop. Here we will remove the <code>hello-world</code> image, for demonstration purposes.</p>
<pre class="{bash}"><code>$ docker image rm 1815c82652c0
Untagged: hello-world:latest
Untagged: hello-world@sha256:f3b3b28a45160805bb16542c9531888519430e9e6d6ffc09d72261b0d26ff74f
Deleted: sha256:1815c82652c03bfd8644afda26fb184f2ed891d921b20a0703b46768f9755c57
Deleted: sha256:45761469c965421a92a69cc50e92c01e0cfa94fe026cdd1233445ea00e96289a</code></pre>
</section>
<section id="if-you-cannot-install-docker" class="level1" data-number="7">
<h1 data-number="7"><span class="header-section-number">7</span> If You Cannot Install Docker</h1>
<p>Installing and running Docker requires administrator-level access to your computer. For example, if using a laptop issued by your employer, you may not have administrator access but may have R and Rstudio already installed. For the benefit of these attendees, we will list individual components that we have used in preparing the workshop.</p>
<p>The major disadvantage of installing the components individually is that you may not be able to run some of the parallel and distributed computing examples. For example, the <a href="https://stat.ethz.ch/R-manual/R-devel/library/parallel/html/mclapply.html">mclapply function depends on forking</a>, and therefore cannot be used directly in Windows. Also, our distributed computing examples rely on MPI, but we (the presenters) do not have experience installing and configuring MPI libraries in Windows.</p>
<p>Check the version numbers of components which you may have already installed - especially R, Rstudio, and any R packages. Some parts of the workshop may not be accessible with older versions.</p>
<section id="install-r" class="level2" data-number="7.1">
<h2 data-number="7.1" class="anchored" data-anchor-id="install-r"><span class="header-section-number">7.1</span> Install R</h2>
<p>Install R from <a href="https://cran.r-project.org/">CRAN</a>. Windows users should navigate to “Download R for Windows” at the top of the page. We are using version 3.4.3.</p>
</section>
<section id="install-rstudio" class="level2" data-number="7.2">
<h2 data-number="7.2" class="anchored" data-anchor-id="install-rstudio"><span class="header-section-number">7.2</span> Install Rstudio</h2>
<p>Install <a href="https://www.rstudio.com/">Rstudio</a>. Click <code>Products =&gt; RStudio</code> on the top menu, and look for a button labeled <code>Download Rstudio Desktop</code>. Select <code>RStudio Desktop Open Source License</code> among the available versions. We are using version 1.1.383.</p>
</section>
<section id="install-the-rcpp-package" class="level2" data-number="7.3">
<h2 data-number="7.3" class="anchored" data-anchor-id="install-the-rcpp-package"><span class="header-section-number">7.3</span> Install the Rcpp package</h2>
<p>See <a href="https://support.rstudio.com/hc/en-us/articles/200486498-Package-Development-Prerequisites" class="uri">https://support.rstudio.com/hc/en-us/articles/200486498-Package-Development-Prerequisites</a> for instructions to install the prerequisites (compilers, important libraries, etc). Once the prerequisites are installed, simply install <code>Rcpp</code> as you would any other package.</p>
<p>To test your Rcpp environment, try the following in RStudio. 1. Select <code>File =&gt; New File =&gt; C++ File</code> from the main menu. This will create a simple Rcpp program.<br>
2. Save the program, say, as <code>example.cpp</code>.<br>
3. Click the <code>Source</code> button, which is located in the upper-right corner of the panel with the <code>example.cpp</code> source code. This should produce the following output.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">timesTwo</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb17-2">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">84</span></span></code></pre></div></div>
</section>
<section id="install-mpi" class="level2" data-number="7.4">
<h2 data-number="7.4" class="anchored" data-anchor-id="install-mpi"><span class="header-section-number">7.4</span> Install MPI</h2>
<p>There are several MPI implementations available, such as OpenMPI and MVAPICH2. The instructions will vary based on your choice of implementation, as well as your hardware and operating system. You are on your own here; good luck! If your installation is successful, you should be able to run the following minimal example.</p>
<p>First, create a new file called <code>hello_mpi.c</code> with the following contents.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode cpp code-with-copy"><code class="sourceCode cpp"><span id="cb18-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;stdio.h&gt;</span></span>
<span id="cb18-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">#include </span><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">&lt;mpi.h&gt;</span></span>
<span id="cb18-3"></span>
<span id="cb18-4"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> main <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> argc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>argv<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[])</span></span>
<span id="cb18-5"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span></span>
<span id="cb18-6">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> id<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> np<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb18-7">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">char</span> processor_name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span>MPI_MAX_PROCESSOR_NAME<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">];</span></span>
<span id="cb18-8">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">int</span> processor_name_len<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb18-9"></span>
<span id="cb18-10">    MPI_Init<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(&amp;</span>argc<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>argv<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb18-11"></span>
<span id="cb18-12">    MPI_Comm_size<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>MPI_COMM_WORLD<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>np<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb18-13">    MPI_Comm_rank<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>MPI_COMM_WORLD<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>id<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb18-14">    MPI_Get_processor_name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>processor_name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span>processor_name_len<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb18-15"></span>
<span id="cb18-16">    printf<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hello world from process </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%03d</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> out of </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%03d</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">, processor name </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%s\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span></span>
<span id="cb18-17">        id<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> np<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> processor_name<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">);</span></span>
<span id="cb18-18"></span>
<span id="cb18-19">    MPI_Finalize<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">();</span></span>
<span id="cb18-20">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb18-21"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span></span></code></pre></div></div>
<p>Now compile and run the program with MPI.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb19-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> mpicc hello-mpi.c <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-o</span> hello-mpi</span>
<span id="cb19-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">$</span> mpirun <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-np</span> 4 hello-mpi</span>
<span id="cb19-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Hello</span> world from process 000 out of 004, processor name localhost</span>
<span id="cb19-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Hello</span> world from process 001 out of 004, processor name localhost</span>
<span id="cb19-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Hello</span> world from process 002 out of 004, processor name localhost</span>
<span id="cb19-6"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">Hello</span> world from process 003 out of 004, processor name localhost</span></code></pre></div></div>
<p>Depending on your platform and choice of MPI implementation, your commands may be different. The string <code>localhost</code> in the output will be replaced with your machine’s hostname.</p>
</section>
<section id="install-r-packages" class="level2" data-number="7.5">
<h2 data-number="7.5" class="anchored" data-anchor-id="install-r-packages"><span class="header-section-number">7.5</span> Install R Packages</h2>
<p>Download the script <a href="https://drive.google.com/uc?export=view&amp;id=12273JFSirYb1G8pPx9fRYNtgZ6IrpFGx">install.R</a> and run it in R to obtain packages which will be used in the workshop. Suppose we have placed it into the directory <code>/path/to/workshop</code>. Run the following command in R.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">source</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/path/to/workshop/install.R"</span>)</span></code></pre></div></div>
<p>To make sure your package versions are up to date, open Rstudio and click <code>Tools =&gt; Check for Package Updates</code> on the main menu.</p>


</section>
</section>

 ]]></description>
  <category>workshop</category>
  <guid>https://andrewraim.github.io/post/2017-12-20-kerala-workshop.html</guid>
  <pubDate>Fri, 05 Jan 2018 05:00:00 GMT</pubDate>
</item>
<item>
  <title>Rcpp Workshop at UMBC</title>
  <link>https://andrewraim.github.io/post/2017-08-07-umbc-workshop-Rcpp.html</link>
  <description><![CDATA[ 




<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>Please note that the workshop date has been moved from Friday Sept 8, 2017 to Friday Sept 22, 2017. See below for details.</p>
</div>
</div>
<p>Iris Gauran and I are planning to give a half-day workshop on Rcpp at UMBC. Tentative details are given below. Part of the workshop will be a “quick start”, where we will demonstrate accessing C++ code from R via toy examples. Attendees are encouraged to prepare their laptops for Rcpp programming, and bring them to the workshop to follow along with this portion.</p>
<p>We will focus on Rcpp programming in RStudio, which provides a common interface across all major platforms (Windows, Mac, and Linux), and helps to automate some procedural tasks.</p>
<ol type="1">
<li>Install R (<a href="http://www.r-project.org" class="uri">http://www.r-project.org</a>). The current version as of this writing is 3.4.1 “Single Candle”.</li>
<li>Install RStudio Desktop (<a href="http://www.rstudio.com" class="uri">http://www.rstudio.com</a>). The current version is 1.0.153.</li>
<li>Install the <code>Rcpp</code>, <code>RcppArmadillo</code>, and <code>RcppGSL</code> packages.</li>
<li>Some additional libraries may be needed to compile C++ programs on your computer. These differ depending on your operating system. See <a href="http://support.rstudio.com/hc/en-us/articles/200486498-Package-Development-Prerequisites" class="uri">http://support.rstudio.com/hc/en-us/articles/200486498-Package-Development-Prerequisites</a>.</li>
</ol>
<p>This may be a good time to upgrade if you have older versions of R, RStudio, or any of the listed packages.</p>
<p>To test your Rcpp environment, try the following in RStudio.</p>
<ol type="1">
<li>Select <code>File =&gt; New File =&gt; C++ File</code> from the main menu. This will create a simple Rcpp program.</li>
<li>Save the program, say, as <code>example.cpp</code>.</li>
<li>Click the <code>Source</code> button, which is located in the upper-right corner of the panel with the <code>example.cpp</code> source code. This should produce the output</li>
</ol>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">R<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">timesTwo</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb1-2">[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">84</span></span></code></pre></div></div>
<hr>
<p><strong>Location:</strong> University of Maryland, Baltimore County.<br>
<strong>Room:</strong> MP 401 (Math/Stat Seminar Room).<br>
<strong>Time:</strong> Friday Sept 22, 1 - 4:30pm.<br>
<strong>Cost:</strong> This workshop is free for the UMBC community.<br>
<strong>Prerequisites:</strong> intermediate knowledge in a high level language like R, Python, or Matlab. Familiarity with R and RStudio is highly recommended.<br>
<strong>Snacks:</strong> Light refreshments (coffee and bagels) will be provided.</p>
<p><strong>Abstract</strong><br>
R is the preferred computing environment for many statisticians, and is used both in research and applied problems. R has achieved tremendous popularity because it is free, open source, and available on all modern platforms (Windows, Mac, and Linux). The R programming language is simple and intuitive, and well-suited for fast prototyping of complicated algorithms. However, R users can often find the performance of their programs to be lacking.</p>
<p>This workshop will demonstrate Rcpp, an extension of R that facilitates interoperability between R and C++ (Eddelbuettel, 2013). With Rcpp, computationally intensive parts of your program can be written in C++ and seamlessly called from R, potentially giving dramatic performance improvements within the familiar R user environment. We will provide a quick start for new Rcpp users through simple examples, and also examine several larger-scale statistical applications. C++ programming is usually more burdensome than R programming, so we will discuss tradeoffs between optimizing R code and migrating to C++, and ways to minimize suffering while making the transition.</p>
<p><strong>References</strong></p>
<ul>
<li>Dirk Eddelbuettel. Seamless R and C++ Integration with Rcpp. Springer, 2013.</li>
<li>Also see <a href="http://www.rcpp.org" class="uri">http://www.rcpp.org</a>.</li>
</ul>



 ]]></description>
  <category>workshop</category>
  <guid>https://andrewraim.github.io/post/2017-08-07-umbc-workshop-Rcpp.html</guid>
  <pubDate>Mon, 07 Aug 2017 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Web Page Migration</title>
  <link>https://andrewraim.github.io/post/2017-03-15-website.html</link>
  <description><![CDATA[ 




<p>I am now using <a href="https://pages.github.com">GitHub Pages</a> to host my website, along with the static site generator <a href="https://jekyllrb.com">Jekyll</a>. My <a href="http://www.umbc.edu/~araim1">previous website</a> was hosted at UMBC.</p>



 ]]></description>
  <category>admin</category>
  <guid>https://andrewraim.github.io/post/2017-03-15-website.html</guid>
  <pubDate>Wed, 15 Mar 2017 04:00:00 GMT</pubDate>
</item>
</channel>
</rss>
