R: a nearly-Lisp Christophe Rhodes Teclo Networks AG April 6, 2011 - - PowerPoint PPT Presentation

r a nearly lisp
SMART_READER_LITE
LIVE PREVIEW

R: a nearly-Lisp Christophe Rhodes Teclo Networks AG April 6, 2011 - - PowerPoint PPT Presentation

R: a nearly-Lisp Christophe Rhodes Teclo Networks AG April 6, 2011 1 / 29 Outline Introduction Examples Repeated Measurement Trellis Graphics R and Lisp 2 / 29 Outline Introduction Examples Repeated Measurement Trellis Graphics R


slide-1
SLIDE 1

R: a nearly-Lisp

Christophe Rhodes

Teclo Networks AG

April 6, 2011

1 / 29

slide-2
SLIDE 2

Outline

Introduction Examples Repeated Measurement Trellis Graphics R and Lisp

2 / 29

slide-3
SLIDE 3

Outline

Introduction Examples Repeated Measurement Trellis Graphics R and Lisp

3 / 29

slide-4
SLIDE 4

Introduction

History and Background: R

“a free software environment for statistical computing and graphics”

4 / 29

slide-5
SLIDE 5

Introduction

History and Background: R

“a free software environment for statistical computing and graphics”

◮ “free”:

  • 1. you don’t have to pay for it;
  • 2. you are (broadly) free to modify it for your own purposes;
  • 3. you don’t get to whine at the R developers if it doesn’t work

for you (unless you pay for support).

4 / 29

slide-6
SLIDE 6

Introduction

History and Background: R

“a free software environment for statistical computing and graphics”

◮ “free”:

  • 1. you don’t have to pay for it;
  • 2. you are (broadly) free to modify it for your own purposes;
  • 3. you don’t get to whine at the R developers if it doesn’t work

for you (unless you pay for support).

◮ “statistical computing”

  • 1. modelling, tests, time-series analysis, classification, clustering,

and so on

  • 2. typical strength: vector computations on large datasets,

provided with BLAS and LAPACK

4 / 29

slide-7
SLIDE 7

Introduction

History and Background: R

“a free software environment for statistical computing and graphics”

◮ “free”:

  • 1. you don’t have to pay for it;
  • 2. you are (broadly) free to modify it for your own purposes;
  • 3. you don’t get to whine at the R developers if it doesn’t work

for you (unless you pay for support).

◮ “statistical computing”

  • 1. modelling, tests, time-series analysis, classification, clustering,

and so on

  • 2. typical strength: vector computations on large datasets,

provided with BLAS and LAPACK

◮ “graphics”

  • 1. many predefined graphical facilities;
  • 2. publication-quality output.

4 / 29

slide-8
SLIDE 8

Introduction

History and Background: R

Abbreviated timeline:

◮ S (John Chambers): similar to but not exactly like Scheme ◮ Public R release in 1993; Free Software in 1995 ◮ Core group formed in 1997 ◮ R version 1.0.0 released in 2000 ◮ biannual releases continue

5 / 29

slide-9
SLIDE 9

Introduction

History and Background: R

Abbreviated timeline:

◮ S (John Chambers): similar to but not exactly like Scheme ◮ Public R release in 1993; Free Software in 1995 ◮ Core group formed in 1997 ◮ R version 1.0.0 released in 2000 ◮ biannual releases continue

Compare with:

◮ S-PLUS (commercial release of S) ◮ SAS, Stata, JAGS ◮ Scilab, Octave, Matlab ◮ Gnuplot, Spreadsheets

5 / 29

slide-10
SLIDE 10

Introduction

History and Background: R

Web:

◮ R home page: http://www.r-project.org/ ◮ Emacs Speaks Statistics: http://ess.r-project.org/ ◮ Comprehensive R Archive Network:

http://cran.r-project.org/

◮ R Journal: http://journal.r-project.org/ ◮ StackOverflow:

http://stackoverflow.com/questions/tagged/r

◮ RSeek: http://www.rseek.org/

Mail / News:

◮ R help: r-help@r-project.org /

gmane.comp.lang.r.general

◮ ESS help: ess-help@stat.math.ethz.ch /

gmane.emacs.ess.general

6 / 29

slide-11
SLIDE 11

Introduction

History and Background: Me

Mathematics Physics

7 / 29

slide-12
SLIDE 12

Introduction

History and Background: Me

Mathematics Physics (Lisp) Hacking

7 / 29

slide-13
SLIDE 13

Introduction

History and Background: Me

Mathematics Physics (Lisp) Hacking Music Information Retrieval

7 / 29

slide-14
SLIDE 14

