<?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:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>4D Pie Charts</title>
	<atom:link href="http://4dpiecharts.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://4dpiecharts.com</link>
	<description>Scientific computing, data viz and general geekery, with examples in R and MATLAB.</description>
	<lastBuildDate>Wed, 30 May 2012 15:47:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='4dpiecharts.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>4D Pie Charts</title>
		<link>http://4dpiecharts.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://4dpiecharts.com/osd.xml" title="4D Pie Charts" />
	<atom:link rel='hub' href='http://4dpiecharts.com/?pushpress=hub'/>
		<item>
		<title>Be assertive!</title>
		<link>http://4dpiecharts.com/2012/05/30/be-assertive/</link>
		<comments>http://4dpiecharts.com/2012/05/30/be-assertive/#comments</comments>
		<pubDate>Wed, 30 May 2012 12:14:38 +0000</pubDate>
		<dc:creator>richierocks</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[assertive]]></category>
		<category><![CDATA[packages]]></category>
		<category><![CDATA[r]]></category>
		<category><![CDATA[robust-code]]></category>
		<category><![CDATA[stats]]></category>

		<guid isPermaLink="false">http://4dpiecharts.com/?p=480</guid>
		<description><![CDATA[assertive, my new package for writing robust code, is now on CRAN. It consists of lots of is functions for checking variables, and corresponding assert functions that throw an error if the condition doesn&#8217;t hold. For example, is_a_number checks that the input is numeric and scalar. In the last two cases, the return value of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=4dpiecharts.com&#038;blog=15320431&#038;post=480&#038;subd=4dpiecharts&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://4dpiecharts.files.wordpress.com/2012/05/assert_package_is_awesome.png"><img src="http://4dpiecharts.files.wordpress.com/2012/05/assert_package_is_awesome.png?w=600" alt="assert_package_is_awesome(&quot;assertive&quot;) returns TRUE." title="This function doesn&#039;t exist, but if it did, it would return TRUE."   class="aligncenter size-full wp-image-481" /></a></p>
<p>assertive, my new package for writing robust code, is now on CRAN.  It consists of lots of <code>is</code> functions for checking variables, and corresponding <code>assert</code> functions that throw an error if the condition doesn&#8217;t hold.  For example, <code>is_a_number</code> checks that the input is numeric and scalar.</p>
<p><pre class="brush: r;">
is_a_number(1)     #TRUE
is_a_number(&quot;a&quot;)   #FALSE
is_a_number(1:10)  #FALSE
</pre></p>
<p>In the last two cases, the return value of FALSE has an attribute &#8220;<code>cause</code>&#8221; that indicates the cause of failure. When &#8220;a&#8221; is the input, the cause is &#8220;<code>"a" is not of type 'numeric'.</code>&#8220;, whereas for <code>1:10</code>, the cause is &#8220;<code>1:10 does not have length one.</code>&#8220;.  You can get or set the cause attribute with the <code>cause</code> function.</p>
<p><pre class="brush: r;">
m &lt;- lm(uptake ~ 1, CO2)
ok &lt;- is_empty_model(m)
if(!ok) cause(ok)
</pre></p>
<p>The <code>assert</code> functions call an <code>is</code> function, and if the result is FALSE, they throw an error; otherwise they do nothing.</p>
<p><pre class="brush: r;">
assert_is_a_number(1)   #OK
assert_is_a_number(&quot;a&quot;) #Throws an error
</pre></p>
<p>There are also some <code>has</code> functions, primarily for checking the presence of attributes.</p>
<p><pre class="brush: r;">
has_names(c(foo = 1, bar = 4, baz = 9))
has_dims(matrix(1:12, nrow = 3))
</pre></p>
<p>Some functions apply to properties of vectors.  In this case, the <code>assert</code> functions can check that all the values conform to the condition, or any of the values conform.</p>
<p><pre class="brush: r;">
x &lt;- -2:2
is_positive(x)              #The last two are TRUE
assert_any_are_positive(x)  #OK
assert_all_are_positive(x)  #Error
</pre></p>
<p>&#8220;Why would you want to use these functions?&#8221;, you may be asking.  The dynamic typing and extreme flexibility of R means that it is very easy to have variables that are the wrong format.  This is particularly true when you are dealing with user input.  So while you know that the sales totals passed to your function should be a vector of non-negative numbers, or that the regular expression should be a single string rather than a character vector, your user may not.  You need to check for these invalid conditions, and return an error message that the user can understand.  assertive makes it easy to do all this. </p>
<p>Since this is the first public release of assertive, it hasn&#8217;t been widely tested.  I&#8217;ve written a moderately comprehensive unit-test suite, but there are likely to be a few minor bugs here and there.  In particular, I suspect there may be one or two typos in the documentation.  Please give the package a try, and let me know if you find any errors, or if you want any other functions adding.</p>
<br /> Tagged: <a href='http://4dpiecharts.com/tag/assertive/'>assertive</a>, <a href='http://4dpiecharts.com/tag/packages/'>packages</a>, <a href='http://4dpiecharts.com/tag/r/'>r</a>, <a href='http://4dpiecharts.com/tag/robust-code/'>robust-code</a>, <a href='http://4dpiecharts.com/tag/stats/'>stats</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/4dpiecharts.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/4dpiecharts.wordpress.com/480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/4dpiecharts.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/4dpiecharts.wordpress.com/480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/4dpiecharts.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/4dpiecharts.wordpress.com/480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/4dpiecharts.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/4dpiecharts.wordpress.com/480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/4dpiecharts.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/4dpiecharts.wordpress.com/480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/4dpiecharts.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/4dpiecharts.wordpress.com/480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/4dpiecharts.wordpress.com/480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/4dpiecharts.wordpress.com/480/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=4dpiecharts.com&#038;blog=15320431&#038;post=480&#038;subd=4dpiecharts&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://4dpiecharts.com/2012/05/30/be-assertive/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/85c1a01d3843ecaa30abd003b65811a8?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">richierocks</media:title>
		</media:content>

		<media:content url="http://4dpiecharts.files.wordpress.com/2012/05/assert_package_is_awesome.png" medium="image">
			<media:title type="html">This function doesn&#039;t exist, but if it did, it would return TRUE.</media:title>
		</media:content>
	</item>
		<item>
		<title>Benford&#8217;s Law and fraud in the Russian election</title>
		<link>http://4dpiecharts.com/2012/03/05/benfords-law-and-fraud-in-the-russian-election/</link>
		<comments>http://4dpiecharts.com/2012/03/05/benfords-law-and-fraud-in-the-russian-election/#comments</comments>
		<pubDate>Mon, 05 Mar 2012 23:18:45 +0000</pubDate>
		<dc:creator>richierocks</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[Benford's Law]]></category>
		<category><![CDATA[election]]></category>
		<category><![CDATA[forensic statistics]]></category>
		<category><![CDATA[goldacre]]></category>
		<category><![CDATA[r]]></category>
		<category><![CDATA[russia]]></category>

		<guid isPermaLink="false">http://4dpiecharts.com/?p=467</guid>
		<description><![CDATA[Earlier today Ben Goldacre posted about using Benford&#8217;s Law to try and detect fraud in the Russian elections. Read that now, or the rest of this post won&#8217;t make sense. This is a loose R translation of Ben&#8217;s Stata code. The data is held in a Google doc. While it is possible to directly retrieve [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=4dpiecharts.com&#038;blog=15320431&#038;post=467&#038;subd=4dpiecharts&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Earlier today Ben Goldacre <a href="http://www.badscience.net/2012/03/is-there-statistical-evidence-of-fraud-in-the-russian-election-data/">posted</a> about using Benford&#8217;s Law to try and detect fraud in the Russian elections.  Read that now, or the rest of this post won&#8217;t make sense.  This is a loose R translation of Ben&#8217;s Stata code.</p>
<p>The data is held in a <a href="https://docs.google.com/spreadsheet/ccc?key=0Aj-gvVaugJowdDRkUnM4S2FjOUJjTVphM1djam9VOUE#gid=0">Google doc</a>.  While it is possible to directly retrieve the contents with R, for a single document it is easier to save it a CSV, and load it from your own machine.</p>
<p><pre class="brush: r;">
russian &lt;- read.csv(&quot;Russian observed results - FullData.csv&quot;)
</pre></p>
<p>There are loads of ways of manipulating data and plotting it in R, and while you can do everything in the base R distribution, I&#8217;m going to use a few packages to make it easier.</p>
<p><pre class="brush: r;">
library(reshape)
library(stringr)
library(ggplot2)
</pre></p>
<p>A little transformation is needed.  We take only the columns containing the counts and manipulate the data into a &#8220;long&#8221; format with only one value per row.</p>
<p><pre class="brush: r;">
russian &lt;- melt(
    russian[, c(&quot;Zhirinovsky&quot;, &quot;Zyuganov&quot;, &quot;Mironov&quot;, &quot;Prokhorov&quot;, &quot;Putin&quot;)], 
    variable_name = &quot;candidate&quot;
)
</pre></p>
<p>Now we add columns containing the first and last digits, extracted using regular expressions.</p>
<p><pre class="brush: r;">
russian &lt;- ddply(
    russian, 
    .(candidate), 
    transform, 
    first.digit = str_extract(value, &quot;[123456789]&quot;),
    last.digit  = str_extract(value, &quot;[[:digit:]]$&quot;))
</pre></p>
<p>The table function gives us the counts of each number, and we compare these against the counts predicted by Benford&#8217;s Law.</p>
<p><pre class="brush: r;">
first_digit_counts &lt;- as.vector(table(russian$first.digit))
first_digit_actual_vs_expected &lt;- data.frame(
  digit            = 1:9,
  actual.count     = first_digit_counts,    
  actual.fraction  = first_digit_counts / nrow(russian),
  benford.fraction = log10(1 + 1 / (1:9))
)
</pre></p>
<p>The counts of the last digit can be obtained in a similar way.</p>
<p><pre class="brush: r;">
last_digit_counts &lt;- as.vector(table(russian$last.digit))
last_digit_actual_vs_expected &lt;- data.frame(
    digit     = 0:9,
    count     = last_digit_counts,    
    fraction  = last_digit_counts / nrow(russian)
)
last_digit_actual_vs_expected$cumulative.fraction &lt;- cumsum(last_digit_actual_vs_expected$fraction)
</pre></p>
<p>Here is the line graph&#8230;</p>
<p><pre class="brush: r;">
a_vs_e &lt;- melt(first_digit_actual_vs_expected[, c(&quot;digit&quot;, &quot;actual.fraction&quot;, &quot;benford.fraction&quot;)], id.var = &quot;digit&quot;)
(fig1_lines &lt;- ggplot(a_vs_e, aes(digit, value, colour = variable)) +
    geom_line() +
    scale_x_continuous(breaks = 1:9) +
    scale_y_continuous(formatter = &quot;percent&quot;) +
    ylab(&quot;Counts with this first digit&quot;) +
    opts(legend.position = &quot;none&quot;)
)
</pre></p>
<p><a href="http://4dpiecharts.files.wordpress.com/2012/03/fig_1_actual_vs_benford1.png"><img src="http://4dpiecharts.files.wordpress.com/2012/03/fig_1_actual_vs_benford1.png?w=600&h=600" alt="Fig 1. Actual percentages of first digits vs. those predicted by Benford&#039;s Law" title="Fig 1. Actual percentages of first digits vs. those predicted by Benford&#039;s Law" width="600" height="600" class="aligncenter size-full wp-image-470" /></a></p>
<p>and the histogram</p>
<p><pre class="brush: r;">
(fig2_hist &lt;- ggplot(russian, aes(value)) +
    geom_histogram(binwidth = 20)
)
</pre></p>
<p><a href="http://4dpiecharts.files.wordpress.com/2012/03/fig_2_vote_counts.png"><img src="http://4dpiecharts.files.wordpress.com/2012/03/fig_2_vote_counts.png?w=600&h=600" alt="Fig 2. Histogram of vote counts in the Russian election" title="Fig 2. Histogram of vote counts in the Russian election" width="600" height="600" class="aligncenter size-full wp-image-469" /></a></p>
<br /> Tagged: <a href='http://4dpiecharts.com/tag/benfords-law/'>Benford's Law</a>, <a href='http://4dpiecharts.com/tag/election/'>election</a>, <a href='http://4dpiecharts.com/tag/forensic-statistics/'>forensic statistics</a>, <a href='http://4dpiecharts.com/tag/goldacre/'>goldacre</a>, <a href='http://4dpiecharts.com/tag/r/'>r</a>, <a href='http://4dpiecharts.com/tag/russia/'>russia</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/4dpiecharts.wordpress.com/467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/4dpiecharts.wordpress.com/467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/4dpiecharts.wordpress.com/467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/4dpiecharts.wordpress.com/467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/4dpiecharts.wordpress.com/467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/4dpiecharts.wordpress.com/467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/4dpiecharts.wordpress.com/467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/4dpiecharts.wordpress.com/467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/4dpiecharts.wordpress.com/467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/4dpiecharts.wordpress.com/467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/4dpiecharts.wordpress.com/467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/4dpiecharts.wordpress.com/467/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/4dpiecharts.wordpress.com/467/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/4dpiecharts.wordpress.com/467/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=4dpiecharts.com&#038;blog=15320431&#038;post=467&#038;subd=4dpiecharts&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://4dpiecharts.com/2012/03/05/benfords-law-and-fraud-in-the-russian-election/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/85c1a01d3843ecaa30abd003b65811a8?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">richierocks</media:title>
		</media:content>

		<media:content url="http://4dpiecharts.files.wordpress.com/2012/03/fig_1_actual_vs_benford1.png" medium="image">
			<media:title type="html">Fig 1. Actual percentages of first digits vs. those predicted by Benford&#039;s Law</media:title>
		</media:content>

		<media:content url="http://4dpiecharts.files.wordpress.com/2012/03/fig_2_vote_counts.png" medium="image">
			<media:title type="html">Fig 2. Histogram of vote counts in the Russian election</media:title>
		</media:content>
	</item>
		<item>
		<title>Radical Statistics was radical</title>
		<link>http://4dpiecharts.com/2012/02/25/radical-statistics-was-radical/</link>
		<comments>http://4dpiecharts.com/2012/02/25/radical-statistics-was-radical/#comments</comments>
		<pubDate>Sat, 25 Feb 2012 09:34:10 +0000</pubDate>
		<dc:creator>richierocks</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[conference]]></category>
		<category><![CDATA[RadStats]]></category>
		<category><![CDATA[stats]]></category>

		<guid isPermaLink="false">https://4dpiecharts.wordpress.com/?p=465</guid>
		<description><![CDATA[Today I went to the Radical Statistics conference in London. RadStats was originally a sort of left wing revolutionary group for statisticians, but these days the emphasis is on exposing dubious statistics by companies and politicians. Here&#8217;s a quick rundown of the day. First up Roy Carr-Hill spoke about the problems with trying to collect [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=4dpiecharts.com&#038;blog=15320431&#038;post=465&#038;subd=4dpiecharts&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Today I went to the Radical Statistics conference in London. RadStats was originally a sort of left wing revolutionary group for statisticians, but these days the emphasis is on exposing dubious statistics by companies and politicians.</p>
<p>Here&#8217;s a quick rundown of the day.</p>
<p>First up Roy Carr-Hill spoke about the problems with trying to collect demographic data and estimating soft measures of societal progress like wellbeing. (Household surveys exclude people not in households, like the homeless soldiers and old people in care homes; and English people claim to be 70% satisfied regardless of the question.)</p>
<p>Next was Val Saunders who started with a useful debunking of done methodological flaws in schizophrenia research, then blew it by detailing her own methodologically flaws research and making overly strong claims to have found the cause of that disease.</p>
<p>Aubrey Blunsohn and David Healy both talked about ways that the pharmaceutical industry fudges results. The list was impressively long, leading me to suspect that far to many people have spent far too long thinking of ways to game the system. The two main recommendations that resonated with me were to extend the trials register to phase 1 trials to avoid unfavourable studies being buried and for raw data to be made available for transparent analysis. Pipe dreams.</p>
<p>After lunch Prem Sikka pointed out that tax avoidance isn&#8217;t just shady companies trying to scam the system, but actually accountancy firms pay people to dream up new wheezes and sell them to those companies.</p>
<p>Ann Pettifor and final speaker Howard Reed had similar talks evangelising Keynesian stimulus (roughly, big government spending in times of recession) for the UK economy amongst some economic myth debunking. Thought provoking, though both speakers neglected to mention the limitations of such stimuli &#8211; you have to avoid spending in pork barrel nonsense (see Japan in the 90s, that buy-a-banger scheme in the UK in 2009) and you have to find a ways to turn of the taps w when recession is over.</p>
<p>The other speaker was Allyson Pollack who discussed debunking a dubious study by Zac Cooper claiming that patients being allowed to choose their surgeon improved success rated treating acute myocardial infarction. Such patients are generally unconscious while having their heart attack so out was inevitably nonsense.</p>
<p>Overall a great day.</p>
<br /> Tagged: <a href='http://4dpiecharts.com/tag/conference/'>conference</a>, <a href='http://4dpiecharts.com/tag/radstats/'>RadStats</a>, <a href='http://4dpiecharts.com/tag/stats/'>stats</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/4dpiecharts.wordpress.com/465/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/4dpiecharts.wordpress.com/465/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/4dpiecharts.wordpress.com/465/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/4dpiecharts.wordpress.com/465/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/4dpiecharts.wordpress.com/465/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/4dpiecharts.wordpress.com/465/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/4dpiecharts.wordpress.com/465/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/4dpiecharts.wordpress.com/465/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/4dpiecharts.wordpress.com/465/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/4dpiecharts.wordpress.com/465/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/4dpiecharts.wordpress.com/465/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/4dpiecharts.wordpress.com/465/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/4dpiecharts.wordpress.com/465/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/4dpiecharts.wordpress.com/465/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=4dpiecharts.com&#038;blog=15320431&#038;post=465&#038;subd=4dpiecharts&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://4dpiecharts.com/2012/02/25/radical-statistics-was-radical/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/85c1a01d3843ecaa30abd003b65811a8?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">richierocks</media:title>
		</media:content>
	</item>
		<item>
		<title>GUI building in R: gWidgets vs Deducer</title>
		<link>http://4dpiecharts.com/2012/02/20/gui-building-in-r-gwidgets-vs-deducer/</link>
		<comments>http://4dpiecharts.com/2012/02/20/gui-building-in-r-gwidgets-vs-deducer/#comments</comments>
		<pubDate>Mon, 20 Feb 2012 22:47:45 +0000</pubDate>
		<dc:creator>richierocks</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[comparison]]></category>
		<category><![CDATA[Deducer]]></category>
		<category><![CDATA[gui]]></category>
		<category><![CDATA[gWidgets]]></category>
		<category><![CDATA[r]]></category>
		<category><![CDATA[software-development]]></category>

		<guid isPermaLink="false">http://4dpiecharts.com/?p=458</guid>
		<description><![CDATA[I&#8217;ve been a user (and fan) of gWidgets for a couple of years now for GUI building in R. (See my introduction to it here.) However, it&#8217;s always good to check out the competition so I&#8217;ve been playing around with Deducer to see how they compare. R can access a number of GUI building frameworks [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=4dpiecharts.com&#038;blog=15320431&#038;post=458&#038;subd=4dpiecharts&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been a user (and fan) of <a href="http://cran.r-project.org/web/packages/gWidgets/index.html" target="_blank">gWidgets</a> for a couple of years now for GUI building in R.  (See my introduction to it <a href="http://4dpiecharts.com/2010/10/06/creating-guis-in-r-with-gwidgets/" target="_blank">here</a>.)  However, it&#8217;s always good to check out the competition so I&#8217;ve been playing around with <a href="http://cran.r-project.org/web/packages/Deducer/index.html" target="_blank">Deducer</a> to see how they compare.</p>
<p>R can access a number of GUI building frameworks including tcltk, GTK, qt, and Java, not to mention HTML.  gWidgets&#8217; big selling point is that is provides a high-level wrapper to all the R wrappers for each framework, so you can write code in a toolkit independent way.  Switching between tcltk and GTK and qt won&#8217;t often be that useful, but if you think you might want to move from a desktop based GUI to a web app, it makes the transition easier.  By contrast, Deducer based upon the rJava, and provides access to the Java Swing framework.  It&#8217;s a slightly lower level library (which means you have to write more lines of code to achieve the same thing), but since you get full access to Swing, it&#8217;s a little more flexible. Deducer also has some features to integrate your GUIs with <a href="http://rforge.net/JGR/" target="_blank">JGR</a>, so if you use that for running R, it&#8217;s perhaps the most natural choice.</p>
<p>To test the two frameworks, I wrote a small GUI for running the Kolmogorov-Smirnoff test (that one of the ones for checking whether or not a variable seems to have been sampled from a particular distribution).  Take a look at the code below to see the comparison.  (Regular reader may notice I&#8217;ve switched from my usual <code>under_casing</code> to <code>camelCasing</code>.  Both the frameworks use this style, so I thought I&#8217;d follow suit for cleanliness.) </p>
<p>First, here are some common variables (labels and the like).</p>
<p><pre class="brush: r;">
#Some sample data to test against
x1 &lt;- rnorm(100)
x2 &lt;- runif(100)

#Widget labels
labelX &lt;- &quot;Variable name for data: &quot;
labelY &lt;- &quot;Distribution to compare to: &quot;
labelAlternative &lt;- &quot;One or two sided test?: &quot;
labelP &lt;- &quot;The p-value is: &quot;

#Choices for comboboxes
choicesAlternative &lt;- eval(formals(ks.test)$alternative)
distributions &lt;- c(
    normal = pnorm, 
    exponential = pexp,
    F = pf,
    &quot;log-normal&quot; = plnorm,
    &quot;Student's t&quot; = pt,
    uniform = punif
)
</pre></p>
<p>This is the gWidgets GUI</p>
<p><pre class="brush: r;">
createKsTestGwidgets &lt;- function()
{
  library(gWidgetstcltk)
  options(guiToolkit = &quot;tcltk&quot;)
  win &lt;- gwindow(&quot;KS Test, gWidgets edition&quot;, visible = FALSE)
  
  frmX &lt;- gframe(&quot;x&quot;, container = win)
  lblX &lt;- glabel(labelX, container = frmX)
  txtX &lt;- gedit(container = frmX)
  
  frmY &lt;- gframe(&quot;y&quot;, container = win)
  lblY &lt;- glabel(labelY, container = frmY)
  cmbY &lt;- gcombobox(names(distributions), container = frmY)
  
  frmAlternative &lt;- gframe(&quot;alternative&quot;, container = win)
  lblAlternative &lt;- glabel(labelAlternative, container = frmAlternative)
  cmbAlternative &lt;- gcombobox(choicesAlternative, container = frmAlternative)
  
  btnCalc &lt;- gbutton(&quot;Calculate&quot;, container = win,
      handler = function(h, ...)
      {
        x &lt;- get(svalue(txtX), mode = &quot;numeric&quot;)
        y &lt;- distributions[[svalue(cmbY)]]
        alternative &lt;- svalue(cmbAlternative)
        ans &lt;- ks.test(x, y, alternative = alternative)
        svalue(txtP) &lt;- format(ans$p.value, digits = 3)
      }
  )
  frmResults &lt;- gframe(&quot;results&quot;, container = win)
  lblP &lt;- glabel(labelP, container = frmResults)
  txtP &lt;- gedit(container = frmResults)
  visible(win) &lt;- TRUE
  
}

createKsTestGwidgets()
</pre></p>
<p>&#8230;and here&#8217;s the Deducer equivalent.</p>
<p><pre class="brush: r;">
createKsTestDeducer &lt;- function()
{
  library(Deducer)
  win &lt;- new(RDialog)
  win$setSize(300L, 500L)
  win$setTitle(&quot;KS TEST, Deducer edition&quot;)
  
  JLabel &lt;- J(&quot;javax.swing.JLabel&quot;)
  lblX &lt;- new(JLabel, labelX)
  addComponent(win, lblX, 1, 1000, 50, 1, rightType = &quot;REL&quot;)
  txtX &lt;- new(TextAreaWidget, &quot;x&quot;)
  addComponent(win, txtX, 51, 1000, 150, 1, rightType = &quot;REL&quot;)
  
  lblY &lt;- new(JLabel, labelY)
  addComponent(win, lblY, 151, 1000, 200, 1, rightType = &quot;REL&quot;)
  
  cmbY &lt;- new(ComboBoxWidget, names(distributions))
  cmbY$setDefaultModel(names(distributions)[1])
  addComponent(win, cmbY, 201, 1000, 300, 1, rightType = &quot;REL&quot;)
  
  lblAlternative &lt;- new(JLabel, labelAlternative)
  addComponent(win, lblAlternative, 301, 1000, 400, 1, rightType = &quot;REL&quot;)
  
  cmbAlternative &lt;- new(ComboBoxWidget, choicesAlternative)
  cmbAlternative$setDefaultModel(choicesAlternative[1])
  addComponent(win, cmbAlternative, 401, 1000, 500, 1, rightType = &quot;REL&quot;)
  
  JButton &lt;- J(&quot;javax.swing.JButton&quot;)
  btnCalc &lt;- new(JButton, &quot;Calculate&quot;)
  addComponent(win, btnCalc, 501, 1000, 601, 1, rightType = &quot;REL&quot;)
  ActionListener &lt;- J(&quot;org.rosuda.deducer.widgets.event.RActionListener&quot;)
  listener &lt;- new(ActionListener)
  calculationHandler &lt;- function(cmd, ActionEvent)
  {
    x &lt;- get(txtX$getText())
    y &lt;- distributions[[cmbY$getModel()]]
    alternative &lt;- cmbAlternative$getModel()
    ans &lt;- ks.test(x, y, alternative = alternative)
    print(ans)
    txtP$setText(format(ans$p.value, digits = 3))
  }
  listener$setFunction(toJava(calculationHandler))
  btnCalc$addActionListener(listener)
  
  lblP &lt;- new(JLabel, labelP)
  addComponent(win, lblP, 601, 1000, 650, 1, rightType = &quot;REL&quot;)
  
  txtP &lt;- new(TextAreaWidget, &quot;results&quot;)
  addComponent(win, txtP, 651, 1000, 750, 1, rightType = &quot;REL&quot;)
    
  win$run()
}

createKsTestDeducer()
</pre></p>
<p>Note that the Deducer example works perfectly under JGR, though I couldn&#8217;t get the button handler to fire when running it from eclipse. This is likely due to my inexperience with the toolkit rather than a fundamental problem with the framework.  Many of the lines are more or less a one-to-one comparison, but Deducer requires you to explicitly specify positions of widgets, and is a little more verbose when you come to add event handling logic.</p>
<p>Either or these frameworks is suitable for the obvious use case of GUI building in R (rapid prototyping of front-ends for non technical users, and for teaching demos), so don&#8217;t sweat your decision too much.</p>
<p>Edit: Fixed variable name casing issues.</p>
<br /> Tagged: <a href='http://4dpiecharts.com/tag/comparison/'>comparison</a>, <a href='http://4dpiecharts.com/tag/deducer/'>Deducer</a>, <a href='http://4dpiecharts.com/tag/gui/'>gui</a>, <a href='http://4dpiecharts.com/tag/gwidgets/'>gWidgets</a>, <a href='http://4dpiecharts.com/tag/r/'>r</a>, <a href='http://4dpiecharts.com/tag/software-development/'>software-development</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/4dpiecharts.wordpress.com/458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/4dpiecharts.wordpress.com/458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/4dpiecharts.wordpress.com/458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/4dpiecharts.wordpress.com/458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/4dpiecharts.wordpress.com/458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/4dpiecharts.wordpress.com/458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/4dpiecharts.wordpress.com/458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/4dpiecharts.wordpress.com/458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/4dpiecharts.wordpress.com/458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/4dpiecharts.wordpress.com/458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/4dpiecharts.wordpress.com/458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/4dpiecharts.wordpress.com/458/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/4dpiecharts.wordpress.com/458/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/4dpiecharts.wordpress.com/458/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=4dpiecharts.com&#038;blog=15320431&#038;post=458&#038;subd=4dpiecharts&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://4dpiecharts.com/2012/02/20/gui-building-in-r-gwidgets-vs-deducer/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/85c1a01d3843ecaa30abd003b65811a8?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">richierocks</media:title>
		</media:content>
	</item>
		<item>
		<title>R hits 10000 questions on stackoverflow</title>
		<link>http://4dpiecharts.com/2012/02/17/r-hits-10000-questions-on-stackoverflow/</link>
		<comments>http://4dpiecharts.com/2012/02/17/r-hits-10000-questions-on-stackoverflow/#comments</comments>
		<pubDate>Fri, 17 Feb 2012 07:53:53 +0000</pubDate>
		<dc:creator>richierocks</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[r]]></category>
		<category><![CDATA[stackoverflow]]></category>

		<guid isPermaLink="false">http://4dpiecharts.com/?p=453</guid>
		<description><![CDATA[A milestone, though not that exciting as questions go. Still, if you haven&#8217;t yet joined the cult of Stack Exchange, take a look here. Tagged: r, stackoverflow<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=4dpiecharts.com&#038;blog=15320431&#038;post=453&#038;subd=4dpiecharts&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://4dpiecharts.files.wordpress.com/2012/02/so_10000th_r_question.png"><img src="http://4dpiecharts.files.wordpress.com/2012/02/so_10000th_r_question.png?w=600&h=371" alt="R&#039;s 10000th question on stackoverflow" title="R&#039;s 10000th question on stackoverflow" width="600" height="371" class="aligncenter size-full wp-image-454" /></a></p>
<p>A milestone, though not that exciting as questions go.  Still, if you haven&#8217;t yet joined the cult of Stack Exchange, take a look <a href="http://stackoverflow.com/questions/tagged/r">here</a>.</p>
<br /> Tagged: <a href='http://4dpiecharts.com/tag/r/'>r</a>, <a href='http://4dpiecharts.com/tag/stackoverflow/'>stackoverflow</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/4dpiecharts.wordpress.com/453/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/4dpiecharts.wordpress.com/453/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/4dpiecharts.wordpress.com/453/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/4dpiecharts.wordpress.com/453/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/4dpiecharts.wordpress.com/453/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/4dpiecharts.wordpress.com/453/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/4dpiecharts.wordpress.com/453/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/4dpiecharts.wordpress.com/453/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/4dpiecharts.wordpress.com/453/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/4dpiecharts.wordpress.com/453/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/4dpiecharts.wordpress.com/453/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/4dpiecharts.wordpress.com/453/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/4dpiecharts.wordpress.com/453/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/4dpiecharts.wordpress.com/453/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=4dpiecharts.com&#038;blog=15320431&#038;post=453&#038;subd=4dpiecharts&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://4dpiecharts.com/2012/02/17/r-hits-10000-questions-on-stackoverflow/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/85c1a01d3843ecaa30abd003b65811a8?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">richierocks</media:title>
		</media:content>

		<media:content url="http://4dpiecharts.files.wordpress.com/2012/02/so_10000th_r_question.png" medium="image">
			<media:title type="html">R&#039;s 10000th question on stackoverflow</media:title>
		</media:content>
	</item>
		<item>
		<title>Viewing the internals of MATLAB Matrices</title>
		<link>http://4dpiecharts.com/2012/01/31/viewing-the-internals-of-matlab-matrices/</link>
		<comments>http://4dpiecharts.com/2012/01/31/viewing-the-internals-of-matlab-matrices/#comments</comments>
		<pubDate>Tue, 31 Jan 2012 16:24:09 +0000</pubDate>
		<dc:creator>richierocks</dc:creator>
				<category><![CDATA[MATLAB]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[matlab]]></category>
		<category><![CDATA[undocumented]]></category>

		<guid isPermaLink="false">http://4dpiecharts.com/?p=450</guid>
		<description><![CDATA[A cool undocumented trick I just learnt from The MathWorks&#8217; Bob Gilmore. If you type Then printing any vector reveals information about its internal representation. For example: The structure address is the address in memory where the matrix is stored, m and n are the number of rows and columns respectively of the matrix, and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=4dpiecharts.com&#038;blog=15320431&#038;post=450&#038;subd=4dpiecharts&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A cool undocumented trick <a href="http://stackoverflow.com/questions/9069085/how-to-find-type-and-memory-location-of-a-defined-symbol-in-mathematica-and-matl">I just learnt</a> from The MathWorks&#8217; Bob Gilmore.  If you type</p>
<p><pre class="brush: matlabkey;">
format debug
</pre></p>
<p>Then printing any vector reveals information about its internal representation.  For example:</p>
<p><pre class="brush: matlabkey;">
x = magic(3)

x =


Structure address = 6bc1ab0 
m = 3
n = 3
pr = d8dccf0 
pi = 0
     8     1     6
     3     5     7
     4     9     2
</pre></p>
<p>The structure address is the address in memory where the matrix is stored, <code>m</code> and <code>n</code> are the number of rows and columns respectively of the matrix, and <code>pr</code> and <code>pi</code> are pointers to the addresses of the matrices storing the real and imaginary components of the matrix.</p>
<p>One interesting thing to look at is the representation of scalar numbers.</p>
<p><pre class="brush: matlabkey;">
 y = 1

y =


Structure address = 6bc31e0 
m = 1
n = 1
pr = d790b90 
pi = 0
     1
</pre></p>
<p>Yep: they are stored in exactly the same way as matrices: in the same way the &#8220;everything in R is a vector&#8221;, everything in MATLAB is a matrix.  To finish up, here are some more examples for you to explore:</p>
<p><pre class="brush: matlabkey;">
% higher dimensional arrays
rand(2, 3, 4)
% cell arrays (unfortunately not that revealing)
{1, magic(3)}
% sparse matrices (very interesting)
sparse(ones(3))
</pre></p>
<br /> Tagged: <a href='http://4dpiecharts.com/tag/debug/'>debug</a>, <a href='http://4dpiecharts.com/tag/matlab/'>matlab</a>, <a href='http://4dpiecharts.com/tag/undocumented/'>undocumented</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/4dpiecharts.wordpress.com/450/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/4dpiecharts.wordpress.com/450/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/4dpiecharts.wordpress.com/450/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/4dpiecharts.wordpress.com/450/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/4dpiecharts.wordpress.com/450/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/4dpiecharts.wordpress.com/450/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/4dpiecharts.wordpress.com/450/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/4dpiecharts.wordpress.com/450/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/4dpiecharts.wordpress.com/450/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/4dpiecharts.wordpress.com/450/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/4dpiecharts.wordpress.com/450/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/4dpiecharts.wordpress.com/450/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/4dpiecharts.wordpress.com/450/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/4dpiecharts.wordpress.com/450/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=4dpiecharts.com&#038;blog=15320431&#038;post=450&#038;subd=4dpiecharts&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://4dpiecharts.com/2012/01/31/viewing-the-internals-of-matlab-matrices/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/85c1a01d3843ecaa30abd003b65811a8?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">richierocks</media:title>
		</media:content>
	</item>
		<item>
		<title>Exploring the functions in a package</title>
		<link>http://4dpiecharts.com/2012/01/26/exploring-the-functions-in-a-package/</link>
		<comments>http://4dpiecharts.com/2012/01/26/exploring-the-functions-in-a-package/#comments</comments>
		<pubDate>Thu, 26 Jan 2012 15:33:30 +0000</pubDate>
		<dc:creator>richierocks</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[ls]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[r]]></category>
		<category><![CDATA[utilities]]></category>

		<guid isPermaLink="false">http://4dpiecharts.com/?p=444</guid>
		<description><![CDATA[Sometimes it can be useful to list all the functions inside a package. This is done in the same way that you would list variables in your workspace. That is, using ls. The syntax is ls(pos = "package:packagename"), which is easy enough if you can remember it. Unfortunately, I never can, and have to type [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=4dpiecharts.com&#038;blog=15320431&#038;post=444&#038;subd=4dpiecharts&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Sometimes it can be useful to list all the functions inside a package.  This is done in the same way that you would list variables in your workspace.  That is, using <code>ls</code>.  The syntax is <code>ls(pos = "package:packagename")</code>, which is easy enough if you can remember it.  Unfortunately, I never can, and have to type <code>search()</code> first to see what the format of that string is.</p>
<p>Today, that problem is solved with a tiny utility function to save remembering things, and to save typing.</p>
<p><pre class="brush: r;">
lsp &lt;- function(package, all.names = FALSE, pattern) 
{
  package &lt;- deparse(substitute(package))
  ls(
      pos = paste(&quot;package&quot;, package, sep = &quot;:&quot;), 
      all.names = all.names, 
      pattern = pattern
  )
}
</pre></p>
<p><code>all.names</code> and <code>pattern</code> behave in the same way as they do in regular <code>ls</code>.  You use it like this:</p>
<p><pre class="brush: r;">
lsp(base)
lsp(base, TRUE)
lsp(base, pattern = &quot;^is&quot;)
</pre></p>
<hr />
<p>EDIT: I&#8217;ve had a couple of questions about the use case, and there are some interesting comments on alternatives.  My thinking behind this function was that I sometimes know I&#8217;ve seen a function in a package but can&#8217;t remember what it&#8217;s called.  If you can hazard a guess at the name, then <code>apropos</code> is probably better, though it looks everywhere on the search path rather than in a particular package.  Autocompletion is also useful for this, but you need to know the first few characters of what you are looking for.  (Activate autocompletions by pressing TAB in R GUI or Rstudio or CTRL+space in eclipse.  I can&#8217;t remember what the shortcut is in emacs, but you probably just mash CTRL+META until you have RSI.)  Finally, the <a href="http://cran.r-project.org/web/packages/unknownR/index.html"><code>unknownR</code></a> package is useful for finding new functions that you hadn&#8217;t heard of yet.</p>
<br /> Tagged: <a href='http://4dpiecharts.com/tag/ls/'>ls</a>, <a href='http://4dpiecharts.com/tag/programming/'>programming</a>, <a href='http://4dpiecharts.com/tag/r/'>r</a>, <a href='http://4dpiecharts.com/tag/utilities/'>utilities</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/4dpiecharts.wordpress.com/444/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/4dpiecharts.wordpress.com/444/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/4dpiecharts.wordpress.com/444/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/4dpiecharts.wordpress.com/444/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/4dpiecharts.wordpress.com/444/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/4dpiecharts.wordpress.com/444/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/4dpiecharts.wordpress.com/444/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/4dpiecharts.wordpress.com/444/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/4dpiecharts.wordpress.com/444/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/4dpiecharts.wordpress.com/444/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/4dpiecharts.wordpress.com/444/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/4dpiecharts.wordpress.com/444/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/4dpiecharts.wordpress.com/444/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/4dpiecharts.wordpress.com/444/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=4dpiecharts.com&#038;blog=15320431&#038;post=444&#038;subd=4dpiecharts&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://4dpiecharts.com/2012/01/26/exploring-the-functions-in-a-package/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/85c1a01d3843ecaa30abd003b65811a8?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">richierocks</media:title>
		</media:content>
	</item>
		<item>
		<title>Adding metadata to variables</title>
		<link>http://4dpiecharts.com/2012/01/06/adding-metadata-to-variables/</link>
		<comments>http://4dpiecharts.com/2012/01/06/adding-metadata-to-variables/#comments</comments>
		<pubDate>Fri, 06 Jan 2012 08:45:03 +0000</pubDate>
		<dc:creator>richierocks</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[assignment]]></category>
		<category><![CDATA[metadata]]></category>
		<category><![CDATA[r]]></category>

		<guid isPermaLink="false">http://4dpiecharts.com/?p=439</guid>
		<description><![CDATA[There are only really two ways to preserve your statistical analyses. You either save the variables that you create, or you save the code that you used to create them. In general the latter is much preferred because at some point you&#8217;ll realise that your model was wrong, or your dataset has changed, and you [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=4dpiecharts.com&#038;blog=15320431&#038;post=439&#038;subd=4dpiecharts&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>There are only really two ways to preserve your statistical analyses.  You either save the variables that you create, or you save the code that you used to create them.  In general the latter is much preferred because at some point you&#8217;ll realise that your model was wrong, or your dataset has changed, and you need to re-run your analysis. If you only stored your variables then you are now stuck rewriting your code in order to create new versions, which is really not fun.  On the other hand, if you saved your code, all your have to do is tweak it and run it.</p>
<p>Occasionally though, just keeping the code and rerunning an analysis isn&#8217;t practical.  The most obvious case being when it takes a long time.  If your model takes more than ten minutes to run, it can be really useful to save its variables <i>as well</i> as the source code.</p>
<p>The problem with saving variables is that when you come back and load them six months later, it isn&#8217;t always obvious what they are or where they came from.  With code, we solve this by using comments to jog our memory, so it would be nice to have an equivalent for variables.  In fact, in R, such a facility exists with the &ndash; you guessed it &ndash; <code>comment</code> function.</p>
<p><pre class="brush: r;">
library(lattice)
comment(barley) &lt;- &quot;Immer's barley data, 1934.  The data from the Morris site may have the wrong years.&quot;
comment(barley)
</pre></p>
<p>The <code>comment</code> function simply stores the string as an attribute of the variable, with some special rules on printing.  Other common attributes that you may be familiar with are <code>names</code> for vectors and lists, and <code>dim</code> and <code>dimnames</code> for matrices.</p>
<p>You can find the names of all the attributes of a variable with the <code>attributes</code> function, and get and set individual attributes with <code>attr</code>.</p>
<p><pre class="brush: r;">
x &lt;- c(apple = 1, banana = 2)
attr(x, &quot;type&quot;) &lt;- &quot;fruit&quot;
attributes(x)
attr(x, &quot;names&quot;) #same as names(x)
</pre></p>
<p>Attributes are really great for storing contextual metadata about a variable.  For starters, when you come back to your saved workspace after those six months you might want to know who created the variable and when.  To get this facility, we need an enhanced version of <code>assign</code>.</p>
<p><pre class="brush: r;">
get_user &lt;- function()
{
  env &lt;- if(.Platform$OS.type == &quot;windows&quot;) &quot;USERNAME&quot; else &quot;USER&quot;
  unname(Sys.getenv(env))    
}  
  
assign_with_metadata &lt;- function(x, value, ..., pos = parent.frame(), inherits = FALSE)
{
  attr(value, &quot;creator&quot;) &lt;- get_user()
  attr(value, &quot;time_created&quot;) &lt;- Sys.time()
  more_attr &lt;- list(...)
  attr_names &lt;- names(more_attr)
  for(i in seq_along(more_attr))
  {
    attr(value, attr_names[i]) &lt;- more_attr[[i]]
  }
  assign(x, value, pos = pos, inherits = inherits)
}

assign_with_metadata(&quot;x&quot;, 1:3, monkey = &quot;chimp&quot;)
</pre></p>
<p>Notice the <code>...</code> that allows you to add arbitrary attributes to the variable.</p>
<p>While this is great, and solves the problem, typing <code>assign_with_metadata</code> is way too clunky.  It would be much easier if we could just use <code>&lt;-</code> to assign variables and get the metadata for free.</p>
<p>Actually, overriding <code>&lt;-</code> itself is going to lead to slowness and likely errors. Since we don&#8217;t want to store metadata for every variable (just the important ones), it is better to define our own operators to do so.</p>
<p><pre class="brush: r;">
`%&lt;-%` &lt;- function(x, value)
{
  xname &lt;- deparse(substitute(x))
  pos &lt;- parent.frame()
  assign_with_metadata(xname, value, pos = pos)
}

`%&lt;&lt;-%` &lt;- function(x, value) 
{
  xname &lt;- deparse(substitute(x))
  pos &lt;- globalenv()
  assign_with_metadata(xname, value, pos = pos)
}

m %&lt;-% &quot;foo&quot;    #local assignment with metadata
f &lt;- function()
{
  n %&lt;&lt;-% &quot;bar&quot; #global assignment with metadata
}
f()
</pre></p>
<p>With these functions, if you want to save your variables for later, simply swap <code>&lt;-</code> for <code>%&lt;-%</code>.</p>
<br /> Tagged: <a href='http://4dpiecharts.com/tag/assignment/'>assignment</a>, <a href='http://4dpiecharts.com/tag/metadata/'>metadata</a>, <a href='http://4dpiecharts.com/tag/r/'>r</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/4dpiecharts.wordpress.com/439/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/4dpiecharts.wordpress.com/439/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/4dpiecharts.wordpress.com/439/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/4dpiecharts.wordpress.com/439/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/4dpiecharts.wordpress.com/439/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/4dpiecharts.wordpress.com/439/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/4dpiecharts.wordpress.com/439/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/4dpiecharts.wordpress.com/439/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/4dpiecharts.wordpress.com/439/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/4dpiecharts.wordpress.com/439/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/4dpiecharts.wordpress.com/439/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/4dpiecharts.wordpress.com/439/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/4dpiecharts.wordpress.com/439/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/4dpiecharts.wordpress.com/439/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=4dpiecharts.com&#038;blog=15320431&#038;post=439&#038;subd=4dpiecharts&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://4dpiecharts.com/2012/01/06/adding-metadata-to-variables/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/85c1a01d3843ecaa30abd003b65811a8?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">richierocks</media:title>
		</media:content>
	</item>
		<item>
		<title>A quick primer on split-apply-combine problems</title>
		<link>http://4dpiecharts.com/2011/12/16/a-quick-primer-on-split-apply-combine-problems/</link>
		<comments>http://4dpiecharts.com/2011/12/16/a-quick-primer-on-split-apply-combine-problems/#comments</comments>
		<pubDate>Fri, 16 Dec 2011 18:29:17 +0000</pubDate>
		<dc:creator>richierocks</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[apply]]></category>
		<category><![CDATA[combine]]></category>
		<category><![CDATA[plyr]]></category>
		<category><![CDATA[r]]></category>
		<category><![CDATA[split]]></category>
		<category><![CDATA[statistics]]></category>

		<guid isPermaLink="false">http://4dpiecharts.com/?p=433</guid>
		<description><![CDATA[I&#8217;ve just answered my hundred billionth question on Stack Overflow that goes something like I want to calculate some statistic for lots of different groups. Although these questions provide a steady stream of easy points, its such a common and basic data analysis concept that I thought it would be useful to have a document [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=4dpiecharts.com&#038;blog=15320431&#038;post=433&#038;subd=4dpiecharts&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just answered my hundred billionth question on Stack Overflow that goes something like</p>
<blockquote><p>
I want to calculate some statistic for lots of different groups.
</p></blockquote>
<p>Although these questions provide a steady stream of easy points, its such a common and basic data analysis concept that I thought it would be useful to have a document to refer people to.</p>
<p>First off, you need to data in the right format.  The canonical form in R is a data frame with one column containing the values to calculate a statistic for and another column containing the group to which that value belongs.  A good example is the InsectSprays dataset, built into R.</p>
<p><pre class="brush: r;">
head(InsectSprays)
  count spray
1    10     A
2     7     A
3    20     A
4    14     A
5    14     A
6    12     A
</pre></p>
<p>These problems are widely known as split-apply-combine problems after the three steps involved in their solution.  Let&#8217;s go through it step by step.</p>
<p>First, we split the <code>count</code> column by the <code>spray</code> column.</p>
<p><pre class="brush: r;">
(count_by_spray &lt;- with(InsectSprays, split(count, spray)))
</pre></p>
<p>Secondly, we apply the statistic to each element of the list.  Lets use the <code>mean</code> here.</p>
<p><pre class="brush: r;">
(mean_by_spray &lt;- lapply(count_by_spray, mean))
</pre></p>
<p>Finally, (if possible) we recombine the list as a vector.</p>
<p><pre class="brush: r;">
unlist(mean_by_spray)
</pre></p>
<p>This procedure is such a common thing that there are many functions to speed up the process.  <code>sapply</code> and <code>vapply</code> do the last two steps together.</p>
<p><pre class="brush: r;">
sapply(count_by_spray, mean)
vapply(count_by_spray, mean, numeric(1))
</pre></p>
<p>We can do even better than that however. <code>tapply</code>, <code>aggregate</code> and <code>by</code> all provide a one-function solution to these S-A-C problems.</p>
<p><pre class="brush: r;">
with(InsectSprays, tapply(count, spray, mean))
with(InsectSprays, by(count, spray, mean))
aggregate(count ~ spray, InsectSprays, mean)
</pre></p>
<p>The <code>plyr</code> package also provides several solutions, with a choice of output format. <code>ddply</code> takes a data frame and returned another data frame, which is what you&#8217;ll want most of the time.  <code>dlply</code> takes a data frame and returns the uncombined list, which is useful if you want to do another processing step before combining.</p>
<p><pre class="brush: r;">
ddply(InsectSprays, .(spray), summarise, mean.count = mean(count))
dlply(InsectSprays, .(spray), summarise, mean.count = mean(count))
</pre></p>
<p>You can read much more on this type of problem and the <code>plyr</code> solution in <a href="http://www.jstatsoft.org/v40/i01">The Split-Apply-Combine Strategy for Data Analysis</a>, in the Journal of Statistical Software, by the ubiquitous Hadley Wickham.</p>
<p>One tiny variation on the problem is when you want the output statistic vector to have the same length as the original input vectors.  For this, there is the <code>ave</code> function (which provides <code>mean</code> as the default function).</p>
<p><pre class="brush: r;">
with(InsectSprays, ave(count, spray))
</pre></p>
<br /> Tagged: <a href='http://4dpiecharts.com/tag/apply/'>apply</a>, <a href='http://4dpiecharts.com/tag/combine/'>combine</a>, <a href='http://4dpiecharts.com/tag/plyr/'>plyr</a>, <a href='http://4dpiecharts.com/tag/r/'>r</a>, <a href='http://4dpiecharts.com/tag/split/'>split</a>, <a href='http://4dpiecharts.com/tag/statistics/'>statistics</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/4dpiecharts.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/4dpiecharts.wordpress.com/433/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/4dpiecharts.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/4dpiecharts.wordpress.com/433/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/4dpiecharts.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/4dpiecharts.wordpress.com/433/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/4dpiecharts.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/4dpiecharts.wordpress.com/433/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/4dpiecharts.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/4dpiecharts.wordpress.com/433/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/4dpiecharts.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/4dpiecharts.wordpress.com/433/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/4dpiecharts.wordpress.com/433/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/4dpiecharts.wordpress.com/433/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=4dpiecharts.com&#038;blog=15320431&#038;post=433&#038;subd=4dpiecharts&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://4dpiecharts.com/2011/12/16/a-quick-primer-on-split-apply-combine-problems/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/85c1a01d3843ecaa30abd003b65811a8?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">richierocks</media:title>
		</media:content>
	</item>
		<item>
		<title>MATLAB&#8217;s stand out new feature</title>
		<link>http://4dpiecharts.com/2011/10/06/matlabs-stand-out-new-feature/</link>
		<comments>http://4dpiecharts.com/2011/10/06/matlabs-stand-out-new-feature/#comments</comments>
		<pubDate>Thu, 06 Oct 2011 17:10:17 +0000</pubDate>
		<dc:creator>richierocks</dc:creator>
				<category><![CDATA[MATLAB]]></category>
		<category><![CDATA[ide]]></category>
		<category><![CDATA[matlab]]></category>
		<category><![CDATA[r2011b]]></category>
		<category><![CDATA[renaming]]></category>
		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://4dpiecharts.com/?p=426</guid>
		<description><![CDATA[It&#8217;s been a while since my last MATLAB post, not because I don&#8217;t love the language, but more because I do most of my blogging from home, where I have no license, and because (mostly thanks to R-bloggers) I get ten times as many page views for the R posts. (TODO: Create MATLAB-bloggers service.) Having [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=4dpiecharts.com&#038;blog=15320431&#038;post=426&#038;subd=4dpiecharts&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a while since my last MATLAB post, not because I don&#8217;t love the language, but more because I do most of my blogging from home, where I have no license, and because (mostly thanks to R-bloggers) I get ten times as many page views for the R posts.  (TODO: Create MATLAB-bloggers service.)</p>
<p>Having returned from holiday (it was lovely, thanks for asking) I&#8217;ve been trying out the latest release of MATLAB &ndash; R2011b.  So far, the standout new feature is the automatic variable renaming.  If you change the name of a variable at the point where it was declared, then pressing Shift+Enter lets MATLAB rename all other instances.  IDEs for statically-typed languages have had this feature for years, but to see it in a dynamically-typed language is very impressive.</p>
<p><a href="http://4dpiecharts.files.wordpress.com/2011/10/matlab_auto_rename.png"><img src="http://4dpiecharts.files.wordpress.com/2011/10/matlab_auto_rename.png?w=600" alt="MATLAB&#039;s variable auto-renaming in action" title="MATLAB&#039;s variable auto-renaming in action"   class="aligncenter size-full wp-image-427" /></a></p>
<br /> Tagged: <a href='http://4dpiecharts.com/tag/ide/'>ide</a>, <a href='http://4dpiecharts.com/tag/matlab/'>matlab</a>, <a href='http://4dpiecharts.com/tag/r2011b/'>r2011b</a>, <a href='http://4dpiecharts.com/tag/renaming/'>renaming</a>, <a href='http://4dpiecharts.com/tag/variables/'>variables</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/4dpiecharts.wordpress.com/426/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/4dpiecharts.wordpress.com/426/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/4dpiecharts.wordpress.com/426/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/4dpiecharts.wordpress.com/426/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/4dpiecharts.wordpress.com/426/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/4dpiecharts.wordpress.com/426/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/4dpiecharts.wordpress.com/426/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/4dpiecharts.wordpress.com/426/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/4dpiecharts.wordpress.com/426/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/4dpiecharts.wordpress.com/426/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/4dpiecharts.wordpress.com/426/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/4dpiecharts.wordpress.com/426/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/4dpiecharts.wordpress.com/426/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/4dpiecharts.wordpress.com/426/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=4dpiecharts.com&#038;blog=15320431&#038;post=426&#038;subd=4dpiecharts&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://4dpiecharts.com/2011/10/06/matlabs-stand-out-new-feature/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/85c1a01d3843ecaa30abd003b65811a8?s=96&#38;d=identicon&#38;r=PG" medium="image">
			<media:title type="html">richierocks</media:title>
		</media:content>

		<media:content url="http://4dpiecharts.files.wordpress.com/2011/10/matlab_auto_rename.png" medium="image">
			<media:title type="html">MATLAB&#039;s variable auto-renaming in action</media:title>
		</media:content>
	</item>
	</channel>
</rss>
