R, Sweave und TikZdevice Uwe Ziegenhagen 4. März 2010
Was ist R oder: Am Anfang war S S Sprache, entwickelt durch Becker & Chambers von den Bell Labs kommerzialisiert als S-Plus R: GNU Implementation von S 1992 durch R. Ihaka und R. Gentleman mehr als 1 000 Pakete im CRAN >500 Projektmitglieder, R Core Team deckt alle Gebiete der Statistik & Datenanalyse ab Plattformen: Win32, Linux/Unixe, Mac OS http://www.r-project.org
Abbildung: R unter Windows Abbildung: Emacs mit ESS Diverse andere Schnittstellen verfügbar JGR/JRI RCOM RPY
R als Taschenrechner http://cran.r-project.org/doc/manuals/r-intro.pdf 1 1+2 2 1*2 3 1/2 4 1-2 5 2^2 6 sqrt (2) 7 sin (pi) # cos, tan 8 trunc (-pi) # -3 9 round (pi) # 3
R Datenstrukturen Vektoren Vektoren der Länge m, ein Typ Matrizen m n Felder, ein Typ Dataframes Liste von Objekten verschiedener Typen 1 a <- 2 # Variablenzuweisung 2 d <- c (1,2,3,4) 3 a 4 d
Variablen, Vektoren und Matrizen 1 a = 1:3 2 b = 2:4 3 c(a,b) # [1] 1 2 3 2 3 4 4 seq (1,2, by = 0.1) [1] 1.1 1.2 1.3 1.4 1.5... 5 rep (1:4,2) # [1] 1 2 3 4 1 2 3 4
Lineare Modelle 1 > x<-1:10 2 > y= rnorm (10) *x 3 > lm(y~x) 4 5 Call : 6 lm( formula = y ~ x) 7 8 Coefficients : 9 ( Intercept ) x 10 0.1079 1.0697
Einfaches Grafik-Beispiel 1 a<- c (1:10) 2 plot (a) a 2 4 6 8 10 2 4 6 8 10 Index
R Grafiktreiber x11()/x11(), windows(), quartz() for Screen postscript(), pdf(), pictex(), xfig(), win.metafile() devgtk(), devjava(), devsvg() tikzdevice()
Beispiel PDF-Ausgabe 1 pdf ( file = "c:/ punkte. pdf ",width = 6, height = 6, 2 onefile = FALSE, family = " Helvetica ", 3 title = " R Graphics Output ", fonts = NULL, 4 version = " 1.4 ",paper = " special ") 5 6 a<- c (1:10) 7 plot (a) 8 9 dev. off ()
Beispiel PicTeX-Ausgabe Veraltet? 1 pictex ( file = " Rplots. tex ", width = 5, height = 4, 2 debug = FALSE,bg = " white ", fg = " black ") 3 a<- c (1:10) 4 plot (a) 5 6 dev. off ()
tikzdevice http://cran.r-project.org/web/packages/tikzdevice/ index.html R Grafikcode wird direkt in TikZ-Code umgewandelt erstellt eingebettete oder eigenständige Dokumente nutzt die im Dokument genutzten Fonts erlaubt auch Mathematik in Captions und Anmerkungen
Beispiel TikZ-Device 1 tikz ( file = "c:/ test2. tex ",standalone =F) 2 # StandAlone =T 3 plot (1:10) 4 5 dev. off ()
Generierter Code (Ausschnitt) 1 % Created by tikzdevice 2 \ begin { tikzpicture }[x=1pt,y=1pt] 3 \draw [ color=white, opacity=0] (0,0) rectangle (505.89,505.89) ; 4 \ begin { scope } 5 \path [clip ] ( 49.20, 61.20) rectangle (480.69,456.69) ; 6 \ definecolor [ named ]{ drawcolor }{ rgb }{0.56,0.96,0.51} 7 \ definecolor [ named ]{ fillcolor }{ rgb }{0.13,0.09,0.52} 8 \ definecolor [ named ]{ drawcolor }{ rgb }{0.00,0.00,0.00} 9 \draw [ color=drawcolor,line cap=round,line join=round,fill opacity=0.00,] ( 65.18, 75.85) circle ( 2.25) ; 10 \draw [ color=drawcolor,line cap=round,line join=round,fill opacity=0.00,] (109.57,116.54) circle ( 2.25) ; 11 \draw [ color=drawcolor,line cap=round,line join=round,fill opacity=0.00,] (153.97,157.22) circle ( 2.25) ; 12 \draw [ color=drawcolor,line cap=round,line join=round,fill opacity=0.00,] (198.36,197.91) circle ( 2.25) ; 13 \draw [ color=drawcolor,line cap=round,line join=round,fill opacity=0.00,] (242.75,238.60) circle ( 2.25) ; 14 \draw [ color=drawcolor,line cap=round,line join=round,fill opacity=0.00,] (287.14,279.29) circle ( 2.25) ; 15 \draw [ color=drawcolor,line cap=round,line join=round,fill opacity=0.00,] (331.53,319.98) circle ( 2.25) ; 16 \draw [ color=drawcolor,line cap=round,line join=round,fill opacity=0.00,] (375.92,360.67) circle ( 2.25) ; 17 \draw [ color=drawcolor,line cap=round,line join=round,fill opacity=0.00,] (420.32,401.35) circle ( 2.25) ; 18 \draw [ color=drawcolor,line cap=round,line join=round,fill opacity=0.00,] (464.71,442.04) circle ( 2.25) ; 19 \end { scope } 20 \ begin { scope }
Sweave Reproducible Research entwickelt von Friedrich Leisch, LMU München Teil des utils Paket (Standard) L A TEX-Dokument enthält TEX und R R Code wird in noweb-syntax eingebettet noweb: literate programming Werkzeug von Norman Ramsey TeX-Datei wird mit Endung.nw gespeichert in R: Aufruf von Sweave("<Dateiname.nw>") 1 latex/pdflatex auf die erstellte TEX-Datei 1 Stangle extrahiert den R-Code
Sweave: example1.nw 1 \ documentclass { scrartcl } 2 \ title { Sweave } 3 \ author { Uwe Ziegenhagen } 4 \ begin { document } 5 6 \ maketitle 7 8 <<>>= 9 1+1 10 @ 11 \ end { document }
Generierter TEX-Code 1 \ documentclass { scrartcl } 2 3 \ title { Sweave } 4 \ author { Uwe Ziegenhagen } 5 \ usepackage { Sweave } 6 \ begin { document } 7 8 \ maketitle 9 10 \ begin { Schunk } 11 \ begin { Sinput } 12 > 1 + 1 13 \ end { Sinput } 14 \ begin { Soutput } 15 [1] 2 16 \ end { Soutput } 17 \ end { Schunk } 18 \ end { document }
Ergebnis des Minimalbeispiels Sweave Uwe Ziegenhagen February 28, 2010 > 1 + 1 [1] 2
Sweave: Optionen für den = Teil echo=false unterdrückt Quellcode results=hide unterdrückt Ergebnisse results=tex unterdrückt verbatim-ausgabe fig=true Ausgabe ist eine Grafik PDF/EPS Dateien pdf=true Erstelle PDF-Version eps=true Erstelle EPS-Version width= <x> Bildbreite in Inch height= <y> Bildhöhe in Inch mit \SweaveOpts<Option> können Optionen auch global gesetzt werden. mit «name, opt=...»= können Code-Teile benannt werden Zugriff auf diese Teile dann per «name»
Sweave: \Sexpr<Befehl> Für skalare Ergebnisse kann alternativ \Sexpr \Sexpr<Befehl> R-Rückgabewert muss String oder zu String konvertierbar sein. ideal für Ausgabe von Ergebnissen im Fließtext
Sweave: \Sexpr<Befehl> 1 \ documentclass [ a4paper ]{ scrartcl } 2 3 \ begin { document } 4 5 <<echo=false, results=hide>>= 6 data ( iris ) # load iris data 7 @ 8 9 Der Datensatz hat \ Sexpr {ncol (iris )} Spalten und \ Sexpr {nrow (iris )} Zeilen. 10 11 <<echo = false >>= 12 summary (iris $ Petal. Length ) 13 @ 14 15 <<echo=false, results=tex>>= 16 xtable (lm(iris $ Sepal. Width ~ iris $ Petal. Length ), 17 caption =" Lineares Model von Sepal. Width und Petal. Length ") 18 @ 19 20 \ begin { center } 21 \ begin { figure }[h] 22 <<fig=true, echo=false>>= 23 pch. vec <- c (16,2,3) [ iris $ Species ] 24 col. vec <- c (16,2,3) [ iris $ Species ] 25 plot ( iris$sepal. Width, iris$petal. Length, 26 col = col.vec, pch=pch. vec ) 27 @ 28 \ caption {Plot von iris \$ Petal. Length vs. iris \$ Sepal. Width } 29 \end { figure } 30 \end { center } 31 \end { document }
Ergebnis Der Datensatz hat 5 Spalten und 150 Zeilen. Min. 1st Qu. Median Mean 3rd Qu. Max. 1.000 1.600 4.350 3.758 5.100 6.900 Estimate Std. Error t value Pr(> t ) 3.4549 0.0761 45.40 0.0000 (Intercept) iris$petal.length -0.1058 0.0183-5.77 0.0000 Table 1: Lineares Model von Sepal.Width und Petal.Length iris$petal.length 1 2 3 4 5 6 7 2.0 2.5 3.0 3.5 4.0 iris$sepal.width Figure 1: Plot von iris$petal.length vs. iris$sepal.width
Sweave: Dynamisches Beispiel 1 \ documentclass { scrartcl } 2 \ begin { document } 3 4 <<echo =f, results = hide >>= 5 windows ( width = 8, height = 4) 6 system ("wget -O d.zip http :// www.ecb.int / stats / eurofxref / eurofxref -hist.zip ") 7 zip.file. extract (file=" eurofxref -hist.csv ",zip="d.zip ",unzip="",dir=getwd ()) 8 data= read.csv (" eurofxref -hist.csv ",sep=",",header=true ) 9 @ 10 11 Der Datensatz enthält \ Sexpr {nrow (data )} Kurse, der aktuelle Kurs (\ Sexpr {data $Date [1]}) lautet \ Sexpr {data$usd [1]} 12 13 \ begin { center } 14 \ begin { figure }[h] 15 <<fig=true,echo=false, width=15, height=6>>= 16 plot ( data$usd,t="l", sub=paste ( nrow ( data )," Datensätze von ",data$date [ nrow ( data )]," bis ",data $ Date [1]),asp =) 17 @ 18 \end { figure } 19 \end { center } 20 \end { document }
Ergebnis Der Datensatz enthält 2858 Kurse, der aktuelle Kurs (2010-03-02) lautet 1.3548 data$usd 0.8 1.0 1.2 1.4 1.6 0 500 1000 1500 2000 2500 Index 2858 Datensätze von 1999 01 04 bis 2010 03 02
Literatur zu R R Core Team An Introduction to R http://cran.r-project.org/doc/manuals/r-intro.pdf Uwe Ligges Programmieren mit R Springer-Verlag Michael J. Crawley Statistics An Introduction using R Wiley John Maindonald und John Brown Data Analysis and Graphics Using R Cambridge Peter Dalgaard Introductory Statistics with R Springer-Verlag