<?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>Statistics, R, Graphics and Fun</title>
	<atom:link href="http://yihui.name/en/feed/" rel="self" type="application/rss+xml" />
	<link>http://yihui.name/en</link>
	<description>Yihui XIE</description>
	<lastBuildDate>Thu, 26 Aug 2010 03:32:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Numbered Figure/Table Captions in beamer</title>
		<link>http://yihui.name/en/2010/08/numbered-figure-table-captions-in-beamer/</link>
		<comments>http://yihui.name/en/2010/08/numbered-figure-table-captions-in-beamer/#comments</comments>
		<pubDate>Sun, 22 Aug 2010 20:44:45 +0000</pubDate>
		<dc:creator>Yihui Xie</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[beamer]]></category>
		<category><![CDATA[Caption]]></category>
		<category><![CDATA[Figure]]></category>
		<category><![CDATA[LaTeX]]></category>
		<category><![CDATA[Numbered Caption]]></category>
		<category><![CDATA[Table]]></category>

		<guid isPermaLink="false">http://yihui.name/en/?p=557</guid>
		<description><![CDATA[I&#8217;m really surprised that most beamer slides I&#8217;ve ever seen have Figure/Table captions like this: Figure: blabla blabla or Table: blabla blabla which should have been Figure 1 or Table 2. Why are the caption numbers missing? This is because beamer does not produce these numbers by default. To enable numbered captions, you have to [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m really surprised that <em>most</em> <a title="http://latex-beamer.sourceforge.net/" href="http://latex-beamer.sourceforge.net/" target="_blank">beamer</a> slides I&#8217;ve ever seen have Figure/Table captions like this:</p>
<p style="text-align: center;">Figure: blabla blabla</p>
<p>or</p>
<p style="text-align: center;">Table: blabla blabla</p>
<p>which should have been Figure <strong>1</strong> or Table <strong>2</strong>.</p>
<p>Why are the caption numbers missing? This is because beamer does not produce these numbers by default. To enable numbered captions, you have to put this in the preamble of your LaTeX document:</p>
<pre>\setbeamertemplate{caption}[numbered]</pre>
<p>I&#8217;m a picky LaTeX users&#8230; I cannot stand captions without numbers.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://yihui.name/en/2007/10/slight-changes-to-boadilla-theme-in-beamer-class/" title="Slight Changes to Boadilla Theme in Beamer Class">Slight Changes to Boadilla Theme in Beamer Class</a></li><li><a href="http://yihui.name/en/2010/03/font-families-for-the-r-pdf-device/" title="Font Families for the R PDF Device">Font Families for the R PDF Device</a></li><li><a href="http://yihui.name/en/2010/02/press-enter-in-lyx-sweave-as-you-wish/" title="Press Enter in LyX Sweave as You Wish">Press Enter in LyX Sweave as You Wish</a></li><li><a href="http://yihui.name/en/2009/11/create-animations-in-pdf-documents-using-r/" title="Create Animations in PDF Documents Using R">Create Animations in PDF Documents Using R</a></li><li><a href="http://yihui.name/en/2008/10/how-to-open-a-file-from-a-hyperlink/" title="How to Open a File from a Hyperlink">How to Open a File from a Hyperlink</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://yihui.name/en/2010/08/numbered-figure-table-captions-in-beamer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Auto-completion in Notepad++ for R Script</title>
		<link>http://yihui.name/en/2010/08/auto-completion-in-notepad-for-r-script/</link>
		<comments>http://yihui.name/en/2010/08/auto-completion-in-notepad-for-r-script/#comments</comments>
		<pubDate>Sat, 14 Aug 2010 22:26:54 +0000</pubDate>
		<dc:creator>Yihui Xie</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[R Language]]></category>
		<category><![CDATA[Auto-completion]]></category>
		<category><![CDATA[Notepad++]]></category>

		<guid isPermaLink="false">http://yihui.name/en/?p=543</guid>
		<description><![CDATA[Auto-completion is fancy in a text editor. Notepad++ does not support auto-completion for the R language, so I spent a couple of hours on creating such an XML file to support R: Put it under &#8216;plugins/APIs&#8216; in the installation directory of Notepad++ (you can see several other XML files there supporting different languages such as [...]]]></description>
			<content:encoded><![CDATA[<span class="notice">Update: now the long hints for function parameters can be broken into several shorter lines.</span>
<p>Auto-completion is fancy in a text editor. Notepad++ does not support auto-completion for the R language, so I spent a couple of hours on creating such an XML file to support R:</p>
<span class="download">Download: <a title="http://yihui.name/en/wp-content/uploads/2010/08/R.xml" href="http://yihui.name/en/wp-content/uploads/2010/08/R.xml">R.xml (938Kb)</a></span>
<p>Put it under &#8216;<code>plugins/APIs</code>&#8216; in the installation directory of Notepad++ (you can see several other XML files there supporting different languages such as C), and make sure you have enabled auto-completion in Notepad++ (<code>Settings --&gt; Preferences --&gt; Backup/Auto-completion</code>). Open an R script and start typing a familiar function (e.g. <code>paste()</code>), you will see some candidates in a drop-down list like this:</p>
<div id="attachment_545" class="wp-caption aligncenter" style="width: 376px"><a href="http://yihui.name/en/wp-content/uploads/2010/08/paste-parameters-notepad++.png"><img class="size-full wp-image-545" title="Show parameters of R functions in Notepad++" src="http://yihui.name/en/wp-content/uploads/2010/08/paste-parameters-notepad++.png" alt="Show parameters of R functions in Notepad++" width="366" height="126" /></a><p class="wp-caption-text">Show parameters of R functions in Notepad++</p></div>
<p>Hit the Enter key if the function name selected in the list is correct for you, then type &#8216;<code>(</code>&#8216; and you will see hints for parameters:</p>
<div id="attachment_544" class="wp-caption aligncenter" style="width: 496px"><a href="http://yihui.name/en/wp-content/uploads/2010/08/paste-auto-completion-notepad++.png"><img class="size-full wp-image-544" title="Auto-completion in Notepad++ for R script" src="http://yihui.name/en/wp-content/uploads/2010/08/paste-auto-completion-notepad++.png" alt="Auto-completion in Notepad++ for R script" width="486" height="40" /></a><p class="wp-caption-text">Auto-completion in Notepad++ for R script</p></div>
<p>The file R.xml was actually generated from R; it contains almost all visible R objects in base R packages as well as recommended packages like MASS. You may create an extended XML file (containing keywords from other packages) by yourself after loading the packages you need into your current workspace, and run:</p>
<pre>source('http://yihui.name/en/wp-content/uploads/2010/08/Npp_R_Auto_Completion.r')
# R.xml will be generated under your current work directory: getwd()</pre>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://yihui.name/en/2010/04/r-is-an-epic-fail/" title="R is an Epic Fail?">R is an Epic Fail?</a></li><li><a href="http://yihui.name/en/2010/03/looking-for-software-paths-in-windows-registry/" title="Looking for Software Paths in Windows Registry">Looking for Software Paths in Windows Registry</a></li><li><a href="http://yihui.name/en/2010/03/a-demo-for-the-ratio-estimation/" title="A Demo for the Ratio Estimation in Sampling Survey (Animation)">A Demo for the Ratio Estimation in Sampling Survey (Animation)</a></li><li><a href="http://yihui.name/en/2010/02/press-enter-in-lyx-sweave-as-you-wish/" title="Press Enter in LyX Sweave as You Wish">Press Enter in LyX Sweave as You Wish</a></li><li><a href="http://yihui.name/en/2009/12/happy-new-year-with-r/" title="Happy New Year with R">Happy New Year with R</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://yihui.name/en/2010/08/auto-completion-in-notepad-for-r-script/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>My Talk on Animations at useR! 2010 (NIST, Gaithersburg)</title>
		<link>http://yihui.name/en/2010/07/my-talk-on-animations-at-user-2010/</link>
		<comments>http://yihui.name/en/2010/07/my-talk-on-animations-at-user-2010/#comments</comments>
		<pubDate>Wed, 21 Jul 2010 20:18:43 +0000</pubDate>
		<dc:creator>Yihui Xie</dc:creator>
				<category><![CDATA[R Language]]></category>
		<category><![CDATA[Animation]]></category>
		<category><![CDATA[useR! 2010]]></category>

		<guid isPermaLink="false">http://yihui.name/en/?p=531</guid>
		<description><![CDATA[As every useR knows, the useR! 2010 conference is being held at NIST in Gaithersburg these days. I have just finished my talk on the R package animation this afternoon. Here are my slides and R code for those who are interested: Have fun, even if you are a PhD! Related Postsalphahull: an R Package [...]]]></description>
			<content:encoded><![CDATA[<p>As every useR knows, the useR! 2010 conference is being held at NIST in Gaithersburg these days. I have just finished my talk on the R package <strong>animation</strong> this afternoon. Here are my slides and R code for those who are interested:</p>
<span class="download">Download: <a title="Slides" href="http://yihui.name/en/wp-content/uploads/2010/07/animation-useR2010-Xie.pdf">Slides</a> (1.6M), and <a title="R code" href="http://yihui.name/en/wp-content/uploads/2010/07/animation-useR2010-Xie.txt">R code</a> (3.6K); Note you may need Acrobat Reader to watch the animations inside the slides.</span>
<p>Have fun, even if you are a PhD!</p>
<p style="text-align: center;"><a href="http://animation.yihui.name"><img class="aligncenter" title="R package animation" src="http://yihui.name//en/wp-content/uploads/2010/07/vi-lilac-chaser.gif" alt="" width="480" height="480" /></a></p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://yihui.name/en/2010/04/alphahull-an-r-package-for-alpha-convex-hull/" title="alphahull: an R Package for Alpha-Convex Hull">alphahull: an R Package for Alpha-Convex Hull</a></li><li><a href="http://yihui.name/en/2010/03/a-demo-for-the-ratio-estimation/" title="A Demo for the Ratio Estimation in Sampling Survey (Animation)">A Demo for the Ratio Estimation in Sampling Survey (Animation)</a></li><li><a href="http://yihui.name/en/2009/12/happy-new-year-with-r/" title="Happy New Year with R">Happy New Year with R</a></li><li><a href="http://yihui.name/en/2009/12/merry-christmas-using-r/" title="Merry Christmas Using R">Merry Christmas Using R</a></li><li><a href="http://yihui.name/en/2009/11/create-animations-in-pdf-documents-using-r/" title="Create Animations in PDF Documents Using R">Create Animations in PDF Documents Using R</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://yihui.name/en/2010/07/my-talk-on-animations-at-user-2010/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>alphahull: an R Package for Alpha-Convex Hull</title>
		<link>http://yihui.name/en/2010/04/alphahull-an-r-package-for-alpha-convex-hull/</link>
		<comments>http://yihui.name/en/2010/04/alphahull-an-r-package-for-alpha-convex-hull/#comments</comments>
		<pubDate>Sat, 17 Apr 2010 05:04:36 +0000</pubDate>
		<dc:creator>Yihui Xie</dc:creator>
				<category><![CDATA[R Graphics]]></category>
		<category><![CDATA[ahull()]]></category>
		<category><![CDATA[Alpha-convex hull]]></category>
		<category><![CDATA[alphahull]]></category>
		<category><![CDATA[Animation]]></category>
		<category><![CDATA[Convex Hull]]></category>
		<category><![CDATA[gWidgets]]></category>
		<category><![CDATA[Journal of Statistical Software]]></category>

		<guid isPermaLink="false">http://yihui.name/en/?p=515</guid>
		<description><![CDATA[new paper on the α-convex hull appeared in the Journal of Statistical Software today (http://www.jstatsoft.org/v34/i05/paper). The α-convex hull is an interesting problem which caught my attention long time ago but I didn&#8217;t know a solution then. R has a function chull() which can generate (indices of) the convex hull for a series of points. Now [...]]]></description>
			<content:encoded><![CDATA[<a href="http://yihui.name/en/2010/04/alphahull-an-r-package-for-alpha-convex-hull/"><span class="dropcap-green">A</span></a> new paper on the α-convex hull appeared in the Journal of Statistical Software today (<a href="http://www.jstatsoft.org/v34/i05/paper">http://www.jstatsoft.org/v34/i05/paper</a>). The α-convex hull is an interesting problem which caught my attention long time ago but I didn&#8217;t know a solution then. R has a function <code>chull()</code> which can generate (indices of) the convex hull for a series of points. Now we can use the R package <code>alphahull</code> to compute the α-convex hull. For those who are not familiar with the α-convex hull, the animation below might be a good illustration for the difference between a convex hull and an α-convex hull. Note how the parameter α affects the shape of the hull:</p>
<p><div id="attachment_516" class="wp-caption aligncenter" style="width: 490px"><a href="http://yihui.name/en/wp-content/uploads/2010/04/alpha-convex-hull.gif"><img class="size-full wp-image-516" title="alpha-convex hull with different alpha's" src="http://yihui.name/en/wp-content/uploads/2010/04/alpha-convex-hull.gif" alt="alpha-convex hull with different alpha's" width="480" height="480" /></a><p class="wp-caption-text">alpha-convex hull with different alpha&#39;s</p></div>
<p>The above animation can be reproduced with the code below (uncomment the lines to create a GIF animation with the <code>animation</code> package):<span id="more-515"></span></p>
<pre>set.seed(123)
theta = runif(n &lt;- 300, 0, 2 * pi)
r = sqrt(runif(n, 0.25^2, 0.5^2))
x = cbind(0.5 + r * cos(theta), 0.5 + r * sin(theta))

## library(animation)
## saveMovie({
    library(alphahull)
    par(mar = rep(0, 4), xaxt = "n", yaxt = "n", bg = "black", col = "white")
    for (alpha in seq(0.25, 0, -0.01)) {
        plot(ahull(x, alpha = alpha), pch = 20, col = "white", panel.last = text(0.5,
            0.5, sprintf("alpha = %.2f", alpha), cex = 2))
    }
## }, moviename = "alpha-convex-hull")
</pre>
<p>Again, you may interactively play with the convex hull using the <code>gWidgets</code> package:</p>
<pre>## install.packages('gWidgetsRGtk2') first if not installed
library(gWidgetsRGtk2)
options(guiToolkit = "RGtk2")

g = glayout(container = gwindow("alpha-convex hull demo"))
g[1, 1:2, expand = TRUE] = ggraphics(container = g)
g[2, 1] = "alpha"
g[2, 2, expand = TRUE] = gslider(from = 0, to = 0.3, value = 0.2, by = 0.01,
    container = g, handler = function(h, ...) {
        par(mar = rep(0, 4), xaxt = "n", yaxt = "n")
        plot(ahull(x, alpha = svalue(h$obj)), pch = 20, ann = FALSE)
    })
</pre>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://yihui.name/en/2010/07/my-talk-on-animations-at-user-2010/" title="My Talk on Animations at useR! 2010 (NIST, Gaithersburg)">My Talk on Animations at useR! 2010 (NIST, Gaithersburg)</a></li><li><a href="http://yihui.name/en/2010/04/formatr-farewell-to-ugly-r-code/" title="formatR: farewell to ugly R code">formatR: farewell to ugly R code</a></li><li><a href="http://yihui.name/en/2010/04/demonstrating-the-power-of-f-test-with-gwidgets/" title="Demonstrating the Power of F Test with gWidgets">Demonstrating the Power of F Test with gWidgets</a></li><li><a href="http://yihui.name/en/2010/03/a-demo-for-the-ratio-estimation/" title="A Demo for the Ratio Estimation in Sampling Survey (Animation)">A Demo for the Ratio Estimation in Sampling Survey (Animation)</a></li><li><a href="http://yihui.name/en/2009/12/happy-new-year-with-r/" title="Happy New Year with R">Happy New Year with R</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://yihui.name/en/2010/04/alphahull-an-r-package-for-alpha-convex-hull/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>R is an Epic Fail?</title>
		<link>http://yihui.name/en/2010/04/r-is-an-epic-fail/</link>
		<comments>http://yihui.name/en/2010/04/r-is-an-epic-fail/#comments</comments>
		<pubDate>Thu, 15 Apr 2010 07:35:38 +0000</pubDate>
		<dc:creator>Yihui Xie</dc:creator>
				<category><![CDATA[R Language]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[SAS]]></category>

		<guid isPermaLink="false">http://yihui.name/en/?p=512</guid>
		<description><![CDATA[I came across this blog post just now: The Next Big Thing, and of course these words caught my attention: [...] However, for me personally and for most users, both individual and organizational, the much greater cost of software is the time it takes to install it, maintain it, learn it and document it. On [...]]]></description>
			<content:encoded><![CDATA[<p>I came across this blog post just now: <a rel="nofollow" href="http://www.thejuliagroup.com/blog/?p=433">The  Next Big Thing</a>, and of course these words caught my attention:</p>
<blockquote><p>[...] However, for me personally and for most users, both individual and organizational, the much greater cost of software is the time it takes to install it, maintain it, learn it and document it. On that, R is an epic fail.</p></blockquote>
<p>I don&#8217;t really understand how (much more?) difficult will it be to install and maintain R. Usually it takes about one minute to install it from the binary (and SAS? SPSS? buy it, find a technician, install it, maintain according to different licenses &#8211; single PC or server or other types, continue to pay only tens of thousand dollars next year, &#8230;). For learning, it depends. I don&#8217;t think it is too difficult for people who know well about statistics, and for the rest of people, do they really feel safe to do something they do not understand? For the documentation, some people prefer simple ones and some prefer handbooks (of SAS-style).</p>
<p>In all, I cannot see why R is an epic fail for the above reasons&#8230;</p>
<p>What? Data visualization?&#8230;</p>
<p>The R community must have been tired of comparing SAS with R. Please don&#8217;t tell <a title="http://yihui.name/en/2010/04/rules-of-thumb-to-meet-r-gurus-in-the-help-list/" href="http://yihui.name/en/2010/04/rules-of-thumb-to-meet-r-gurus-in-the-help-list/">Prof Frank Harrell</a> about this post&#8230;</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://yihui.name/en/2010/08/auto-completion-in-notepad-for-r-script/" title="Auto-completion in Notepad++ for R Script">Auto-completion in Notepad++ for R Script</a></li><li><a href="http://yihui.name/en/2010/03/looking-for-software-paths-in-windows-registry/" title="Looking for Software Paths in Windows Registry">Looking for Software Paths in Windows Registry</a></li><li><a href="http://yihui.name/en/2010/03/a-demo-for-the-ratio-estimation/" title="A Demo for the Ratio Estimation in Sampling Survey (Animation)">A Demo for the Ratio Estimation in Sampling Survey (Animation)</a></li><li><a href="http://yihui.name/en/2010/02/press-enter-in-lyx-sweave-as-you-wish/" title="Press Enter in LyX Sweave as You Wish">Press Enter in LyX Sweave as You Wish</a></li><li><a href="http://yihui.name/en/2009/12/happy-new-year-with-r/" title="Happy New Year with R">Happy New Year with R</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://yihui.name/en/2010/04/r-is-an-epic-fail/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>formatR: farewell to ugly R code</title>
		<link>http://yihui.name/en/2010/04/formatr-farewell-to-ugly-r-code/</link>
		<comments>http://yihui.name/en/2010/04/formatr-farewell-to-ugly-r-code/#comments</comments>
		<pubDate>Tue, 13 Apr 2010 16:11:45 +0000</pubDate>
		<dc:creator>Yihui Xie</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[R Programming]]></category>
		<category><![CDATA[formatR]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[gWidgets]]></category>
		<category><![CDATA[gWidgetsRGtk2]]></category>
		<category><![CDATA[R code]]></category>
		<category><![CDATA[tidy.source()]]></category>

		<guid isPermaLink="false">http://yihui.name/en/?p=506</guid>
		<description><![CDATA[It is not uncommon to see messy R code which is almost not human-readable like this: # rotation of the word "Animation" # in a loop; change the angle and color # step by step for (i in 1:360) { # redraw the plot again and again plot(1,ann=FALSE,type="n",axes=FALSE) # rotate; use rainbow() colors text(1,1,"Animation",srt=i,col=rainbow(360)[i],cex=7*i/360) # [...]]]></description>
			<content:encoded><![CDATA[<p>It is not uncommon to see messy R code which is almost not human-readable like this:</p>
<pre> # rotation of the word "Animation"
# in a loop; change the angle and color
# step by step
for (i in 1:360) {
 # redraw the plot again and again
plot(1,ann=FALSE,type="n",axes=FALSE)
# rotate; use rainbow() colors
text(1,1,"Animation",srt=i,col=rainbow(360)[i],cex=7*i/360)
# pause for a while
Sys.sleep(0.01)}
</pre>
<p>Apparently it is pain reading unformatted R code, but on the other hand, it is natural for us to be lazy. I don&#8217;t care about adding spaces or indent to my raw R code &#8212; I&#8217;ll concentrate on programming first and format my code later. The R package &#8216;<code><a href="http://cran.r-project.org/web/packages/formatR/index.html">formatR</a></code>&#8216; is intended to help us format our messy R code. Two lines of R code will show you the graphical interface of <code>formatR</code>:</p>
<pre># formatR depends on RGtk+, will be installed automatically
# please use the latest version of R (&gt;=2.10.1)
install.packages('formatR')
library(formatR)
# or formatR()
</pre>
<p>Then you can either paste your code into the text box or click the &#8220;Open&#8221; button to open an existing R code file. Click the &#8220;Convert&#8221; button and you are done!</p>
<div id="attachment_507" class="wp-caption aligncenter" style="width: 467px"><a href="http://yihui.name/en/wp-content/uploads/2010/04/formatR-screenshot1.png"><img class="size-full wp-image-507" title="formatR: unformatted R code" src="http://yihui.name/en/wp-content/uploads/2010/04/formatR-screenshot1.png" alt="formatR: unformatted R code" width="457" height="312" /></a><p class="wp-caption-text">formatR: unformatted R code</p></div>
<div id="attachment_508" class="wp-caption aligncenter" style="width: 467px"><a href="http://yihui.name/en/wp-content/uploads/2010/04/formatR-screenshot2.png"><img class="size-full wp-image-508" title="formatR: tidy R code" src="http://yihui.name/en/wp-content/uploads/2010/04/formatR-screenshot2.png" alt="formatR: tidy R code" width="457" height="312" /></a><p class="wp-caption-text">formatR: tidy R code</p></div>
<p>There are several options in the &#8220;Preferences&#8221; panel, e.g. you can specify whether to keep comments or blank lines, or specify the width of the formatted R code.</p>
<p>No matter how messy your code looks like, <code>formatR</code> can make it tidy and structured as long as there are no syntax errors in your R code. If you prefer the command line interface, you may want to take a look at the function <code>tidy.source()</code> in the <code><a href="http://animation.yihui.name">animation</a></code> package.</p>
<p>Currently there are problems with the encoding of multi-byte characters, and I have not figured out how to deal with them.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://yihui.name/en/2010/03/r-tips-in-stat-511/" title="R Tips in Stat 511">R Tips in Stat 511</a></li><li><a href="http://yihui.name/en/2010/04/demonstrating-the-power-of-f-test-with-gwidgets/" title="Demonstrating the Power of F Test with gWidgets">Demonstrating the Power of F Test with gWidgets</a></li><li><a href="http://yihui.name/en/2009/03/enhanced-tidy-source/" title="Enhanced tidy.source() (Preserve Some Comments)">Enhanced tidy.source() (Preserve Some Comments)</a></li><li><a href="http://yihui.name/en/2010/04/alphahull-an-r-package-for-alpha-convex-hull/" title="alphahull: an R Package for Alpha-Convex Hull">alphahull: an R Package for Alpha-Convex Hull</a></li><li><a href="http://yihui.name/en/2007/10/jokes-in-statistics-a-talk-to-be-given-in-cueb/" title="&#8220;Jokes in Statistics&#8221; &#8212; A Talk to be Given in CUEB (Slides &#038; Materials)">&#8220;Jokes in Statistics&#8221; &#8212; A Talk to be Given in CUEB (Slides &#038; Materials)</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://yihui.name/en/2010/04/formatr-farewell-to-ugly-r-code/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Rules of Thumb to Meet R Gurus in the Help List</title>
		<link>http://yihui.name/en/2010/04/rules-of-thumb-to-meet-r-gurus-in-the-help-list/</link>
		<comments>http://yihui.name/en/2010/04/rules-of-thumb-to-meet-r-gurus-in-the-help-list/#comments</comments>
		<pubDate>Mon, 05 Apr 2010 19:33:40 +0000</pubDate>
		<dc:creator>Yihui Xie</dc:creator>
				<category><![CDATA[Blabla]]></category>
		<category><![CDATA[R Language]]></category>
		<category><![CDATA[Bill Venables]]></category>
		<category><![CDATA[Brian Ripley]]></category>
		<category><![CDATA[Douglas Bates]]></category>
		<category><![CDATA[Frank Harrell]]></category>
		<category><![CDATA[Martin Maechler]]></category>
		<category><![CDATA[R Gurus]]></category>
		<category><![CDATA[R-help]]></category>
		<category><![CDATA[Rule of Thumb]]></category>

		<guid isPermaLink="false">http://yihui.name/en/?p=503</guid>
		<description><![CDATA[Here is my personal list of rules of thumb for people who want to meet some R gurus (quickly) in the R help mailing list (R-help@R-project.org): If you want to meet Dr Bill Venables, just say something about Type III Sum of Squares (better if you also mention the &#8220;unbeatable&#8221; SAS); If you want to [...]]]></description>
			<content:encoded><![CDATA[<p>Here is my personal list of rules of thumb for people who want to meet some R gurus (quickly) in the R help mailing list (<code>R-help@R-project.org</code>):</p>
<ul>
<li>If you want to meet Dr <strong>Bill Venables</strong>, just say something about <strong>Type III Sum of Squares</strong> (better if you also mention the &#8220;unbeatable&#8221; <strong>SAS</strong>);</li>
<li>If you want to meet Prof <strong>Douglas Bates</strong>, say something about <strong>LSMEANS</strong> (of course, with SAS) and P-values for the fixed effects in <code>lmer()</code> (or wait in the mixed-models group <code>r-sig-mixed-models@r-project.org</code> &#8212; he often shows up there);</li>
<li>If you want to meet Prof <strong>Frank Harrell</strong> Jr, say SAS is unbeatable (or efficient, golden-standard, high-quality graphics, whatever);</li>
<li>If you want to meet Dr <strong>Martin Mächler</strong>, say something like &#8220;I need help on a <strong>library</strong> called ***&#8221; (it is said that he would show up in 5 mins upon such mistakes, but I feel he is tired of correcting people who don&#8217;t know the difference between a &#8220;package&#8221; and a &#8220;library&#8221; now);</li>
<li>If you want to meet Prof <strong>Brian Ripley</strong> (<a title="http://yihui.name/en/2009/10/50000-revisions-committed-to-r/" href="http://yihui.name/en/2009/10/50000-revisions-committed-to-r/" target="_blank">the-professor-on-whom-the-sun-never-sets</a>), well, I guess you can say anything, because he is so devoted to the mailing list that you can see him <a title="http://en.wikipedia.org/wiki/Almost_everywhere" href="http://en.wikipedia.org/wiki/Almost_everywhere" target="_blank">a.e.</a>, but you have to be careful enough not to be &#8220;Ripleyed&#8221;;</li>
</ul>
<p>I&#8217;ve been reading the mailing list for about 2 years, so I may not know enough about all the gurus. Let me know if I missed anyone. The above list is not given for serious purpose, and my real point is I learned a lot from their advice and arguments.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://yihui.name/en/2009/10/50000-revisions-committed-to-r/" title="50000 Revisions Committed to R">50000 Revisions Committed to R</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://yihui.name/en/2010/04/rules-of-thumb-to-meet-r-gurus-in-the-help-list/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Demonstrating the Power of F Test with gWidgets</title>
		<link>http://yihui.name/en/2010/04/demonstrating-the-power-of-f-test-with-gwidgets/</link>
		<comments>http://yihui.name/en/2010/04/demonstrating-the-power-of-f-test-with-gwidgets/#comments</comments>
		<pubDate>Sat, 03 Apr 2010 05:05:59 +0000</pubDate>
		<dc:creator>Yihui Xie</dc:creator>
				<category><![CDATA[R Graphics]]></category>
		<category><![CDATA[R Programming]]></category>
		<category><![CDATA[Theories]]></category>
		<category><![CDATA[F test]]></category>
		<category><![CDATA[gWidgets]]></category>
		<category><![CDATA[gWidgetsRGtk2]]></category>
		<category><![CDATA[Hypothesis Test]]></category>
		<category><![CDATA[Power]]></category>

		<guid isPermaLink="false">http://yihui.name/en/?p=496</guid>
		<description><![CDATA[e know the real distribution of the F statistic in linear models &#8212; it is a non-central F distribution. Under H0, we have a central F distribution. Given 1 &#8211; α, we can compute the probability of (correctly) rejecting H0. I created a simple demo to illustrate how the power changes as other parameters vary, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://yihui.name/en/2010/04/demonstrating-the-power-of-f-test-with-gwidgets/"><span class="dropcap-green">W</span></a>e know the real distribution of the F statistic in linear models &#8212; it is a non-central F distribution. Under H<sub>0</sub>, we have a central F distribution. Given 1 &#8211; α, we can compute the probability of (correctly) rejecting H<sub>0</sub>. I created a simple demo to illustrate how the power changes as other parameters vary, e.g. the degrees of freedoms, the non-central parameter and alpha. Here is the video:</p>
<p style="text-align: center;"><object style="width: 400px; height: 600px;" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="400" height="600" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://vimeo.com/moogaloop.swf?clip_id=10647395&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" /><embed style="width: 400px; height: 600px;" type="application/x-shockwave-flash" width="400" height="600" src="http://vimeo.com/moogaloop.swf?clip_id=10647395&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1"></embed></object></p>
<p style="text-align: center;"><a href="http://vimeo.com/10647395">The Power of F Test</a></p>
<p>And for those who might be interested, here is the code (you need to install the <code>gWidgets</code> package first and I recommend the <code>RGtk2</code> interface). Have fun:<span id="more-496"></span></p>
<pre>## install.packages('gWidgetsRGtk2') first if not installed
if (!require("gWidgetsRGtk2")) install.packages("gWidgetsRGtk2")
if (!require("cairoDevice")) install.packages("cairoDevice")
library(gWidgetsRGtk2)
options(guiToolkit = "RGtk2")
tbl = glayout(container = gwindow("Power of the F Test"),
    spacing = 0)
tbl[1, 1:4, anchor = c(0, 0), expand = TRUE] = g.f = ggraphics(container = tbl,
    expand = TRUE, ps = 11)
tbl[2, 1, anchor = c(1, 0)] = "numerator df"
tbl[2, 2, anchor = c(0, 0), expand = TRUE] = g.dfn = gslider(from = 1,
    to = 50, value = 3, container = tbl, handler = function(h,
        ...) {
        p.Ftest(dfn = svalue(h$obj))
    })
tbl[2, 3, anchor = c(1, 0)] = "denominator df"
tbl[2, 4, anchor = c(0, 0), expand = TRUE] = g.dfd = gslider(from = 1,
    to = 50, value = 20, container = tbl, handler = function(h,
        ...) {
        p.Ftest(dfd = svalue(h$obj))
    })
tbl[3, 1, anchor = c(1, 0)] = "delta^2"
tbl[3, 2, anchor = c(0, 0), expand = TRUE] = g.ncp = gslider(from = 0,
    to = 100, value = 10, container = tbl, handler = function(h,
        ...) {
        p.Ftest(ncp = svalue(h$obj))
    })
tbl[3, 3, anchor = c(1, 0)] = "alpha"
tbl[3, 4, anchor = c(0, 0), expand = TRUE] = g.alpha = gslider(from = 0,
    to = 1, by = 0.01, value = 0.05, container = tbl, handler = function(h,
        ...) {
        p.Ftest(alpha = svalue(h$obj))
    })
tbl[4, 1, anchor = c(1, 0)] = "x range"
tbl[4, 2:4, anchor = c(0, 0), expand = TRUE] = g.xr = gslider(from = 1,
    to = 50, value = 15, container = tbl, handler = function(h,
        ...) {
        p.Ftest(xr = svalue(h$obj))
    })
## draw the graph
p.Ftest = function(dfn = svalue(g.dfn), dfd = svalue(g.dfd),
    ncp = svalue(g.ncp), alpha = svalue(g.alpha), xr = svalue(g.xr)) {
    x = seq(0.001, xr, length.out = 300)
    yc = df(x, dfn, dfd)
    yn = df(x, dfn, dfd, ncp = ncp)
    par(mar = c(4.5, 4, 1, 0.05))
    plot(x, yc, type = "n", ylab = "Density", ylim = c(0, max(yc,
        yn)))
    xq = qf(1 - alpha, dfn, dfd)
    polygon(c(xq, x[x &gt;= xq], xr), c(0, yn[x &gt; xq], 0), col = "gray",
        border = NA)
    lines(x, yc, lty = 1)
    lines(x, yn, lty = 2)
    legend("topright", c(as.expression(substitute(F[list(df1,
        df2)] ~ " density", list(df1 = dfn, df2 = dfd))), as.expression(substitute(F[list(df1,
        df2)](ncp) ~ " density", list(df1 = dfn, df2 = dfd, ncp = ncp))),
        as.expression(substitute("Power = " ~ p, list(p = round(1 -
            pf(xq, dfn, dfd, ncp = ncp), 4))))), lty = c(1:2,
        NA), fill = c(NA, NA, "gray"), border = NA, bty = "n")
    return(1 - pf(xq, dfn, dfd, ncp = ncp))
}
p.Ftest()
</pre>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://yihui.name/en/2010/04/formatr-farewell-to-ugly-r-code/" title="formatR: farewell to ugly R code">formatR: farewell to ugly R code</a></li><li><a href="http://yihui.name/en/2010/03/r-tips-in-stat-511/" title="R Tips in Stat 511">R Tips in Stat 511</a></li><li><a href="http://yihui.name/en/2010/04/alphahull-an-r-package-for-alpha-convex-hull/" title="alphahull: an R Package for Alpha-Convex Hull">alphahull: an R Package for Alpha-Convex Hull</a></li><li><a href="http://yihui.name/en/2008/09/eyeball-test-to-identify-an-unfair-coin/" title="&#8220;Eyeball Test&#8221; to Identify An Unfair Coin (or A False Record)">&#8220;Eyeball Test&#8221; to Identify An Unfair Coin (or A False Record)</a></li><li><a href="http://yihui.name/en/2008/01/structural-equation-modeling-where-are-advantages/" title="Structural Equation Modeling: Where Are Advantages?">Structural Equation Modeling: Where Are Advantages?</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://yihui.name/en/2010/04/demonstrating-the-power-of-f-test-with-gwidgets/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Looking for Software Paths in Windows Registry</title>
		<link>http://yihui.name/en/2010/03/looking-for-software-paths-in-windows-registry/</link>
		<comments>http://yihui.name/en/2010/03/looking-for-software-paths-in-windows-registry/#comments</comments>
		<pubDate>Sun, 28 Mar 2010 20:58:20 +0000</pubDate>
		<dc:creator>Yihui Xie</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[R Programming]]></category>
		<category><![CDATA[ImageMagick]]></category>
		<category><![CDATA[OpenBUGS]]></category>
		<category><![CDATA[PATH variable]]></category>
		<category><![CDATA[R Language]]></category>
		<category><![CDATA[readRegistry()]]></category>
		<category><![CDATA[Windows Registry]]></category>

		<guid isPermaLink="false">http://yihui.name/en/?p=484</guid>
		<description><![CDATA[hen we want to call external programs in R under Windows, we often need to know the paths of these programs. For instance, we may want to know where ImageMagick is installed, as we need the convert (convert.exe) utility to convert images to other formats, or where OpenBUGS is installed because we need this path [...]]]></description>
			<content:encoded><![CDATA[<a href="http://yihui.name/en/2010/03/looking-for-software-paths-in-windows-registry/"><span class="dropcap-brown">W</span></a>hen we want to call external programs in R under Windows, we often need to know the paths of these programs. For instance, we may want to know where ImageMagick is installed, as we need the <code>convert</code> (<code>convert.exe</code>) utility to convert images to other formats, or where OpenBUGS is installed because we need this path to use the function <code>bugs()</code>. Usually this problem does not exist under Linux, because the executables (or their symbolic links) are often put in the directories which are in the environment variable <code>PATH</code> (e.g. <code>/usr/bin</code>, <code>/usr/local/bin</code>).</p>
<p>However, we may be able to find the paths through the registry if the installation will save the path info in the registry hive. The R function is <code>readRegistry()</code>:</p>
<pre>## ImageMagick:
## I used this trick in the function saveMovie (the animation package)
&gt; readRegistry("SOFTWARE\\ImageMagick\\Current")
$BinPath
[1] "C:\\Program Files\\ImageMagick"
$CoderModulesPath
[1] "C:\\Program Files\\ImageMagick\\modules\\coders"
$ConfigurePath
[1] "C:\\Program Files\\ImageMagick\\config"
$FilterModulesPath
[1] "C:\\Program Files\\ImageMagick\\modules\\filters"
$LibPath
[1] "C:\\Program Files\\ImageMagick"
$QuantumDepth
[1] 16
$Version
[1] "6.3.8"

## OpenBUGS
&gt; r = names(readRegistry("Software\\Microsoft\\Windows\\ShellNoRoam\\MUICache",
+    "HCU"))
&gt; dirname(r[grep("OpenBUGS\\.exe", r)])
[1] "C:/Program Files/OpenBUGS"
</pre>
<p>There is no guarantee for this approach to work on any Windows platforms, but I think this is better than explaining what is the <code>PATH</code> variable to some Windows users&#8230;<span id="more-484"></span></p>
<h2>Examples</h2>
<p>Here I show two examples for ImageMagick and OpenBUGS respectively. Make sure you are using Windows and have already installed ImageMagick and OpenBUGS. The first example shows how to find ImageMagick and call it to convert a sequence of images (generated by R) to a GIF animation:</p>
<pre>## find the bin path of ImageMagick
magick.path = readRegistry("SOFTWARE\\ImageMagick\\Current")$BinPath

if (nzchar(magick.path) &amp;&amp; require(animation)) {
    convert = shQuote(normalizePath(file.path(magick.path, "convert.exe")))

    ## record the demo 'recur.tree' in png images
    png("magickEx%d.png")
    demo(recur.tree)
    dev.off()

    ## call convert.exe to convert png images to GIF
    cmd = paste(sprintf("%s -delay", convert), 100, "-loop 0 magickEx*.png magickEx.gif")

    ## try to open the GIF animation
    if (system(cmd) == 0)
        shell.exec(file.path(getwd(), "magickEx.gif"))
}
</pre>
<p>You should be able to see the output as this:</p>
<p><div id="attachment_489" class="wp-caption aligncenter" style="width: 490px"><a href="http://yihui.name/en/wp-content/uploads/2010/03/magickEx.gif"><img class="size-full wp-image-489" title="ImageMagick Example" src="http://yihui.name/en/wp-content/uploads/2010/03/magickEx.gif" alt="ImageMagick Example" width="480" height="480" /></a><p class="wp-caption-text">ImageMagick Example</p></div>
<p>OpenBUGS example:</p>
<pre>r = names(readRegistry("Software\\Microsoft\\Windows\\ShellNoRoam\\MUICache",
    "HCU"))
bugsDIR = dirname(r[grep("OpenBUGS\\.exe", r)])
if (length(bugsDIR == 1)) {
    library(R2WinBUGS)
    library(BRugs)
    ## this example comes from ?bugs in R2WinBUGS
    model.file = system.file(package = "R2WinBUGS", "model",
        "schools.txt")
    data(schools)
    J = nrow(schools)
    y = schools$estimate
    sigma.y = schools$sd
    data = list("J", "y", "sigma.y")
    inits = function() {
        list(theta = rnorm(J, 0, 100), mu.theta = rnorm(1, 0,
            100), sigma.theta = runif(1, 0, 100))
    }
    parameters = c("theta", "mu.theta", "sigma.theta")
    schools.sim = bugs(data, inits, parameters, model.file,
        n.chains = 3, n.iter = 5000, bugs.directory = bugsDIR,
        program = "OpenBUGS")
    print(schools.sim)
    plot(schools.sim)
}
</pre>
<p>You might now be able to run the above examples in your Windows version, and I&#8217;ll appreciate feedbacks from Vista and Win7 users. If the above approaches do not work, you may run the command <code>regedit</code> (Start &#8211;&gt; Run) and search for ImageMagick/OpenBUGS, then change my code accordingly, because the locations of the registry hive for ImageMagick/OpenBUGS may not be the same for different versions of Windows. My session info is as below:</p>
<pre>&gt; sessionInfo()
R version 2.10.1 (2009-12-14)
i386-pc-mingw32 

locale:
[1] LC_COLLATE=Chinese_People's Republic of China.936  LC_CTYPE=Chinese_People's Republic of China.936
[3] LC_MONETARY=Chinese_People's Republic of China.936 LC_NUMERIC=C
[5] LC_TIME=Chinese_People's Republic of China.936    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] BRugs_0.5-3          R2WinBUGS_2.1-16     coda_0.13-5          lattice_0.18-3
[5] formatR_0.1-1        gWidgetsRGtk2_0.0-64 gWidgets_0.0-40      animation_1.1-2
[9] MASS_7.3-5          

loaded via a namespace (and not attached):
[1] grid_2.10.1   RGtk2_2.12.18 tools_2.10.1

&gt; Sys.info()[1:3]
 sysname                      release                      version
 "Windows"                         "XP" "build 2600, Service Pack 3"
</pre>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://yihui.name/en/2010/08/auto-completion-in-notepad-for-r-script/" title="Auto-completion in Notepad++ for R Script">Auto-completion in Notepad++ for R Script</a></li><li><a href="http://yihui.name/en/2010/04/r-is-an-epic-fail/" title="R is an Epic Fail?">R is an Epic Fail?</a></li><li><a href="http://yihui.name/en/2010/03/a-demo-for-the-ratio-estimation/" title="A Demo for the Ratio Estimation in Sampling Survey (Animation)">A Demo for the Ratio Estimation in Sampling Survey (Animation)</a></li><li><a href="http://yihui.name/en/2010/02/press-enter-in-lyx-sweave-as-you-wish/" title="Press Enter in LyX Sweave as You Wish">Press Enter in LyX Sweave as You Wish</a></li><li><a href="http://yihui.name/en/2009/12/happy-new-year-with-r/" title="Happy New Year with R">Happy New Year with R</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://yihui.name/en/2010/03/looking-for-software-paths-in-windows-registry/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>A Demo for the Ratio Estimation in Sampling Survey (Animation)</title>
		<link>http://yihui.name/en/2010/03/a-demo-for-the-ratio-estimation/</link>
		<comments>http://yihui.name/en/2010/03/a-demo-for-the-ratio-estimation/#comments</comments>
		<pubDate>Wed, 24 Mar 2010 18:35:28 +0000</pubDate>
		<dc:creator>Yihui Xie</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[R Graphics]]></category>
		<category><![CDATA[Theories]]></category>
		<category><![CDATA[Animation]]></category>
		<category><![CDATA[R Language]]></category>
		<category><![CDATA[Ratio Estimation]]></category>
		<category><![CDATA[sampling survey]]></category>

		<guid isPermaLink="false">http://yihui.name/en/?p=478</guid>
		<description><![CDATA[mber Watkins gave me a suggestion on the animation for the ratio estimation, and I think this is a good topic for my animation package. I&#8217;ve finished writing the initial version of the function sample.ratio() for this package, which will appear in the version 1.1-2 a couple of days later. As we know, the benefit [...]]]></description>
			<content:encoded><![CDATA[<a href="http://yihui.name/en/2010/03/a-demo-for-the-ratio-estimation/"><span class="dropcap-grey">A</span></a>mber Watkins gave me a suggestion on the animation for the ratio estimation, and I think this is a good topic for my <a href="http://cran.r-project.org/package=animation" target="_blank"><code>animation</code></a> package. I&#8217;ve finished writing the initial version of the function <code>sample.ratio()</code> for this package, which will appear in the version 1.1-2 a couple of days later.</p>
<p>As we know, the benefit of ratio estimation is that sampling skewness may be  adjusted for, because the estimation of <img src="http://www.forkosh.dreamhost.com/mimetex.cgi?%5Creverse%20%5Cbar%7BY%7D" title="\bar{Y}" alt="\bar{Y}" align="absmiddle" /> will make use of the information in the relationship of <em>X</em> and <em>Y</em>: <img src="http://www.forkosh.dreamhost.com/mimetex.cgi?%5Creverse%20%5Cbar%7BX%7D%20%5Ccdot%20%28%5Cbar%7By%7D%2F%5Cbar%7Bx%7D%29" title="\bar{X} \cdot (\bar{y}/\bar{x})" alt="\bar{X} \cdot (\bar{y}/\bar{x})" align="absmiddle" />. Here is a demo (we can see the ratio estimate, denoted by the red line, generally performs better than <img src="http://www.forkosh.dreamhost.com/mimetex.cgi?%5Creverse%20%5Cbar%7By%7D" title="\bar{y}" alt="\bar{y}" align="absmiddle" />):</p>
<p><div id="attachment_480" class="wp-caption aligncenter" style="width: 490px"><a href="http://yihui.name/en/wp-content/uploads/2010/03/ratio-estimation.gif"><img class="size-full wp-image-480" title="An animation demo for the ratio estimation" src="http://yihui.name/en/wp-content/uploads/2010/03/ratio-estimation.gif" alt="An animation demo for the ratio estimation" width="480" height="480" /></a><p class="wp-caption-text">An animation demo for the ratio estimation</p></div>
<p><span id="more-478"></span></p>
<p>And here is the code:</p>
<pre>## To appear in animation 1.1-2: see documentation there
sample.ratio = function(x = runif(50, 0, 5), R = 1,
    y = R * x + rnorm(x), size = length(x)/2, p.col = c("blue",
        "red"), p.cex = c(1, 3), p.pch = c(20, 21), m.col = c("black",
        "gray"), legend.loc = "topleft", ...) {
    nmax = ani.options("nmax")
    interval = ani.options("interval")
    N = length(x)
    for (i in 1:nmax) {
        idx = sample(N, size)
        plot(x, y, col = p.col[1], pch = p.pch[1], cex = p.cex[1],
            ...)

        points(x[idx], y[idx], col = p.col[2], pch = p.pch[2],
            cex = p.cex[2])
        abline(v = c(mean(x), mean(x[idx])), h = c(mean(y), mean(y[idx])),
            col = m.col, lty = c(2, 1))
        abline(h = mean(x) * mean(y[idx])/mean(x[idx]), col = p.col[2])
        legend(legend.loc, expression(bar(X), bar(x), bar(X) %.%
            (bar(y)/bar(x)), bar(Y), bar(y)), lty = c(2, 1, 1,
            2, 1), col = c(m.col[c(1, 2)], p.col[2], m.col[c(1,
            2)]), bty = "n", ncol = 2)
        Sys.sleep(interval)
    }
}

library(animation)

sample.ratio()

## Save as an HTML page
ani.start()
sample.ratio(c(runif(50, 0, 2), runif(50, 4, 6)), size = 20)
ani.stop()

## Reproduce the above GIF animation
saveMovie({
    set.seed(123)
    sample.ratio(c(runif(50, 0, 2), runif(50, 4, 6)), size = 20)
}, moviename = "ratio-estimation", para = list(mar = c(4, 4,
    1, 0.5), mgp = c(2, 1, 0)))
</pre>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://yihui.name/en/2007/10/animations-in-survey-sampling/" title="Animations in Survey Sampling">Animations in Survey Sampling</a></li><li><a href="http://yihui.name/en/2009/12/happy-new-year-with-r/" title="Happy New Year with R">Happy New Year with R</a></li><li><a href="http://yihui.name/en/2009/12/merry-christmas-using-r/" title="Merry Christmas Using R">Merry Christmas Using R</a></li><li><a href="http://yihui.name/en/2009/11/create-animations-in-pdf-documents-using-r/" title="Create Animations in PDF Documents Using R">Create Animations in PDF Documents Using R</a></li><li><a href="http://yihui.name/en/2009/10/50000-revisions-committed-to-r/" title="50000 Revisions Committed to R">50000 Revisions Committed to R</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://yihui.name/en/2010/03/a-demo-for-the-ratio-estimation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