Introduction

History and Background: Me

Mathematics Physics (Lisp) Hacking Music Information Retrieval today

7 / 29

slide-15
SLIDE 15

Introduction

R syntax

Very close to the original S:

◮ constants: numeric (1, 3:5, 4.2) and text ("foo") ◮ operators: arithmetic (+, *, %*%) and logical (<, &, %in%) ◮ function calls:

◮ seq(1,10) ◮ seq(from=1, 10) ◮ seq(to=10, from=1) ◮ seq(1, 10, by=1)

◮ assignment: <- (also =) ◮ loop constructs: while, for ◮ conditional expressions: if (but see also ifelse)

8 / 29

slide-16
SLIDE 16

R data types

◮ vectors

◮ character ◮ numeric ◮ double ◮ integer ◮ complex ◮ logical

◮ list (generic vectors, dotted pairs) ◮ data frames ◮ attributes

9 / 29

slide-17
SLIDE 17

Introduction

R semantics

Function calls and scope:

◮ lexical binding ◮ (abbreviatable) keyword arguments ◮ lazy argument evaluation ◮ split-horizon scoping ◮ copy-on-write modification

◮ <<- to override

◮ first-class environments, argument to eval

10 / 29

slide-18
SLIDE 18

Outline

Introduction Examples Repeated Measurement Trellis Graphics R and Lisp

11 / 29

slide-19
SLIDE 19

Outline

Introduction Examples Repeated Measurement Trellis Graphics R and Lisp

12 / 29

slide-20
SLIDE 20

Examples

Repeated Measurement

Motivation:

◮ Simple example ◮ Introduction to functionality

13 / 29

slide-21
SLIDE 21

Examples

Repeated Measurement

Motivation:

◮ Simple example ◮ Introduction to functionality ◮ Single most useful thing to know about measurement

13 / 29

slide-22
SLIDE 22

Examples

Repeated Measurement

Setup:

◮ Some quantity that we want to measure; ◮ Measurement is noisy.

◮ could be ‘random noise’ in our equipment; ◮ could be other systematic effects;

More specifically:

◮ measure: {} → µ + ǫ ◮ ǫ ∼ D(0, σ2) ◮ Cov(ǫi, ǫj) = 0

14 / 29

slide-23
SLIDE 23

Examples

Repeated Measurement

What do we expect when we take a measurement?

◮ a value somewhere near the ‘true’ value; ◮ but could be a long way away; ◮ in general, don’t even know how much noise there is.

Everyone knows what to do: take more measurements and average...

15 / 29

slide-24
SLIDE 24

Examples

Repeated Measurement

What do we expect when we take a measurement?

◮ a value somewhere near the ‘true’ value; ◮ but could be a long way away; ◮ in general, don’t even know how much noise there is.

Everyone knows what to do: take more measurements and average...

◮ ...but why?

15 / 29

slide-25
SLIDE 25

Examples

Repeated Measurement

We expect that the average we compute is, on average, the true value: E

  • 1

N

N

  • i

xi

  • = 1

N

N

  • i

E (xi) = µ

16 / 29

slide-26
SLIDE 26

Examples

Repeated Measurement

We expect that the average we compute is, on average, the true value: E

  • 1

N

N

  • i

xi

  • = 1

N

N

  • i

E (xi) = µ What is the variance about this true value? Var

  • 1

N

N

  • i

xi

  • = 1

N2 Var N

  • i

xi

  • = 1

N2 × Nσ2 = σ2 N

16 / 29

slide-27
SLIDE 27

Examples

Repeated Measurement

We expect that the average we compute is, on average, the true value: E

  • 1

N

N

  • i

xi

  • = 1

N

N

  • i

E (xi) = µ What is the variance about this true value? Var

  • 1

N

N

  • i

xi

  • = 1

N2 Var N

  • i

xi

  • = 1

N2 × Nσ2 = σ2 N Standard deviation of the average scales as

1 √ N

16 / 29

slide-28
SLIDE 28

Examples

Repeated Measurement

We expect that the average we compute is, on average, the true value: E

  • 1

N

N

  • i

xi

  • = 1

N

N

  • i

E (xi) = µ What is the variance about this true value? Var

  • 1

N

N

  • i

xi

  • = 1

N2 Var N

  • i

xi

  • = 1

N2 × Nσ2 = σ2 N Standard deviation of the average scales as

1 √ N

[wait a minute, this was meant to be a talk about R]

16 / 29

slide-29
SLIDE 29

Outline

Introduction Examples Repeated Measurement Trellis Graphics R and Lisp

17 / 29

slide-30
SLIDE 30

