1. Introduction excerpt from the lecture at ETHZ (1V + 1U) , Autumn - - PowerPoint PPT Presentation

1 introduction
SMART_READER_LITE
LIVE PREVIEW

1. Introduction excerpt from the lecture at ETHZ (1V + 1U) , Autumn - - PowerPoint PPT Presentation

MAS Medizinphysik Using R MAS Medizinphysik Using R 1. Introduction excerpt from the lecture at ETHZ (1V + 1U) , Autumn Sem. 2010 In this Chapter you will ... Cornelia Schwierz, Andreas Papritz, ... learn what R is ... see a few first


slide-1
SLIDE 1

MAS Medizinphysik — Using R

excerpt from the lecture at ETHZ (1V + 1U), Autumn Sem. 2010

Cornelia Schwierz, Andreas Papritz, Martin M¨ achler <maechler@stat.math.ethz.ch>

Seminar f¨ ur Statistik, ETH Z¨ urich

January 20, 2011

0partly based on work by Werner Stahel and Manuel Koller 0slides rendered (by L

AT

EX) on January 19, 2011 1 / 154

MAS Medizinphysik — Using R

1. Introduction

In this Chapter you will ... ... learn what R is ... see a few first examples ... learn how to operate R ... learn how to read in data ... learn how to quit an R session

2 / 154

1.1 What is R?

◮ R is a software environment for statistical computing. ◮ R is based on commands. Implements the S language. ◮ There is an inofficial menu based interface called R-Commander. ◮ Drawbacks of menus: difficult to store what you do. A script of

commands

◮ documents the analysis and ◮ allows for easy repetition with changed data, options, ...

◮ R is free software. http://www.r-project.org

Supported operating systems: Linux, Mac OS X, Windows

◮ Language for exchanging statistical methods among researchers

3 / 154

1.2 Other Statistical Software

◮ S+ (formerly “S-PLUS”) same programming language,

  • commercial. Features a GUI.

◮ SPSS: good for standard procedures. ◮ SAS: all-rounder, good for large data sets, complicated analyses. ◮ Systat: Analysis of Variance, easy-to-use graphics system. ◮ Excel: Good for getting (a small!) dataset ready. Very limited

collection of statistical methods. Not for serious data analysis!

◮ Matlab: Mathematical methods. Statistical methods limited.

Similar “paradigm”, less flexible structure.

4 / 154

slide-2
SLIDE 2

1.3 Introductory examples

A dataset that we have stored before in the system is called

d.sport

weit kugel hoch disc stab speer punkte OBRIEN 7.57 15.66 207 48.78 500 66.90 8824 BUSEMANN 8.07 13.60 204 45.04 480 66.86 8706 DVORAK 7.60 15.82 198 46.28 470 70.16 8664 : : : : : : : : : : : : : : : : : : : : : : : : CHMARA 7.75 14.51 210 42.60 490 54.84 8249

Draw a histogram of the results of variable kugel : We type

hist(d.sport[,"kugel"])

The graphics window is opened automatically. We have called the function hist with argument

d.sport[,"kugel"] . [, j] is used to select the column j .

5 / 154

◮ Scatter plot: type

plot(d.sport[,"kugel"], d.sport[,"speer"])

◮ First argument: x coordinates; second: y coordinates ◮ Many(!) optional arguments:

plot(d.sport[,"kugel"],d.sport[,"speer"], xlab="ball push",ylab="javelin",pch=7)

◮ Scatter plot matrix

pairs(d.sport)

Every column of d.sport is plotted against all other columns.

6 / 154

1.4 Using R

◮ Within a window running R, you will see the prompt ’> ’.

You type a command and get a result and a new prompt.

> hist(d.sport[,"kugel"]) >

An incomplete statement can be continued on the next line

> plot(d.sport[,"kugel"], + d.sport[,"speer"])

7 / 154

An R statement1 is typically either

◮ a name of an object −

→ object is displayed

> d.sport

◮ a call to a function −

→ graphical or numerical result

> hist(d.sport[,"kugel"])

◮ an assignment

> a <- 2*pi/360 > mn <- mean(d.sport[,"kugel"])

stores the mean of d.sport[,"kugel"] under the name mn

1R “statement”: more precisely R “function call” 8 / 154

slide-3
SLIDE 3

Get a dataset from a text file on the internet and assign it to a name:

> d.sport <- read.table( + "http://stat.ethz.ch/Teaching/Datasets/WBL/sport.dat")

For data files with a one-line header, you need to set the option

header = TRUE ,

> d... <- read.table(... , header = TRUE)

To download the file first to the local computer, R provides the command

> download.url("http://...")

Use file browser (of the underlying operating system) to open a file:

> d.sport <- read.table(file.choose())

9 / 154

Reading and Writing Data

Read a file in table format and create a data frame from it. With cases corresponding to lines and variables to fields.

◮ Text-files:

>

read.table(file, header = FALSE, sep = "",

+

dec = ".", row.names, col.names,...)

◮ Excel-files:

>

read.csv(file, sep = ",", dec=".",...)

>

read.csv2(file, sep = ";", dec=",",...)

Get all possible arguments and defaults with ?read.table

10 / 154

Reading Data (ctd.)

◮ Tabulator-separated files:

>

read.delim(file, sep = "\t", dec=".",...)

>

read.delim2(file, sep = "\t", dec=",",...)

◮ R-Data:

>

load(file=’’myanalysis.Rdata’’)

>

load(file=’’C:/myanalysis.Rdata’’)

11 / 154

To save or write data to a file:

◮ Text-files:

>

write.table(x, file = "", append = FALSE,

+

sep = " ",eol = "\n", na = "NA", dec = ".",

+

row.names = TRUE, col.names = TRUE, ...)

where x is the data object to be stored.

◮ Excel-files:

>

write.csv(...)

>

write.csv2(...)

◮ R-Data files:

>

save(..., file, ascii = FALSE,...)

Example:

>

x <- c(1:20)

>

y <- d.sport$kugel

>

save(x, y, file = "xy.Rdata")

12 / 154

slide-4
SLIDE 4

◮ R stores all created “objects” in your workspace. List them by

either ls() or equivalently, objects() :

> ls()

[1] "a" "d.sport" "mn"

◮ Objects have names like

a, fun, d.sport

◮ R provides a huge number of functions and other objects ◮ Arguments of functions are provided either by using their name,

e.g. read.table(...,header=TRUE) , or by placing them at their defined position (as defined in the help-pages).

◮ You can see the function definition (“source”) by typing its name

without ():

> read.table

◮ Comments can be added using “#” :

> ls() ## Comments are ignored by R

13 / 154

Getting Help

◮ Documentation on the arguments etc. of a function

(or dataset provided by the system):

> help(hist)

  • r

?hist

On the help page, the section “See Also...” contains related functions that could help you further.

◮ Search for a specific keyword:

> help.search("matrix")

Lists packages and functions related to or using “matrix”.

Note: Takes a long time when you have many extra R packages installed

◮ For many functions and data sets, examples are provided on the

help page (?matrix). You can execute them directly,

> example("matrix")

14 / 154

Resources on the internet

◮ R’s Project page http://www.r-project.org/2 ◮ CRAN: use Swiss mirror3 http://cran.CH.r-project.org/:

Links to Search (several search possibilites), Task Views (thematic collections of functions), Contributed (electronic Documentation, Introductions) and FAQs. The following list could be extended “infinitely”:

◮ http://search.r-project.org/: Search specific for R, also

accessed via R function RSiteSearch() . Functions, Help, etc.

◮ http://www.rseek.org/: A “Google-type” search specific for

  • R. Delivers Functions, Help Forums, etc.

2all URLs on this page are “clickable” 3the Swiss CRAN mirror is at stat.ethz.ch 15 / 154

1.5 Scripts and Editors

Instead of typing commands into the R console, you can generate commands by an editor and then “send” them to R ... and later modify (correct, expand) and send again. Text Editors supporting R

◮ WinEdt:

http://www.winedt.com/

◮ Emacs4 with ESS:

http://ESS.r-project.org/5

◮ Tinn-R:

http://www.sciviews.org/Tinn-R/

◮ . . . and several more, partly depending on platform (Windows /

Mac / Linux) . . . . . .

4http://www.gnu.org/software/emacs/ 5For Windows and Mac, on the Downloads tab, look for the “All-in-one installation”

by Vincent Goulet

16 / 154

slide-5
SLIDE 5

The Tinn-R Window

17 / 154

Define Tinn-R Keyboard Shortcuts: Menu item R / Hotkeys of R

18 / 154

Leave the R session:

> q()

You get the question:

Save workspace image? [y/n/c]:

If you answer ”y”, your objects will be available for your next session. Note that we usually answer “n”6, but always store the script (*.R) files.

6and M.M. even eliminates that question by starting R as R --no-save 19 / 154

MAS Medizinphysik — Using R

2. Basics

In this Chapter you will ... ... learn how to select elements from a data set ... find out about vectors (numerical, logical, character) ... use R as a calculator ... learn how to create and manipulate matrices

20 / 154

slide-6
SLIDE 6

2.1 Vectors

Functions and operations are usually applied to whole “collections” instead of single numbers, including “vectors”, “matrices”, “data.frames” ( d.sport )

◮ Numbers can be combined into “vectors”

using the function c() (“combine”):

> v <- c(4,2,7,8,2) > a <- c(3.1, 5, -0.7, 0.9, 1.7) > u <- c(v,a) > u

[1] 4.0 2.0 7.0 8.0 2.0 3.1 5.0 -0.7 0.9 1.7

21 / 154

◮ Generate a sequence of consecutive integers:

> seq(1, 9)

[1] 1 2 3 4 5 6 7 8 9

Since sequences of integers are needed very often, this can be abbreviated to 1:9 . Equally spaced numbers: Use argument by (default: 1):

> seq(0, 3, by=0.5)

[1] 0.0 0.5 1.0 1.5 2.0 2.5 3.0

