/
session2.Rmd
262 lines (208 loc) · 5.85 KB
/
session2.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
---
title: "Training in ade4 in R - Module I: Basic methods"
subtitle: "Principal component analysis"
author: "Stéphane Dray"
date: '`r Sys.Date()`'
output:
xaringan::moon_reader:
lib_dir: libs
css: [custom.css, xaringan-themer.css]
nature:
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
includes:
before_body: preamble.tex
---
```{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
knitr::opts_chunk$set(tidy=TRUE, tidy.opts=list(width.cutoff=50), fig.height= 4, fig.width=4, dev='svg', fig.align="center", fig.path = "fig/")
```
```{r xaringan-themer, include=FALSE}
library(xaringanthemer)
style_duo(primary_color = "#1F4257", secondary_color = "#F97B64", table_row_even_background_color = "#1F4257")
```
---
# Data structure
.left-column[
```{r, echo = FALSE}
knitr::include_graphics("img/onetable.png")
```
]
.right-column[
* One table with *p* variables measured on *n* individuals
* All variables are **quantitative**
* For instance
- sites $\times$ environmental variables
- species $\times$ traits
- individuals $\times$ alleles
- populations $\times$ alleles
]
---
# Objectives
* Identify what is the main information contained in the table
- Identify which variables are the most linked
- Identify the principal differences/similarities between individuals
---
# Data
We consider the `meaudret` data set
```{r}
library(ade4)
data(meaudret)
names(meaudret)
dim(meaudret$env)
names(meaudret$env)
```
---
The data set contains an environmental table with 20 measurements of 9 environmental variables. The measurements have been made in 5 sites at each season along a small French stream (see `?meaudret`)
```{r}
head(meaudret$design)
```
We want to know
* what are the main environmental gradients, i.e., which variables co-vary (if any)
* which samples have similar/different environmental conditions
---
# Principal component analysis
* $\mathbf{X}$ contains centred or scaled variables
* $\mathbf{Q} = \mathbf{I}_p$ is the identity matrix (diagonal matrix with 1s)
* $\mathbf{D} = \frac{1}{n}\mathbf{I}_n$ is the diagonal matrix with $\frac{1}{n}$
.column-left[
```{r, echo = FALSE}
knitr::include_graphics("img/onetable.png")
```
]
.column-center[
.center[
`dudi.pca`
```{r, echo = FALSE}
knitr::include_graphics("img/arrow.png")
```
]
]
.column-right[
```{r, echo = FALSE}
knitr::include_graphics("img/pca-map.png")
```
]
---
# Maximized criteria
* For individuals
$$ Q(\mathbf{a})=\sqnorm{\mathbf{XQa}}{\mb{D}} = \sqnorm{\mathbf{Xa}}{\frac{1}{n}\mb{I}_n} = var(\mathbf{Xa})= \lambda
$$
* For variables
- Centred data ( $x_{ij} - \bar{x}_j$ )
$$S(\mathbf{b})=\sqnorm{\mathbf{X}\tr\mathbf{Db}}{\mb{Q}} =\sqnorm{\frac{1}{n}\mathbf{X}\tr\mathbf{b}}{\mb{I}_p} = \sum_{j=1}^p{cov^2(\mathbf{x}_j, \mathbf{b})} = \lambda$$
- Scaled data ( $(x_{ij} - \bar{x}_j) / s_j$ )
$$S(\mathbf{b})=\sqnorm{\mathbf{X}\tr\mathbf{Db}}{\mb{Q}} =\sqnorm{\frac{1}{n}\mathbf{X}\tr\mathbf{b}}{\mb{I}_p} = \sum_{j=1}^p{cor^2(\mathbf{x}_j, \mathbf{b})} = \lambda$$
---
# The `dudi.pca` function
## Arguments
```{r}
args(dudi.pca)
```
* `df` is a `data.frame` with the data
* `row.w` and `col.w` are optional vectors of weights
* `center` and `scale` define the standardization of the data
* `scannf` and `nf` allow to set the number of dimensions to interpret
```{r}
pca.meau <- dudi.pca(meaudret$env, scannf = FALSE)
```
---
## Returned values
```{r}
names(pca.meau)
```
It returns an object of class `dudi` containing:
- `$eig`: eigenvalues ( $\mb{\Lambda}$ )
- `$cw`: column weights ( $\mb{Q}=\mb{I}_p$ )
- `$lw`: row weights ( $\mb{D}=\frac{1}{n}\mb{I}_n$ )
- `$tab`: transformed data table ( $\mb{X}$ )
- `$c1`: principal axes or variable loadings ( $\mb{A}$ )
- `$li`: row scores ( $\mb{L}=\mathbf{XA}$ )
- `$l1`: principal components ( $\mb{B}$ )
- `$co`: column scores ( $\mb{C}=\frac{1}{n}\mb{X}\tr\mb{B}$ )
---
# Graphical representation and interpretation
As we have *two* analyses (individuals and variables spaces), two representations can be defined:
* **distance biplot** where $\mb{A}$ and $\mb{L}=\mathbf{XA}$ (`$c1`, `$li`) are superimposed.
* **correlation biplot** where $\mb{B}$ and $\mb{C}=\frac{1}{n}\mb{X}\tr\mb{B}$ (`$l1`, `$co`) are superimposed.
In the first interpretation, PCA finds coefficients for variables (`$c1`) to compute a linear combination (`$li`) that provides an ordination of individuals with the greatest dispersion (maximum variance).
In the second interpretation, PCA provides a linear combination (`$l1`) that maximise the correlations (`$co`) with all variables (or covariances for centred PCA). Hence, it is the best summary of the variables.
---
## The `biplot` function
```{r, message=FALSE}
library(adegraphics)
```
.pull-left[
```{r}
biplot(pca.meau)
```
]
.pull-right[
```{r}
biplot(pca.meau, permute = TRUE)
```
]
---
## Separate representations
.pull-left[
```{r}
s.label(pca.meau$li)
```
]
.pull-right[
```{r}
s.arrow(pca.meau$co)
```
]
---
## Separate representations
.pull-left[
```{r}
s.label(pca.meau$li)
```
]
.pull-right[
```{r}
s.corcircle(pca.meau$co)
```
]
---
## Separate representations
.pull-left[
```{r}
s.class(pca.meau$li, meaudret$design[,1], col = TRUE)
```
]
.pull-right[
```{r}
s.arrow(pca.meau$co)
```
]
---
# To scale or not to scale
Scaling should be performed when we do not want that differences in variances affect the results
```{r}
pca.meau.c <- dudi.pca(meaudret$env, scannf = FALSE, scale = FALSE)
```
.pull-left[
```{r, out.width="70%", echo=FALSE}
biplot(pca.meau, row.main = "scale = TRUE")
```
]
.pull-right[
```{r, out.width="70%", echo=FALSE}
biplot(pca.meau.c, row.main = "scale = FALSE")
```
]
In our case, we must scale the data as differences in variances are mainly due to differences in units
---
# Inertia statistics
```{r}
summary(pca.meau)
```
---
# PCA in practice
.center[
[Go to practical 2](../../practical/session2/session2.html)
]