Examples

Trellis Graphics

Motivation:

◮ Clear display of complex, multivariate information ◮ Rapid experimentation ◮ Adequate defaults, hooks everywhere

18 / 29

slide-31
SLIDE 31

Examples

Trellis Graphics

Motivation:

◮ Clear display of complex, multivariate information ◮ Rapid experimentation ◮ Adequate defaults, hooks everywhere ◮ Teach how not to lie with statistics ◮ Defeat ‘bad graph of the week’ syndrome

18 / 29

slide-32
SLIDE 32

Examples

Trellis Graphics

Distinct graphical and graphing system, originally for S+:

◮ Multipanel Conditioning ◮ Banking to 45° ◮ Automation ◮ Customization

Becker, R. A. and Cleveland, W. S., S-PLUS Trellis Graphics User’s Manual, Seattle: MathSoft, Inc., Murray Hill: Bell Labs, 1996.

19 / 29

slide-33
SLIDE 33

Examples

Trellis Graphics: Multipanel Conditioning dotplot(variety~yield|site, data = barley, groups = year, key = simpleKey(levels(barley$year), space = "right"), xlab = "yield")

20 / 29

slide-34
SLIDE 34

Examples

Trellis Graphics: Multipanel Conditioning dotplot(variety~yield|site, data = barley, groups = year, key = simpleKey(levels(barley$year), space = "right"), xlab = "yield")

yield

Svansota

  • No. 462

Manchuria

  • No. 475

Velvet Peatland Glabron

  • No. 457

Wisconsin No. 38 Trebi 20 30 40 50 60

  • Grand Rapids
  • Duluth

20 30 40 50 60

  • University Farm

Svansota

  • No. 462

Manchuria

  • No. 475

Velvet Peatland Glabron

  • No. 457

Wisconsin No. 38 Trebi

  • Morris

20 30 40 50 60

  • Crookston
  • Waseca

1932 1931

  • 20 / 29
slide-35
SLIDE 35

Examples

Trellis Graphics: Banking to 45°

xyplot(sunspot.year) xyplot(sunspot.year, aspect="xy")

21 / 29

slide-36
SLIDE 36

Examples

Trellis Graphics: Banking to 45°

xyplot(sunspot.year) xyplot(sunspot.year, aspect="xy")

Yearly Sunspots

sunspot.year

1700 1750 1800 1850 1900 1950 2000 50 100 150

Year sunspot.year

1700 1750 1800 1850 1900 1950 2000 100

21 / 29

slide-37
SLIDE 37

Examples

Trellis Graphics: Automation

useOuterStrips( xyplot(log10(counts)~hour|weekday+month, data=biocAccess, type=c("p", "a"), as.table=TRUE, pch=".", cex=2))

22 / 29

slide-38
SLIDE 38

Examples

Trellis Graphics: Automation

useOuterStrips( xyplot(log10(counts)~hour|weekday+month, data=biocAccess, type=c("p", "a"), as.table=TRUE, pch=".", cex=2))

hour log10(counts)

2.5 3.0 3.5 4.0

Monday Jan

5 10 15 20

Tuesday Jan Wednesday Jan

5 10 15 20

Thursday Jan Friday Jan

5 10 15 20

Saturday Jan Sunday Jan Monday Feb Tuesday Feb Wednesday Feb Thursday Feb Friday Feb Saturday Feb

2.5 3.0 3.5 4.0

Sunday Feb

2.5 3.0 3.5 4.0

Monday Mar Tuesday Mar Wednesday Mar Thursday Mar Friday Mar Saturday Mar Sunday Mar Monday Apr Tuesday Apr Wednesday Apr Thursday Apr Friday Apr Saturday Apr

2.5 3.0 3.5 4.0

Sunday Apr

2.5 3.0 3.5 4.0 5 10 15 20

Monday May Tuesday May

5 10 15 20

Wednesday May Thursday May

5 10 15 20

Friday May Saturday May

5 10 15 20

Sunday May

22 / 29

slide-39
SLIDE 39

Examples

Trellis Graphics: Customization

  • 23 / 29
slide-40
SLIDE 40

Trellis Graphics

Lattice

Implementation of Trellis concepts in R:

◮ ‘high-level’ functions: xyplot, bwplot, densityplot ◮ panel functions: panel.xyplot, panel.bwplot,

panel.densityplot

◮ utility functions: useOuterStrips, resizePanels

Sarkar, D., Lattice: Multivariate Data Visualization with R, New York: Springer Science+Business Media, 2008.

24 / 29

slide-41
SLIDE 41

Outline

Introduction Examples Repeated Measurement Trellis Graphics R and Lisp

