<?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/"
	xmlns:media="http://search.yahoo.com/mrss"
>

<channel>
	<title>robfelty.org &#187; R</title>
	<atom:link href="http://robfelty.org/tag/r/feed" rel="self" type="application/rss+xml" />
	<link>http://robfelty.org</link>
	<description></description>
	<lastBuildDate>Sat, 05 Feb 2011 23:28:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Bash one-liners to the rescue</title>
		<link>http://robfelty.org/2008/07/15/bash-one-liners-to-the-rescue</link>
		<comments>http://robfelty.org/2008/07/15/bash-one-liners-to-the-rescue#comments</comments>
		<pubDate>Tue, 15 Jul 2008 13:52:14 +0000</pubDate>
		<dc:creator>robfelty</dc:creator>
				<category><![CDATA[general]]></category>
		<category><![CDATA[linguistics]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[sox]]></category>
		<category><![CDATA[wav]]></category>

		<guid isPermaLink="false">http://test.robfelty.org/?p=103</guid>
		<description><![CDATA[I recently find myself using handy bash one-liners more all the time. I think that this is where unix/linux can really start to shine. There are so many little programs that just do one thing, and one thing well. But &#8230; <a href="http://robfelty.org/2008/07/15/bash-one-liners-to-the-rescue">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I recently find myself using handy bash one-liners more all the time. I think that this is where unix/linux can really start to shine. There are so many little programs that just do one thing, and one thing well. But the ability to combine these together through pipes means you have extremely flexible and powerful tools at the ready. </p>
<p>I have been working on a new project at work to come up with some lists for testing speech recognition. We decided to use the TIMIT database, which contains recordings of many different sentences from many different speakers all around America. I first wrote a perl script to generate some basic stats on the sentences, like how many words were in each sentence, and what the word frequency for those words is. Then I wrote a perl script to randomly select some of the sentences, and create several different lists of sentences. Finally, I wrote an R script which took the original .wav files, and mixed in signal-dependent noise in one channel, so that we can vary the signal to noise ratio during presentation of the stimuli by adjusting the balance on our sound system. </p>
<p>Along the way, I ran into a couple problems with the original sound files. It turns out that 446 of the 6300 sound files were clipped, and highly distorted. I noticed this on my own in listening to a few of the files I had generated with R. I could have gone through all 6300 files manually, and removed the distorted ones, but that would have taken a long time. Instead,  I used the program <em>sox</em>, which is a low-level, powerful audio processing program. I first used the find command to find all .wav files in the directory I was interested in (including sub-directories), then I passed each file to sox, and told sox not to play the output , but instead just give me some stats (-n stat). After some testing with a few clipped, and non-clipped files, I realized that for clipped files, the output from sox ended with a line that said either &#8220;Try: blah blah&#8221;, or &#8220;Can&#8217;t determine type&#8221;. I then later discovered that there might still be clipped files, and these would have a maximum amplitude of 1 or minimum or -1. So I knew that any clipped file would produce this output. So I passed the results from sox to grep (notice I had to redirect STDERR to STDOUT 2>&#038;1), and then if the output contained a line starting with  &#8220;Try:&#8221;  or &#8220;Can&#8217;t&#8221;, then I moved that file the $file.clipped.</p>
<div class="codecolorer-container bash dawn" style="overflow:auto;white-space:nowrap;width:0"><div class="bash codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="kw1">for</span> <span class="kw2">file</span> <span class="kw1">in</span> <span class="sy0">`</span><span class="kw2">find</span> . <span class="re5">-name</span> <span class="st0">&quot;*.wav&quot;</span> -print<span class="sy0">`</span>; <span class="kw1">do</span> <br />
&nbsp; <span class="kw1">if</span> <span class="br0">&#91;</span><span class="br0">&#91;</span> <span class="sy0">`</span><span class="kw2">sox</span> <span class="re1">$file</span> <span class="re5">-n</span> <span class="kw2">stat</span> <span class="nu0">2</span><span class="sy0">&gt;&amp;</span><span class="nu0">1</span> <span class="sy0">|</span> <span class="kw2">grep</span> <span class="re5">-E</span> <span class="st0">&quot;^(Try:|Can't|(Minimum|Maximum) amplitude:\s+-?1\.00)&quot;</span><span class="sy0">`</span> <span class="br0">&#93;</span><span class="br0">&#93;</span>; <span class="kw1">then</span><br />
&nbsp; &nbsp; <span class="kw3">echo</span> <span class="st0">&quot;<span class="es2">$file</span> CLIPPED&quot;</span>; <br />
&nbsp; &nbsp; <span class="kw2">mv</span> <span class="re1">$file</span> <span class="re1">$file</span>.clipped; <br />
&nbsp; <span class="kw1">fi</span>; <br />
<span class="kw1">done</span></div></div>
<p>After doing this, I simply amended my perl script which randomly generated lists to make sure that the wav file actually existed. Clipped files now ended in .clipped, instead of .wav.</p>
<p>There was an additional problem I had previously discovered with these sound files. They seemed to have some non-standard headers in them, which meant that the R script I was using to add noise to them couldn&#8217;t read the files. However, passing the files through sox made the files readable by R. (Windows Media Player on a Windows box couldn&#8217;t read the files either.) I only wanted to process the files I was actually going to add noise to, so I used another handy little bash one-liner. This one cuts a column of the file which contains all the sentences I am going to use, and then for each filename, processes the file through sox, and outputs it to the destination directory of my choosing.</p>
<div class="codecolorer-container bash dawn" style="overflow:auto;white-space:nowrap;width:0"><div class="bash codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="kw1">for</span> <span class="kw2">file</span> <span class="kw1">in</span> <span class="sy0">`</span><span class="kw2">cut</span> <span class="re5">-f</span> <span class="nu0">18</span> <span class="re5">-d</span> $<span class="st_h">'\t'</span> timitLists2.txt<span class="sy0">`</span>; <br />
&nbsp; <span class="kw1">do</span> <span class="kw2">sox</span> <span class="re1">$file</span> ~<span class="sy0">/</span>R<span class="sy0">/</span>work<span class="sy0">/</span>timit<span class="sy0">/</span>clean<span class="sy0">/`</span><span class="kw2">basename</span> <span class="re1">$file</span><span class="sy0">`</span>; <br />
<span class="kw1">done</span></div></div>
<p>Note that I have expanded the code into one more line, but pretty much they are one-liners. I think technically a one-liner doesn&#8217;t involve successive commands, which the first example does, but the first command is just an echo, to make sure I know what it is doing. </p>
]]></content:encoded>
			<wfw:commentRss>http://robfelty.org/2008/07/15/bash-one-liners-to-the-rescue/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Working on making fancy graphs with R / fixed versus random babble</title>
		<link>http://robfelty.org/2008/04/03/working-on-making-fancy-graphs-with-r-fixed-versus-random-babble</link>
		<comments>http://robfelty.org/2008/04/03/working-on-making-fancy-graphs-with-r-fixed-versus-random-babble#comments</comments>
		<pubDate>Fri, 04 Apr 2008 02:26:10 +0000</pubDate>
		<dc:creator>robfelty</dc:creator>
				<category><![CDATA[linguistics]]></category>
		<category><![CDATA[babble]]></category>
		<category><![CDATA[bar chart]]></category>
		<category><![CDATA[figures]]></category>
		<category><![CDATA[graphs]]></category>
		<category><![CDATA[noise]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[S-plus]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[speech science]]></category>
		<category><![CDATA[spoken word recognition]]></category>
		<category><![CDATA[statistics]]></category>

		<guid isPermaLink="false">http://test.robfelty.org/?p=87</guid>
		<description><![CDATA[I have been working on learning R for several months now, and continue to get better at it and enjoy it more all the time. I am currently working on a spoken word recognition project at work. The task we &#8230; <a href="http://robfelty.org/2008/04/03/working-on-making-fancy-graphs-with-r-fixed-versus-random-babble">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I have been working on learning R for several months now, and continue to get better at it and enjoy it more all the time. I am currently working on a spoken word recognition project at work. The task we are using is quite simple. Participants listen to words that have been mixed with multi-talker babble (kind of like background conversation at a cocktail party), and type in what they hear. We are analyzing the errors they make to try to discover what sorts of words are activated in the brain, and how people organize the words they know in their brains. </p>
<p>We started running subjects in the summer of 2007, and after running 48 subjects, we made a few methodological changes. One change is that we switched from using the same segment of babble as background noise to using a random segment of babble. We also re-leveled the sound files after adding the babble so that all the words were presented at about the same volume. I recently analyzed the data from these two groups. It turns out the the results from the first 48 subjects performed significantly better than subjects 49-96. However, I realized that these are different subjects, so it could be due to the different subjects, and not the change in methods. Therefore I split each group into 2 subgroups. Now I had 4 groups &#8212; 1-24, 24-48, 49-72, and 73-96. The subgroups did not differ statistically from one another, but the main groups did. This seemed to indicate that listeners were becoming habituated to the noise, and could therefore tune it out a bit better, and thus hear the words better. </p>
<p>I originally displayed this in a table, but it was pretty cumbersome, so I decided to make a bar chart instead. I have seen some people produce bar charts such as these where they indicate which groups are statistically different using brackets to group bars, and an asterisk to mark them as statistically different. I also wanted to add error bars in. Adam Krawitz (another post-doc at IU) gave me some code to get started on the error bars, and I found some code from a post to the R-help list about making brackets. First here is the figure:<br />
<a href='http://test.robfelty.org/wp-content/uploads/2008/04/48v96.png'><img src="http://test.robfelty.org/wp-content/uploads/2008/04/48v96-300x209.png" alt="R plot using the Brack function with error bars and statistics displayed" title="R plot using the Brack function with error bars and statistics displayed" width="300" height="209" class="aligncenter size-medium wp-image-89" /></a><br />
And here is the code:</p>
<div class="codecolorer-container r dawn" style="overflow:auto;white-space:nowrap;width:0"><div class="r codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="co0"># make a nice bar plot</span><br />
<span class="co0"># first we find the mean of each group</span><br />
bardata <span class="sy0">&lt;</span>- c<span class="br0">&#40;</span>mean<span class="br0">&#40;</span>items1to24<span class="re1">$pc</span>,na.rm=T<span class="br0">&#41;</span>,<br />
&nbsp; mean<span class="br0">&#40;</span>items25to48<span class="re1">$pc</span>,na.rm=T<span class="br0">&#41;</span>,<br />
&nbsp; mean<span class="br0">&#40;</span>items49to72<span class="re1">$pc</span>,na.rm=T<span class="br0">&#41;</span>,<br />
&nbsp; mean<span class="br0">&#40;</span>items73to96<span class="re1">$pc</span>,na.rm=T<span class="br0">&#41;</span><br />
<span class="br0">&#41;</span><br />
<br />
<span class="co0">#Then we do t-tests to calculate the 95% confidence intervals for our error bars</span><br />
<span class="re2">items1to24T</span>=t.test<span class="br0">&#40;</span>items1to24<span class="re1">$pc</span><span class="br0">&#41;</span><br />
<span class="re2">items25to48T</span>=t.test<span class="br0">&#40;</span>items25to48<span class="re1">$pc</span><span class="br0">&#41;</span><br />
<span class="re2">items49to72T</span>=t.test<span class="br0">&#40;</span>items49to72<span class="re1">$pc</span><span class="br0">&#41;</span><br />
<span class="re2">items73to96T</span>=t.test<span class="br0">&#40;</span>items73to96<span class="re1">$pc</span><span class="br0">&#41;</span><br />
<span class="re2">lowerConf</span>=c<span class="br0">&#40;</span>items1to24T<span class="re1">$conf</span>.int<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>,<br />
&nbsp; items25to48T<span class="re1">$conf</span>.int<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>,<br />
&nbsp; items49to72T<span class="re1">$conf</span>.int<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>,<br />
&nbsp; items73to96T<span class="re1">$conf</span>.int<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><br />
<span class="br0">&#41;</span><br />
<span class="re2">upperConf</span>=c<span class="br0">&#40;</span>items1to24T<span class="re1">$conf</span>.int<span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span>,<br />
&nbsp; items25to48T<span class="re1">$conf</span>.int<span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span>,<br />
&nbsp; items49to72T<span class="re1">$conf</span>.int<span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span>,<br />
&nbsp; items73to96T<span class="re1">$conf</span>.int<span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span><br />
<span class="br0">&#41;</span><br />
<br />
<span class="co0"># we set up some nice colors</span><br />
<span class="re2">barcolors</span>=c<span class="br0">&#40;</span><span class="st_h">'#FF3333'</span>,<span class="st_h">'#FF3333'</span>,<span class="st_h">'#4444FF'</span>,<span class="st_h">'#4444FF'</span><span class="br0">&#41;</span><br />
bar <span class="sy0">&lt;</span>- barplot<span class="br0">&#40;</span>bardata, ylim = c<span class="br0">&#40;</span><span class="nu0">0</span>,<span class="nu0">1</span><span class="br0">&#41;</span>, <span class="re2"><span class="kw2">col</span></span>=barcolors, names.arg=c<span class="br0">&#40;</span><span class="st0">&quot;1-24&quot;</span>,<span class="st0">&quot;25-48&quot;</span>,<span class="st0">&quot;49-72&quot;</span>, <span class="st0">&quot;73-96&quot;</span><span class="br0">&#41;</span>,<span class="re2">ylab</span>=<span class="st_h">'proportion correct'</span>, <span class="re2">xlab</span>=<span class="st_h">'subject group'</span>,<span class="re2">xlim</span>=c<span class="br0">&#40;</span><span class="nu0">0</span>,<span class="nu0">1</span><span class="br0">&#41;</span>, <span class="re2">width</span>=.24,<span class="re2">tck</span>=.04<span class="br0">&#41;</span><br />
<span class="co0">#we use the arrows function to do the error bars</span><br />
arrows<span class="br0">&#40;</span>bar, upperConf, bar, lowerConf,<br />
&nbsp; &nbsp; &nbsp; &nbsp;length = <span class="nu0">0.05</span>, <span class="co0"># width of the arrowhead</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;angle = <span class="nu0">90</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp;code = <span class="nu0">3</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#41;</span><br />
<br />
<br />
<span class="co0"># function to draw curly braces in red</span><br />
<span class="co0"># x1...y2 are the ends of the brace</span><br />
<span class="co0"># for upside down braces, x1 &gt; x2 and y1 &gt; y2</span><br />
<span class="co0"># taken from R help</span><br />
<span class="co0"># http://finzi.psych.upenn.edu/R/Rhelp02a/archive/112010.html</span><br />
library<span class="br0">&#40;</span>grid<span class="br0">&#41;</span> <span class="co0">#requires the grid library</span><br />
Brack <span class="sy0">&lt;</span>- <span class="kw1">function</span><span class="br0">&#40;</span>x1,y1,x2,y2,h<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
x2 <span class="sy0">&lt;</span>- x2-x1; y2 <span class="sy0">&lt;</span>- y2-y1<br />
v1 <span class="sy0">&lt;</span>- viewport<span class="br0">&#40;</span><span class="re2">x</span>=x1,<span class="re2">y</span>=y1,<span class="re2">width</span>=sqrt<span class="br0">&#40;</span>x2^<span class="nu0">2</span>+y2^<span class="nu0">2</span><span class="br0">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re2">height</span>=h,<span class="re2">angle</span>=<span class="nu0">180</span><span class="sy0">*</span>atan2<span class="br0">&#40;</span>y2,x2<span class="br0">&#41;</span><span class="sy0">/</span>pi,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="re2">just</span>=c<span class="br0">&#40;</span><span class="st0">&quot;left&quot;</span>,<span class="st0">&quot;bottom&quot;</span><span class="br0">&#41;</span>,<span class="re2">gp</span>=gpar<span class="br0">&#40;</span><span class="re2"><span class="kw2">col</span></span>=<span class="st0">&quot;black&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
pushViewport<span class="br0">&#40;</span>v1<span class="br0">&#41;</span><br />
grid.curve<span class="br0">&#40;</span><span class="re2">x2</span>=<span class="nu0">0</span>,<span class="re2">y2</span>=<span class="nu0">0</span>,<span class="re2">x1</span>=.125,<span class="re2">y1</span>=.5,<span class="re2">curvature</span>=.5<span class="br0">&#41;</span><br />
grid.move.to<span class="br0">&#40;</span>.125,.5<span class="br0">&#41;</span><br />
grid.line.to<span class="br0">&#40;</span>.375,.5<span class="br0">&#41;</span><br />
grid.curve<span class="br0">&#40;</span><span class="re2">x1</span>=.375,<span class="re2">y1</span>=.5,<span class="re2">x2</span>=.5,<span class="re2">y2</span>=<span class="nu0">1</span>,<span class="re2">curvature</span>=.5<span class="br0">&#41;</span><br />
grid.curve<span class="br0">&#40;</span><span class="re2">x2</span>=<span class="nu0">1</span>,<span class="re2">y2</span>=<span class="nu0">0</span>,<span class="re2">x1</span>=.875,<span class="re2">y1</span>=.5,<span class="re2">curvature</span>=-.5<span class="br0">&#41;</span><br />
grid.move.to<span class="br0">&#40;</span>.875,.5<span class="br0">&#41;</span><br />
grid.line.to<span class="br0">&#40;</span>.625,.5<span class="br0">&#41;</span><br />
grid.curve<span class="br0">&#40;</span><span class="re2">x2</span>=.625,<span class="re2">y2</span>=.5,<span class="re2">x1</span>=.5,<span class="re2">y1</span>=<span class="nu0">1</span>,<span class="re2">curvature</span>=.5<span class="br0">&#41;</span><br />
popViewport<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#125;</span><br />
<br />
<span class="co0"># Now we use the brack function </span><br />
<span class="co0"># brackY1 and brack Y2 determine the y-value for the placement of the brackets. </span><br />
<span class="co0"># I had to fudge this a bit to get it to look right</span><br />
<span class="re2">brackY1</span>=<span class="nu0">1.05</span><span class="sy0">*</span>max<span class="br0">&#40;</span>upperConf<span class="br0">&#91;</span><span class="nu0">1</span>:<span class="nu0">2</span><span class="br0">&#93;</span><span class="br0">&#41;</span><br />
<span class="re2">brackY2</span>=<span class="nu0">1.21</span><span class="sy0">*</span>max<span class="br0">&#40;</span>upperConf<span class="br0">&#91;</span><span class="nu0">3</span>:<span class="nu0">4</span><span class="br0">&#93;</span><span class="br0">&#41;</span><br />
Brack<span class="br0">&#40;</span>bar<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>+.07,brackY1,bar<span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span>-.01,brackY1,.07<span class="br0">&#41;</span><br />
Brack<span class="br0">&#40;</span>bar<span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span>-.09,brackY2,bar<span class="br0">&#91;</span><span class="nu0">4</span><span class="br0">&#93;</span>-.17,brackY2,.11<span class="br0">&#41;</span><br />
<br />
<span class="co0"># And now we also make some straight line groupings</span><br />
arrows<span class="br0">&#40;</span>mean<span class="br0">&#40;</span>bar<span class="br0">&#91;</span><span class="nu0">1</span>:<span class="nu0">2</span><span class="br0">&#93;</span><span class="br0">&#41;</span>,<span class="nu0">1.4</span><span class="sy0">*</span>upperConf<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>,mean<span class="br0">&#40;</span>bar<span class="br0">&#91;</span><span class="nu0">3</span>:<span class="nu0">4</span><span class="br0">&#93;</span><span class="br0">&#41;</span>, <span class="nu0">1.4</span><span class="sy0">*</span>upperConf<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>,<span class="re2">length</span>=<span class="nu0">0</span><span class="br0">&#41;</span><br />
arrows<span class="br0">&#40;</span>mean<span class="br0">&#40;</span>bar<span class="br0">&#91;</span><span class="nu0">1</span>:<span class="nu0">2</span><span class="br0">&#93;</span><span class="br0">&#41;</span>,<span class="nu0">1.4</span><span class="sy0">*</span>upperConf<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>,mean<span class="br0">&#40;</span>bar<span class="br0">&#91;</span><span class="nu0">1</span>:<span class="nu0">2</span><span class="br0">&#93;</span><span class="br0">&#41;</span>, <span class="nu0">1.35</span><span class="sy0">*</span>upperConf<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>,<span class="re2">length</span>=<span class="nu0">0</span><span class="br0">&#41;</span><br />
arrows<span class="br0">&#40;</span>mean<span class="br0">&#40;</span>bar<span class="br0">&#91;</span><span class="nu0">3</span>:<span class="nu0">4</span><span class="br0">&#93;</span><span class="br0">&#41;</span>,<span class="nu0">1.4</span><span class="sy0">*</span>upperConf<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>,mean<span class="br0">&#40;</span>bar<span class="br0">&#91;</span><span class="nu0">3</span>:<span class="nu0">4</span><span class="br0">&#93;</span><span class="br0">&#41;</span>, <span class="nu0">1.35</span><span class="sy0">*</span>upperConf<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>,<span class="re2">length</span>=<span class="nu0">0</span><span class="br0">&#41;</span><br />
arrows<span class="br0">&#40;</span>mean<span class="br0">&#40;</span>bar<span class="br0">&#91;</span><span class="nu0">2</span>:<span class="nu0">3</span><span class="br0">&#93;</span><span class="br0">&#41;</span>,<span class="nu0">1.4</span><span class="sy0">*</span>upperConf<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>,mean<span class="br0">&#40;</span>bar<span class="br0">&#91;</span><span class="nu0">2</span>:<span class="nu0">3</span><span class="br0">&#93;</span><span class="br0">&#41;</span>, <span class="nu0">1.45</span><span class="sy0">*</span>upperConf<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>,<span class="re2">length</span>=<span class="nu0">0</span><span class="br0">&#41;</span><br />
<span class="co0"># we label the groupings</span><br />
text<span class="br0">&#40;</span>mean<span class="br0">&#40;</span>bar<span class="br0">&#91;</span><span class="nu0">1</span>:<span class="nu0">2</span><span class="br0">&#93;</span><span class="br0">&#41;</span>,<span class="nu0">1.25</span><span class="sy0">*</span>upperConf<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>,<span class="st0">&quot;fixed babble&quot;</span>,<span class="re2">cex</span>=.9,<span class="re2"><span class="kw2">col</span></span>=barcolors<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#41;</span><br />
text<span class="br0">&#40;</span>mean<span class="br0">&#40;</span>bar<span class="br0">&#91;</span><span class="nu0">3</span>:<span class="nu0">4</span><span class="br0">&#93;</span><span class="br0">&#41;</span>,<span class="nu0">1.25</span><span class="sy0">*</span>upperConf<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>,<span class="st0">&quot;random babble&quot;</span>,<span class="re2">cex</span>=.9,<span class="re2"><span class="kw2">col</span></span>=barcolors<span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span><span class="br0">&#41;</span><br />
<br />
<span class="co0"># we add the stats in</span><br />
text<span class="br0">&#40;</span>mean<span class="br0">&#40;</span>bar<span class="br0">&#91;</span><span class="nu0">2</span>:<span class="nu0">3</span><span class="br0">&#93;</span><span class="br0">&#41;</span>,<span class="nu0">1.55</span><span class="sy0">*</span>upperConf<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>,paste<span class="br0">&#40;</span><span class="st0">&quot;t =&quot;</span>, formatC<span class="br0">&#40;</span>itemBetweenT<span class="re1">$statistic</span>,<span class="re2">digits</span>=<span class="nu0">3</span><span class="br0">&#41;</span>, <span class="st0">&quot;, p&quot;</span>, prettyPval<span class="br0">&#40;</span>itemBetweenT<span class="re1">$p</span>.value<span class="br0">&#41;</span><span class="br0">&#41;</span>,<span class="re2">cex</span>=.9<span class="br0">&#41;</span><br />
<br />
<span class="co0"># and we add the values on the bars as well</span><br />
text<span class="br0">&#40;</span>bar<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>,.7<span class="sy0">*</span>bardata<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>,formatC<span class="br0">&#40;</span>bardata<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>,<span class="re2">digits</span>=<span class="nu0">3</span><span class="br0">&#41;</span>,<span class="re2"><span class="kw2">col</span></span>=<span class="st_h">'white'</span><span class="br0">&#41;</span><br />
text<span class="br0">&#40;</span>bar<span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span>,.7<span class="sy0">*</span>bardata<span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span>,formatC<span class="br0">&#40;</span>bardata<span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span>,<span class="re2">digits</span>=<span class="nu0">3</span><span class="br0">&#41;</span>,<span class="re2"><span class="kw2">col</span></span>=<span class="st_h">'white'</span><span class="br0">&#41;</span><br />
text<span class="br0">&#40;</span>bar<span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span>,.7<span class="sy0">*</span>bardata<span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span>,formatC<span class="br0">&#40;</span>bardata<span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span>,<span class="re2">digits</span>=<span class="nu0">3</span><span class="br0">&#41;</span>,<span class="re2"><span class="kw2">col</span></span>=<span class="st_h">'white'</span><span class="br0">&#41;</span><br />
text<span class="br0">&#40;</span>bar<span class="br0">&#91;</span><span class="nu0">4</span><span class="br0">&#93;</span>,.7<span class="sy0">*</span>bardata<span class="br0">&#91;</span><span class="nu0">4</span><span class="br0">&#93;</span>,formatC<span class="br0">&#40;</span>bardata<span class="br0">&#91;</span><span class="nu0">4</span><span class="br0">&#93;</span>,<span class="re2">digits</span>=<span class="nu0">3</span><span class="br0">&#41;</span>,<span class="re2"><span class="kw2">col</span></span>=<span class="st_h">'white'</span><span class="br0">&#41;</span><br />
<br />
<span class="co0"># and then I print it to a pdf file</span><br />
dev.print<span class="br0">&#40;</span>pdf,<span class="st0">&quot;fig/48v96.pdf&quot;</span>,<span class="re2">height</span>=<span class="nu0">2.8</span>,<span class="re2">width</span>=<span class="nu0">4</span>,<span class="re2">pointsize</span>=<span class="nu0">11</span><span class="br0">&#41;</span></div></div>
<p>While these results were suggestive of a difference, it is also possible that the difference was due to the re-leveling, and not the fixed vs. random babble. If the listeners were really habituating to the fixed babble, this should show up as an improvement over the course of the experiment. Therefore I also compared the learning rates of these two groups. It is normal for participants to get better as they get more familiar with a task, so we will always expect some learning. However if the participants are becoming habituated to a particular aspect of the stimuli, (in this case the fact that the same segment of babble is being used over and over again), then we would expect more learning in this group. This figure displays the percent correct over a moving 30 trial window. That is, the first point represents trials 1-30, the second point 2-31 and so on. It looks like the fixed-babble group is learning more. To test this statistically, I subtracted the fixed babble values from the random babble values, and performed a correlation on these values against the trial window. If the learning rates are the same, then there should be no correlation. If the fixed babble group is learning more rapidly however, we should see the difference between the two groups increase over the course of the experiment, which is what we found. Here is the nice figure:<br />
<a href='http://test.robfelty.org/wp-content/uploads/2008/04/learning.png'><img src="http://test.robfelty.org/wp-content/uploads/2008/04/learning-300x209.png" alt="R figure with multiple y axes" title="R figure with multiple y axes" width="300" height="209" class="aligncenter size-medium wp-image-90" /></a><br />
And here is the code:</p>
<div class="codecolorer-container r dawn" style="overflow:auto;white-space:nowrap;width:0"><div class="r codecolorer" style="font-family:Monaco,Lucida Console,monospace"><span class="co0">#compute the percent correct for each window</span><br />
<span class="re2">numPoints</span>=min<span class="br0">&#40;</span>length<span class="br0">&#40;</span>unique<span class="br0">&#40;</span>Trials1<span class="re1">$Trial</span><span class="br0">&#41;</span><span class="br0">&#41;</span>,length<span class="br0">&#40;</span>unique<span class="br0">&#40;</span>Trials1<span class="re1">$Trial</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>-trialWindow<br />
&nbsp; <span class="re2">pc1</span>=c<span class="br0">&#40;</span><span class="nu0">1</span>:numPoints<span class="br0">&#41;</span><br />
&nbsp; <span class="re2">pc2</span>=c<span class="br0">&#40;</span><span class="nu0">1</span>:numPoints<span class="br0">&#41;</span><br />
&nbsp; <span class="re2">xvalues</span>=c<span class="br0">&#40;</span><span class="nu0">1</span>:numPoints<span class="br0">&#41;</span><br />
&nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span>i <span class="kw1">in</span> <span class="nu0">0</span>:numPoints<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="re2">upperTrials1</span>=subset<span class="br0">&#40;</span>Trials1,Trials1<span class="re1">$Trial</span><span class="sy0">&gt;</span>i<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="re2">theseTrials1</span>=subset<span class="br0">&#40;</span>upperTrials1,upperTrials1<span class="re1">$Trial</span><span class="sy0">&lt;</span>=i+trialWindow<span class="br0">&#41;</span><br />
&nbsp; &nbsp; corr1 = <span class="br0">&#40;</span>nrow<span class="br0">&#40;</span>subset<span class="br0">&#40;</span>theseTrials1, theseTrials1<span class="re1">$correct</span>==<span class="st0">&quot;CORRECT&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; wrong1 = <span class="br0">&#40;</span>nrow<span class="br0">&#40;</span>subset<span class="br0">&#40;</span>theseTrials1,theseTrials1<span class="re1">$correct</span>==<span class="st0">&quot;WRONG&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; missing1 = <span class="br0">&#40;</span>nrow<span class="br0">&#40;</span>subset<span class="br0">&#40;</span>theseTrials1,theseTrials1<span class="re1">$correct</span>==<span class="st0">&quot;MISSING&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; nonword1 = <span class="br0">&#40;</span>nrow<span class="br0">&#40;</span>subset<span class="br0">&#40;</span>theseTrials1,theseTrials1<span class="re1">$correct</span>==<span class="st0">&quot;NONWORD&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; pc1<span class="br0">&#91;</span>i<span class="br0">&#93;</span> = corr1<span class="sy0">/</span><span class="br0">&#40;</span>corr1+wrong1+nonword1+missing1<span class="br0">&#41;</span><br />
<br />
&nbsp; &nbsp; <span class="re2">upperTrials2</span>=subset<span class="br0">&#40;</span>Trials2,Trials2<span class="re1">$Trial</span><span class="sy0">&gt;</span>i<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="re2">theseTrials2</span>=subset<span class="br0">&#40;</span>upperTrials2,upperTrials2<span class="re1">$Trial</span><span class="sy0">&lt;</span>=i+trialWindow<span class="br0">&#41;</span><br />
&nbsp; &nbsp; corr2 = <span class="br0">&#40;</span>nrow<span class="br0">&#40;</span>subset<span class="br0">&#40;</span>theseTrials2, theseTrials2<span class="re1">$correct</span>==<span class="st0">&quot;CORRECT&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; wrong2 = <span class="br0">&#40;</span>nrow<span class="br0">&#40;</span>subset<span class="br0">&#40;</span>theseTrials2,theseTrials2<span class="re1">$correct</span>==<span class="st0">&quot;WRONG&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; missing2 = <span class="br0">&#40;</span>nrow<span class="br0">&#40;</span>subset<span class="br0">&#40;</span>theseTrials2,theseTrials2<span class="re1">$correct</span>==<span class="st0">&quot;MISSING&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; nonword2 = <span class="br0">&#40;</span>nrow<span class="br0">&#40;</span>subset<span class="br0">&#40;</span>theseTrials2,theseTrials2<span class="re1">$correct</span>==<span class="st0">&quot;NONWORD&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; pc2<span class="br0">&#91;</span>i<span class="br0">&#93;</span> = corr2<span class="sy0">/</span><span class="br0">&#40;</span>corr2+wrong2+nonword2+missing2<span class="br0">&#41;</span><br />
<br />
&nbsp; &nbsp; xvalues<span class="br0">&#91;</span>i<span class="br0">&#93;</span>=i;<br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="co0"># compute the differences between the two groups</span><br />
<span class="re2">diffs</span>=pc1-pc2<br />
<br />
<span class="co0">#set up some graphical paramets</span><br />
&nbsp; &nbsp; par<span class="br0">&#40;</span><span class="re2">mar</span>=c<span class="br0">&#40;</span><span class="nu0">3</span>,<span class="nu0">3</span>,<span class="nu0">0.3</span>,<span class="nu0">3</span><span class="br0">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re2">fin</span>=c<span class="br0">&#40;</span><span class="nu0">4.1</span>,<span class="nu0">2.9</span><span class="br0">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re2">fig</span>=c<span class="br0">&#40;</span><span class="nu0">0</span>,<span class="nu0">1</span>,<span class="nu0">0</span>,<span class="nu0">1</span><span class="br0">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re2">mgp</span>=c<span class="br0">&#40;</span><span class="nu0">2</span>,<span class="nu0">1</span>,<span class="nu0">0</span><span class="br0">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co0">#usr=c(min(xdata),max(xdata),0,1),</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re2">lab</span>=c<span class="br0">&#40;</span><span class="nu0">10</span>,<span class="nu0">10</span>,<span class="nu0">3</span><span class="br0">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co0">#ps=11,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re2">family</span>=<span class="st_h">'serif'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#41;</span><br />
<span class="co0"># plot the first group</span><br />
plot<span class="br0">&#40;</span>xvalues,pc1,<span class="re2"><span class="kw2">col</span></span>=<span class="st_h">'#EE0000'</span>,<span class="re2">pch</span>=<span class="st0">&quot;+&quot;</span>,<span class="re2">ylim</span>=c<span class="br0">&#40;</span>.4,.65<span class="br0">&#41;</span>,<span class="re2">axes</span>=F,<span class="re2">xlab</span>=<span class="st_h">'trial window'</span>, <span class="re2">ylab</span>=<span class="st_h">'proportion correct'</span>,<span class="re2">cex</span>=.8<span class="br0">&#41;</span><br />
<span class="co0"># add the second group</span><br />
points<span class="br0">&#40;</span>xvalues,pc2,<span class="re2"><span class="kw2">col</span></span>=<span class="st_h">'#0000EE'</span>,<span class="re2">pch</span>=<span class="st0">&quot;x&quot;</span>,<span class="re2">ylim</span>=c<span class="br0">&#40;</span>.4,.65<span class="br0">&#41;</span>,<span class="re2">cex</span>=.8<span class="br0">&#41;</span><br />
<br />
<span class="co0"># how much to add to the fitline y values to make it fit on the plot</span><br />
<span class="re2">fudge</span>=.42<br />
<span class="co0"># plot the slope of the line we fitted to the differences</span><br />
<span class="re2">fitline</span>=lsfit<span class="br0">&#40;</span>xvalues,diffs+fudge<span class="br0">&#41;</span><br />
abline<span class="br0">&#40;</span>fitline,<span class="re2"><span class="kw2">col</span></span>=<span class="st_h">'#000F00'</span><span class="br0">&#41;</span><br />
<span class="co0"># do a correlation test</span><br />
<span class="re2">slope</span>=cor.test<span class="br0">&#40;</span>xvalues,diffs<span class="br0">&#41;</span>;<br />
<span class="co0"># the stats to put on the graph</span><br />
<span class="re2">stats</span>=paste<span class="br0">&#40;</span><span class="st_h">'r= '</span>, formatC<span class="br0">&#40;</span>slope<span class="re1">$estimate</span>,<span class="re2">digits</span>=<span class="nu0">3</span><span class="br0">&#41;</span>, <span class="st0">&quot;, p &quot;</span>, prettyPval<span class="br0">&#40;</span>slope<span class="re1">$p</span>.value<span class="br0">&#41;</span>,<span class="re2">sep</span>=<span class="st_h">''</span><span class="br0">&#41;</span><br />
<br />
<span class="co0"># put the values on the axes</span><br />
axis<span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span> <span class="co0">#bottom</span><br />
<span class="re2">axis2values</span>=c<span class="br0">&#40;</span>.4,.45,.5,.55,.6,.65<span class="br0">&#41;</span><br />
axis<span class="br0">&#40;</span><span class="nu0">2</span>,<span class="re2">at</span>=axis2values<span class="br0">&#41;</span> <span class="co0">#left</span><br />
axis<span class="br0">&#40;</span><span class="nu0">4</span>,<span class="re2">at</span>=axis2values,<span class="re2">lab</span>=formatC<span class="br0">&#40;</span>axis2values-fudge,<span class="re2">digits</span>=<span class="nu0">2</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="co0">#right</span><br />
box<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="co0">#draw a box around the figure</span><br />
<br />
<span class="co0"># put the stats on the graph</span><br />
text<span class="br0">&#40;</span><span class="nu0">280</span>,.53,stats,<span class="re2">cex</span>=.75<span class="br0">&#41;</span><br />
<br />
<span class="co0">#label the right axis</span><br />
mtext<span class="br0">&#40;</span><span class="st0">&quot;difference between groups&quot;</span>, <span class="re2">side</span>=<span class="nu0">4</span>,<span class="re2">line</span>=<span class="nu0">2</span><span class="br0">&#41;</span><br />
<br />
<span class="co0"># label the points</span><br />
text<span class="br0">&#40;</span><span class="nu0">80</span>,.64,<span class="st0">&quot;fixed babble&quot;</span>,<span class="re2"><span class="kw2">col</span></span>=<span class="st_h">'#EE0000'</span>, <span class="re2">cex</span>=.8<span class="br0">&#41;</span><br />
text<span class="br0">&#40;</span><span class="nu0">275</span>,.44,<span class="st0">&quot;random babble&quot;</span>,<span class="re2"><span class="kw2">col</span></span>=<span class="st_h">'#0000EE'</span>, <span class="re2">cex</span>=.8<span class="br0">&#41;</span><br />
<span class="co0"># print as pdf</span><br />
dev.print<span class="br0">&#40;</span>pdf,<span class="st0">&quot;fig/learning.pdf&quot;</span>,<span class="re2">height</span>=<span class="nu0">2.8</span>,<span class="re2">width</span>=<span class="nu0">4</span>,<span class="re2">pointsize</span>=<span class="nu0">11</span><span class="br0">&#41;</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://robfelty.org/2008/04/03/working-on-making-fancy-graphs-with-r-fixed-versus-random-babble/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:thumbnail url="http://test.robfelty.org/wp-content/uploads/2008/04/48v96-300x209.png" />
		<media:content url="http://test.robfelty.org/wp-content/uploads/2008/04/48v96-300x209.png" medium="image">
			<media:title type="html">R plot using the Brack function with error bars and statistics displayed</media:title>
		</media:content>

		<media:thumbnail url="http://test.robfelty.org/wp-content/uploads/2008/04/learning-300x209.png" />
		<media:content url="http://test.robfelty.org/wp-content/uploads/2008/04/learning-300x209.png" medium="image">
			<media:title type="html">R figure with multiple y axes</media:title>
		</media:content>
	</item>
		<item>
		<title>vi key bindings in R</title>
		<link>http://robfelty.org/2007/11/28/vi-key-bindings-in-r</link>
		<comments>http://robfelty.org/2007/11/28/vi-key-bindings-in-r#comments</comments>
		<pubDate>Wed, 28 Nov 2007 22:11:40 +0000</pubDate>
		<dc:creator>robfelty</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[key bindings]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[statistics]]></category>
		<category><![CDATA[vi]]></category>

		<guid isPermaLink="false">http://test.robfelty.org/2007/11/28/vi-key-bindings-in-r/</guid>
		<description><![CDATA[I am working on learning R. For those that don&#8217;t know, R is a programming language and program a bit similar to Matlab, but is particularly designed for statistics. It is free and open source, and pretty fast and flexible. &#8230; <a href="http://robfelty.org/2007/11/28/vi-key-bindings-in-r">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I am working on learning <a href='http://www.r-project.org/'>R</a>. For those that don&#8217;t know, R is a programming language and program a bit similar to Matlab, but is particularly designed for statistics. It is free and open source, and pretty fast and flexible. It has a pretty nice library interface, with lots of user contributed libraries at <a href='http://cran.r-project.org'>CRAN</a> (The comprehensive R archive network &#8212; modeled after CTAN and CPAN (Tex and Perl respectively)). </p>
<p>On *nix systems, R uses the readline library to give a command history. So I can simply type the up arrow, and find the last command I used. I just discovered that it can also search the command history in the same way as in my BASH shell. Since I like <em>vi</em>, I use <em>vi</em> key bindings in bash. I was pleasantly surprised when R automatically used these as well. I am not sure if it gets them from my .bashrc file or my .inputrc file, since I have vi keymaps set in both.<br />
.bashrc:</p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;width:0"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">set -o vi</div></div>
<p>.inputrc</p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;width:0"><div class="text codecolorer" style="font-family:Monaco,Lucida Console,monospace">set editing-mode vi<br />
set keymap vi</div></div>
<p>So when I am using R, I can search the history for &#8220;foo&#8221; by typing:<br />
&lt;esc&gt; /foo</p>
<p>and then use &#8216;n&#8217; and &#8216;N&#8217; to go to the next or previous match.</p>
<p>What fun!!</p>
]]></content:encoded>
			<wfw:commentRss>http://robfelty.org/2007/11/28/vi-key-bindings-in-r/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