◮ Repetition:

> rep(0.7, 5)

[1] 0.7 0.7 0.7 0.7 0.7

> rep(c(1, 3, 5), length=8)

[1] 1 3 5 1 3 5 1 3

22 / 154

◮ Basic functions for vectors:

Call, Example Description

length(v)

Length of a vector, number of elements

sum(v)

Sum of all elements

mean(v)

arithmetic mean

var(v)

empirical variance

range(v)

range These functions have additional optional arguments. Check their help pages to find out more.

23 / 154

2.2 Arithmetic

Simple arithmetic is as expected:

◮ > 2+5

[1] 7

Operations:

+

  • *

/ ˆ (Exponentiation)

See ?Arithmetic . Further: logic (→ ?Logic ) and comparison (→ ?Comparison ) operators (see 2.4 below). A full list of available operators is also found on7

◮ Priorities as usual. Use parentheses!

> (2:5) ˆ 2

[1] 4 9 16 25

◮ These operations are applied to vectors elementwise.

> (2:5) ˆ c(2,3,1,0)

[1] 4 27 4 1

7http://cran.r-project.org/doc/manuals/R-lang.html#Operators 24 / 154

slide-7
SLIDE 7

◮ Elements are recycled if operations are carried out with vectors

that do not have the same length:

> (1:6)*(1:2)

[1] 1 4 3 8 5 12

> (1:5) - (0:1) ## with a warning

[1] 1 1 3 3 5 Warning message: longer object length is not a multiple of shorter object length in: (1:5) - (0:1)

> (1:6)-(0:1) ## no warning

[1] 1 1 3 3 5 5

Be careful, there is no warning in the last case!

25 / 154

2.3 Character Vectors

◮ Character strings:

"abc" , "nut 999"

Combine strings into vector of ”mode” character:

> names <- c("Urs", "Anna", "Max", "Pia")

◮ Length (in characters) of strings:

> nchar(names)

[1] 3 4 3 3

◮ String manipulations:

> substring(names,3,4)

[1] "s" "na" "x" "a"

> paste(names, "Z.")

[1] "Urs Z." "Anna Z." "Max Z." "Pia Z."

> paste("X",1:3, sep="")

[1] "X1" "X2" "X3"

26 / 154

2.4 Logical Vectors

◮ Logical vectors contain elements TRUE , FALSE , or NA

> rep(c(TRUE, FALSE), length=6)

[1] TRUE FALSE TRUE FALSE TRUE FALSE

◮ Often result from comparisons

< <= > >= == !=

> (1:5) >= 3

[1] FALSE FALSE TRUE TRUE TRUE

◮ or logical operations: & (and), | (or), ! (not):

> a

[1] 3.1 5.0 -0.7 0.9 1.7

> i <- (2 < a) & (a < 5) > i

[1] TRUE FALSE FALSE FALSE FALSE

27 / 154

2.5 Selecting elements

Select elements from vectors or data.frames: [ ] , [,]

> v

[1] 4 2 7 8 2

> v[c(1,3,5)]

[1] 4 7 2

> d.sport[c(1,3,5),1:3]

weit kugel hoch OBRIEN 7.57 15.66 207 DVORAK 7.60 15.82 198 HAMALAINEN 7.48 16.32 198

Drop elements: negative indices:

> d.sport[-(3:12),c("kugel","punkte")]

kugel punkte OBRIEN 15.66 8824 BUSEMANN 13.60 8706 SMITH 16.97 8271 MUELLER 14.69 8253 CHMARA 14.51 8249

28 / 154

slide-8
SLIDE 8

For data.frames, use names of columns or rows:

> d.sport[c("OBRIEN","DVORAK"), + c("kugel","speer","punkte")]

kugel speer punkte OBRIEN 15.66 66.90 8824 DVORAK 15.82 70.16 8664

Using logical vectors:

> a

[1] 3.1 5.0 -0.7 0.9 1.7

> a[c(TRUE,FALSE,TRUE,TRUE,FALSE,FALSE)]

[1] 3.1 -0.7 0.9

Similarly use logical operations to select from a data.frame

> d.sport[d.sport[,"kugel"] > 16, c(2,7)]

kugel punkte HAMALAINEN 16.32 8613 PENALVER 16.91 8307 SMITH 16.97 8271

29 / 154

2.6 Matrices

Matrices are “data tables” like data.frames, but they can

  • nly contain data of a single type (numeric or character)

◮ Generate a matrix (1):

> m1 <- matrix(1:6, nrow=2, ncol=3); m1