25 / 29

slide-42
SLIDE 42

R and Lisp

Lisp-like features in R

analogues to read, eval, print

◮ parse:

◮ apply to character vector or file; ◮ returns a list of expressions, optionally decorated with source

location information;

◮ eval

◮ takes (list of) expressions as argument; ◮ returns value of last expression; ◮ optional (lexical) environment argument, defaults to local

◮ deparse

◮ string representation of object ◮ approximately inverse of parse 26 / 29

slide-43
SLIDE 43

R and Lisp

Lisp-like features in R

with-output-to-string:

◮ capture.output ◮ also similar stream concept to/from a variety of sources

27 / 29

slide-44
SLIDE 44

R and Lisp

Lisp-like features in R

with-output-to-string:

◮ capture.output ◮ also similar stream concept to/from a variety of sources

setf:

◮ x <- 3 ◮ foo(x) <- 3

◮ ‘foo<-‘(x, value=3) 27 / 29

slide-45
SLIDE 45

R and Lisp

Lisp-like features in R

with-output-to-string:

◮ capture.output ◮ also similar stream concept to/from a variety of sources

setf:

◮ x <- 3 ◮ foo(x) <- 3

◮ ‘foo<-‘(x, value=3)

conditions, handlers and restarts:

◮ try ◮ tryCatch ◮ withCallingHandlers

◮ signalCondition ◮ simpleError, simpleWarning, simpleMessage ◮ withRestarts, findRestart, invokeRestart 27 / 29

slide-46
SLIDE 46

R and Lisp

Lisp-like features in R

Macros:

◮ substitute: approximately like once-only ◮ eval.parent: approximately like , or ,@

28 / 29

slide-47
SLIDE 47

R and Lisp

Lisp-like features in R

Macros:

◮ substitute: approximately like once-only ◮ eval.parent: approximately like , or ,@ withRetryRestart <- function(description, expr) { call <- substitute(expr) retry <- TRUE while(retry) { retry <- FALSE withRestarts(eval.parent(call), retry=list(description=description, handler=function() retry <<- TRUE)) } }

28 / 29

slide-48
SLIDE 48

R and Lisp

Lisp-like features in R

Macros:

◮ substitute: approximately like once-only ◮ eval.parent: approximately like , or ,@ withRetryRestart <- function(description, expr) { call <- substitute(expr) retry <- TRUE while(retry) { retry <- FALSE withRestarts(eval.parent(call), retry=list(description=description, handler=function() retry <<- TRUE)) } } withRetryRestart("retry SLIME interactive evaluation request", value <- eval(parse(text=string), envir=globalenv()))

28 / 29

slide-49
SLIDE 49

R and Lisp

Lisp-like features in R: SLIME

SLIME: Superior Lisp Interaction Mode for Emacs.

◮ started in 2003 for CMUCL only; ◮ it was too easy to port to other Common Lisps

29 / 29

slide-50
SLIDE 50

R and Lisp

Lisp-like features in R: SLIME

SLIME: Superior Lisp Interaction Mode for Emacs.

◮ started in 2003 for CMUCL only; ◮ it was too easy to port to other Common Lisps

Vague server (swank) backends for:

◮ Ikarus, Kawa, Larceny, MIT Scheme, R6RS ◮ Goo ◮ Jolt ◮ Ruby

(other client implementations: Climacs, Vim, MCLIDE)

29 / 29

slide-51
SLIDE 51

R and Lisp

Lisp-like features in R: SLIME

SLIME: Superior Lisp Interaction Mode for Emacs.

◮ started in 2003 for CMUCL only; ◮ it was too easy to port to other Common Lisps

Vague server (swank) backends for:

◮ Ikarus, Kawa, Larceny, MIT Scheme, R6RS ◮ Goo ◮ Jolt ◮ Ruby

(other client implementations: Climacs, Vim, MCLIDE) swankr: http://common-lisp.net/~crhodes/swankr/

◮ Lots doesn’t work. Please help!

29 / 29

slide-52
SLIDE 52

R and Lisp

Lisp-like features in R: SLIME

SLIME: Superior Lisp Interaction Mode for Emacs.

◮ started in 2003 for CMUCL only; ◮ it was too easy to port to other Common Lisps

Vague server (swank) backends for:

◮ Ikarus, Kawa, Larceny, MIT Scheme, R6RS ◮ Goo ◮ Jolt ◮ Ruby

(other client implementations: Climacs, Vim, MCLIDE) swankr: http://common-lisp.net/~crhodes/swankr/

◮ Lots doesn’t work. Please help!

Any questions?

29 / 29