typo fixed.
[l3full.git] / l3doc / rpydemo.org
blob34d7bf7619e239aad381b954f387d111c8aa1c1f
1 # -*- org -*- 
2
3 #+LaTeX:    \def\mdash{}{---}
4 #+BEGIN_HTML
5 <div id="toc">
6 <a href="index.html">Up</a>
7 [TABLE-OF-CONTENTS]
8 </div> 
10 <div id="content">
11 #+END_HTML
13    This is the RPy demo from [[http://rpy.sourceforge.net/rpy_demo.html][rpy.sf.net]] adjusted for the L3 worksheet
14    interface[1].  In particular, all =print= statements displaying
15    data are removed (they are not needed with interactive data
16    viewing), and the =print= commands providing header information are
17    turned into outline headers (when viewing data next to the source,
18    there is no need to replicate this information).
20 -----------
22 * Start with the script
23 Starting point for this demo is the script, produced as usual via a
24 text editor; it contains far too much detail to make graphical editing
25 practical: 
27 :if ("outline", "External code"):
28 :    inline '''if 1:
29 :    from rpy import *
30 :    d = r.capabilities()
31 :    if d['png']:
32 :        device = r.png 
33 :        ext='png'
34 :    else:
35 :        device = r.postscript
36 :        ext='ps'
37 :    '''
38
39 :if ("outline", "Parameters"):
40 :    img_width = 400
41 :    img_height = 300
42 :    ed = r.faithful["eruptions"] 
43
44 :if ("outline", "Summary of Old Faithful eruption duration data"):
45 :    r.sink('_summary-%d.txt' % new_id())
46 :    r.print_(r.summary(ed))
47 :    r.sink()
48
49 :if ("outline", "Stem-and-leaf plot of Old Faithful eruption duration data"):
50 :    r.sink('_stemleaf-%d.txt' % new_id())
51 :    r.stem(ed)
52 :    r.sink()
53
54 :if ("outline", "Old Faithful eruptions"):
55 :    file = 'faithful_histogram-%d.%s' % (new_id(), ext)
56 :    device(file, width=img_width, height=img_height)
57 :    r.hist(ed,r.seq(1.6, 5.2, 0.2), prob=1,col="lightgreen",
58 :           main="Old Faithful eruptions",
59 :           xlab="Eruption duration (seconds)")
60 :    r.lines(r.density(ed,bw=0.1),col="orange") 
61 :    r.rug(ed)
62 :    r.dev_off()
63
64 :if ("outline", "Old Faithful eruptions longer than 3 seconds"):
65 :    title = "Empirical cumulative distribution function of \nOld Faithful eruptions longer than 3 seconds"  
66 :    long_ed = filter(lambda x: x > 3, ed)
67 :    file = 'faithful_ecdf-%d.%s' % (new_id(), ext)
68 :    device(file, width=img_width,height=img_height)
69 :    r.library('stepfun')
70 :    r.plot(r.ecdf(long_ed), do_points=0, verticals=1, main = title) 
71 :    x = r.seq(3,5.4,0.01)
72 :    r.lines(r.seq(3,5.4,0.01),
73 :            r.pnorm(r.seq(3,5.4,0.01),
74 :                    mean=r.mean(long_ed), sd=r.sqrt(r.var(long_ed))),
75 :            lty=3, lwd=2, col="red")
76 :    r.dev_off()
77
78 :if ("outline", "Quantile plots"):
79 :    file = 'faithful_qq-%d.%s' % (new_id(), ext)
80 :    device(file, width=img_width, height=img_height)
81 :    r.par(pty="s")
82 :    r.qqnorm(long_ed, col="blue")
83 :    r.qqline(long_ed, col="red")
84 :    r.dev_off()
85
86 :if ("outline", "Shapiro-Wilks normality test of Old Faithful eruptions longer than 3 seconds"):
87 :    r.library('ctest')
88 :    sw = r.shapiro_test(long_ed)
89
90 :if ("outline", "One-sample Kolmogorov-Smirnov test of Old Faithful eruptions longer than 3 seconds"):
91 :    ks = r.ks_test(long_ed,"pnorm", mean=r.mean(long_ed),
92 :                   sd=r.sqrt(r.var(long_ed)))
94 * Move the script into the worksheet
95 This could be loaded all at once but for exploration working with one
96 section at a time is better.  Using =edit > L3 terminal console=, get
97 a console on the terminal RPy was started from.  There are now three
98 windows, the source code, the terminal, and the gui:
100 [[file:images/rpydemo-1.png][file:images/rpydemo-1-sm.png]]
102 The gui accumulates the expressions entered into the terminal.  After
103 copying the first three outlines, this is 
105 [[file:images/rpydemo-2.png][file:images/rpydemo-2-sm.png]]
107 * Examine key-value data
108 By expanding the outline, we see the three code lines; the =r.sink=
109 command put plain text output into a file (which we can inspect if
110 desired).  But to just view the results, a simple menu selection is
111 enough:
113 [[file:images/rpydemo-3.png][file:images/rpydemo-3-sm.png]]
115 This brings the script and its output together in one place and there is
116 no redundant information -- the header describes what we're looking
117 at, without additional =print= statements to provide the same
118 information in the output.
120 * View some graphs 
121 The graphical abilities of R can be used similarly.  Running the whole
122 script first, we can selectively view plots.  In the following image,
123 we see the script section setting plot parameters on the left, with
124 the (generated) file name and contents on the right:
126 [[file:images/rpydemo-4.png][file:images/rpydemo-4-sm.png]]
128 Again, the script and the output are together, so we don't have to
129 manually make the association between several graphs on one side and
130 multiple script fragments on the other.
133 -----------
135 Footnotes: 
137 [1]  This demo requires R and RPy to be installed.  Your system's
138 package handler may provide these, e.g.
139 :   apt-get install python-rpy
140 on Ubuntu or Debian GNU/Linux or
141 :   yum install rpy
142 on Fedora 6 or newer systems.
144 The [[file:index.html#alternate-installation][self-contained L3 installer]] also
145 has rules for installing these locally.  The commands
146 :   cd <path to installer>/bindings/rpy
147 :   make install
148 will download, compile and "install" rpy and dependencies.  Note that
149 a FORTRAN compiler is required.
151 -----------
154 #+HTML: </div> 
155 #+TITLE:     RPy demo in L3
156 #+AUTHOR:    Michael Hohn
157 #+EMAIL:     mhhohn@users.sourceforge.net
158 #+LANGUAGE:  en
159 #+TEXT:      
160 #+OPTIONS:   H:2 num:t toc:2 \n:nil @:t ::t |:t ^:nil f:t *:t TeX:t LaTeX:t skip:nil p:nil
161 # Local Variables:
162 # org-export-html-style: "<link rel=stylesheet href=\"l3style.css\" type=\"text/css\">"
163 # org-format-latex-header: "\\documentclass{article}\n\\usepackage{fullpage}         % do not remove\n\\usepackage{amssymb}\n\\usepackage[usenames]{color}\n\\usepackage{amsmath}\n\\usepackage{latexsym}\n\\usepackage[mathscr]{eucal}\n\\usepackage{bera}\n\\usepackage{textcmds}\n\\pagestyle{empty}             % do not remove"
164 # org-export-latex-packages-alist: (("" "bera"))
165 # org-export-latex-preamble: "\\documentclass[11pt,letter]{article}\n\\usepackage[utf8]{inputenc}\n\\usepackage[T1]{fontenc}\n\\usepackage{hyperref}\n\\usepackage{fullpage}\n\\usepackage[usenames]{color}\n\\usepackage{amsmath}\n\\usepackage{latexsym}\n\\usepackage[mathscr]{eucal}\n\\usepackage{textcmds}"
166 # End: