<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>johnramey &#187; r</title>
	<atom:link href="http://johnramey.net/category/r/feed/" rel="self" type="application/rss+xml" />
	<link>http://johnramey.net/blog</link>
	<description>Don&#039;t think. Compute.</description>
	<lastBuildDate>Wed, 07 Dec 2011 23:52:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Pseudo-Random vs. Random Numbers in R</title>
		<link>http://johnramey.net/blog/2011/11/25/pseudo-random-vs-random-numbers-in-r/</link>
		<comments>http://johnramey.net/blog/2011/11/25/pseudo-random-vs-random-numbers-in-r/#comments</comments>
		<pubDate>Fri, 25 Nov 2011 09:08:11 +0000</pubDate>
		<dc:creator>ramhiser</dc:creator>
				<category><![CDATA[r]]></category>
		<category><![CDATA[pseudo-random numbers]]></category>
		<category><![CDATA[random numbers]]></category>

		<guid isPermaLink="false">http://johnramey.net/blog/?p=239</guid>
		<description><![CDATA[Happy Thanksgiving, everyone. Earlier today, I found an interesting post from Bo Allen on pseudo-random vs random numbers, where the author uses a simple bitmap (heat map) to show that the rand function in PHP has a systematic pattern and compares these to truly random numbers obtained from random.org. The post&#8217;s results suggest that pseudo-randomness in PHP is [...]]]></description>
			<content:encoded><![CDATA[<p>Happy Thanksgiving, everyone. Earlier today, I found <a href="http://www.boallen.com/random-numbers.html">an interesting post from Bo Allen</a> on <a href="http://en.wikipedia.org/wiki/Pseudorandom_number_generator">pseudo-random</a> vs <a href="http://en.wikipedia.org/wiki/Random_number">random numbers</a>, where the author uses a simple bitmap (<a href="http://en.wikipedia.org/wiki/Heat_map">heat map</a>) to show that the <strong>rand</strong> function in <a href="http://www.php.net/">PHP</a> has a systematic pattern and compares these to <strong>truly</strong> <strong>random</strong> numbers obtained from <a href="http://www.random.org/">random.org</a>. The post&#8217;s results suggest that pseudo-randomness in <a href="http://www.php.net/">PHP</a> is faulty and, in general, should not be underestimated in practice. Of course, the findings should not be too surprising, as there is a large body of literature on the subtleties, philosophies, and implications of the <strong>pseudo</strong> aspect of the most common approaches to random number generation. However, it is silly that <a href="http://www.php.net/">PHP</a>&#8216;s <a href="http://en.wikipedia.org/wiki/Random_number_generation">random number generator (RNG)</a> displays such an obvious pattern nowadays because there are several decent, well-studied pseudo-<a href="http://en.wikipedia.org/wiki/Random_number_generation">RNG</a> algorithms available as well as numerous tests for randomness.  For a good introduction to <a href="http://en.wikipedia.org/wiki/Random_number_generation">RNG</a>, I recommend <a href="http://www.johndcook.com/blog/2010/12/06/how-to-test-a-random-number-generator-2/">John D. Cook&#8217;s discussion on testing a random number generator</a>.</p>
<p>Now, I would never use <a href="http://www.php.net/">PHP</a> for any (serious) statistical analysis, partly due to my fondness for <a href="http://www.r-project.org/">R</a>, nor do I doubt the practicality of the <a href="http://en.wikipedia.org/wiki/Random_number_generation">RNG</a> in <a href="http://www.r-project.org/">R</a>. But I was curious to see what would happen. So, created equivalent plots in <a href="http://www.r-project.org/">R</a> to see if a <strong>rand</strong> equivalent would exhibit a systematic pattern like in <a href="http://www.php.net/">PHP</a>, even if less severe. Also, for comparison, I chose to use <a href="http://cran.r-project.org/web/packages/random/index.html">the <strong>random</strong> package</a>, from <a href="http://dirk.eddelbuettel.com/">Dirk Eddelbuettel</a>, to draw <strong>truly random</strong> numbers from <a href="http://www.random.org/">random.org</a>. Until today, I had only heard of the <strong>random</strong> package but had never used it.</p>
<p>I have provided the function <strong>rand_bit_matrix</strong>, which requires the number of rows and columns to display in the plotted bitmap. To create the bitmaps, I used the <a href="http://cran.r-project.org/web/packages/pixmap/index.html"><strong>pixmap</strong> package</a> rather than the much-loved <a href="http://had.co.nz/ggplot2/"><strong>ggplot2</strong> package</a>, simply because of how easy it was for me to create the plots. (If you are concerned that I have lost the faith, please note that I am aware of the awesomeness of <strong><a href="http://had.co.nz/ggplot2/">ggplot2</a></strong> and <a href="http://johnramey.net/blog/2011/06/05/conways-game-of-life-in-r-with-ggplot2-and-animation/">its ability</a> <a href="http://learnr.wordpress.com/2010/01/26/ggplot2-quick-heatmap-plotting/">to create heat maps</a>.)</p>
<p>It is important to note that there were two challenges that I encountered when using drawing <strong>truly random numbers</strong>.</p>
<ol>
<li>Only 10,000 numbers can be drawn at once from <a href="http://www.random.org/">random.org</a>. (This is denoted as <strong>max_n_random.org</strong> in the function below.)</li>
<li>There is a daily limit to the number of times the <a href="http://www.random.org/">random.org</a> service will provide numbers.</li>
</ol>
<p>To overcome the first challenge, I split the total number of bits into separate calls, if necessary. This approach, however, increases our number of requests, and after too many requests, you will see the error: <strong>random.org suggests to wait until tomorrow</strong>. Currently, I do not know the exact number of allowed requests or if the amount of requested random numbers is a factor, but looking back, I would guess about 20ish large requests is too much.</p>
<p>Below, I have plotted 500 x 500 bitmaps based on the <em>random</em> bits from both of <a href="http://www.r-project.org/">R</a> and <a href="http://www.random.org/">random.org</a>. As far as I can tell, no apparent patterns are visible in either plot, but from the graphics alone, our conclusions are limited to ruling out obvious systematic patterns, which were exhibited from the <a href="http://www.php.net/">PHP</a> code. I am unsure if the <a href="http://www.php.net/">PHP</a> folks formally tested their <a href="http://en.wikipedia.org/wiki/Random_number_generation">RNG</a> algorithms for <strong>randomness</strong>, but even if they did, the code in both <a href="http://www.r-project.org/">R</a> and <a href="http://www.php.net/">PHP</a> is straightforward and provides a quick eyeball test. Armed with similar plots alone, the <a href="http://www.php.net/">PHP</a> devs could have sought for better <a href="http://en.wikipedia.org/wiki/Random_number_generation">RNG</a> algorithms &#8212; perhaps, borrowed those from <a href="http://www.r-project.org/">R</a>.</p>
<p>Click the images for a larger view.</p>
<p style="text-align: center;"><a href="http://johnramey.net/blog/wp-content/uploads/2011/11/bitmap_R.png"><img class="aligncenter size-large wp-image-272" style="border-style: none; border-color: white; border-width: 0px;" title="Pseudo-Random Bits in R" src="http://johnramey.net/blog/wp-content/uploads/2011/11/bitmap_R-1024x617.png" alt="" width="768" height="463" /></a></p>
<p>&nbsp;</p>
<p style="text-align: center;"><a href="http://johnramey.net/blog/wp-content/uploads/2011/11/bitmap_random_org.png"><img class="aligncenter size-large wp-image-273" style="border-style: none; border-color: #ffffff; border-width: 0px;" title="Truly Random Bits from Random.org" src="http://johnramey.net/blog/wp-content/uploads/2011/11/bitmap_random_org-1024x617.png" alt="" width="768" height="463" /></a></p>
<p>&nbsp;</p>
<p>Finally, I have provided my <a href="http://www.r-project.org/">R</a> code, which you may tinker with, copy, manipulate, utilize, etc. without legal action and without fear of <a href="http://www.hulu.com/watch/4476/futurama-death-by-snoo-snoo">death by snoo snoo</a>. Mmmm. Snoo snoo.</p>
<p>&nbsp;</p>
<div id="gist-1392602" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'>library<span class="p">(</span><span class="s">&#39;plyr&#39;</span><span class="p">)</span></div><div class='line' id='LC2'>library<span class="p">(</span><span class="s">&#39;pixmap&#39;</span><span class="p">)</span></div><div class='line' id='LC3'>library<span class="p">(</span><span class="s">&#39;random&#39;</span><span class="p">)</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'>rand_bit_matrix <span class="o">&lt;-</span> <span class="kr">function</span><span class="p">(</span>num_rows <span class="o">=</span> <span class="m">500</span><span class="p">,</span> num_cols <span class="o">=</span> <span class="m">500</span><span class="p">,</span></div><div class='line' id='LC6'>&nbsp;&nbsp;max_n_random.org <span class="o">=</span> <span class="m">10000</span><span class="p">,</span> seed <span class="o">=</span> <span class="kc">NULL</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC7'>&nbsp;&nbsp;<span class="c1"># I have copied the following function directly from help(&quot;integer&quot;).</span></div><div class='line' id='LC8'>&nbsp;&nbsp;is.wholenumber <span class="o">&lt;-</span> <span class="kr">function</span><span class="p">(</span>x<span class="p">,</span> tol <span class="o">=</span> <span class="m">.</span>Machine<span class="p">$</span>double.eps<span class="o">^</span><span class="m">0.5</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;abs<span class="p">(</span>x <span class="o">-</span> round<span class="p">(</span>x<span class="p">))</span> <span class="o">&lt;</span> tol</div><div class='line' id='LC10'>&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC11'>&nbsp;&nbsp;</div><div class='line' id='LC12'>&nbsp;&nbsp;<span class="c1"># The number of bits to draw at &quot;random&quot;.</span></div><div class='line' id='LC13'>&nbsp;&nbsp;n <span class="o">&lt;-</span> num_rows <span class="o">*</span> num_cols</div><div class='line' id='LC14'>&nbsp;&nbsp;<span class="kr">if</span><span class="p">(</span>n <span class="o">&lt;=</span> <span class="m">0</span> <span class="o">||</span> !is.wholenumber<span class="p">(</span>n<span class="p">))</span> <span class="p">{</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;stop<span class="p">(</span><span class="s">&quot;The number of bits &#39;n&#39; should be a natural number.&quot;</span><span class="p">)</span></div><div class='line' id='LC16'>&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC17'><br/></div><div class='line' id='LC18'>&nbsp;&nbsp;<span class="kr">if</span><span class="p">(</span>!is.null<span class="p">(</span>seed<span class="p">))</span> <span class="p">{</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;set.seed<span class="p">(</span>seed<span class="p">)</span></div><div class='line' id='LC20'>&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC21'><br/></div><div class='line' id='LC22'>&nbsp;&nbsp;<span class="c1"># Create a matrix of pseudo-random bits.</span></div><div class='line' id='LC23'>&nbsp;&nbsp;bits_R <span class="o">&lt;-</span> replicate<span class="p">(</span>n <span class="o">=</span> num_cols<span class="p">,</span> sample<span class="p">(</span>c<span class="p">(</span><span class="m">0</span><span class="p">,</span> <span class="m">1</span><span class="p">),</span> size <span class="o">=</span> num_rows<span class="p">,</span> replace <span class="o">=</span> <span class="kc">TRUE</span><span class="p">))</span></div><div class='line' id='LC24'>&nbsp;&nbsp;</div><div class='line' id='LC25'>&nbsp;&nbsp;<span class="c1"># Because random.org will only return a maximum of 10,000 numbers at a time,</span></div><div class='line' id='LC26'>&nbsp;&nbsp;<span class="c1"># we break this up into several calls.</span></div><div class='line' id='LC27'>&nbsp;&nbsp;seq_n_random.org <span class="o">&lt;-</span> rep.int<span class="p">(</span>x <span class="o">=</span> max_n_random.org<span class="p">,</span> times <span class="o">=</span> n <span class="o">%/%</span> max_n_random.org<span class="p">)</span></div><div class='line' id='LC28'>&nbsp;&nbsp;<span class="kr">if</span><span class="p">(</span>n <span class="o">%%</span> max_n_random.org <span class="o">&gt;</span> <span class="m">0</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC29'>&nbsp;&nbsp;&nbsp;&nbsp;seq_n_random.org <span class="o">&lt;-</span> c<span class="p">(</span>seq_n_random.org<span class="p">,</span> n <span class="o">%%</span> max_n_random.org<span class="p">)</span></div><div class='line' id='LC30'>&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC31'>&nbsp;&nbsp;bits_random.org <span class="o">&lt;-</span> lapply<span class="p">(</span>seq_n_random.org<span class="p">,</span> <span class="kr">function</span><span class="p">(</span>n<span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC32'>&nbsp;&nbsp;&nbsp;&nbsp;try_default<span class="p">(</span>randomNumbers<span class="p">(</span>n <span class="o">=</span> n<span class="p">,</span> min <span class="o">=</span> <span class="m">0</span><span class="p">,</span> max <span class="o">=</span> <span class="m">1</span><span class="p">,</span> col <span class="o">=</span> <span class="m">1</span><span class="p">),</span> <span class="kc">NA</span><span class="p">)</span> </div><div class='line' id='LC33'>&nbsp;&nbsp;<span class="p">})</span></div><div class='line' id='LC34'>&nbsp;&nbsp;</div><div class='line' id='LC35'>&nbsp;&nbsp;bits_random.org <span class="o">&lt;-</span> matrix<span class="p">(</span>unlist<span class="p">(</span>bits_random.org<span class="p">),</span> nrow <span class="o">=</span> num_rows<span class="p">,</span> ncol <span class="o">=</span> num_cols<span class="p">)</span></div><div class='line' id='LC36'>&nbsp;&nbsp;</div><div class='line' id='LC37'>&nbsp;&nbsp;list<span class="p">(</span>R <span class="o">=</span> bits_R<span class="p">,</span> random.org <span class="o">=</span> bits_random.org<span class="p">)</span></div><div class='line' id='LC38'><span class="p">}</span></div><div class='line' id='LC39'><br/></div><div class='line' id='LC40'>bit_mats <span class="o">&lt;-</span> rand_bit_matrix<span class="p">(</span>num_rows <span class="o">=</span> <span class="m">500</span><span class="p">,</span> num_cols <span class="o">=</span> <span class="m">500</span><span class="p">,</span> seed <span class="o">=</span> <span class="m">42</span><span class="p">)</span></div><div class='line' id='LC41'><br/></div><div class='line' id='LC42'>with<span class="p">(</span>bit_mats<span class="p">,</span></div><div class='line' id='LC43'>&nbsp;&nbsp;plot<span class="p">(</span>pixmapGrey<span class="p">(</span>data <span class="o">=</span> R<span class="p">,</span> nrow <span class="o">=</span> nrow<span class="p">(</span>R<span class="p">),</span> ncol <span class="o">=</span> ncol<span class="p">(</span>R<span class="p">)),</span> main <span class="o">=</span> <span class="s">&quot;R&quot;</span><span class="p">)</span></div><div class='line' id='LC44'><span class="p">)</span></div><div class='line' id='LC45'>with<span class="p">(</span>bit_mats<span class="p">,</span></div><div class='line' id='LC46'>&nbsp;&nbsp;plot<span class="p">(</span>pixmapGrey<span class="p">(</span>data <span class="o">=</span> random.org<span class="p">,</span> nrow <span class="o">=</span> nrow<span class="p">(</span>random.org<span class="p">),</span> ncol <span class="o">=</span> ncol<span class="p">(</span>random.org<span class="p">)),</span> main <span class="o">=</span> <span class="s">&quot;random.org&quot;</span><span class="p">)</span></div><div class='line' id='LC47'><span class="p">)</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1392602/398cdef7cbdd4cf91225db1e64fc93c22875aa8f/gistfile1.r" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1392602#file_gistfile1.r" style="float:right;margin-right:10px;color:#666">gistfile1.r</a>
            <a href="https://gist.github.com/1392602">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://johnramey.net/blog/2011/11/25/pseudo-random-vs-random-numbers-in-r/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Conway&#8217;s Game of Life in R with ggplot2 and animation</title>
		<link>http://johnramey.net/blog/2011/06/05/conways-game-of-life-in-r-with-ggplot2-and-animation/</link>
		<comments>http://johnramey.net/blog/2011/06/05/conways-game-of-life-in-r-with-ggplot2-and-animation/#comments</comments>
		<pubDate>Mon, 06 Jun 2011 00:04:08 +0000</pubDate>
		<dc:creator>ramhiser</dc:creator>
				<category><![CDATA[r]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[foreach]]></category>
		<category><![CDATA[game of life]]></category>
		<category><![CDATA[ggplot2]]></category>

		<guid isPermaLink="false">http://johnramey.net/blog/?p=133</guid>
		<description><![CDATA[In undergrad I had a computer science professor that piqued my interest in applied mathematics, beginning with Conway&#8217;s Game of Life. At first, the Game of Life (not the board game) appears to be quite simple &#8212; perhaps, too simple &#8212; but it has been widely explored and is useful for modeling systems over time. [...]]]></description>
			<content:encoded><![CDATA[<p>In undergrad I had a computer science professor that piqued my interest in applied mathematics, beginning with <a href="http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life">Conway&#8217;s Game of Life</a>. At first, the Game of Life (not the board game) appears to be quite simple &#8212; perhaps, too simple &#8212; but it has been widely explored and is useful for modeling systems over time. It has been forever since I wrote my first version of this in C++, and I happily report that there will be no nonsense here.</p>
<p>The basic idea is to start with a grid of cells, where each cell is either a zero (dead) or a one (alive). We are interested in watching the population behavior over time to see if the population dies off, has some sort of equilibrium, etc. John Conway studied many possible ways to examine population behaviors and ultimately decided on the following rules, which we apply to each cell for the current tick (or generation).</p>
<ol>
<li>Any live cell with fewer than two live neighbours dies, as if caused by under-population.</li>
<li>Any live cell with two or three live neighbours lives on to the next generation.</li>
<li>Any live cell with more than three live neighbours dies, as if by overcrowding.</li>
<li>Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction</li>
</ol>
<p>Although there are <a href="http://www.statisticsblog.com/2010/05/game-of-life-in-r/">other versions</a> of this in R, I decided to give it a shot myself. I am not going to provide a walkthrough of the code as I may normally do, but the code should be simple enough to understand for one proficient in R. It may have been unnecessary to implement this with the foreach package, but I wanted to get some more familiarity with foreach, so I did.</p>
<p>The set of grids is stored as a list, where each element is a matrix of zeros and ones. Each matrix is then converted to an image with ggplot2, and the sequence of images is exported as a GIF with the animation package.</p>
<p>Let me know if you improve on my code any. I&#8217;m always interested in learning how to do things better.</p>

<div class="wp_codebox"><table><tr id="p1333"><td class="code" id="p133code3"><pre class="c" style="font-family:monospace;">library<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'foreach'</span><span style="color: #009900;">&#41;</span>
library<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'ggplot2'</span><span style="color: #009900;">&#41;</span>
library<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'animation'</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #339933;"># Determines how many neighboring cells around the (j,k)th cell have living organisms.</span>
<span style="color: #339933;"># The conditionals are used to check if we are at a boundary of the grid.</span>
how_many_neighbors <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>grid<span style="color: #339933;">,</span> j<span style="color: #339933;">,</span> k<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  size <span style="color: #339933;">&lt;-</span> nrow<span style="color: #009900;">&#40;</span>grid<span style="color: #009900;">&#41;</span>
  count <span style="color: #339933;">&lt;-</span> <span style="color: #0000dd;">0</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>j <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    count <span style="color: #339933;">&lt;-</span> count <span style="color: #339933;">+</span> grid<span style="color: #009900;">&#91;</span>j<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> k<span style="color: #009900;">&#93;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>k <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> count <span style="color: #339933;">&lt;-</span> count <span style="color: #339933;">+</span> grid<span style="color: #009900;">&#91;</span>j<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> k<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>k <span style="color: #339933;">&lt;</span> size<span style="color: #009900;">&#41;</span> count <span style="color: #339933;">&lt;-</span> count <span style="color: #339933;">+</span> grid<span style="color: #009900;">&#91;</span>j<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> k<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>j <span style="color: #339933;">&lt;</span> size<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    count <span style="color: #339933;">&lt;-</span> count <span style="color: #339933;">+</span> grid<span style="color: #009900;">&#91;</span>j<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span>k<span style="color: #009900;">&#93;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>k <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> count <span style="color: #339933;">&lt;-</span> count <span style="color: #339933;">+</span> grid<span style="color: #009900;">&#91;</span>j<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> k<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>k <span style="color: #339933;">&lt;</span> size<span style="color: #009900;">&#41;</span> count <span style="color: #339933;">&lt;-</span> count <span style="color: #339933;">+</span> grid<span style="color: #009900;">&#91;</span>j<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> k<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>k <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> count <span style="color: #339933;">&lt;-</span> count <span style="color: #339933;">+</span> grid<span style="color: #009900;">&#91;</span>j<span style="color: #339933;">,</span> k<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>k <span style="color: #339933;">&lt;</span> size<span style="color: #009900;">&#41;</span> count <span style="color: #339933;">&lt;-</span> count <span style="color: #339933;">+</span> grid<span style="color: #009900;">&#91;</span>j<span style="color: #339933;">,</span> k<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span>
  count
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #339933;"># Creates a list of matrices, each of which is an iteration of the Game of Life.</span>
<span style="color: #339933;"># Arguments</span>
<span style="color: #339933;"># size: the edge length of the square</span>
<span style="color: #339933;"># prob: a vector (of length 2) that gives probability of death and life respectively for initial config</span>
<span style="color: #339933;"># returns a list of grids (matrices)</span>
game_of_life <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>size <span style="color: #339933;">=</span> <span style="color: #0000dd;">10</span><span style="color: #339933;">,</span> num_reps <span style="color: #339933;">=</span> <span style="color: #0000dd;">50</span><span style="color: #339933;">,</span> prob <span style="color: #339933;">=</span> c<span style="color: #009900;">&#40;</span><span style="color:#800080;">0.5</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.5</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  grid <span style="color: #339933;">&lt;-</span> list<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  grid<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;-</span> replicate<span style="color: #009900;">&#40;</span>size<span style="color: #339933;">,</span> sample<span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> size<span style="color: #339933;">,</span> replace <span style="color: #339933;">=</span> TRUE<span style="color: #339933;">,</span> prob <span style="color: #339933;">=</span> prob<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  dev_null <span style="color: #339933;">&lt;-</span> foreach<span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> seq_len<span style="color: #009900;">&#40;</span>num_reps<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span>do<span style="color: #339933;">%</span> <span style="color: #009900;">&#123;</span>
    grid<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;-</span> grid<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>i<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span>
    foreach<span style="color: #009900;">&#40;</span>j <span style="color: #339933;">=</span> seq_len<span style="color: #009900;">&#40;</span>size<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%:%</span>
      foreach<span style="color: #009900;">&#40;</span>k <span style="color: #339933;">=</span> seq_len<span style="color: #009900;">&#40;</span>size<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span>do<span style="color: #339933;">%</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #339933;"># Apply game rules.</span>
        num_neighbors <span style="color: #339933;">&lt;-</span> how_many_neighbors<span style="color: #009900;">&#40;</span>grid<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> j<span style="color: #339933;">,</span> k<span style="color: #009900;">&#41;</span>
        alive <span style="color: #339933;">&lt;-</span> grid<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #339933;">,</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">1</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>alive <span style="color: #339933;">&amp;&amp;</span> num_neighbors <span style="color: #339933;">&lt;=</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> grid<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #339933;">,</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;-</span> <span style="color: #0000dd;">0</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>alive <span style="color: #339933;">&amp;&amp;</span> num_neighbors <span style="color: #339933;">&gt;=</span> <span style="color: #0000dd;">4</span><span style="color: #009900;">&#41;</span> grid<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #339933;">,</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;-</span> <span style="color: #0000dd;">0</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>alive <span style="color: #339933;">&amp;&amp;</span> num_neighbors <span style="color: #339933;">==</span> <span style="color: #0000dd;">3</span><span style="color: #009900;">&#41;</span> grid<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #339933;">,</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;-</span> <span style="color: #0000dd;">1</span>
      <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  grid
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #339933;"># Converts the current grid (matrix) to a ggplot2 image</span>
grid_to_ggplot <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>grid<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #339933;"># Permutes the matrix so that melt labels this correctly.</span>
  grid <span style="color: #339933;">&lt;-</span> grid<span style="color: #009900;">&#91;</span>seq.<span style="color: #993333;">int</span><span style="color: #009900;">&#40;</span>nrow<span style="color: #009900;">&#40;</span>grid<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#93;</span>
  grid <span style="color: #339933;">&lt;-</span> melt<span style="color: #009900;">&#40;</span>grid<span style="color: #009900;">&#41;</span>
  grid$value <span style="color: #339933;">&lt;-</span> factor<span style="color: #009900;">&#40;</span>ifelse<span style="color: #009900;">&#40;</span>grid$value<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Alive&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Dead&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  p <span style="color: #339933;">&lt;-</span> ggplot<span style="color: #009900;">&#40;</span>grid<span style="color: #339933;">,</span> aes<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">=</span>X1<span style="color: #339933;">,</span> y<span style="color: #339933;">=</span>X2<span style="color: #339933;">,</span> z <span style="color: #339933;">=</span> value<span style="color: #339933;">,</span> color <span style="color: #339933;">=</span> value<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  p <span style="color: #339933;">&lt;-</span> p <span style="color: #339933;">+</span> geom_tile<span style="color: #009900;">&#40;</span>aes<span style="color: #009900;">&#40;</span>fill <span style="color: #339933;">=</span> value<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  p  <span style="color: #339933;">+</span> scale_fill_manual<span style="color: #009900;">&#40;</span>values <span style="color: #339933;">=</span> c<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Dead&quot;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;white&quot;</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Alive&quot;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;black&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>As an example, I have created a 20-by-20 grid with a 10% chance that its initial values will be alive. The simulation has 250 iterations. You may add more, but this takes long enough already.</p>

<div class="wp_codebox"><table><tr id="p1334"><td class="code" id="p133code4"><pre class="c" style="font-family:monospace;">game_grids <span style="color: #339933;">&lt;-</span> game_of_life<span style="color: #009900;">&#40;</span>size <span style="color: #339933;">=</span> <span style="color: #0000dd;">20</span><span style="color: #339933;">,</span> num_reps <span style="color: #339933;">=</span> <span style="color: #0000dd;">250</span><span style="color: #339933;">,</span> prob <span style="color: #339933;">=</span> c<span style="color: #009900;">&#40;</span><span style="color:#800080;">0.1</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.9</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
grid_ggplot <span style="color: #339933;">&lt;-</span> lapply<span style="color: #009900;">&#40;</span>game_grids<span style="color: #339933;">,</span> grid_to_ggplot<span style="color: #009900;">&#41;</span>
saveGIF<span style="color: #009900;">&#40;</span>lapply<span style="color: #009900;">&#40;</span>grid_ggplot<span style="color: #339933;">,</span> print<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> clean <span style="color: #339933;">=</span> TRUE<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p><a href="http://johnramey.net/blog/wp-content/uploads/2011/06/animation.gif"><img class="aligncenter size-full wp-image-139" title="animation" src="http://johnramey.net/blog/wp-content/uploads/2011/06/animation.gif" alt="" width="480" height="480" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://johnramey.net/blog/2011/06/05/conways-game-of-life-in-r-with-ggplot2-and-animation/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Converting a String to a Variable Name On-The-Fly and Vice-versa in R</title>
		<link>http://johnramey.net/blog/2010/12/28/converting-a-string-to-a-variable-name-on-the-fly-and-vice-versa-in-r/</link>
		<comments>http://johnramey.net/blog/2010/12/28/converting-a-string-to-a-variable-name-on-the-fly-and-vice-versa-in-r/#comments</comments>
		<pubDate>Wed, 29 Dec 2010 02:32:45 +0000</pubDate>
		<dc:creator>ramhiser</dc:creator>
				<category><![CDATA[r]]></category>

		<guid isPermaLink="false">http://johnramey.net/blog/?p=41</guid>
		<description><![CDATA[Recently, I had a professor ask me how to take a string and convert it to an R variable name on-the-fly. One possible way is: x &#60;- 42 eval&#40;parse&#40;text = &#34;x&#34;&#41;&#41; &#91;1&#93; 42 Now, suppose we want to go the other way. The trick is just as simple: x &#60;- 42 deparse&#40;substitute&#40;x&#41;&#41; &#91;1&#93; &#34;x&#34;]]></description>
			<content:encoded><![CDATA[<p>
Recently, I had a professor ask me how to take a string and convert it to an R variable name on-the-fly.  One possible way is:
</p>

<div class="wp_codebox"><table><tr id="p417"><td class="code" id="p41code7"><pre class="c" style="font-family:monospace;">x <span style="color: #339933;">&lt;-</span> <span style="color: #0000dd;">42</span>
eval<span style="color: #009900;">&#40;</span>parse<span style="color: #009900;">&#40;</span>text <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;x&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #0000dd;">42</span></pre></td></tr></table></div>

<p>
Now, suppose we want to go the other way.  The trick is just as simple:
</p>

<div class="wp_codebox"><table><tr id="p418"><td class="code" id="p41code8"><pre class="c" style="font-family:monospace;">x <span style="color: #339933;">&lt;-</span> <span style="color: #0000dd;">42</span>
deparse<span style="color: #009900;">&#40;</span>substitute<span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #ff0000;">&quot;x&quot;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://johnramey.net/blog/2010/12/28/converting-a-string-to-a-variable-name-on-the-fly-and-vice-versa-in-r/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Automatic Simulation Queueing in R</title>
		<link>http://johnramey.net/blog/2010/12/28/automatic-simulation-queueing-in-r/</link>
		<comments>http://johnramey.net/blog/2010/12/28/automatic-simulation-queueing-in-r/#comments</comments>
		<pubDate>Tue, 28 Dec 2010 17:15:23 +0000</pubDate>
		<dc:creator>ramhiser</dc:creator>
				<category><![CDATA[r]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[parallel]]></category>

		<guid isPermaLink="false">http://johnramey.net/?p=24</guid>
		<description><![CDATA[I spend much of my time writing R code for simulations to compare the supervised classification methods that I have developed with similar classifiers from the literature.  A large challenge is to determine which datasets (whether artificial/simulated or real) are interesting comparisons.  Even if we restricted ourselves to multivariate Gaussian data, there are a large [...]]]></description>
			<content:encoded><![CDATA[<p>
I spend much of my time writing R code for simulations to compare the supervised classification methods that I have developed with similar classifiers from the literature.  A large challenge is to determine which datasets (whether artificial/simulated or real) are interesting comparisons.  Even if we restricted ourselves to <a href="http://en.wikipedia.org/wiki/Multivariate_normal_distribution">multivariate Gaussian data</a>, there are a large number of <a href="http://en.wikipedia.org/wiki/Covariance_matrix">covariance matrix</a> configurations that we could use to simulate the data.  In other words, there are too many possibilities to consider all of them.  However, it is often desirable to consider as many as possible.
</p>
<p>
Parallel processing certainly has reduced the runtime for simulations.  In fact, most of my simulations are ridiculously parallelizeable, so I can run multiple simulations side-by-side.
</p>
<p>
I have been searching for ways to automate a lot of what I do, so I can spend less time on the mundane portions of simulation and focus on classification improvement.  As a first attempt, I have written some R code that generates a Bash script that can be queued on <a href="http://www.baylor.edu/lib/factech/index.php?id=39621">my university&#8217;s high-performance computer</a>.  The code to create a Bash script is create.shell.file(), which is given here:
</p>

<div class="wp_codebox"><table><tr id="p2414"><td class="code" id="p24code14"><pre class="c" style="font-family:monospace;"><span style="color: #339933;"># Arguments:</span>
<span style="color: #339933;">#	shell.file: The name of the shell file (usually ends in '.sh').</span>
<span style="color: #339933;">#	r.file: The name of the R file that contains the actual R simulation.</span>
<span style="color: #339933;">#	output.file: The name of the file where all output will be echoed.</span>
<span style="color: #339933;">#	r.options: The options used when R is called.</span>
<span style="color: #339933;">#	sim.args: The simulation arguments that will be passed to the R file.</span>
<span style="color: #339933;">#</span>
create.<span style="color: #202020;">shell</span>.<span style="color: #202020;">file</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>shell.<span style="color: #202020;">file</span><span style="color: #339933;">,</span> r.<span style="color: #202020;">file</span><span style="color: #339933;">,</span> output.<span style="color: #202020;">file</span><span style="color: #339933;">,</span> r.<span style="color: #202020;">options</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;--no-save --slave&quot;</span><span style="color: #339933;">,</span> sim.<span style="color: #202020;">args</span> <span style="color: #339933;">=</span> NULL<span style="color: #339933;">,</span> chmod <span style="color: #339933;">=</span> TRUE<span style="color: #339933;">,</span> chmod.<span style="color: #202020;">permissions</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;750&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	args.<span style="color: #993333;">string</span> <span style="color: #339933;">&lt;-</span> <span style="color: #ff0000;">''</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>is.<span style="color: #000000; font-weight: bold;">null</span><span style="color: #009900;">&#40;</span>sim.<span style="color: #202020;">args</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> args.<span style="color: #993333;">string</span> <span style="color: #339933;">&lt;-</span> paste<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'--args'</span><span style="color: #339933;">,</span> sim.<span style="color: #202020;">args</span><span style="color: #009900;">&#41;</span>
	r.<span style="color: #202020;">command</span> <span style="color: #339933;">&lt;-</span> paste<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'R'</span><span style="color: #339933;">,</span> r.<span style="color: #202020;">options</span><span style="color: #339933;">,</span> args.<span style="color: #993333;">string</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'&lt;'</span><span style="color: #339933;">,</span> r.<span style="color: #202020;">file</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'&gt;'</span><span style="color: #339933;">,</span> output.<span style="color: #202020;">file</span><span style="color: #009900;">&#41;</span>
	sink<span style="color: #009900;">&#40;</span>shell.<span style="color: #202020;">file</span><span style="color: #009900;">&#41;</span>
		cat<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'#!/bin/bash<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #009900;">&#41;</span>
		cat<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'#PBS -S /bin/bash<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #009900;">&#41;</span>
		cat<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'echo &quot;Starting R at `date`&quot;<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #009900;">&#41;</span>
		cat<span style="color: #009900;">&#40;</span>r.<span style="color: #202020;">command</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #009900;">&#41;</span>
		cat<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'echo &quot;R run completed at `date`&quot;<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #009900;">&#41;</span>
	sink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
&nbsp;
	<span style="color: #339933;"># If the chmod flag is TRUE, then we will chmod the created file to have the appropriate chmod.permissions.</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>chmod<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		chmod.<span style="color: #202020;">command</span> <span style="color: #339933;">&lt;-</span> paste<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;chmod&quot;</span><span style="color: #339933;">,</span> chmod.<span style="color: #202020;">permissions</span><span style="color: #339933;">,</span> shell.<span style="color: #202020;">file</span><span style="color: #009900;">&#41;</span>
		system<span style="color: #009900;">&#40;</span>chmod.<span style="color: #202020;">command</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>
To actually queue the simulation, we make a call to queue.sim():
</p>

<div class="wp_codebox"><table><tr id="p2415"><td class="code" id="p24code15"><pre class="c" style="font-family:monospace;"><span style="color: #339933;"># Arguments:</span>
<span style="color: #339933;">#	sim.config.df: a dataframe that contains the current simulation configuration.</span>
<span style="color: #339933;">#	sim.name: The name of the simulation. The queued sim will be prepended to the queue name.</span>
<span style="color: #339933;">#	np: The number of processors to use for this simulation.</span>
<span style="color: #339933;">#	npn: The number of processors to use per node for this simulation.</span>
<span style="color: #339933;">#	email: The email address that will be notified upon completion or an error.</span>
<span style="color: #339933;">#	cleanup: Delete all of the shell files after the simulations are queued?</span>
<span style="color: #339933;">#	verbose: Echo the status of the current task?</span>
<span style="color: #339933;">#</span>
queue.<span style="color: #202020;">sim</span> <span style="color: #339933;">&lt;-</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>sim.<span style="color: #202020;">config</span>.<span style="color: #202020;">df</span><span style="color: #339933;">,</span> sim.<span style="color: #202020;">type</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;rlda-duin&quot;</span><span style="color: #339933;">,</span> np <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> npn <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> email <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;johnramey@gmail.com&quot;</span><span style="color: #339933;">,</span> cleanup <span style="color: #339933;">=</span> FALSE<span style="color: #339933;">,</span> verbose <span style="color: #339933;">=</span> TRUE<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	sim.<span style="color: #202020;">config</span> <span style="color: #339933;">&lt;-</span> paste<span style="color: #009900;">&#40;</span>names<span style="color: #009900;">&#40;</span>sim.<span style="color: #202020;">config</span>.<span style="color: #202020;">df</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> sim.<span style="color: #202020;">config</span>.<span style="color: #202020;">df</span><span style="color: #339933;">,</span> collapse <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;-&quot;</span><span style="color: #339933;">,</span> sep <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>
	sim.<span style="color: #202020;">name</span> <span style="color: #339933;">&lt;-</span> paste<span style="color: #009900;">&#40;</span>sim.<span style="color: #202020;">type</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;-&quot;</span><span style="color: #339933;">,</span> sim.<span style="color: #202020;">config</span><span style="color: #339933;">,</span> sep <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>
	shell.<span style="color: #202020;">file</span> <span style="color: #339933;">&lt;-</span> paste<span style="color: #009900;">&#40;</span>sim.<span style="color: #202020;">name</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;.sh&quot;</span><span style="color: #339933;">,</span> sep <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>
	r.<span style="color: #202020;">file</span> <span style="color: #339933;">&lt;-</span> paste<span style="color: #009900;">&#40;</span>sim.<span style="color: #202020;">type</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'.r'</span><span style="color: #339933;">,</span> sep <span style="color: #339933;">=</span> <span style="color: #ff0000;">''</span><span style="color: #009900;">&#41;</span>
	out.<span style="color: #202020;">file</span> <span style="color: #339933;">&lt;-</span> paste<span style="color: #009900;">&#40;</span>sim.<span style="color: #202020;">name</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'.out'</span><span style="color: #339933;">,</span> sep <span style="color: #339933;">=</span> <span style="color: #ff0000;">''</span><span style="color: #009900;">&#41;</span>
	sim.<span style="color: #202020;">args</span> <span style="color: #339933;">&lt;-</span> paste<span style="color: #009900;">&#40;</span>sim.<span style="color: #202020;">config</span>.<span style="color: #202020;">df</span><span style="color: #339933;">,</span> collapse <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot; &quot;</span><span style="color: #009900;">&#41;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>verbose<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		cat<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;sim.config:&quot;</span><span style="color: #339933;">,</span> sim.<span style="color: #202020;">config</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>
		cat<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;sim.name:&quot;</span><span style="color: #339933;">,</span> sim.<span style="color: #202020;">name</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>
		cat<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;shell.file:&quot;</span><span style="color: #339933;">,</span> shell.<span style="color: #202020;">file</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>
		cat<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;r.file:&quot;</span><span style="color: #339933;">,</span> r.<span style="color: #202020;">file</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>
		cat<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;out.file:&quot;</span><span style="color: #339933;">,</span> out.<span style="color: #202020;">file</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>
		cat<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;sim.args:&quot;</span><span style="color: #339933;">,</span> sim.<span style="color: #202020;">args</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>verbose<span style="color: #009900;">&#41;</span> cat<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Creating shell file<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>
	create.<span style="color: #202020;">shell</span>.<span style="color: #202020;">file</span><span style="color: #009900;">&#40;</span>shell.<span style="color: #202020;">file</span><span style="color: #339933;">,</span> r.<span style="color: #202020;">file</span><span style="color: #339933;">,</span> out.<span style="color: #202020;">file</span><span style="color: #339933;">,</span> sim.<span style="color: #202020;">args</span> <span style="color: #339933;">=</span> sim.<span style="color: #202020;">args</span><span style="color: #009900;">&#41;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>verbose<span style="color: #009900;">&#41;</span> cat<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Creating shell file...done!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>
&nbsp;
	<span style="color: #339933;"># Example</span>
	<span style="color: #339933;"># scasub -np 8  -npn 8 -N &quot;rlda-prostate&quot; -m &quot;johnramey@gmail.com&quot; ./rlda-prostate.sh</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>verbose<span style="color: #009900;">&#41;</span> cat<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Queueing simulation<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>
	queue.<span style="color: #202020;">command</span> <span style="color: #339933;">&lt;-</span> paste<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;scasub -np &quot;</span><span style="color: #339933;">,</span> np<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot; -npn &quot;</span><span style="color: #339933;">,</span> npn<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot; -N '&quot;</span><span style="color: #339933;">,</span> sim.<span style="color: #202020;">name</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;' -m '&quot;</span><span style="color: #339933;">,</span> email<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;' ./&quot;</span><span style="color: #339933;">,</span> shell.<span style="color: #202020;">file</span><span style="color: #339933;">,</span> sep <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>verbose<span style="color: #009900;">&#41;</span> cat<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Queue command:<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #339933;">,</span> queue.<span style="color: #202020;">command</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>
	system<span style="color: #009900;">&#40;</span>queue.<span style="color: #202020;">command</span><span style="color: #009900;">&#41;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>verbose<span style="color: #009900;">&#41;</span> cat<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Queueing simulation...done!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>cleanup<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>verbose<span style="color: #009900;">&#41;</span> cat<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Cleaning up shell files<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>
		file.<span style="color: #202020;">remove</span><span style="color: #009900;">&#40;</span>shell.<span style="color: #202020;">file</span><span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>verbose<span style="color: #009900;">&#41;</span> cat<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Cleaning up shell files...done<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>
Let&#8217;s look at an example to see what is actually happening.  Suppose that we have a simulation file called &#8220;gaussian-sim.r&#8221; that generates N observations from two different p-dimensional Gaussian distributions each having the identity covariance matrix. Of course, this is a boring example, but it&#8217;s a start.  One interesting question that always arises is: &#8220;Does classification performance degrade for small values of N and (extremely) large values of p?&#8221;  We may wish to answer this question with a simulation study by looking at many values of N and many values of p and see if we can find a cutoff where classification performance declines. Let&#8217;s further suppose that for each configuration that we will repeat the experiment B times. (As a note, I&#8217;m not going to actually examine the gaussian-sim.r file or its contents here. I may return to this example later and extend it, but for now I&#8217;m going to focus on the automated queueing.)  We can queue the simulation for each of several configurations the following code:
</p>

<div class="wp_codebox"><table><tr id="p2416"><td class="code" id="p24code16"><pre class="c" style="font-family:monospace;">library<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'plyr'</span><span style="color: #009900;">&#41;</span>
sim.<span style="color: #202020;">type</span> <span style="color: #339933;">&lt;-</span> <span style="color: #ff0000;">&quot;gaussian-sim&quot;</span>
np <span style="color: #339933;">&lt;-</span> <span style="color: #0000dd;">8</span>
npn <span style="color: #339933;">&lt;-</span> <span style="color: #0000dd;">8</span>
verbose <span style="color: #339933;">&lt;-</span> TRUE
cleanup <span style="color: #339933;">&lt;-</span> TRUE
&nbsp;
N <span style="color: #339933;">&lt;-</span> seq.<span style="color: #993333;">int</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">10</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">50</span><span style="color: #339933;">,</span> by <span style="color: #339933;">=</span> <span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span>
p <span style="color: #339933;">&lt;-</span> seq.<span style="color: #993333;">int</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">250</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1000</span><span style="color: #339933;">,</span> by <span style="color: #339933;">=</span> <span style="color: #0000dd;">250</span><span style="color: #009900;">&#41;</span>
B <span style="color: #339933;">&lt;-</span> <span style="color: #0000dd;">1000</span>
&nbsp;
sim.<span style="color: #202020;">configurations</span> <span style="color: #339933;">&lt;-</span> expand.<span style="color: #202020;">grid</span><span style="color: #009900;">&#40;</span>N <span style="color: #339933;">=</span> N<span style="color: #339933;">,</span> p <span style="color: #339933;">=</span> p<span style="color: #339933;">,</span> B <span style="color: #339933;">=</span> B<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #339933;"># Queue a simulation for each simulation configuration</span>
d_ply<span style="color: #009900;">&#40;</span>sim.<span style="color: #202020;">configurations</span><span style="color: #339933;">,</span> .<span style="color: #009900;">&#40;</span>N<span style="color: #339933;">,</span> p<span style="color: #339933;">,</span> B<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> queue.<span style="color: #202020;">sim</span><span style="color: #339933;">,</span> sim.<span style="color: #202020;">type</span> <span style="color: #339933;">=</span> sim.<span style="color: #202020;">type</span><span style="color: #339933;">,</span> np <span style="color: #339933;">=</span> np<span style="color: #339933;">,</span> npn <span style="color: #339933;">=</span> npn<span style="color: #339933;">,</span> cleanup <span style="color: #339933;">=</span> cleanup<span style="color: #339933;">,</span> verbose <span style="color: #339933;">=</span> verbose<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>
This will create a Bash script with a descriptive name. For example, with the above code, a file called &#8220;gaussian-sim-N10-p1000-B1000.sh&#8221; is created.  Here are its contents:
</p>

<div class="wp_codebox"><table><tr id="p2417"><td class="code" id="p24code17"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#!/bin/bash</span>
<span style="color: #339933;">#PBS -S /bin/bash</span>
echo <span style="color: #ff0000;">&quot;Starting R at `date`&quot;</span>
R <span style="color: #339933;">--</span>no<span style="color: #339933;">-</span>save <span style="color: #339933;">--</span>slave <span style="color: #339933;">--</span>args <span style="color: #0000dd;">10</span> <span style="color: #0000dd;">1000</span> <span style="color: #0000dd;">1000</span> <span style="color: #339933;">&lt;</span> gaussian<span style="color: #339933;">-</span>sim.<span style="color: #202020;">r</span> <span style="color: #339933;">&gt;</span> gaussian<span style="color: #339933;">-</span>sim<span style="color: #339933;">-</span>N10<span style="color: #339933;">-</span>p1000<span style="color: #339933;">-</span>B1000.<span style="color: #202020;">out</span>
echo <span style="color: #ff0000;">&quot;R run completed at `date`&quot;</span></pre></td></tr></table></div>

<p>
A note about the shell file created.  The actual call to R can be customized, but this call has worked well for me. I certainly could call R in batch mode, but I never do without any specific reason. Perhaps one is more efficient than the other? I&#8217;m not sure about this.</p>
<p>Next, for each *.sh file created, the following command is executed to queue the R script using the above configuration.
</p>

<div class="wp_codebox"><table><tr id="p2418"><td class="code" id="p24code18"><pre class="c" style="font-family:monospace;">scasub <span style="color: #339933;">-</span>np <span style="color: #0000dd;">8</span> <span style="color: #339933;">-</span>npn <span style="color: #0000dd;">8</span> <span style="color: #339933;">-</span>N <span style="color: #ff0000;">'gaussian-sim-N10-p1000-B1000'</span> <span style="color: #339933;">-</span>m <span style="color: #ff0000;">'johnramey@gmail.com'</span> .<span style="color: #339933;">/</span>gaussian<span style="color: #339933;">-</span>sim<span style="color: #339933;">-</span>N50<span style="color: #339933;">-</span>p1000<span style="color: #339933;">-</span>B1000.<span style="color: #202020;">sh</span></pre></td></tr></table></div>

<p>
The scasub command is used for my university&#8217;s HPC.  I know that there are other systems out there, but you can always alter my code to suit your needs.  Of course, your R script needs to take advantage of the commandArgs() function in R to use the above code.</p>
]]></content:encoded>
			<wfw:commentRss>http://johnramey.net/blog/2010/12/28/automatic-simulation-queueing-in-r/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Autocorrelation Matrix in R</title>
		<link>http://johnramey.net/blog/2010/12/26/autocorrelation-matrix-in-r/</link>
		<comments>http://johnramey.net/blog/2010/12/26/autocorrelation-matrix-in-r/#comments</comments>
		<pubDate>Sun, 26 Dec 2010 04:55:09 +0000</pubDate>
		<dc:creator>ramhiser</dc:creator>
				<category><![CDATA[r]]></category>
		<category><![CDATA[statistics]]></category>
		<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://johnramey.net/blog/?p=12</guid>
		<description><![CDATA[I have been simulating a lot of data lately  with various covariance (correlation) structures, and one that I have been using is the autocorrelation (or autoregressive) structure, where there is a &#8220;lag&#8221; between variables. The matrix is a v-dimension matrix of the form $$\begin{bmatrix} 1 &#38; \rho &#38; \rho^2 &#38; \dots &#38; \rho^{v-1}\\ \rho &#38; [...]]]></description>
			<content:encoded><![CDATA[<p>I have been simulating a lot of data lately  with various covariance (correlation) structures, and one that I have been using is the autocorrelation (or autoregressive) structure, where there is a &#8220;lag&#8221; between variables. The matrix is a v-dimension matrix of the form</p>
<p>$$\begin{bmatrix} 1 &amp; \rho &amp; \rho^2 &amp; \dots &amp; \rho^{v-1}\\ \rho &amp; 1&amp; \ddots &amp; \dots &amp; \rho^{v-2}\\ \vdots &amp; \ddots &amp; \ddots &amp; \ddots &amp; \vdots\\ \rho^{v-2} &amp; \dots &amp; \ddots &amp; \ddots &amp; \rho\\ \rho^{v-1} &amp; \rho^{v-2} &amp; \dots &amp; \rho &amp; 1 \end{bmatrix}$$,</p>
<p>where \(\rho \in [-1, 1]\) is the lag. Notice that the lag decays to 0 as v increases.</p>
<p>My goal was to make the construction of such a matrix simple and easy in R.  The method that I used explored a function I have not used yet in R called &#8220;lower.tri&#8221; for the lower triangular part of the matrix.  The upper triangular part is referenced with &#8220;upper.tri.&#8221;</p>
<p>My code is as follows:</p>

<div class="wp_codebox"><table><tr id="p1221"><td class="code" id="p12code21"><pre class="c" style="font-family:monospace;">autocorr.<span style="color: #202020;">mat</span></pre></td></tr></table></div>

<p>I really liked it because I feel that it is simple, but then I found <a href="http://tolstoy.newcastle.edu.au/R/e2/help/07/05/16585.html">Professor Peter Dalgaard&#8217;s method</a>, which I have slightly modified. It is far better than mine, easy to understand, and slick. Oh so slick. Here it is:</p>

<div class="wp_codebox"><table><tr id="p1222"><td class="code" id="p12code22"><pre class="c" style="font-family:monospace;">autocorr.<span style="color: #202020;">mat</span></pre></td></tr></table></div>

<p>Professor Dalgaard&#8217;s method puts mine to shame. It is quite obvious how to do it once it is seen, but I certainly wasn&#8217;t thinking along those lines.</p>
]]></content:encoded>
			<wfw:commentRss>http://johnramey.net/blog/2010/12/26/autocorrelation-matrix-in-r/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