[,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6

> m2 <- matrix(1:6, ncol=2, byrow=TRUE); m2

[,1] [,2] [1,] 1 2 [2,] 3 4 [3,] 5 6

◮ Transpose: t(m1) equals m2 . ◮ Selection of elements as with data.frames:

> m1[2,1:3]

[1] 2 4 6

30 / 154

◮ Generate a matrix (2):

> rbind(m1, -(1:3)) ## add row

[,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 [3,]

  • 1
  • 2
  • 3

> cbind(m2, 100) ## add column

[,1] [,2] [,3] [1,] 1 2 100 [2,] 3 4 100 [3,] 5 6 100

◮ Vectors are typically treated as 1-column matrices and sometimes

for convenience as 1-row ones. as.matrix(v), cbind(v), rbind(v) explicitly do so for a vector v.

31 / 154

◮ Matrix multiplication:

> A <- m1 %*% m2; A

[,1] [,2] [1,] 35 44 [2,] 44 56

◮ Functions for linear algebra are available, e.g., x = A−1b

> b <- 2:3 > x <- solve(A, b) ; x

[1] -0.83333 0.70833

> A %*% x # == b

  • - as 1-col. matrix (!)

[,1] [1,] 2 [2,] 3

see ?solve , ?crossprod , ?qr , ?eigen , ?svd , . . . 8.

8or for instance: http://www.statmethods.net/advstats/matrix.html 32 / 154

slide-9
SLIDE 9

MAS Medizinphysik — Using R

3. Simple Statistics

In this Chapter you will ... ... get to know useful function for objects ... repeat simple function for descriptive statistics ... learn about factor variables ... compare groups of data ... perform a simple hypothesis test

33 / 154

3.1 Useful summary functions for objects

To get an overview of a data set and the distribution of each variable contained:

◮ Dimension of data set

> dim(d.sport)

[1] 15 7

◮ First/Last few lines of a data set

> head(d.sport,n=3) ## default is n=6

weit kugel hoch disc stab speer punkte OBRIEN 7.57 15.66 207 48.78 500 66.90 8824 BUSEMANN 8.07 13.60 204 45.04 480 66.86 8706 DVORAK 7.60 15.82 198 46.28 470 70.16 8664

> tail(d.sport,n=3) ## default is n=6

weit kugel hoch disc stab speer punkte SMITH 7.47 16.97 195 49.54 500 64.34 8271 MUELLER 7.25 14.69 195 45.90 510 66.10 8253 CHMARA 7.75 14.51 210 42.60 490 54.84 8249

34 / 154

◮ Get the names of the variables contained

> names(d.sport)

[1] "weit" "kugel" "hoch" "disc" "stab" "speer" [7] "punkte"

◮ Show the structure of a data set

> str(d.sport)

’data.frame’: 15 obs. of 7 variables: $ weit : num 7.57 8.07 7.6 7.77 7.48 7.88 7.64 7.61 7.27 7.49 $ kugel : num 15.7 13.6 15.8 15.3 16.3 ... $ hoch : int 207 204 198 204 198 201 195 213 207 204 ... $ disc : num 48.8 45 46.3 49.8 49.6 ... $ stab : int 500 480 470 510 500 540 540 520 470 470 ... $ speer : num 66.9 66.9 70.2 65.7 57.7 ... $ punkte: int 8824 8706 8664 8644 8613 8543 8422 8318 8307 8300

35 / 154

◮ Show a summary of the variables (quartiles and min, max for

numeric variables, counts for factors – see below)

> summary(d.sport)

weit kugel hoch disc Min. :7.25 Min. :13.5 Min. :195 Min. :42.6 1st Qu.:7.47 1st Qu.:14.6 1st Qu.:196 1st Qu.:44.3 Median :7.60 Median :15.3 Median :204 Median :45.9 Mean :7.60 Mean :15.2 Mean :202 Mean :46.4 3rd Qu.:7.76 3rd Qu.:15.7 3rd Qu.:206 3rd Qu.:48.9 Max. :8.07 Max. :17.0 Max. :213 Max. :49.8 stab speer punkte Min. :470 Min. :52.2 Min. :8249 1st Qu.:480 1st Qu.:57.4 1st Qu.:8278 Median :500 Median :64.3 Median :8318 Mean :498 Mean :62.0 Mean :8445 3rd Qu.:510 3rd Qu.:66.5 3rd Qu.:8628 Max. :540 Max. :70.2 Max. :8824

36 / 154

slide-10
SLIDE 10

3.2 Simple Statistical Functions

◮ Estimation of a ”location parameter”: mean(x)

median(x)

> mean(d.sport$kugel)

[1] 15.199

> median(d.sport$kugel)

[1] 15.31

◮ Quantiles quantile(x)

> quantile(d.sport$kugel)

0% 25% 50% 75% 100% 13.53 14.60 15.31 15.74 16.97

◮ Variance:

var(x)

> var(d.sport$kugel)

[1] 1.1445

37 / 154

◮ Correlation: cor(x,y) – Look at a plot before!

> plot(d.sport[,"kugel"], d.sport[,"speer"])

  • 13.5

14.0 14.5 15.0 15.5 16.0 16.5 17.0 55 60 65 70 d.sport[, "kugel"] d.sport[, "speer"]

> cor(d.sport[,"kugel"], d.sport[,"speer"])

[1] -0.14645

38 / 154

◮ Correlation matrix:

> pairs(d.sport[,1:3])

weit

13.5 14.0 14.5 15.0 15.5 16.0 16.5 17.0

  • 7.4

7.8

  • 13.5

15.0 16.5

  • kugel
  • 7.4

7.6 7.8 8.0

  • 195

200 205 210 195 205

hoch

> C3 <- cor(d.sport[,1:3]); round(100*C3) ## in percent

weit kugel hoch weit 100

  • 63

34 kugel

  • 63

100

  • 9

hoch 34

  • 9

100

39 / 154

3.3 Factors

Groups, or categorial variables are indicated by factors. E.g. number of a measurement station, the type of species, the type of treatment, etc. In statistical analysis factors MUST be specified correctly to produce the right results (e.g. in an analysis of variance or for regression). ALWAYS check your data ( str() ) before starting an analysis. To produce a factor variable: – create a vector in the usual way c(), rep(), seq() – and then use the function as.factor() .

40 / 154

slide-11
SLIDE 11

An example: Suppose the participants contained in d.sport started in 3 different groups:

> groupnum <- rep(1:3,each=5) > d.sport$group <- as.factor(groupnum) > str(d.sport)

’data.frame’: 15 obs. of 8 variables: $ weit : num 7.57 8.07 7.6 7.77 7.48 7.88 7.64 7.61 7.27 7.49 ... $ kugel : num 15.7 13.6 15.8 15.3 16.3 ... $ hoch : int 207 204 198 204 198 201 195 213 207 204 ... $ disc : num 48.8 45 46.3 49.8 49.6 ... $ stab : int 500 480 470 510 500 540 540 520 470 470 ... $ speer : num 66.9 66.9 70.2 65.7 57.7 ... $ punkte: int 8824 8706 8664 8644 8613 8543 8422 8318 8307 8300 $ group : Factor w/ 3 levels "1","2","3": 1 1 1 1 1 2 2 2 2 2 ...

> levels(d.sport$group) <- c("Zurich","New York","Tokyo")

41 / 154

> head(d.sport,n=10)

weit kugel hoch disc stab speer punkte group OBRIEN 7.57 15.66 207 48.78 500 66.90 8824 Zurich BUSEMANN 8.07 13.60 204 45.04 480 66.86 8706 Zurich DVORAK 7.60 15.82 198 46.28 470 70.16 8664 Zurich FRITZ 7.77 15.31 204 49.84 510 65.70 8644 Zurich HAMALAINEN 7.48 16.32 198 49.62 500 57.66 8613 Zurich NOOL 7.88 14.01 201 42.98 540 65.48 8543 New York ZMELIK 7.64 13.53 195 43.44 540 67.20 8422 New York GANIYEV 7.61 14.71 213 44.86 520 53.70 8318 New York PENALVER 7.27 16.91 207 48.92 470 57.08 8307 New York HUFFINS 7.49 15.57 204 48.72 470 60.62 8300 New York

42 / 154

3.4 Simple Statistical Functions (ctd.)

> summary(d.sport)

weit kugel hoch disc Min. :7.25 Min. :13.5 Min. :195 Min. :42.6 1st Qu.:7.47 1st Qu.:14.6 1st Qu.:196 1st Qu.:44.3 Median :7.60 Median :15.3 Median :204 Median :45.9 Mean :7.60 Mean :15.2 Mean :202 Mean :46.4 3rd Qu.:7.76 3rd Qu.:15.7 3rd Qu.:206 3rd Qu.:48.9 Max. :8.07 Max. :17.0 Max. :213 Max. :49.8 stab speer punkte group Min. :470 Min. :52.2 Min. :8249 Zurich :5 1st Qu.:480 1st Qu.:57.4 1st Qu.:8278 New York:5 Median :500 Median :64.3 Median :8318 Tokyo :5 Mean :498 Mean :62.0 Mean :8445 3rd Qu.:510 3rd Qu.:66.5 3rd Qu.:8628 Max. :540 Max. :70.2 Max. :8824

43 / 154

◮ Count number of cases with same value:

> table(d.sport[,"group"])

Zurich New York Tokyo 5 5 5

◮ Cross-table

> table(d.sport[,"group"],d.sport[,"kugel"])

13.53 13.6 14.01 14.51 14.69 14.71 14.85 15.31 15.52 Zurich 1 1 New York 1 1 1 Tokyo 1 1 1 15.57 15.66 15.82 16.32 16.91 16.97 Zurich 1 1 1 New York 1 1 Tokyo 1

− → The table command is not very useful for numerical

  • variables. Use cut() (see next) or xtab() .

44 / 154

slide-12
SLIDE 12

◮ Subdivide a numerical variable into intervals, e.g. for cross-tables

  • r plots: cut()

> table(d.sport[,"group"], + cut(d.sport[,"kugel"],breaks=4))

(13.5,14.4] (14.4,15.2] (15.2,16.1] (16.1,17] Zurich 1 3 1 New York 2 1 1 1 Tokyo 3 1 1

45 / 154

3.5 Comparison of Groups

Often in statistics, we want to compare different groups of data.

d.sport now contains data for 3 different groups with 5 people

  • each. Let’s store the kugel results for each group separately:

> y1 <- d.sport[d.sport[,"group"]=="Zurich","kugel"] > y1

[1] 15.66 13.60 15.82 15.31 16.32

> y2 <- d.sport[d.sport[,"group"]=="New York","kugel"] > y3 <- d.sport[d.sport[,"group"]=="Tokyo","kugel"]

Comparison of the different groups: – can look at a cross-table (see above) – plot the distribution of the results in each group (better!) – use a statistical test to compare groups − → Build hypotheses based on plots and prior knowledge!

46 / 154

Boxplot for samples of data (1)

> boxplot(y1,y2,y3,ylab="kugel",xlab="group")

  • 13.5

14.5 15.5 16.5 group kugel 47 / 154

Boxplot for samples of data (2) – with factors: R knows it has to separate the data according to the factor variable ”group”!

> plot(d.sport[,"group"],d.sport[,"kugel"], + xlab="group", ylab="kugel")

  • Zurich

New York Tokyo 13.5 14.5 15.5 16.5 group kugel 48 / 154

slide-13
SLIDE 13

3.6 Hypothesis Tests

Do two groups differ in their ”location”? (t-test in Exercises) No assumption about distribution of data: − → Wilcoxon’s Rank Sum Test

> wilcox.test(y1,y3,paired=FALSE)

Wilcoxon rank sum test data: y1 and y3 W = 15, p-value = 0.6905 alternative hypothesis: true location shift is not equal to 0

> wilcox.test(y1,y2,paired=FALSE)

Wilcoxon rank sum test data: y1 and y2 W = 16, p-value = 0.5476 alternative hypothesis: true location shift is not equal to 0

49 / 154

MAS Medizinphysik — Using R

4. Missing Values

In this Chapter you will ... ... see how missing values are specified ... learn how functions deal with missing values ... find out how to properly read in data with missings

50 / 154

4.1 Identifying Missing Values

In practice, some data values may be missing.

◮ Here, we fake this situation.

> kugel <- d.sport[,"kugel"] > kugel[2] <- NA > kugel

[1] 15.66 NA 15.82 15.31 16.32 14.01 13.53 14.71 16.91 15.57 [11] 14.85 15.52 16.97 14.69 14.51

NA means ‘Not Available’ and typically indicates missing data.

◮ Which elements of kugel are missing?

> kugel == NA

[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA

== does not work. Use is.na() instead:

> is.na(kugel)

[1] FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [11] FALSE FALSE FALSE FALSE FALSE

51 / 154

4.2 Missing Values and Function Calls

◮ Applying functions to vectors with missing values:

> mean(kugel)

[1] NA

> mean(kugel, na.rm=TRUE)

[1] 15.313

◮ Other simple functions also have the na.rm argument ◮ For ”higher” functions (e.g. wilcox.test ), the argument

na.action defines how missing values are handled. na.action=na.omit : omit cases with NAs

◮ Plotting functions work.

52 / 154

slide-14
SLIDE 14

Drop the NA elements:

> kugel[!is.na(kugel)]

[1] 15.66 15.82 15.31 16.32 14.01 13.53 14.71 16.91 15.57 14.85 [11] 15.52 16.97 14.69 14.51

> # alternative, with more general methods: > na.omit(kugel)

na.omit(df) drops rows of a data.frame df which contain

missing value(s).

53 / 154

4.3 Specify Missing Values

How to specify missings when reading in data:

> d.dat <- read.table(..., na.strings=c(".",-999))

Default: empty fields are taken as NA for numerical variables. ... or clean your data later:

> d.dat[d.dat==-99] <- NA

54 / 154

MAS Medizinphysik — Using R

5. Write your own Function

In this chapter you will ... ... learn how to write your own functions ... and use them in other functions ... see a simple function example

55 / 154

Syntax: fnname <- function( arg(s) ) { statements } A simple function: Get the maximal value of a vector and its index.

> f.maxi <- function(data) { + mx <- max(data, na.rm=TRUE) # get max element + i <- match(mx, data) # position of max in data + c(max=mx, pos=i) # result of function + }

Output of function is a named vector. The use of return() is

  • ptional.

> f.maxi(c(3,4,78,2))

max pos 78 3

(Note: R provides the function which.max )

56 / 154

slide-15
SLIDE 15

This function can now be used in apply :

> apply(d.sport, 2, f.maxi)

weit kugel hoch disc stab speer punkte max 8.07 16.97 213 49.84 540 70.16 8824 pos 2.00 13.00 8 4.00 6 3.00 1

Note: Use functions when you can. They make your code more legible and simplify the analysis. You can include the functions at the end of your main programme, or collect all your functions in one R-script (e.g. myfunctions.R ) and make the functions available by

> source("myfunctions.R")

More about best-practices in programming will follow in the last block

  • f this lecture course.

R is open-source: Look at, and learn from, the existing functions!

57 / 154

MAS Medizinphysik — Using R

6. Scatter- and Boxplots

In this lecture you will ... . . . get a flavour of graphics systems available in R . . . learn how to create scatterplots and boxplots . . . learn how to use formulae in plots . . . learn how to add axis labels and titles to plots . . . learn to select color, type and size of symbols . . . learn how to control the scales of axes

58 / 154

6.1 Overview

Several R graphics functions have been presented so far:

> plot(d.sport[,"kugel"])

  • 2

4 6 8 10 12 14 13.5 14.0 14.5 15.0 15.5 16.0 16.5 17.0

Index d.sport[, "kugel"]

59 / 154

> plot(d.sport[,"kugel"], d.sport[,"speer"])

  • 13.5

14.0 14.5 15.0 15.5 16.0 16.5 17.0 55 60 65 70

d.sport[, "kugel"] d.sport[, "speer"]

60 / 154

slide-16
SLIDE 16

> pairs(d.sport)

weit

13.5 15.5

  • 44

48

  • 55

65

  • 7.4

7.8

  • 13.5

15.5

  • kugel
  • hoch
  • ●●
  • 195

205

  • 44

48

  • disc
  • stab
  • 470

510

  • 55

65

  • speer
  • 7.4

7.8

  • 195

205

  • 470

510

  • ● ●
  • ●●
  • 8300

8700 8300 8700

punkte 61 / 154

> boxplot(y1,y2,y3,ylab="kugel",xlab="group")

  • 13.5

14.0 14.5 15.0 15.5 16.0 16.5 17.0

group kugel

62 / 154

Many more “standard” graphics functions to come:

scatter.smooth , matplot , image , . . . lines , points , text , . . . par , identify , pdf , jpeg , . . .

Alternatives to “standard” graphics functions ⇒ functions of package lattice ⇒ function of package ggplot2

63 / 154

An example using function xyplot of package lattice

> data(tips, package="reshape"); library(lattice) > xyplot(tip˜total_bill|sex+smoker, data=tips)

total_bill tip

2 4 6 8 10 10 20 30 40 50

  • Female

No

  • Male

No

  • Female

Yes

10 20 30 40 50 2 4 6 8 10

  • Male

Yes 64 / 154

slide-17
SLIDE 17

Same plot using function qplot of package ggplot2

> library(ggplot2) > qplot(x=total_bill, y=tip, data=tips, + facets=smoker˜sex)

total_bill tip

2 4 6 8 10 2 4 6 8 10 Female

  • 10

20 30 40 50 Male

  • 10

20 30 40 50 No Yes

65 / 154

Six kinds of standard R graphics functions:

◮ High-level plotting functions such as plot

⇒ to generate a new graphical display of data.

◮ Low-level plotting functions such as lines

⇒ to add further graphical elements to an existing graph.

◮ “Interactive” functions such as identify

⇒ to amend or collect information interactively from a ⇒ graph.

◮ “Device” control functions such as pdf

⇒ to manipulate windows and files that display or store ⇒ graphs.

◮ “Control” functions such as par

⇒ to control the appearance of graphs.

66 / 154

6.2 Scatterplot

Display of the values of two variables plotted against each other. Syntax:

plot(x, y, main=c1, xlab=c2, ylab=c3, ...) x,y: two numeric vectors c1, c2, c2: any character strings (must be quoted)

For the meaning of ... : ⇒

  • cf. ?plot

Example: Exploring Meuse data on heavy metals in soil

> library(sp); data(meuse) > str(meuse)

67 / 154

’data.frame’: 155 obs. of 14 variables: $ x : num 181072 181025 181165 181298 181307 ... $ y : num 333611 333558 333537 333484 333330 ... $ cadmium: num 11.7 8.6 6.5 2.6 2.8 3 3.2 2.8 2.4 1.6 ... $ copper : num 85 81 68 81 48 61 31 29 37 24 ... $ lead : num 299 277 199 116 117 137 132 150 133 80 ... $ zinc : num 1022 1141 640 257 269 ... $ elev : num 7.91 6.98 7.8 7.66 7.48 ... $ dist : num 0.00136 0.01222 0.10303 0.19009 0.27709 ... $ om : num 13.6 14 13 8 8.7 7.8 9.2 9.5 10.6 6.3 ... $ ffreq : Factor w/ 3 levels "1","2","3": 1 1 1 1 1 1 1 1 1 1 ... $ soil : Factor w/ 3 levels "1","2","3": 1 1 1 2 2 2 2 1 1 2 ... $ lime : Factor w/ 2 levels "0","1": 2 2 2 1 1 1 1 1 1 1 ... $ landuse: Factor w/ 15 levels "Aa","Ab","Ag",..: 4 4 4 11 4 11 4 $ dist.m : num 50 30 150 270 380 470 240 120 240 420 ...

68 / 154

slide-18
SLIDE 18

> plot(x=meuse[,"x"], y=meuse[,"y"], asp=1, + xlab="easting", ylab="northing", + main="position of soil sampling locations")

  • ● ●
  • 178000

179000 180000 181000 182000 330000 331000 332000 333000

position of soil sampling locations easting northing

69 / 154

Three variants ways to invoke plot :

◮ Plot of the values of a single vector against the indices of the

vector elements

> plot(meuse[,"zinc"], ylab="zinc")

  • 50

100 150 500 1000 1500

Index zinc

◮ Scatterplot of two columns of a matrix or a dataframe

> plot(meuse[,c("x","y")], asp=1)

70 / 154

◮ Use of a formula to specify the x- and y-variable out of a data

frame (cf. ?plot.formula)

> plot(zinc˜dist, data=meuse, + main="Zn vs. distance to river")

  • 0.0

0.2 0.4 0.6 0.8 500 1000 1500

Zn vs. distance to river dist zinc

71 / 154

6.3 Digression: Statistical Models, Formula Objects

Statistics is concerned with relations between “variables”. Prototype: Relationship between target variable Y and explanatory variables X1, X2, ... ⇒ Regression.

◮ Symbolic notation of such a relation:

Y ∼ X1 + X2

This symbolic notation is an S object (of class formula ) (The notation is also used in other statistical packages.)

◮ Further example for use of a formula:

> plot(punkte˜kugel+speer, data=d.sport)

gives 2 scatterplots, punkte (vertical) against

kugel and speer , respectively (horizontal axis).

72 / 154

slide-19
SLIDE 19
  • 13.5

14.5 15.5 16.5 8300 8500 8700

kugel punkte

  • 55

60 65 70 8300 8500 8700

speer punkte

73 / 154

6.4 Arguments common to many graphics functions

main="..." , xlab="..." , ylab="..." "..." : any character string (must be quoted!)

⇒ to set title and labels of axes (cf. ?title )

log="x" , log="y" , log="xy"

⇒ for logarithmic scaling of axes (cf. ?plot.window )

xlim=c(xmin,xmax) , ylim=c(ymin,ymax) , xmin, xmax, ymin, ymax:

numeric scalars ⇒ to set range of values displayed (cf. ?plot.window )

asp=n n : numeric scalar

⇒ to set aspect ratio of axes (cf. ?plot.window )

74 / 154

Common arguments of plot (continued):

type=c c: a single character such as "p" for points, "l" for lines, "b"

for points and lines, ”n” for an “empty” plot, etc. ⇒ for selecting type of plot (cf. ?plot )

pch=i or pch=c i : an integer; c: a single character such as "a"

⇒ for choosing symbols (cf. ?points )

cex=n

⇒ for choosing size of symbols (cf. ?plot.default )

col=i or col=color color: keyword such as "red" , "blue" , etc

⇒ for choosing color of symbols (cf. ?plot.default and

colors() )

75 / 154

Example: logarithmic scale of axes

> plot(zinc ˜ dist, data=meuse, log="y")

  • 0.0

0.2 0.4 0.6 0.8 200 500 1000 2000

dist zinc

76 / 154

slide-20
SLIDE 20

Example: setting the range of axes

> plot(zinc ˜ dist, data = meuse, + xlim=c(-1,2), ylim=c(100,3000))

  • ● ●
  • ●●
  • ● ●
  • −1.0

−0.5 0.0 0.5 1.0 1.5 2.0 500 1000 1500 2000 2500 3000

dist zinc

77 / 154

Example: connecting points by lines (cf. ?plot )

> xv <- c(0,1,1,0); yv <- c(0,0,1,1) > plot(x=xv, y=yv, type="p", xlab="", ylab="") > plot(x=xv, y=yv, type="l", xlab="", ylab="")

  • 0.0

0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0 78 / 154

Example: choosing symbol type, color and size (cf. ?points )

> plot(log10(zinc) ˜ sqrt(dist), data=meuse, + pch = 3, col="red", cex=3)

0.0 0.2 0.4 0.6 0.8 2.2 2.4 2.6 2.8 3.0 3.2

sqrt(dist) log10(zinc)

79 / 154

Example: choosing symbol type, color and size

> plot(1:25, pch=1:25, cex=2, col=1:8)

5 10 15 20 25 5 10 15 20 25

Index 1:25

80 / 154

slide-21
SLIDE 21

Example: choosing symbol type, color and size

> plot(y ˜ x, data = meuse, asp = 1, ## [asp]ect ratio := 1 + col= as.numeric(ffreq), + cex= sqrt(zinc)/10)

  • ● ●
  • 178000

179000 180000 181000 182000 330000 331000 332000 333000

x y

81 / 154

6.5 Boxplot

Syntax:

boxplot(x1,x2, ..., notch=l1, horizontal=l2, ...) x1,x2, . . . : numeric vectors l1 (logical): controls whether “notches” are added to roughly test

whether group medians are significantly different

l2 (logical): controls whether horizontal boxplots are generated

. . . : many more arguments (cf. ?boxplot )

82 / 154

Example: a single boxplot

> boxplot(x=meuse[,"zinc"], horizontal=TRUE, range=2, + col="lightyellow", border="red", + xlab="zinc content", main="Zinc Meuse data")

  • 500

1000 1500

Zinc Meuse data zinc content

83 / 154

Example: variant to generate boxplots of several variables

> boxplot(meuse[, c("zinc","lead","copper","cadmium")], + log="y", ylab="metal content [mg/kg]", col = 2:5)

  • zinc

lead copper cadmium 5e−01 5e+00 5e+01 5e+02

metal content [mg/kg]

84 / 154

slide-22
SLIDE 22

Example: boxplot of one variable for several groups of a factor

> boxplot(zinc ˜ ffreq, data=meuse, log="y", notch=TRUE, + names= c("often", "intermediate", "rarely"), + xlab= "flooding", ylab= "zinc [mg/kg]")

  • ften

intermediate rarely 200 500 1000 2000

flooding zinc [mg/kg]

85 / 154

In this lecture you have . . . . . . got a flavour of graphics systems available in R ⇒ “standard” graphics, lattice , ggplot2 . . . learnt how to create scatterplots and boxplots ⇒ functions plot , boxplot . . . learnt how to use formulae for generating plots . . . learnt how to connect points in a scatterplot by lines ⇒ argument type . . . learnt how to add axis labels and titles to plots ⇒ arguments main , xlab , ylab . . . learnt to select color, type and size of symbols ⇒ arguments col , pch , cex . . . learnt how to control the scales of axes ⇒ arguments asp , log , xlim , ylim

86 / 154

MAS Medizinphysik — Using R

7. Controlling the visual aspects

  • f a graphic

In this lecture you will learn . . . . . . how to add points and lines to an existing plot, . . . how to plot arrows and polygons, . . . how to amend a plot by additional text and a legend, . . . how to interactively identify observations displayed as points in a plot, . . . how to read the coordinates of points chosen interactively in a graphics window.

87 / 154

7.1 Adding further points and lines to a graphic

Use points to add further points to a graph created before by a high-level plotting function such as plot . Syntax:

points(x=x, y=y, pch=i1, col=i2 or col=color, cex=n) x, y: two numeric vectors i1, i2: integers (scalars or vectors) color: color name (scalar or vector) n: numeric (scalar or vector)

Remarks:

◮ ± same arguments as for plot ◮ points can also be used with formula and data

arguments (cf. ?points.formula )

88 / 154

slide-23
SLIDE 23

Example: adding Cu data to a plot of lead˜dist for Meuse data

> plot(lead ˜ dist, data=meuse, log = "y", + ylim=c(min(lead,copper), max(lead,copper)), + xlab= "distance to river", ylab= "mg/kg", + main= "Pb and Cu vs. distance to river") > points(copper ˜ dist, data=meuse, pch=2, col="red")

  • 0.0

0.2 0.4 0.6 0.8 20 50 100 200 500

Pb and Cu vs. distance to river distance to river mg/kg

89 / 154

Use lines to add lines that connect successive points to an existing plot. Syntax:

lines(x=x, y=y, lty=i or lty=line type, lwd=n, ...) x, y: two numeric vectors i: integer (scalar) to select line type (cf. ?par ) line type: keyword such as "dotted" to select line type (cf.

?par )

n: numeric scalar to select line width

. . . : further arguments such as col to select line color Remarks:

◮ ± same arguments as for plot and points ◮ lines can also be used with formula and data

arguments (cf. ?lines.formula )

90 / 154

Example: adding outline of river Meuse to plot of sampling locations

> data(meuse.riv) > str(meuse.riv)

num [1:176, 1:2] 182004 182137 182252 182314 182332 ...

> plot(y ˜ x, data = meuse, asp = 1, pch=16) > lines(meuse.riv, lty="dotdash", lwd=2, col="blue")

  • ● ●
  • ● ●
  • 177000

178000 179000 180000 181000 182000 183000 330000 331000 332000 333000

x y

91 / 154

Use abline to add straight lines to an existing plot. Syntax:

abline(v=x, ...) abline(h=y, ...) abline(a=n1, b=n1, ...) x: coordinate(s) where to draw vertical straight line(s) (scalar or

vector)

y: coordinate(s) where to draw horizontal straight line(s) (scalar or

vector)

n1, n2: numeric scalars for intercept and slope of straight line

. . . : further arguments such as col , lty , lwd Remarks:

◮ the straight lines extend over the entire plot window

92 / 154

slide-24
SLIDE 24

Example: adding straight lines to a plot

> plot(lead ˜ dist, data=meuse) > abline(h=c(200, 500), col=c("orange", "red"), + lty="dashed", lwd=2) > abline(v=0.2, col=4, lty=3, lwd=5) > abline(a=500, b=-500, lty="dotdash", lwd=2, + col="black")

  • 0.0

0.2 0.4 0.6 0.8 100 200 300 400 500 600

dist lead

93 / 154

7.2 Adding line segments, arrows and polygons

Use segments to add arbitrary line segments to an existing plot. Syntax:

segments(x0=xstart, y0=ystart, x1=xend, y1=yend, ...) xstart, xend: vectors with x-coordinate of begin and end of line

segments (scalars or vectors)

ystart, yend: vectors with y-coordinate of begin and end of line

segments (scalars or vectors) . . . : further arguments such as col , lty , lwd Remarks:

◮ line segments are drawn from ( x0 , y0 ) to ( x1 , y1 ) ◮ arguments col , lty , lwd may be vectors with the same

length as x0

◮ function arrows adds arrows to a plot (± same syntax)

94 / 154

Example: adding line segments to a plot

> plot(x=c(-1,1), y=c(-1,1), asp=1, type="n") > alpha <- seq(0, by=pi/18, length.out=36) > segments(x0=rep(0, 36), y0=rep(0,36), + x1=cos(alpha), y1=sin(alpha), col=1:8)

−1.0 −0.5 0.0 0.5 1.0 −1.0 −0.5 0.0 0.5 1.0

c(−1, 1) c(−1, 1)

95 / 154

Example: adding arrows to a plot

> plot(x= c(-1,1), y= c(-1,1), type="n", asp=1) > alpha <- seq(0, by=pi/6, length.out=60) > da <- pi/7 > radius <- seq(0.9, 0.1, along.with=alpha) > arrows(x0=radius*cos(alpha), y0=radius*sin(alpha), + x1=radius*cos(alpha+da), y1=radius*sin(alpha+da), + length=0.05, code=2, angle=60, col=grey(radius))

−1.5 −1.0 −0.5 0.0 0.5 1.0 1.5 −1.0 −0.5 0.0 0.5 1.0

c(−1, 1) c(−1, 1)

96 / 154

slide-25
SLIDE 25

Use polygon to add a polygon to an existing plot. Syntax:

polygon(x=x, y=y, density=n1, angle=n2, col=i1, border=i2, ...)

x, y: numeric vectors n1, n2: density and angle of lines when polygons are hatched

(scalars)

i1, i2: fill and border color of polygons (scalars)

. . . : further argument such as lty Remarks:

◮ colors can also be specified by valid color names ◮ first and last points are always connected by a line

97 / 154

Example: adding a polygon to a plot

> par(mfrow=c(1,2)) > xv <- c(0,0.5,1,1,0.5,0); yv=c(0,0,1,0,1,1) > plot(x=c(0,1), y=c(0,1), asp=1, type="n") > lines(xv, yv, type="b", + pch=as.character(1:length(xv))) > plot(x=c(0,1), y=c(0,1), asp=1, type="n") > polygon(xv, yv, border="blue", col="cyan", + density=12, angle=0, lwd=2)

0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0

c(0, 1) c(0, 1)

1 2 3 4 5 6 0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0

c(0, 1) c(0, 1)

98 / 154

Example: adding the polygon of the river Meuse to a plot of sampling locations

> plot(y ˜ x, data=meuse, asp=1, type="n") > polygon(meuse.riv, col="cyan", border=NA) > points(y ˜ x, data=meuse, pch=16) > box() # draws a rectangle around plot window

177000 178000 179000 180000 181000 182000 183000 330000 331000 332000 333000

x y

  • ● ●
  • ● ●
  • 99 / 154

7.3 Amending plots by additional text and legends

Points in a scatterplot are labelled by text . Syntax:

text(x=x, y=y, labels=c , pos=i, ...) x, y: two numeric vectors c: vector of character strings with the text to label the points i: integer to control whether labels are plotted below (1), to the left

(2), above (3) or to the right (4) of the points (scalar or vector) . . . : further arguments such as col and cex Remarks:

◮ x and y may specify arbitrary coordinates within the plot window ◮ position where labels are plotted can be further fine-tuned by

adj and offset arguments (cf. ?text )

◮ one cannot use a formula in text

100 / 154

slide-26
SLIDE 26

Example: labelling sample points of Meuse data by landuse info

> plot(y˜x, data=meuse, asp=1, pch=16) > text(meuse[,c("x","y")], labels=meuse[,"landuse"], + pos=4, cex=0.7)

  • ● ●
  • 178000

179000 180000 181000 182000 330000 331000 332000 333000

x y

Ah AhAhGa Ah Ga Ah Ab Ab W Fh Ag WAh Ah W W W W Am Am Ag Ah W W Ab Ag Ah Ag B Ag AhB B Ab Ab Am W W Am Ga W Ah Ah Am Ah Ah Bw Bw Ab Ah W W W W Fw Fw Fw W W Fw W Ah W W Ah Fw Bw Ab Ab W WAh W W Ah Am W WW W W W Ah W W W Ah Am Am Am Am W W Am Am Am Ah W W SPO W Am Ah Fw Ah Ah Fw STA DEN Fw Ah Ah Ah W STA Bw Ah Aa W Tv Fw Ah Ah Ah Am Am Am W Ah W Aa Am Am Am W W W Ah Ah Fw W Bw Bw W W Ah Am Ah Am Ah Ah W W

101 / 154

Example: adding text at an arbitrary position in plot window

> plot(y˜x, data=meuse, asp=1, pch=16) > text(meuse[,c("x","y")], labels=meuse[,"landuse"], + pos=4, cex=0.5) > text(178900, 332000, cex=2, col="red", srt=54, + labels="point labels too small !")

  • ● ●
  • 178000

179000 180000 181000 182000 330000 331000 332000 333000

x y

Ah Ah Ah Ga Ah Ga Ah Ab Ab W Fh Ag W Ah Ah W W W W Am Am Ag Ah W W Ab Ag Ah Ag B Ag AhB B Ab Ab Am W W Am Ga W Ah Ah Am Ah Ah Bw Bw Ab Ah W W W W Fw Fw Fw W W Fw W Ah W W Ah Fw Bw Ab Ab W WAh W W Ah Am W W W W W W Ah W W W Ah Am Am Am Am W W Am Am Am Ah W W SPO W Am Ah Fw Ah Ah Fw STA DEN Fw Ah Ah Ah W STA Bw Ah Aa W Tv Fw Ah Ah Ah Am Am Am W Ah W Aa Am Am Am W W W Ah Ah Fw W Bw Bw W W Ah Am Ah Am Ah Ah W W

p

  • i

n t l a b e l s t

  • s

m a l l !

102 / 154

More sophisticated text annotation is added by legend to a plot. Syntax:

legend(x=x, y=y, legend=c, pch=i1, lty=i2 ,...) x, y: coordinates where the legend should be plotted c: vector of character strings with labels of categories i1, i2: vector of integers with type of plotting symbol or line type for

categories . . . : further arguments such as col and cex Remarks:

◮ The position of the legend is either specified by x and y or by a

keyword such as "topright" , "bottomleft" , etc. (cf.

legend for allowed keywords).

◮ Position of legend can be fine-tuned by arguments xjust and

yjust if x and y are given (cf. ?legend ).

103 / 154

Example: a legend annotating flooding frequency for Meuse data

> plot(y˜x, data=meuse, asp=1, col=as.numeric(ffreq)) > legend("topleft", pch=1, col=c("black","red","green"), + legend = c("frequent","intermediate","rare"))

  • ● ●
  • 178000

179000 180000 181000 182000 330000 331000 332000 333000

x y

  • frequent

intermediate rare 104 / 154

slide-27
SLIDE 27

Example: a more complex legend for bubble plot of Meuse zinc data

> plot(y˜x, data=meuse, asp=1, cex=sqrt(zinc)/15) > zn.label <- c(100,200,500,1000,2000) > legend(x=183000, y=330000, xjust=1, yjust=0, + x.intersp=1.5, y.intersp=1.7, pch=1, + legend=c("Zn mg/kg", zn.label), + pt.cex=c(NA,sqrt(zn.label)/15), bty="n")

  • 177000

178000 179000 180000 181000 182000 183000 330000 331000 332000 333000

x y

  • Zn mg/kg

100 200 500 1000 2000 105 / 154

MAS Medizinphysik — Using R

8. More on Statistics

In this chapter you will learn about . . . . . . distributions in R (N, t, Binomial, Poisson, etc) . . . visualizing them . . . using them in computations . . . draw random samples from them.

106 / 154

8.1 Distributions and Random Numbers

In statistics, we have two kinds of distributions:

  • 1. data (x1, x2, . . . , xn) and its empirical distribution Fn(t),

arithmetic mean ¯ X := 1/n n

i=1 xi, standard deviation, etc.

and

  • 2. random variable, say X (abstract!) and its (theoretical) distribution,

expectation E(X), Var(X), etc. Such distributions are characterized by either

◮ a density f(x), ◮ a cumulative distribution function F(x) =

  • u f(u) du,

◮ or a quantile function q(α) := F −1(α), (α ∈ (0, 1)). 107 / 154

(1) Data – empirical distribution

> par(mfcol = 1:2) > hist(X) ; plot( ecdf(X) )

Histogram of X

X Frequency 12 14 16 18 20 22 24 26 5 10 15 12 14 16 18 20 22 24 26 0.0 0.2 0.4 0.6 0.8 1.0

ecdf(X)

x Fn(x)

  • ● ●
  • 108 / 154
slide-28
SLIDE 28

(2) Random Variable X ∼ N(20, 3): F(t), f(t) = F ′(t)

10 15 20 25 30 0.00 0.04 0.08 0.12

[d]ensity f()

x dnorm(x, m = 20, s = 3) 10 15 20 25 30 0.0 0.2 0.4 0.6 0.8 1.0

[p]robability distribution F()

x pnorm(x, m = 20, s = 3) 0.952 0.0 0.2 0.4 0.6 0.8 1.0 10 15 20 25 30

[q]uantile function

p qnorm(x, m = 20, s = 3) 0.952

[r]andom sample from distr.

X <− rnorm(5000, m = 20, s = 3) Density 10 15 20 25 30 0.00 0.04 0.08 0.12

109 / 154

Random Variable X ∼ χ2

10 : F(t) and f(t) = F ′(t)

5 10 15 20 25 0.00 0.04 0.08

[d]ensity f()

x dchisq(x, df = 10) 5 10 15 20 25 0.0 0.2 0.4 0.6 0.8 1.0

[p]robability distribution F()

x pchisq(x, df = 10) 0.868 0.0 0.2 0.4 0.6 0.8 1.0 5 10 15 20 25

[q]uantile function

p qchisq(x, df = 10) 0.868

[r]andom sample from distr.

X <− rchisq(5000, df = 10) Density 10 20 30 0.00 0.04 0.08

110 / 154

Distributions in R — 4 × n functions

“d”, “p”, “q”, “r”

4 functions for every distribution family E.g., the normal distribution is characterized by:

◮ Density function f(x) (here, f(x) = φ(x) := 1 √ 2πe−x2/2)

> dnorm(0.5, mean=0, sd=1)

[1] 0.35207

◮ Cumulative Probability function F(x) =

  • t f(t) dt

> pnorm(c(1, 1.96), mean=0, sd=1)

[1] 0.84134 0.97500

◮ Quantile function (q(p) = F −1(p), i.e., F(q(p)) = p):

> qnorm(c(0.25,0.975), mean=100, sd=10)

[1] 93.255 119.600

◮ Random number generator function (→ X1, X2, . . . , Xn ∼ F i.i.d.):

> rnorm(5, mean=2, sd=2)

[1] 2.9193 0.2035 5.5272 2.3145 3.4525

111 / 154

Poisson distribution:

dpois, ppois, qpois, rpois

> rpois(10, lambda=3.5)

[1] 3 1 2 7 6 3 6 3 3 6

Prepend “d”, “p”, “q”, or “r” to these distribution “name stems”:

Discrete Distributions

binom

Binomial distribution

pois

Poisson distribution

hyper

Hypergeometric distribution . . . . . . (more) . . . Continuous Distributions

unif

Uniform distribution

exp

Exponential distribution

norm

Normal distribution

lnorm

Log-Normal distribution

t, f, chisq

t-, F-, χ2− (Chisquare-) distribution

weibull, gamma

Weibull, Gamma distribution . . . . . . (many more) . . .

112 / 154

slide-29
SLIDE 29

Prepend “d”, “p”, “q”, or “r” to distribution “name”, e.g.:

> dunif( (0:10)/10 ) # density of *uniform* is constant!

[1] 1 1 1 1 1 1 1 1 1 1 1

> pbinom( 0:5, size = 5, prob = 1/2)

[1] 0.03125 0.18750 0.50000 0.81250 0.96875 1.00000

> pexp(1:3, rate = 1/2)

[1] 0.39347 0.63212 0.77687

> qnorm(0.975) # ‘‘the famous number’’

[1] 1.9600

> qt (0.975, df = c(3,10,20, 100)) # larger

[1] 3.1824 2.2281 2.0860 1.9840

113 / 154

Densities of F (”Fisher”) distributions, df:

> curve(df(x, df1=n1[1], df2=2*n1[1]), 0, 4, col=1, n=400, ylab="", + main=expression(F[list(nu[1],nu[2])] * " - distributions for" > abline(h=0,v=0, col="gray", lty=2) > for(j in 2:length(n1)) + curve(df(x, df1=n1[j], df2=2*n1[j]), add=TRUE, col=j, n = 200) > legend("topright", l.exp, lty=1, col= 1:length(n1), inset=.02)

1 2 3 4 0.0 0.5 1.0 1.5

Fν1, ν2 − distributions for ν2 = 2ν1

x

(ν1 = 50, ν2 = 100) (ν1 = 20, ν2 = 40) (ν1 = 10, ν2 = 20) (ν1 = 6, ν2 = 12) (ν1 = 4, ν2 = 8) (ν1 = 3, ν2 = 6) (ν1 = 2, ν2 = 4)

114 / 154

8.2 Random Numbers

◮ “Random” numbers are generated by a deterministic function.

Nevertheless, two identical calls give different results.

> runif(4)

[1] 0.6968280 0.7292185 0.0076653 0.2468306

> runif(4)

[1] 0.78849 0.91503 0.20256 0.92238

How this? The function gets a vector .Random.seed .

◮ To obtain the same numbers again, use ...

> set.seed(27) > runif(1)

[1] 0.97175

> set.seed(27) > runif(1)

[1] 0.97175

115 / 154

Random Numbers − → Simulation !

Very important application: “Simulation” of complicated models, situations, etc via random numbers: E.g., what is a “correct” 90%–confidence interval for the 10%-trimmed mean of 20 observations Xi ∼ t3 (i = 1, . . . , 20)? Answer not known from theory . . . .. ⇒Simulation gives a good approximate result easily:

> Sim <- rep(NA, 1000) > for(i in 1:1000) + Sim[i] <- mean(rt(20, df=3), trim = 0.10)

and from this empirical distribution (given by its sample values Sim[i], get a 90%-interval by cutting 5% on each side:

> quantile(Sim, c(0.05, 0.95))

5% 95%

  • 0.50644

0.45538

116 / 154

slide-30
SLIDE 30

8.3 Visualization of distributions

◮ Discrete distributions:

> plot(0:15, dpois(0:15, lambda=3.5), + type="h", lwd = 4, col = "gray")

5 10 15 0.00 0.05 0.10 0.15 0.20 0:15 dpois(0:15, lambda = 3.5) 117 / 154

◮ Continuous distributions:

> curve(dnorm(x,5,2), xlim=c(-1,10), + main="normal distribution")

2 4 6 8 10 0.00 0.05 0.10 0.15 0.20

normal distribution

x dnorm(x, 5, 2)

118 / 154

MAS Medizinphysik — Using R

9. Elements of the R Language

In this chapter you will learn about . . . . . . Control structures, i.e. loops, if–else, etc. . . . Objects with properties; class, methods . . . Applying functions in a “functional sense” . . . Error messages, debugging etc . . . R packages; CRAN, etc: a glimpse of “The R World” Additionally, possibly, . . . Character string manipulations . . . “Literate Programming”: R + L

A

T EX, via Sweave . . . Mathematical expressions in plots . . . Save and Load R results and code.

119 / 154

9.1 Control Structures: Loops

Loops are basic for programming. Most important one: for

◮ Calculate the first twelve elements of the Fibonacci series.

> fib <- c(1,1) > for(i in 1:10) + fib <- c(fib, fib[i]+fib[i+1]) > fib

[1] 1 1 2 3 5 8 13 21 34 55 89 144

◮ Other loop constructs:

while(cond) {...} ;

> x <- 1 > while(abs(x - (cx <- cos(x))) > 10ˆ-8) { + x <- cx + cat(".") + }

.............................................

> c(x, cos(x)) # the same !

[1] 0.73909 0.73909

120 / 154

slide-31
SLIDE 31

9.1 Control Structures: Loops — repeat, break, . . .

◮ or repeat { ...

} ; notably the latter needs break to jump out of the loop:

> ## repeat until "right-click" : > repeat { + x0 <- locator(1)$x + if(length(x0) < 1)## right clicking leaves loop + break + + draw.my.things( x0 ) + }

121 / 154

◮ Note: Instead of for loops, you can (and should!) often use

more elegant and efficient operations,

◮ e.g., instead of

> n <- length(x); y <- x > for(i in 1:n) + y[i] <- x[i] * sin(pi * x[i])

use simply

> Y <- x * sin(pi * x)

◮ Of course, that’s equivalent:

> identical(Y, y)

[1] TRUE

◮ In more complicated cases, it is often advisable to *apply()

functions instead of for(.)

{...} , see below!

122 / 154

9.2 Control Structures: if – else

◮ Conditional evaluation: if(.)

... [ else ... ] Syntax:

if(logical) A

  • r

if(logical) A1 else A2

E.g., For the Fibonacci construction loop,

> fib <- c(1,1) ; i <- 1 > repeat { + fib <- c(fib, fib[i]+fib[i+1]) + if ( fib[(i <- i+1)+1] > 10000 ) break + } > fib

[1] 1 1 2 3 5 8 13 21 34 [12] 144 233 377 610 987 1597 2584 4181 6765 10946

◮ with optional else

> if(sum(y) > 0) log(sum(y)) else "negative sum"

[1] "negative sum"

123 / 154

9.2 Control Structures: if – else — if(cc) . . . : value

Already seen above (implicitly): if(cc) A returns a value:

> u <- 1 > x1 <- if(uˆ2 == u) "are the same" ; x1

[1] "are the same"

> u <- 2 > x2 <- if(uˆ2 == u) "are the same" ; x2

NULL

i.e., if(cond) A when cond is false, has value NULL:

> length(NULL)

[1] 0

> c(2,NULL,pi)

[1] 2.0000 3.1416

> is.null(NULL)

[1] TRUE

124 / 154

slide-32
SLIDE 32

if(cc) . . . return()s a value (2)

A (simplistic!) example of computing “significance stars” from P-values:

> myStar <- function(x) { if(x < .01) "**" else + if(x < .05) "*" else "" } > myStar(0.024)

[1] "*"

> myStar(0.2)

[1] ""

> myStar(0.002)

[1] "**"

125 / 154

> tst3 <- function(x) { + if(x %% 3 == 0) paste("HIT:", x) else format(x %% 3) + } > c(tst3(17), tst3(27)) [1] "2" "HIT: 27"

> tst4 <- function(x) { + if(x < -2) "pretty negative" + else if(x < 1) "close to zero" + else if(x < 3) "in [1, 3)" else "large" + } > xvec <- c(-5, -1:4) > cbind(x=xvec, "tst4(x)" = sapply(xvec, tst4)) x tst4(x) [1,] "-5" "pretty negative" [2,] "-1" "close to zero" [3,] "0" "close to zero" [4,] "1" "in [1, 3)" [5,] "2" "in [1, 3)" [6,] "3" "large" [7,] "4" "large"

126 / 154

switch

◮ Instead of nested if (..)

A else if (..) B else C clauses, sometimes can use switch():, e.g.

> center <- function(x, type) { + switch(type, + mean = mean(x), + median = median(x), + trimmed = mean(x, trim = .1)) + } > x <- rcauchy(10) > center(x, "mean") [1] -0.098097 > center(x, "median") [1] -0.5877 > center(x, "trimmed") [1] 0.0092007

127 / 154

ifelse(cond, r1, r2)

◮ a “vectorized if function: ifelse

Select elements from 2 vectors based on condition:

> x <- 1:12 > ifelse(x > 5, 10, x)

[1] 1 2 3 4 5 10 10 10 10 10 10 10

◮ can be nested:

> ifelse(x < 5, 5, ifelse(x > 9, 10, x))

[1] 5 5 5 5 5 6 7 8 9 10 10 10

128 / 154

slide-33
SLIDE 33

ifelse() allows to define vectorized piecewise defined functions:

> huber3 <- function(x) ifelse(x < -3, -3, + ifelse(x < 3, x, 3))

> curve(huber3, -5, 6, lwd=2, asp=1) > abline(h=0,v=0, col="gray", lty=2) > curve(ifelse(x < -2, (x+3)ˆ2 -2, + ifelse(x < -1/2, -0.5, + ifelse(x < 2, 3, 5-x))), + add=TRUE, col="tomato", n=400)

−4 −2 2 4 6 −3 −2 −1 1 2 3 x huber3 (x) 129 / 154

9.3 R Objects

The basic building blocks of R are called “objects”. – They come in “classes”:

◮ numeric, character, . . . one-dim. sequence of numbers, strings, ◮

. . . ; “building blocks” of R : called atomic9 vectors

◮ matrix

two dimensional array of numbers, character strings, . . .

◮ array

(1–, 2–, 3–, . . . )dimensional; 2-dim. array =: matrix.

◮ data.frame two dimensional, (numbers, “strings”, factors, . . . ) ◮ formula

specifying (regression, plot, . . . ) “model”

◮ function

also an object!

◮ list

very general collection of objects, → see below

◮ ...

and more

9see help page ?is.atomic, or maybe demo(is.things) for more 130 / 154

array — k-dimensional matrix

Matrices are 2-dimensional, an array can be k-dimensional (k ≥ 1). E.g., 3-dimensional, a “layer of matrices”:

> a <- array(1:30, dim=c(3,5,2)) > a

, , 1 [,1] [,2] [,3] [,4] [,5] [1,] 1 4 7 10 13 [2,] 2 5 8 11 14 [3,] 3 6 9 12 15 , , 2 [,1] [,2] [,3] [,4] [,5] [1,] 16 19 22 25 28 [2,] 17 20 23 26 29 [3,] 18 21 24 27 30

131 / 154

array — (2)

> a <- array(1:30, dim=c(3,5,2)) > is.array(a)

[1] TRUE

> dim(a[ 1, , ]) # the first slice of a[]

[1] 5 2

> m <- a[ , 2, ] ; m

[,1] [,2] [1,] 4 19 [2,] 5 20 [3,] 6 21

> is.matrix(m) # a "slice" of a 3-d array is a matrix

[1] TRUE

132 / 154

slide-34
SLIDE 34

There are specific functions to examine the kind of an object, apart from class() (see also below),

> class(d.sport)

[1] "data.frame"

lower level functions mode() and typeof() can be useful. This information and more, namely the “inner” structure of an object, is available by str() :

> str(d.sport)

’data.frame’: 15 obs. of 7 variables: $ weit : num 7.57 8.07 7.6 7.77 7.48 7.88 7.64 7.61 7.27 7.49 ... $ kugel : num 15.7 13.6 15.8 15.3 16.3 ... $ hoch : int 207 204 198 204 198 201 195 213 207 204 ... $ disc : num 48.8 45 46.3 49.8 49.6 ... $ stab : int 500 480 470 510 500 540 540 520 470 470 ... $ speer : num 66.9 66.9 70.2 65.7 57.7 ... $ punkte: int 8824 8706 8664 8644 8613 8543 8422 8318 8307 8300

133 / 154

9.4 Object Oriented Programming

◮ Each object has a class, shown by class(object) :

> class(a) [1] "array" > c(class(m), class(m[,1]), class(d.sport)) # save space on slide [1] "matrix" "integer" "data.frame"

◮ Many functions do rather different things according to

the class of the first argument. Most prominently: print()

  • r plot() are “generic

function”s. Examine class of first argument and then call a “method” (function) accordingly. Example: plot(speer˜kugel, data=d.sport) calls the “formula method” of the “plot generic function”.

134 / 154

◮ The most basic generic function is print() 10.

Example: > r.t (or print(r.t) ) calls the "htest" “method” of print :

> r.t <- wilcox.test(extra ˜ group, data=sleep) > r.t

Wilcoxon rank sum test with continuity correction data: extra by group W = 25.5, p-value = 0.06933 alternative hypothesis: true location shift is not equal to 0

Note: The print() function is called whenever no explicit function is called 11: R is “auto – printing”.

10and/or show() for formal classes (aka “S4” classes) 11and the invisible() flag has not been activated; e.g., “A <- b” is “invisible” 135 / 154

(where the internal structure is quite different:

> str(r.t)

List of 7 $ statistic : Named num 25.5 ..- attr(*, "names")= chr "W" $ parameter : NULL $ p.value : num 0.0693 $ null.value : Named num 0 ..- attr(*, "names")= chr "location shift" $ alternative: chr "two.sided" $ method : chr "Wilcoxon rank sum test with continuity correction" $ data.name : chr "extra by group"

  • attr(*, "class")= chr "htest"

)

136 / 154

slide-35
SLIDE 35

Find available methods

> length(methods(print)) # ** MANY **

[1] 158

> methods(print)

[1] "print.acf" "print.anova" "print.aov" "print.aovlist" [5] "print.ar" "print.Arima" "print.arima0" "print.AsIs" [9] "print.aspell" "print.basedInt" "print.bibentry" "print.Bibtex" [13] "print.by" "print.checkFF" "print.checkRd" "print.checkTnF" [17] "print.citation" "print.codoc" "print.Date" "print.dDA" [21] "print.default" "print.density" "print.difftime" "print.dist" [25] "print.DLLInfo" "print.ecdf" "print.factanal" "print.factor" [29] "print.family" "print.formula" "print.ftable" "print.function" ......

137 / 154

Find available methods for plot() :

> methods(plot)

[1] plot.acf* plot.data.frame* plot.decomposed.ts* [4] plot.default plot.dendrogram* plot.density [7] plot.ecdf plot.factor* plot.formula* [10] plot.hclust* plot.histogram* plot.HoltWinters* [13] plot.isoreg* plot.lm plot.medpolish* [16] plot.mlm plot.ppr* plot.prcomp* [19] plot.princomp* plot.profile.nls* plot.spec [22] plot.spec.coherency plot.spec.phase plot.stepfun [25] plot.stl* plot.table* plot.ts [28] plot.tskernel* plot.TukeyHSD Non-visible functions are asterisked

Now, from these, we have already used implicitly

plot.default , the default method12, plot.formula , in

plot(y x, ...), plot.factor (which gave boxplots),

plot.data.frame (giving a scatter plot matrix, as with pairs()),

etc

12In this (S3) scheme, a generic function func always has a default method,

func.default() to be used when no specific method is defined for the class

  • f an argument.

138 / 154

◮ Apart from basic classes like matrix , formula , list , etc,

many functions, notably those fitting a statistical model, return their result of a specific class . Example: Linear regression ( − → function lm() )

> r.lm <- lm(speer ˜ kugel, data=d.sport) > class(r.lm)

[1] "lm"

◮ These classes come with “methods” for

print , plot , summary > summary(r.lm) > plot(r.lm)

## explained in another lecture ...

139 / 154

9.5 Attributes

In order to store all kinds of useful information along with an object, each object can have “attributes”.

◮ Some attributes we have met before: class, names ◮ dim is an attribute of matrices and data.frames ◮ row.names and names contain the row- and column

names of data.frames. ( dimnames for matrices.)

◮ All of these are obtained by a function with the same name:

> dim(d.sport)

[1] 15 7

140 / 154

slide-36
SLIDE 36

◮ All attributes of an object can be seen by attributes :

> attributes(d.sport)

$names [1] "weit" "kugel" "hoch" "disc" "stab" "speer" "punkte" $class [1] "data.frame" $row.names [1] "OBRIEN" "BUSEMANN" "DVORAK" "FRITZ" "HAMALAINEN" [6] "NOOL" "ZMELIK" "GANIYEV" "PENALVER" "HUFFINS" [11] "PLAZIAT" "MAGNUSSON" "SMITH" "MUELLER" "CHMARA"

◮ You will rarely use attributes actively.

Often, you do not see them when you just “print” an object

(the method of the object’s class for print does not show them.)

In other words, they are “intestines” of R .

141 / 154

9.7 Packages

◮ In R, by default you “see” only a basic set of functions, e.g.,

c , read.table , mean , plot , . . . , . . . .

◮ They are found in your “search path” of packages

> search() # the first is "your workspace"

[1] ".GlobalEnv" "package:graphics" "package:grDevices" [4] "package:datasets" "package:stats" "package:utils" [7] "package:methods" "Autoloads" "package:base"

> ls(pos=1) # == ls() ˜= "your workspace" - learned in

[1] "Mlibrary" "pkg" "tpkgs"

> str(ls(pos=2)) # content of the 2nd search() entry

chr [1:87] "abline" "arrows" "assocplot" "axis" "Axis" ...

> str(ls(pos=9)) # content of the 9th search() entry

chr [1:1167] "ˆ" "˜" "<" "<<-" "<=" "<-" "=" "==" ...

146 / 154

◮ The default list of R objects (functions, some data sets) is actually

not so small: Let’s call ls() on each search() entry:

> ls.srch <- sapply(grep("package:", search(), + value=TRUE), # "package:<name>" + ls, all.names = TRUE) > fn.srch <- sapply(ls.srch, function(nm) { + nm[ sapply(lapply(nm, get), is.function) ] }) > rbind(cbind(ls = (N1 <- sapply(ls.srch, length)), + funs = (N2 <- sapply(fn.srch, length))), + TOTAL = c(sum(N1), sum(N2)))

ls funs package:graphics 87 87 package:grDevices 101 98 package:datasets 105 1 package:stats 500 499 package:utils 184 182 package:methods 357 214 package:base 1246 1216 TOTAL 2580 2297

i.e., 2297 functions in R version 2.11.1.

147 / 154

◮ Till now, we have used functions from packages “base”, “stats”,

“utils”, “graphics”, and “grDevices” without a need to be aware of that.

◮ find("name") can be used:

> c(find("print"), find("find"))

[1] "package:base" "package:utils"

> ## sophisticated version of rbind(find("mean"), find("quantile"), > cbind(sapply(c("mean", "quantile", "read.csv", "plot"), + find))

[,1] mean "package:base" quantile "package:stats" read.csv "package:utils" plot "package:graphics"

148 / 154

slide-37
SLIDE 37

◮ R already comes with 27 packages pre-installed, namely the

“standard (or “base”) packages

base, datasets, graphics, grDevices, grid, methods, splines, stats, stats4, tcltk, tools, utils

and the “recommended” packages

boot, class, cluster, codetools, foreign, KernSmooth, lattice, MASS, Matrix, mgcv, nlme, nnet, rpart, spatial, survival

149 / 154

◮ Additional functions (and datasets) are obtained by

(possibly first installing and then) loading additional “packages”.

◮ > library(MASS)

  • r

require(MASS)

◮ How to find a command and the corresponding package?

> help.search("...")

14, (see Intro) ◮ On the internet: CRAN (http://cran.r-project.org, see

Resources on the internet (slide 15)

is a huge repository15 of R packages, written by many experts.

◮ CRAN Task Views help find packages by application area ◮ What does a package do?

> help(package = class)

  • r (←

→)

> library(help = class)

. Example (of small recommended) package:

> help(package = class)

14can take l..o..n..g.. (only the first time it’s called in an R session !) 15actually a distributed Network with a server and many mirrors, 150 / 154

> help(package = class)

Information f¨ ur Paket ’class’ Description: Package: class Priority: recommended Version: 7.3-3 Date: 2010-12-06 Depends: R (>= 2.5.0), stats, utils Imports: MASS Author: Brian Ripley <ripley@stats.ox.ac.uk>. Maintainer: Brian Ripley <ripley@stats.ox.ac.uk> Description: Various functions for classification. Title: Functions for Classification License: GPL-2 | GPL-3 URL: http://www.stats.ox.ac.uk/pub/MASS4/ LazyLoad: yes Packaged: 2010-12-06 11:46:04 UTC; ripley Repository: CRAN Date/Publication: 2010-12-09 11:56:32 Built: R 2.12.0; x86_64-unknown-linux-gnu; 2010-12-10

151 / 154

Index: SOM Self-Organizing Maps: Online Algorithm batchSOM Self-Organizing Maps: Batch Algorithm condense Condense training set for k-NN classifier knn k-Nearest Neighbour Classification knn.cv k-Nearest Neighbour Cross-Validatory Classification knn1 1-nearest neighbour classification lvq1 Learning Vector Quantization 1 lvq2 Learning Vector Quantization 2.1 lvq3 Learning Vector Quantization 3 lvqinit Initialize a LVQ Codebook lvqtest Classify Test Set from LVQ Codebook multiedit Multiedit for k-NN Classifier

  • lvq1

Optimized Learning Vector Quantization 1 reduce.nn Reduce Training Set for a k-NN Classifier somgrid Plot SOM Fits

152 / 154

slide-38
SLIDE 38

Installing packages from CRAN

◮ Via the “Packages” menu (in GUIs for R, e.g., on Mac, Windows) ◮ Directly via install.packages()16.

Syntax:

install.packages(pkgs,lib,repos = getOption(”repos”), ...) pkgs: character vector names of packages whose current

versions should be downloaded from the repositories.

lib: character vector giving the library directories where

to install the packages. If missing, defaults to the first element of .libPaths().

repos: character with base URL(s) of the repositories to use,

typically from a CRAN mirror. You can choose it interactively via chooseCRANmirror() or explicitly by options(repos= c(CRAN="http://...")) .

. . .: many more (optional) arguments.

16which is called anyway from the menu functions 153 / 154

Installing packages – Examples

◮ Install once, then use it via require() or library):

> chooseCRANmirror() > install.packages("sfsmisc") > ## For use: > require(sfsmisc) # to ‘‘load and attach’’ it

> install.packages("sp", # using default ’lib’ + repos = "http://cran.CH.r-project.org")

◮ or into a non-default library of packages:

> install.packages("sp", lib = "my_R_folder/library", + repos = "http://cran.CH.r-project.org") > ## and now load it from that library (location): > library(sp, lib = "my_R_folder}/library")

Note that you need “write permission” in the corresponding “library”, i.e., folder of packages (by default: .libPaths()[1]).

154 / 154