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

<channel>
	<title>Quantum Forest</title>
	<atom:link href="http://www.quantumforest.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.quantumforest.com</link>
	<description>A shoebox for scribbles on data analysis, photos and sound by Luis A. Apiolaza</description>
	<lastBuildDate>Mon, 20 May 2013 00:14:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Jetsam 7: the wing</title>
		<link>http://www.quantumforest.com/2013/05/jetsam-7-the-wing/</link>
		<comments>http://www.quantumforest.com/2013/05/jetsam-7-the-wing/#comments</comments>
		<pubDate>Fri, 17 May 2013 11:30:41 +0000</pubDate>
		<dc:creator>Luis</dc:creator>
				<category><![CDATA[jetsam]]></category>
		<category><![CDATA[photos]]></category>

		<guid isPermaLink="false">http://www.quantumforest.com/?p=1825</guid>
		<description><![CDATA[]]></description>
				<content:encoded><![CDATA[<div id="attachment_1826" class="wp-caption alignnone" style="width: 594px"><a href="http://www.quantumforest.com/wp-content/uploads/2013/05/the-wing.jpg"><img src="http://www.quantumforest.com/wp-content/uploads/2013/05/the-wing-1024x768.jpg" alt="Winter flying between Blenheim and Christchurch (Photo: Luis)." width="584" height="438" class="size-large wp-image-1826" /></a><p class="wp-caption-text">Winter flying between Blenheim and Christchurch (Photo: Luis).</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.quantumforest.com/2013/05/jetsam-7-the-wing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Analyzing a simple experiment with heterogeneous variances using asreml, MCMCglmm and SAS</title>
		<link>http://www.quantumforest.com/2013/05/analyzing-a-simple-experiment-with-heterogeneous-variances-using-asreml-mcmcglmm-and-sas/</link>
		<comments>http://www.quantumforest.com/2013/05/analyzing-a-simple-experiment-with-heterogeneous-variances-using-asreml-mcmcglmm-and-sas/#comments</comments>
		<pubDate>Fri, 17 May 2013 11:26:02 +0000</pubDate>
		<dc:creator>Luis</dc:creator>
				<category><![CDATA[asreml]]></category>
		<category><![CDATA[bayesian]]></category>
		<category><![CDATA[linear models]]></category>
		<category><![CDATA[MCMCglmm]]></category>
		<category><![CDATA[r]]></category>
		<category><![CDATA[rblogs]]></category>
		<category><![CDATA[sas]]></category>

		<guid isPermaLink="false">http://www.quantumforest.com/?p=1805</guid>
		<description><![CDATA[I was working with a small experiment which includes families from two Eucalyptus species and thought it would be nice to code a first analysis using alternative approaches. The experiment is a randomized complete block design, with species as fixed &#8230; <a href="http://www.quantumforest.com/2013/05/analyzing-a-simple-experiment-with-heterogeneous-variances-using-asreml-mcmcglmm-and-sas/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>I was working with a small experiment which includes families from two <em>Eucalyptus</em> species and thought it would be nice to code a first analysis using alternative approaches. The experiment is a randomized complete block design, with species as fixed effect and family and block as a random effects, while the response variable is growth strain (in \( \mu \epsilon\)).</p>
<p>When looking at the trees one can see that the residual variances will be very different. In addition, the trees were growing in plastic bags laid out in rows (the blocks) and columns. Given that trees were growing in bags siting on flat terrain, most likely the row effects are zero.</p>
<p>Below is the code for a first go in R (using both MCMCglmm and ASReml-R) and SAS. I had stopped using SAS for several years, mostly because I was running a mac for which there is no version. However, a few weeks ago I started accessing it via their <a href="http://www.sas.com/govedu/edu/programs/od_academics.html">OnDemand for Academics</a> program via a web browser.</p>
<p>The R code using REML looks like:</p>
<pre class="brush: r; title: ; notranslate">
# Options
options(stringsAsFactors = FALSE)
setwd('~/Dropbox/research/2013/growth-stress')

# Packages
require(ggplot2)
require(asreml)
require(MCMCglmm)


# Reading data, renaming, factors, etc
gs = read.csv('eucalyptus-growth-stress.csv')
summary(gs)

# Both argophloia and bosistoana
gsboth = subset(gs, !is.na(strain))
gsboth = within(gsboth, {
	species = factor(species)
	row = factor(row)
	column = factor(column)
	fam = factor(fam)
})            



ma = asreml(strain ~ species, random = ~ fam + row, 
            rcov = ~ at(species):units,
            data = gsboth)

summary(ma)$varcomp

#                                   gamma  component std.error   z.ratio constraint
#fam!fam.var                    27809.414  27809.414 10502.036 2.6480022   Positive
#row!row.var                     2337.164   2337.164  3116.357 0.7499666   Positive
#species_E.argopholia!variance 111940.458 111940.458 26609.673 4.2067580   Positive
#species_E.bosistoana!variance  63035.256  63035.256  7226.768 8.7224681   Positive
</pre>
<p>While using MCMC we get estimates in the ballpark by using:</p>
<pre class="brush: r; title: ; notranslate">
# Priors
bothpr = list(R = list(V = diag(c(50000, 50000)), nu = 3), 
              G = list(G1 = list(V = 20000, nu = 0.002), 
                       G2 = list(V = 20000, nu = 0.002),
                       G3 = list(V = 20000, nu = 0.002)))

# MCMC                    
m2 = MCMCglmm(strain ~ species, random = ~ fam + row + column, 
              rcov = ~ idh(species):units,
              data = gsboth,
              prior = bothpr,
              pr = TRUE,
              family = 'gaussian',
              burnin = 10000,
              nitt = 40000,
              thin = 20,
              saveX = TRUE,
              saveZ = TRUE,
              verbose = FALSE)

summary(m2)

# Iterations = 10001:39981
# Thinning interval  = 20
# Sample size  = 1500 
# 
# DIC: 3332.578 
# 
# G-structure:  ~fam
# 
# post.mean l-95% CI u-95% CI eff.samp
# fam     30315    12211    55136     1500
# 
# ~row
# 
# post.mean l-95% CI u-95% CI eff.samp
# row      1449    5.928     6274    589.5
# 
# R-structure:  ~idh(species):units
# 
# post.mean l-95% CI u-95% CI eff.samp
# E.argopholia.units    112017    71152   168080     1500
# E.bosistoana.units     65006    52676    80049     1500
# 
# Location effects: strain ~ species 
# 
# post.mean l-95% CI u-95% CI eff.samp  pMCMC    
# (Intercept)            502.21   319.45   690.68     1500 &lt;7e-04 ***
# speciesE.bosistoana   -235.95  -449.07   -37.19     1361  0.036 * 
</pre>
<p>The SAS code is not that disimilar, except for the clear demarcation between data processing (data step, for reading files, data transformations, etc) and specific procs (procedures), in this case to summarize data, produce a boxplot and fit a mixed model.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="sas" style="font-family:monospace;"><span style="color: #006400; font-style: italic;">* termstr=CRLF accounts for the windows-like line endings of the data set;</span>
<span style="color: #000080; font-weight: bold;">data</span> gs;
  <span style="color: #0000ff;">infile</span> <span style="color: #a020f0;">&quot;/home/luis/Projects/euc-strain/growthstresses.csv&quot;</span> 
        dsd termstr=CRLF firstobs=<span style="color: #2e8b57; font-weight: bold;">2</span>;
  <span style="color: #0000ff;">input</span> row column species $ family $ strain;
  <span style="color: #0000ff;">if</span> strain ^= .;
<span style="color: #000080; font-weight: bold;">run</span>;
&nbsp;
<span style="color: #000080; font-weight: bold;">proc summary</span> <span style="color: #000080; font-weight: bold;">data</span> = gs print;
  class species;
  <span style="color: #0000ff;">var</span> strain;
<span style="color: #000080; font-weight: bold;">run</span>;
&nbsp;
<span style="color: #000080; font-weight: bold;">proc boxplot</span> <span style="color: #000080; font-weight: bold;">data</span> = gs;
  plot strain<span style="color: #006400; font-style: italic;">*species;</span>
<span style="color: #000080; font-weight: bold;">run</span>;
&nbsp;
<span style="color: #000080; font-weight: bold;">proc mixed</span> <span style="color: #000080; font-weight: bold;">data</span> = gs;
  class row species family;
  model strain = species;
  random row family;
  repeated species / <span style="color: #0000ff;">group</span>=species;
<span style="color: #000080; font-weight: bold;">run</span>;
&nbsp;
<span style="color: #006400; font-style: italic;">/*
Covariance Parameter Estimates
Cov Parm 	Group 	               Estimate
row 	  	                       2336.80
family 	  	                       27808
species 	species E.argoph 	111844
species 	species E.bosist 	63036
*/</span></pre></td></tr></table></div>

<div id="attachment_1819" class="wp-caption alignnone" style="width: 650px"><a href="http://www.quantumforest.com/wp-content/uploads/2013/05/SASBoxplot1.png"><img src="http://www.quantumforest.com/wp-content/uploads/2013/05/SASBoxplot1.png" alt="SAS boxplot for the data set." width="640" height="480" class="size-full wp-image-1819" /></a><p class="wp-caption-text">SAS boxplot for the data set.</p></div>
<p>I like working with multiple languages and I realized that, in fact, I missed SAS a bit. It was like meeting an old friend; at the beginning felt strange but we were quickly chatting away after a few minutes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.quantumforest.com/2013/05/analyzing-a-simple-experiment-with-heterogeneous-variances-using-asreml-mcmcglmm-and-sas/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Jetsam 6: Uppsala river winter</title>
		<link>http://www.quantumforest.com/2013/05/jetsam-6-uppsala-river-winter/</link>
		<comments>http://www.quantumforest.com/2013/05/jetsam-6-uppsala-river-winter/#comments</comments>
		<pubDate>Tue, 07 May 2013 05:24:58 +0000</pubDate>
		<dc:creator>Luis</dc:creator>
				<category><![CDATA[jetsam]]></category>
		<category><![CDATA[photos]]></category>

		<guid isPermaLink="false">http://www.quantumforest.com/?p=1795</guid>
		<description><![CDATA[]]></description>
				<content:encoded><![CDATA[<div id="attachment_1796" class="wp-caption alignnone" style="width: 594px"><a href="http://www.quantumforest.com/wp-content/uploads/2013/05/uppsala-river-winter.jpg"><img src="http://www.quantumforest.com/wp-content/uploads/2013/05/uppsala-river-winter-768x1024.jpg" alt="Snowless December in Uppsala (Photo: Luis)." width="584" height="778" class="size-large wp-image-1796" /></a><p class="wp-caption-text">Snowless December in Uppsala (Photo: Luis).</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.quantumforest.com/2013/05/jetsam-6-uppsala-river-winter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Subsetting data</title>
		<link>http://www.quantumforest.com/2013/05/subsetting-data/</link>
		<comments>http://www.quantumforest.com/2013/05/subsetting-data/#comments</comments>
		<pubDate>Tue, 07 May 2013 05:12:47 +0000</pubDate>
		<dc:creator>Luis</dc:creator>
				<category><![CDATA[r]]></category>
		<category><![CDATA[rblogs]]></category>
		<category><![CDATA[teaching]]></category>

		<guid isPermaLink="false">http://www.quantumforest.com/?p=1778</guid>
		<description><![CDATA[At School we use R across many courses, because students are supposed to use statistics under a variety of contexts. Imagine their disappointment when they pass stats and discovered that R and statistics haven&#8217;t gone away! When students start working &#8230; <a href="http://www.quantumforest.com/2013/05/subsetting-data/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>At <a href="http://www.forestry.ac.nz/">School</a> we use R across many courses, because students are supposed to use statistics under a variety of contexts. Imagine their disappointment when they pass stats and discovered that R and statistics haven&#8217;t gone away! </p>
<p>When students start working with real data sets one of their first stumbling blocks is subsetting data. We have data sets and either they are required to deal with different subsets or there is data cleaning to do. For some reason, many students struggle with what should be a simple task.</p>
<p>If one thinks of data as as a matrix/2-dimensional array, subsetting boils down to extracting the needed rows (cases) and columns (variables). In the R world one can do this in a variety of ways, ranging from the cryptic to the explicit and clear. For example, let&#8217;s assume that we have a dataset called <code>alltrials</code> with heights and stem diameters for a number of trees in different trials (We may have a bunch of additional covariates and experimental design features that we&#8217;ll ignore for the moment). How do we extract all trees located in Christchurch?</p>
<p>Two common approaches are:</p>
<pre class="brush: r; title: ; notranslate">
mytrial = alltrials[alltrials$location == 'Christchurch', ]

mytrial = subset(alltrials, location == 'Christchurch')
</pre>
<p>While both expressions are equivalent, the former reads like <a href="http://en.wikipedia.org/wiki/Klingon_language">Klingon</a> to students, while the latter makes explicit that we are obtaining a subset of the original data set. This can easily be expanded to more complex conditions; for example to include all trees from Christchurch that are taller than 10 m:</p>
<pre class="brush: r; title: ; notranslate">
mytrial = alltrials[alltrials$location == 'Christchurch' &amp; alltrials$height &gt; 10, ]

mytrial = subset(alltrials, location == 'Christchurch' &amp; height &gt; 10)
</pre>
<p>I think the complication with the Klingonian notation comes mostly from two sources:</p>
<ul>
<li>Variable names for subsetting the data set are not directly accessible, so we have to prefix them with the NameOfDataset$, making the code more difficult to read, particularly if we join several conditions with <code>&#038;</code> and <code>|</code>.</li>
<li>Hanging commas: if we are only working with rows or columns we have to acknowledge it by suffixing or prefixing with a comma, which are often forgotten.</li>
</ul>
<p>Both points result on frustrating error messages like </p>
<ul>
<li><code>Error in `[.data.frame`(alltrials, location == 'Christchurch', ) : object 'location' not found</code> for the first point or</li>
<li><code>Error in `[.data.frame`(alltrials, alltrials$location == 'Christchurch') undefined columns selected</code> for the second point.</li>
</ul>
<p>The generic forms of these two notations are:</p>
<pre class="brush: r; title: ; notranslate">
dataset[what to do with rows, what to do with columns]

subset(dataset, what to do with rows, what to do with columns)
</pre>
<p>We often want to keep a subset of the observed cases <strong>and</strong> keep (or drop) specific variables. For example, we want to keep trees in 'Christchurch' and we want to ignore diameter, because the assessor was 'high' that day:</p>
<pre class="brush: r; title: ; notranslate">
# With this notation things get a bit trickier
# The easiest way is to provide the number of the variable
# Here diameter is the 5th variable in the dataset
mytrial = alltrials[all.trials$location == 'Christchurch' &amp; all.trials$height &gt; 10, -5]

# This notation is still straightforward
mytrial = subset(alltrials, location == 'Christchurch' &amp; height &gt; 10, select = -diameter)
</pre>
<p>There are, however, situations where Klingon is easier or more efficient. For example, to take a random sample of 100 trees from the full dataset:</p>
<pre class="brush: r; title: ; notranslate">

mytrial = alltrials[sample(1:nrow(alltrials), 100, replace = FALSE),]
</pre>
<p>If you are interested in this issue <a href="http://www.statmethods.net/management/subset.html">Quick-R</a> has a good description of subsetting. I'm sure this basic topic must has been covered many times, although I doubt anyone used Klingon in the process.</p>
<div id="attachment_1643" class="wp-caption alignnone" style="width: 594px"><a href="http://www.quantumforest.com/wp-content/uploads/2012/12/mega-blocks.jpg"><img src="http://www.quantumforest.com/wp-content/uploads/2012/12/mega-blocks-1024x768.jpg" alt="Gratuitous picture: building blocks for research (Photo: Luis)." width="584" height="438" class="size-large wp-image-1643" /></a><p class="wp-caption-text">Gratuitous picture: building blocks for R (Photo: Luis).</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.quantumforest.com/2013/05/subsetting-data/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Jetsam 5: Suburbia bus stop</title>
		<link>http://www.quantumforest.com/2013/05/jetsam-5-suburbia-bus-stop/</link>
		<comments>http://www.quantumforest.com/2013/05/jetsam-5-suburbia-bus-stop/#comments</comments>
		<pubDate>Wed, 01 May 2013 01:29:43 +0000</pubDate>
		<dc:creator>Luis</dc:creator>
				<category><![CDATA[jetsam]]></category>
		<category><![CDATA[photos]]></category>

		<guid isPermaLink="false">http://www.quantumforest.com/?p=1768</guid>
		<description><![CDATA[]]></description>
				<content:encoded><![CDATA[<div id="attachment_1769" class="wp-caption alignnone" style="width: 594px"><a href="http://www.quantumforest.com/wp-content/uploads/2013/05/suburbia-stop.jpg"><img src="http://www.quantumforest.com/wp-content/uploads/2013/05/suburbia-stop-768x1024.jpg" alt="Suburbia bus stop, Christchurch." width="584" height="778" class="size-large wp-image-1769" /></a><p class="wp-caption-text">Suburbia bus stop, Christchurch (Photo: Luis).</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.quantumforest.com/2013/05/jetsam-5-suburbia-bus-stop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Protectionism under another name</title>
		<link>http://www.quantumforest.com/2013/05/protectionism-under-another-name/</link>
		<comments>http://www.quantumforest.com/2013/05/protectionism-under-another-name/#comments</comments>
		<pubDate>Wed, 01 May 2013 00:24:06 +0000</pubDate>
		<dc:creator>Luis</dc:creator>
				<category><![CDATA[policy]]></category>

		<guid isPermaLink="false">http://www.quantumforest.com/?p=1755</guid>
		<description><![CDATA[This morning Radio New Zealand covered a story (audio) where Tomatoes New Zealand (TNZ, the growers association) was asking the Government to introduce compulsory labeling for irradiated products (namely imported Australian tomatoes), stating that consumers deserve an informed choice (TNZ &#8230; <a href="http://www.quantumforest.com/2013/05/protectionism-under-another-name/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>This morning Radio New Zealand <a href="http://www.radionz.co.nz/national/programmes/morningreport/audio/2553550/growers-call-for-the-labelling-of-irradiated-aussie-tomatoes.asx">covered a story</a> (audio) where <a href="http://www.tomatoesnz.co.nz/">Tomatoes New Zealand</a> (TNZ, the growers association) was asking the Government to introduce compulsory labeling for irradiated products (namely imported Australian tomatoes), stating that consumers deserve an informed choice (<a href="http://www.tomatoesnz.co.nz/news_full.htm?news_id=110">TNZ Press Release</a>). Two points that I think merit attention:</p>
<ul>
<li><a href="http://en.wikipedia.org/wiki/Food_irradiation">Food irradiation</a> is perfectly safe: it does not make food radioactive, it does not alter the nutritional value of food and reduces the presence (or completely eliminates) the presence of microorganisms that cause disease or pests (the latter being the reason for irradiation in this case).
</li>
<li>The second point is that the call for labeling does not come from <em>consumers</em> (or an organization representing them) but from <em>producers</em> that face competition.
</li>
</ul>
<p>This situation reminded me of Milton Friedman talking about professional licenses <em>The <strong>justification</strong> offered is always the same: to protect the consumer. However, the <strong>reason</strong> is demonstrated by observing who lobbies</em>. Who is doing the lobbying is very telling in this case, particularly because there is no real reason to induce fear on the consumer, except that irradiation sounds too close to radioactive, and therefore TNZ is hoping to steer consumers away from imported tomatoes. Given that TNZ is for informing the consumer they could label tomatoes with &#8216;many of the characteristics in this tomato are the <a href="http://www.seedquest.com/yellowpages/europe/holland/k/keygene/articles/breedingwith_greengene_mutations.htm">product</a> of <a href="http://www.nytimes.com/2012/06/29/science/flavor-is-the-price-of-tomatoes-scarlet-hue-geneticists-say.html?_r=0">mutations</a>&#8216;. Harmless but scary.</p>
<p>P.S. The media is now <a href="http://www.stuff.co.nz/business/farming/cropping/8618860/Fears-over-treated-Aussie-tomatoes">picking up the story</a>. Shameful manipulation.<br />
P.S.2 Give that TNZ is in favor of the right to know I want a list of all chemicals used in the production of New Zealand tomatoes, how good is their water management and the employment practices of tomato growers.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.quantumforest.com/2013/05/protectionism-under-another-name/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://www.radionz.co.nz/national/programmes/morningreport/audio/2553550/growers-call-for-the-labelling-of-irradiated-aussie-tomatoes.asx" length="502" type="video/asf" />
		</item>
		<item>
		<title>Jetsam 4: polypods, portrait of cables</title>
		<link>http://www.quantumforest.com/2013/04/jetsam-4-polypods-portrait-of-cables/</link>
		<comments>http://www.quantumforest.com/2013/04/jetsam-4-polypods-portrait-of-cables/#comments</comments>
		<pubDate>Fri, 26 Apr 2013 04:22:19 +0000</pubDate>
		<dc:creator>Luis</dc:creator>
				<category><![CDATA[jetsam]]></category>
		<category><![CDATA[photos]]></category>

		<guid isPermaLink="false">http://www.quantumforest.com/?p=1722</guid>
		<description><![CDATA[]]></description>
				<content:encoded><![CDATA[<div id="attachment_1744" class="wp-caption alignnone" style="width: 594px"><a href="http://www.quantumforest.com/wp-content/uploads/2013/04/polypod1.jpg"><img src="http://www.quantumforest.com/wp-content/uploads/2013/04/polypod1-1024x768.jpg" alt="I was fascinated by the shape of the power cords (Photo: Luis)." width="584" height="438" class="size-large wp-image-1744" /></a><p class="wp-caption-text">I was fascinated by the shape of the power cords (Photo: Luis).</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.quantumforest.com/2013/04/jetsam-4-polypods-portrait-of-cables/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Learning to code in R</title>
		<link>http://www.quantumforest.com/2013/04/learning-to-code-in-r/</link>
		<comments>http://www.quantumforest.com/2013/04/learning-to-code-in-r/#comments</comments>
		<pubDate>Fri, 26 Apr 2013 04:14:45 +0000</pubDate>
		<dc:creator>Luis</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[r]]></category>
		<category><![CDATA[rblogs]]></category>
		<category><![CDATA[teaching]]></category>

		<guid isPermaLink="false">http://www.quantumforest.com/?p=1725</guid>
		<description><![CDATA[It used to be that the one of the first decisions to make when learning to program was between compiled (e.g. C or FORTRAN) and interpreted (e.g. Python) languages. In my opinion these days one would have to be a &#8230; <a href="http://www.quantumforest.com/2013/04/learning-to-code-in-r/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>It used to be that the one of the first decisions to make when learning to program was between compiled (e.g. C or FORTRAN) and interpreted (e.g. Python) languages. In my opinion these days one would have to be a masochist to <em>learn</em> with a compiled language: the extra compilation time and obscure errors are a killer when learning.</p>
<p>Today the decision would be between using a generic interpreted language (e.g. Python) and an interpreted domain specific language (DSL) like R, MATLAB, etc. While <a href="http://www.johndcook.com/blog/">some people</a> prefer generic languages, I&#8217;d argue that immediate feedback and easy accomplishment of useful tasks are a great thing when one is learning something for the first time.</p>
<p>As an example, a while ago my son asked me what I was doing in the computer and I told him that I was programming some analyses in R. I showed that the program was spitting back some numbers and plots, a fact that he found totally unremarkable and uninteresting. I searched in internet and I found <a href="http://scratch.mit.edu/">Scratch</a>, a visual programming language, that let&#8217;s the user moves blocks representing code around and build interactive games: now my son was sold. Together we are learning about loops, control structures and variables, drawing characters, etc. We are programming because the problems are i- much more interesting for him and ii- achievable in a short time frame.</p>
<div id="attachment_1737" class="wp-caption alignnone" style="width: 489px"><a href="http://www.quantumforest.com/wp-content/uploads/2013/04/scratch.jpeg"><img src="http://www.quantumforest.com/wp-content/uploads/2013/04/scratch.jpeg" alt="An example scratch script." width="479" height="438" class="size-full wp-image-1737" /></a><p class="wp-caption-text">An example scratch script.</p></div>
<p>Learning to program for statistics, or other scientific domains for that matter, is not that different from being a kid and learning programming. Having to do too much to get even a mildly interesting result is frustrating and discouraging; it is not that the learner is dumb, but that he has to build too many functions to get a meager reward. This is why I&#8217;d say that you should use whatever language already has a large amount of functionality (&#8216;batteries included&#8217; in Python parlance) for your discipline. Choose rightly and you are half-way there.</p>
<p>&#8216;But&#8217; someone will say, R is not a real language. Sorry, but it is a real <a href="http://www.quantumforest.com/2012/01/r-is-a-language/">language</a> (Turing complete and the whole shebang) with oddities, as any other language, granted. As with human languages, the more you study the easier it gets to learn a new language. In fact, the syntax for many basic constructs in R is highly similar to alternatives:</p>
<pre class="brush: r; title: ; notranslate">
# This is R code
# Loop
for(i in 1:10){
    print(i)
}
#[1] 1
#[1] 2
#[1] 3
#[1] 4
#[1] 5
#[1] 6
#[1] 7
#[1] 8
#[1] 9
#[1] 10

# Control 
if(i == 10) {
    print('It is ten!')
}

#[1] &quot;It is ten!&quot;
</pre>
<pre class="brush: python; title: ; notranslate">
# This is Python code
# Loop
for i in range(1,11): # Python starts indexing from zero
    print(i)

#1
#2
#3
#4
#5
#6
#7
#8
#9
#10

# Control
if i == 10:
    print(&quot;It is ten!&quot;)

#It is ten!
</pre>
<p>By the way, I formatted the code to highlight similarities. Of course there are plenty of differences between the languages, but many of the building blocks (the core ideas if you will) are shared. You learn one and the next one gets easier.</p>
<p>How does one start learning to program? If I look back at 1985 (yes, last millennium) I was struggling to learn how to program in <a href="https://en.wikipedia.org/wiki/BASIC">BASIC</a> when, suddenly, I had an epiphany. The sky opened and I heard a choir singing <a href="http://www.youtube.com/watch?v=aI0P1NnUFxc">György Ligeti&#8217;s Atmosphères</a> (from 2001: a Space Odyssey, you know) and then I realized: we are only breaking problems in little pieces and dealing with them one at the time. I&#8217;ve been breaking problems into little pieces since then. What else did you expect? Anyhow, if you feel that you want to learn how to code in R, or whatever language is the best option for your problems, start small. Just a few lines will do. Read other people&#8217;s code but, again, only small pieces that are supposed to do something small. At this stage is easy to get discouraged because everything is new and takes a lot of time. Don&#8217;t worry: everyone has to go through this process.</p>
<p>Many people struggle vectorizing the code so it runs faster. Again, don&#8217;t worry at the beginning if your code is slow. Keep on writing and learning. Read <del datetime="2013-05-01T00:26:51+00:00">Norman</del> Noam Ross&#8217;s <a href="http://www.noamross.net/blog/2013/4/25/faster-talk.html">FasteR! HigheR! StongeR! — A Guide to Speeding Up R Code for Busy People</a>. The guide is very neat and useful, although you don&#8217;t need super fast code, not yet at least. You need working, readable and reusable code. You may not even need code: actually, try to understand the problem, the statistical theory, before you get coding like crazy. Programming will help you understand your problem a lot better, but you need a helpful starting point.</p>
<p>Don&#8217;t get distracted with the politics of research and repeatability and trendy things like git (noticed that I didn&#8217;t even link to them?). You&#8217;ll learn them in time, once you got a clue about how to program.</p>
<p>P.S. The code used in the examples could be shortened and sped up dramatically (e.g. <code>1:10</code> or <code>range(1, 11)</code>) but it is not the point of this post.<br />
P.S.2. A while ago I wrote <a href="http://www.quantumforest.com/2012/01/r-is-a-language/">R is a language</a>, which could be useful connected to this post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.quantumforest.com/2013/04/learning-to-code-in-r/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Jetsam 3: walking in a soggy day in Christchurch</title>
		<link>http://www.quantumforest.com/2013/04/jetsam-3-walking-in-a-soggy-day/</link>
		<comments>http://www.quantumforest.com/2013/04/jetsam-3-walking-in-a-soggy-day/#comments</comments>
		<pubDate>Sun, 21 Apr 2013 03:08:25 +0000</pubDate>
		<dc:creator>Luis</dc:creator>
				<category><![CDATA[jetsam]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.quantumforest.com/?p=1715</guid>
		<description><![CDATA[It was a rainy day in Christchurch. Looking forward to receive the new windscreen for the recorder.]]></description>
				<content:encoded><![CDATA[<p><iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F88797811"></iframe></p>
<p>It was a rainy day in Christchurch. Looking forward to receive the new windscreen for the recorder.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.quantumforest.com/2013/04/jetsam-3-walking-in-a-soggy-day/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Jetsam 2: bad apples</title>
		<link>http://www.quantumforest.com/2013/04/jetsam-2/</link>
		<comments>http://www.quantumforest.com/2013/04/jetsam-2/#comments</comments>
		<pubDate>Tue, 16 Apr 2013 10:50:15 +0000</pubDate>
		<dc:creator>Luis</dc:creator>
				<category><![CDATA[jetsam]]></category>
		<category><![CDATA[photos]]></category>

		<guid isPermaLink="false">http://www.quantumforest.com/?p=1709</guid>
		<description><![CDATA[]]></description>
				<content:encoded><![CDATA[<div id="attachment_1710" class="wp-caption alignnone" style="width: 594px"><a href="http://www.quantumforest.com/wp-content/uploads/2013/04/bad-apple.jpg"><img src="http://www.quantumforest.com/wp-content/uploads/2013/04/bad-apple-1024x766.jpg" alt="Bad apple (Photo: Luis)." width="584" height="436" class="size-large wp-image-1710" /></a><p class="wp-caption-text">Bad apple (Photo: Luis).</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.quantumforest.com/2013/04/jetsam-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 1.871 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2013-05-23 15:31:29 -->
