Introduction to R Programming Basics
Introduction to R Programming Basics
An introduction to
Thibaut Jombart
16 November 2015
1/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Outline
Introduction
Basics
Graphics
Functions
Modelling tools
Getting fancy
2/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Plan
Introduction
Basics
Types of objects
Basic object handling
Advanced object handling
Graphics
Basic graphics
ggplot2
Functions
Modelling tools
Linear models
Distributions
Optimization
Getting fancy
3/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
What is ?
4/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
• google ‘R’
• The project: [Link]/
• distribution on CRAN: [Link]/[Link]
• available on Windows, MacOSX, Linux
5/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
6/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
7/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Getting started
8/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
And then...
Things look like this. Not so pretty.
9/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Getting help
Different ways of getting help:
• ?foo/help("foo"): access the help page
of foo
• ??bar/[Link]("foo"): look for
foo in help pages
• RSiteSearch("foo"): search foo in
help pages & forum archives
• dedicated mailing lists:
[Link]/mailman/listinfo
• The -epi project:
[Link]/site/
therepiproject
10/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Plan
Introduction
Basics
Types of objects
Basic object handling
Advanced object handling
Graphics
Basic graphics
ggplot2
Functions
Modelling tools
Linear models
Distributions
Optimization
Getting fancy
11/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
[1] 1 2 3 4 5 6 7 8 9 10
> class(a)
[1] "integer"
[1] 1.1 2.1 3.1 4.1 5.1 6.1 7.1 8.1 9.1 10.1
> class(b)
[1] "numeric"
13/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
> class(a)
[1] "character"
14/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
> class(a)
[1] "factor"
> levels(a)
15/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
> class(a)
[1] "logical"
16/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Vectors
Vectors contain series of values of a given type:
> a <- c(1,2,10,-1, 1.123)
> a
> class(a)
[1] "numeric"
> [Link](a)
[1] TRUE
> length(a)
[1] 5
17/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Matrices
Matrices contain series of values of a given type, in a table:
> a <- matrix(sample(1:12),ncol=4)
> a
> class(a)
[1] "matrix"
> [Link](a)
[1] TRUE
> dim(a)
[1] 3 4
18/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Data frames
A [Link] is a table where different variables (columns) can
have different types:
> a <- [Link](age=c(10,54,3), sex=c("m","f","m"))
> a
age sex
1 10 m
2 54 f
3 3 m
> class(a)
[1] "[Link]"
> dim(a)
[1] 3 2
19/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Lists
A list can store any collection of objects of any types in different
slots:
> a <- list(age=c(10,54,3), sex=c("m","f","m"),
+ swab=matrix(sample(c("+","-"),replace=TRUE, 10), nrow=2,
+ dimnames=list(NULL, paste("t",1:5,sep=""))))
> a
$age
[1] 10 54 3
$sex
[1] "m" "f" "m"
$swab
t1 t2 t3 t4 t5
[1,] "+" "+" "-" "-" "+"
[2,] "-" "+" "+" "+" "+"
> class(a)
[1] "list"
> length(a)
[1] 3
20/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
21/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
21/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
21/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
21/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
21/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
21/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
21/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
21/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
21/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
[1] 10 9 8 7 6 5 4 3 2 1
> x[c(1,2,5)]
[1] 10 9 6
> letters[2:10]
[1] "b" "c" "d" "e" "f" "g" "h" "i" "j"
> letters[-(1:20)]
22/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
[1] TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
> x[x<5]
[1] 1 2 3 4
> x[x %% 3 == 0]
[1] 3 6 9
23/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
a b c d e f g h i j
44 74 72 42 22 5 96 36 95 50
> x[c("c","d","a","i")]
c d a i
72 42 44 95
24/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
[1] 0.70 0.26 0.05 1.38 -0.42 0.75 0.68 0.02 -0.50 2.01
[11] -1.46 1.02 0.97 -0.40
[1] 0.70 0.26 0.05 1.38 0.00 0.75 0.68 0.02 0.00 2.01 0.00 1.02
[13] 0.97 0.00
25/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
> x[c(3,1),]
> x[2,4:5]
[1] 11 14
> x[3,3]
[1] 9
26/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Subsetting lists
x[foo] to return a list, x[[foo]] for a single element:
> x <- list(a=rnorm(4), hi="Hello", stuff=letters[1:10]); x
$a
[1] -1.215042 1.867920 -2.428956 1.008787
$hi
[1] "Hello"
$stuff
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
> x[c(1,3)]
$a
[1] -1.215042 1.867920 -2.428956 1.008787
$stuff
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
> x[[3]]
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
> x$stuff
27/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
28/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
> !a # not A
[1] TRUE
[1] FALSE
[1] 1 2
29/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
> a[]
[1] 3 2 5 10
30/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
[1] 5
> mean(a>0)
[1] 0.3125
> a * (a>0)
[1] 0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5
31/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
60
Frequency
40
20
0
0 2 4 6 8 10 12
32/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
40
20
0
0 2 4 6 8 10 12
33/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Solution (continued):
> mean(x)
[1] 2.835922
> var(x)
[1] 4.243519
> mean(x>=3)
[1] 0.4176
34/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Finding matches
a %in% b looks for matches of a in b and returns a logical:
> a <- c('b','z','a','f')
> b <- letters[1:10]
> b
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
> a %in% b
[1] 2 NA 1 6
35/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
[1] 2 4 5
36/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
250
200
150
Frequency
100
50
0
0 5 10 15
38/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Onset to hospitalisation
250
200
150
Frequency
100
50
0
0 5 10 15
> summary(delay1)
39/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Hospitalisation to discharge
25
20
15
Frequency
10
5
0
5 10 15 20 25 30 35
> summary(delay2)
40/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Onset to death
7
6
5
Frequency
4
3
2
1
0
0 10 20 30 40
> summary(delay3)
41/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
[1] 0.7272727
42/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
[1] 0.7272727
42/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Plan
Introduction
Basics
Types of objects
Basic object handling
Advanced object handling
Graphics
Basic graphics
ggplot2
Functions
Modelling tools
Linear models
Distributions
Optimization
Getting fancy
43/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
[1] "[Link]"
> dim(iris)
[1] 150 5
> head(iris)
45/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Basic scatterplot
> plot(iris$[Link], iris$[Link])
2.5
● ●●
● ●
●●●● ● ● ● ●
● ● ●
●●●● ● ●
2.0
●●●● ● ●
●● ● ●
●●
● ● ● ●● ● ● ●
● ●
● ● ● ●
iris$[Link]
1.5
● ●●● ●●●
● ● ●●● ●
● ● ● ●●
●●●●●
●● ● ● ●
● ●●
1.0
● ● ● ●●
●
0.5
●
● ●●● ●
●●● ●
● ●●
● ● ●●
●●
●●●● ●
● ●●
1 2 3 4 5 6 7
iris$[Link]
● ●●
● ●
●●●● ● ● ● ●
● ● ●
●●●● ● ●
2.0
●●●● ● ●
●● ● ●
●●
● ● ● ●● ● ● ●
● ●
● ● ● ●
1.5
● ●●● ●●●
Petal width
● ● ●●● ●
● ● ● ●●
●●●●●
●● ● ● ●
● ●●
1.0
● ● ● ●●
●
0.5
●
● ●●● ●
●●● ●
● ●●
● ● ●●
●●
●●●● ●
● ●●
1 2 3 4 5 6 7
Petal length
47/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Adding color
> col <- iris$Species;levels(col)
● ●●
● ●
●●●● ● ● ● ●
● ● ●
●●●● ● ●
2.0
●●●● ● ●
●● ● ●
●●
● ● ● ●● ● ● ●
● ●
● ● ● ●
iris$[Link]
1.5
● ●●● ●●●
● ● ●●● ●
● ● ● ●●
●●●●●
●● ● ● ●
● ●●
1.0
● ● ● ●●
●
0.5
●
● ●●● ●
●●● ●
● ●●
● ● ●●
●●
●●●● ●
● ●●
1 2 3 4 5 6 7
iris$[Link]
48/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Changing symbols
Exercise: using the same trick as for color, use different symbols
for the species:
Iris data − petal length vs petal width
2.5
● ● ●
● ●
● ● ● ● ● ● ● ●
● ● ●
● ● ● ● ● ●
2.0
● ● ● ● ● ●
● ● ● ●
● ● ● ● ● ● ● ●
●
iris$[Link]
1.5
● ●
●
1.0
0.5
1 2 3 4 5 6 7
iris$[Link]
49/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
● ● ● ● ●●
0 5 10 15 20
pch = ...
50/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Changing symbols
Solution:
> symb <- iris$Species
> levels(symb) <- c(4, 5, 20)
> symb <- [Link]([Link](symb))
> plot(iris$[Link], iris$[Link], pch=symb, col=col)
> title("Iris data - petal length vs petal width")
● ● ●
● ●
● ● ● ● ● ● ● ●
● ● ●
● ● ● ● ● ●
2.0
● ● ● ● ● ●
● ● ● ●
● ● ● ● ● ● ● ●
●
iris$[Link]
1.5
● ●
●
1.0
0.5
1 2 3 4 5 6 7
iris$[Link] 51/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Adding a legend
> plot(iris$[Link], iris$[Link], pch=symb, col=col)
> title("Iris data - petal length vs petal width")
> legend("bottomright", col=c("royalblue","red3","gold"),
+ pch=c(4, 5, 20), leg=levels(iris$Species))
● ●
● ● ● ● ● ● ● ●
● ● ●
● ● ● ● ● ●
2.0
● ● ● ● ● ●
● ● ● ●
● ● ● ● ● ● ● ●
●
iris$[Link]
1.5
● ●
●
1.0
0.5
setosa
versicolor
●
virginica
1 2 3 4 5 6 7
iris$[Link]
52/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
2.0 2.5 3.0 3.5 4.0 0.5 1.0 1.5 2.0 2.5
● ● ●
● ● ● ● ● ●● ● ●●
7.5
● ● ●
● ● ●
● ● ●
● ● ● ● ●● ● ● ●
● ● ●
●● ● ● ● ● ●
● ● ● ● ● ●
● ●● ● ● ●●● ● ● ●●●
6.5
● ● ●● ● ● ● ● ●
●● ●● ● ●● ●● ●●●
[Link]
● ●●● ●● ●●● ● ● ● ●● ●●
● ● ● ● ● ●
● ● ● ● ● ●
● ● ●● ● ●
● ● ●
●● ● ● ●
● ● ●
5.5
● ● ●
● ● ●
4.5
2.0 2.5 3.0 3.5 4.0
● ● ● ● ● ●
● ● ●
●● ●● ●●
●●●
● ●
●● ●● ●
● ● ●
● ●● ●● ●●
[Link] ● ●●●
● ●
● ● ● ●●
●● ●● ● ●● ● ● ●
● ●
●
●
●
● ●●
● ●●●●
●
● ● ● ● ●
● ● ●●● ● ● ●● ● ● ● ● ● ●●●●● ●
● ●● ●●● ●●
● ● ● ● ● ●
● ● ● ● ● ● ● ●●●●
● ● ●
7
● ● ●
●● ● ● ● ●●●
● ● ● ● ● ●
● ● ● ● ● ● ●● ● ● ●
6
● ● ●● ● ●
● ●●● ●● ● ● ●
● ● ● ●●
● ●
● ●● ● ● ●●●● ●●●● ● ● ● ● ●
●● ●
● ●● ●● ● ●● ● ●
● ● ● ●
●● ● ● ●
● ● ● ● ●● ●
● ● ●●● ● ●● ● ●●
5
●● ● ●
●●●●
● ●● ●
● ●
● ● ●
● ●
● ● ●
[Link]
4
3
2
1
0.5 1.0 1.5 2.0 2.5
● ● ● ● ● ● ●●
● ● ● ● ● ● ● ●
● ● ●●● ● ● ●●● ● ●●●● ● ● ● ●
●● ● ● ● ● ●● ●
● ●●● ● ● ● ●● ● ●●●● ● ●
●● ● ● ● ● ● ● ● ● ●●●● ● ●
● ●● ● ● ●● ●● ● ●
●●●●●●● ● ●● ● ●●●●●● ●● ● ●● ● ● ●
● ● ●
● ● ●
● ● ● ● ●●
[Link]
● ● ●
53/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Documentation: [Link]/current/
54/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Documentation: [Link]/current/
54/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Documentation: [Link]/current/
54/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Documentation: [Link]/current/
54/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Documentation: [Link]/current/
54/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Documentation: [Link]/current/
54/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Documentation: [Link]/current/
54/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
2.5 ● ●●
● ●
●●●● ● ● ● ●
● ● ●
●●●● ● ●
2.0 ●●●● ● ●
●● ● ●
●● ● ●● ● ● ●
● ●
● ● ● ●
● ● ●●● ●
● ●●●●●●●
●● ● ● ●
● ●●
1.0 ● ● ● ●●
0.5 ●
● ●●● ●
●●● ●
● ●●●●●● ●
● ●●
0.0
2 4 6
[Link]
55/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
2.5
2.0
1.5
Species
[Link]
● setosa
versicolor
virginica
1.0
0.5 ●
● ●●● ●
●●● ●
● ●●●●●● ●
● ●●
0.0
2 4 6
[Link]
56/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
2.5
2.0
1.5
Species
[Link]
● setosa
versicolor
virginica
1.0
0.5 ●
● ●●● ●
●●● ●
● ●●●●●● ●
● ●●
0.0
2 4 6
[Link]
57/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
2.5
2.0
Species
1.5
[Link]
setosa
versicolor
virginica
1.0
0.5
0.0
2 4 6
[Link]
58/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
2.0
Species
Petal width (cm)
1.5
setosa
versicolor
virginica
1.0
0.5
0.0
2 4 6
Petal length (cm)
59/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
60/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
100
Weekly incidence
Hospital
1
2
3
4
50
61/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
100
Weekly incidence
Hospital
1
2
3
4
50
62/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
ggplot2 : facetting
Exercise: using facet grid(rows ∼ columns), create the
multi-panel figure below:
40
1
20
40
2
Weekly incidence
20
Hospital
1
0 2
3
40 4
3
20
40
4
20
0
Feb 15 Mar 01 Mar 15 Apr 01
Date of onset
63/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
ggplot2 : facetting
Solution:
> p + facet_grid(Hospital ~ .)
40
1
20
40
2
Weekly incidence
20
Hospital
1
0 2
3
40 4
3
20
40
4
20
0
Feb 15 Mar 01 Mar 15 Apr 01
Date of onset
64/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Plan
Introduction
Basics
Types of objects
Basic object handling
Advanced object handling
Graphics
Basic graphics
ggplot2
Functions
Modelling tools
Linear models
Distributions
Optimization
Getting fancy
65/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
A simple example
+ ## return result
+ return(x)
+ }
>
> foo(9)
[1] 10
> foo([Link]("2001-01-01"))
[1] "2001-01-02"
66/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
[1] 10
> temp
67/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
[1] 1009
> temp
[1] 1000
> x
[1] 9
68/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
3
2
1
a
0
−1
−2
−3
0 2 4 6 8 10 12
69/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Plan
Introduction
Basics
Types of objects
Basic object handling
Advanced object handling
Graphics
Basic graphics
ggplot2
Functions
Modelling tools
Linear models
Distributions
Optimization
Getting fancy
70/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Linear regression
yi = β0 + β1 xi + i
71/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
2.5
● ●●
● ●
●●●● ● ● ● ●
● ● ●
●●●● ● ●
2.0
●●●● ● ●
●● ● ●
●●
● ● ● ●● ● ● ●
● ●
● ● ● ●
iris$[Link]
1.5
● ●●● ●●●
● ● ●●● ●
● ● ● ●●
●●●●●
●● ● ● ●
● ●●
1.0
● ● ● ●●
●
0.5
●
● ●●● ●
●●● ●
● ●●
● ● ●●
●●
●●●● ●
● ●●
1 2 3 4 5 6 7
iris$[Link]
The function lm
Call:
lm(formula = [Link] ~ [Link], data = iris)
Coefficients:
(Intercept) [Link]
-0.3631 0.4158
73/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
● ●●
● ●
●●●● ● ● ● ●
● ● ●
●●●● ● ●
2.0
●●●● ● ●
●● ● ●
●●
● ● ● ●● ● ● ●
● ●
● ● ● ●
iris$[Link]
1.5
● ●●● ●●●
● ● ●●● ●
● ● ● ●●
●●●●●
●● ● ● ●
● ●●
1.0
● ● ● ●●
●
0.5
●
● ●●● ●
●●● ●
● ●●
● ● ●●
●●
●●●● ●
● ●●
1 2 3 4 5 6 7
iris$[Link]
74/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
75/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
76/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
0.4
0.3
p(X)
0.2
0.1
0.0
−4 −2 0 2 4
77/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Cumulative distribution
1.0
0.8
0.6
p(X>=x)
0.4
0.2
0.0
−4 −2 0 2 4
78/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Quantile distribution
2
1
value of X
0
−1
−2
quantile
79/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Random variate
> x <- rnorm(1000)
> head(x,5)
Histogram of x
0.4
0.3
Density
0.2
0.1
0.0
−3 −2 −1 0 1 2 3 4
x 80/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Other distributions
Similar functions exist for a range of distributions:
• runif: uniform distribution
• rpois: Poisson
• rbinom: binomial
• rgamma: gamma
• rbeta: beta
• rchisq: χ2
• rexp: exponential
• rgeom: geometric
• rcauchy: Cauchy
• mvrnorm: multivariate Normal
See dedicated task view: [Link]/web/views.
81/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
−0.2
−0.3
−0.4
0 2 4 6 8 10
x 82/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
[1] 2.394531
> res$value
[1] -0.1637565
83/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
●
0.1
0.0
−0.1
weird
−0.2
−0.3
−0.4
0 2 4 6 8 10
84/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
(Intercept) [Link]
-0.3630755 0.4157554
●
observed ● ● ●
predicted ● ●
● ● ● ● ● ● ● ●
● ● ●
● ● ● ● ● ●
2.0
● ● ● ● ● ●
● ● ● ●
● ● ● ● ● ● ● ●
● ●
● ● ● ●
1.5
● ● ● ● ● ● ●
Petal width
● ● ● ● ● ●
● ● ● ● ● ● ● ●
● ● ● ● ●
● ● ●
1.0
● ● ● ● ●
●
0.5
● ● ● ● ●
● ● ● ●
● ● ● ● ● ● ● ●
● ● ●
1 2 3 4 5 6 7
Petal length
85/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
86/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
87/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Plan
Introduction
Basics
Types of objects
Basic object handling
Advanced object handling
Graphics
Basic graphics
ggplot2
Functions
Modelling tools
Linear models
Distributions
Optimization
Getting fancy
88/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Histogram of x
1e+05
8e+04
6e+04
Frequency
4e+04
2e+04
0e+00
0 5 10 15
> ll1
[1] -1418994
90/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Using a sapply:
> [Link](
+ ll2 <- sum(sapply(1:1e6, function(i) dnorm(x[i], mean=m[i], log=TRUE)))
+ )
> ll2
[1] -1418994
91/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
> ll3
[1] -1418994
92/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Literate programming
93/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
Sweave vs knitr
Sweave
Sweave pdf latex
• compilation: [Link] −
−−−−→ [Link] −−−−−→ [Link]
• chunks of code embedded in LATEXdocument
• needs specific headers to compile
• limited options
knitr
knit2pdf
• compilation: [Link] −−−−−→ [Link]
• syntax nearly identical to Sweave
• syntax highlighting
• very flexible, simpler
See: [Link]
94/95
Introduction Basics Graphics Functions Modelling tools Getting fancy
To conclude
Lots of resources:
• ’s official website: [Link]/
• mailing lists: [Link]/[Link]
• task views: [Link]/web/views/
• The -epi project:
[Link]/site/therepiproject/
• The book by Crawley (2012), Wiley.
95/95