forked from jcoliver/learn-r
-
Notifications
You must be signed in to change notification settings - Fork 0
/
003-intro-multivariate.Rmd
574 lines (462 loc) · 30 KB
/
003-intro-multivariate.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
---
title: "Introduction to Multivariate Statistics in R"
author: "Jeff Oliver"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
html_document:
css: stylesheets/markdown-styles.css
---
An introduction to using the R statistics package and the RStudio interface for multivariate statistics.
#### Learning objectives
1. Prepare data in spreadsheet program (e.g. Excel, LibreOffice Calc) for export to R
2. Read data from files into R
3. Run Principal Components Analysis (PCA) and graphically display results
4. Perform Discriminant Function Analysis (DFA) and interpret the results
## Setup
### Workspace organization
First we need to setup our development environment. We need to create two folders: 'data' will store the data we will be analyzing, and 'output' will store the results of our analyses.
```{r, eval = FALSE}
dir.create(path = "data")
dir.create(path = "output")
```
### Preparing data in a format R can read
+ Download data file from [https://jcoliver.github.io/learn-r/data/otter-mandible-data.xlsx](https://jcoliver.github.io/learn-r/data/otter-mandible-data.xlsx) or [http://tinyurl.com/otter-data](http://tinyurl.com/otter-data) (the latter just re-directs to the former). These data are a subset of those used in a study on skull morphology and diet specialization in otters [doi: 10.1371/journal.pone.0143236](http://dx.doi.org/10.1371/journal.pone.0143236).
+ Open this file, otter-mandible-data.xlsx, in spreadsheet program like Microsoft Excel<sup>®</sup> or LibreOffice Calc.
+ Save a copy of the file as a CSV (comma-separated values) file named 'otter-mandible-data.csv' in the data folder you created above:
+ In MS Excel<sup>®</sup>, select File > Save As... and in the dialog that appears, select CSV from the type dropdown menu.
+ In LibreOffice Calc, select File > Save As... and in the dialog that appears, select Text CSV (.csv) in the Format dropdown in the lower-right portion of the dialog.
![(A) Skull morphology of _Lutra lutra_. (B) Six mandible measurements used in this lesson: m1 = mandible ramus width, m2 = mandible ramus height, m3 = moment arm temporalis, m4 = outlever at carnassal, m5 = moment arm masseter, m6 = jaw length. Adapted from [https://commons.wikimedia.org/wiki/File:MSU_V2P1b_-_Lutra_lutra_skull.png](https://commons.wikimedia.org/wiki/File:MSU_V2P1b_-_Lutra_lutra_skull.png).](images/otter-mandible.svg)
### Reading data into R for PCA
```{r}
otter <- read.csv(file = "data/otter-mandible-data.csv", header = TRUE)
```
Missing data can cause problems in downstream analyses, so we will just remove any rows that have missing data. Here we replace the original data object `otter` with one in which there are no missing values. Note, this _does not_ alter the data in the original file we read into R; it only alters the data object `otter` currently in R's memory.
```{r}
otter <- na.omit(otter)
```
And because R _does not_ automatically re-number the rows when we drop those with `NA` values, we can force re-numbering via:
```{r}
rownames(otter) <- NULL
```
## Principal Components Analysis
Why PCA? Very briefly, Principal Components Analysis is a way of re-describing the variation observed in your data. It serves as a means of reducing the dimensionality of data (i.e. reducing the number of predictor variables) and is often used for exploratory analyses. The full rationale and mathematically underpinnings are waaaaaaaay beyond the scope of this lesson, and other resources already do a fairly good job of explaining PCA. If you want a few perspectives for a relatively novice audience, check out this [Why PCA? (or "how to explain PCA to your grandmother")](http://stats.stackexchange.com/questions/2691/making-sense-of-principal-component-analysis-eigenvectors-eigenvalues) thread at Stack Overflow. If you are more inclined to print media, I highly recommend B.F.J. Manly's _Multivariate Statistical Methods: A primer_ (2004), which provides an excellent introduction to a variety of multivariate statistic topics.
### Running PCA
So, on to the code:
```{r}
pca.fit <- prcomp(x = otter[, -c(1:3)], scale. = TRUE)
```
That's PCA. We pass the data to the `x` parameter, skipping the first three columns `[, -c(1:3)]` because those columns have the specimen information (species identity and accession information). We also set the `scale.` parameter to `TRUE` because we want to transform the data so each column has a mean of zero and a variance of one.
To look at the results, we use the `summary` command and assign the output to a variable.
```{r}
pca.summary <- summary(pca.fit)
ls(pca.summary) # Lists the objects produced by summary
```
We are interested to know (1) what are the important factors that emerge from the PCA (i.e. which ones explain a lot of variation) and (2) what do these factors actually say about the variation observed in our data. For (1), look at the `importance` object in the summary:
```{r}
pca.summary$importance
```
The second row, `Proportion of Variance`, shows how much variation in the data is described by each component; notice that the first component, PC1, explains the most variance, `r round(pca.summary$sdev[1]^2/sum(pca.summary$sdev^2), 4)`, or `r round(pca.summary$sdev[1]^2/sum(pca.summary$sdev^2) * 100, 2)`% of the total variance, the second component explains the second most variance (`r round(pca.summary$sdev[2]^2/sum(pca.summary$sdev^2) * 100, 2)`%), and so on, with each successive component explaining a lower proportion of the total variance. For the remainder of the lesson, we will focus on the first two principal components, PC1 and PC2, which together explain `r round((pca.summary$sdev[1]^2 + pca.summary$sdev[2]^2)/sum(pca.summary$sdev^2) * 100, 2)`% of the observed variation in the skull measurements.
### A brief interpretion of PCA results (part one)
But what about that variation? What are the principal components actually explaining? To address this (point 2 from above), we need to look at the loadings of the PCA. The `rotation` object from the summary call has the information we are interested in. Focus on the values in the PC1 column:
```{r}
pca.summary$rotation
```
```{r echo = FALSE}
# Crude, but should be fine
direction <- "positive"
if (sum(pca.summary$rotation) < 0) {
direction <- "negative"
}
```
Looking at the signs of the loadings, we see they are all the same (`r direction`), thus this first component, explaining most of the variation in the measurements, is really just reflecting variation in size. That is, since all the loadings have the same sign, large values for one skull measurement generally coincide with large values for other skull measurements for this first component.
The second principal component is a little more interesting. Two of the variables, m1 and m4 don't contribute much to the component (the magnitudes of their respective loadings (.173 and .031) are small compared to the other four skull measurements). The remaining four indicate a shape difference, with one pair of variables having positive loadings and one pair having negative loadings. This interpretation of the second principal component would benefit greatly from a graphical representation.
### Plotting PCA results
Plotting the results of a PCA can be done using a simple call to the `biplot` function:
```{r}
biplot(x = pca.fit)
```
But that figure really leaves much to be desired, and gets messier with larger sample sizes and more variables. If you want to find out more about how that figure is produced, look at the documentation for `biplot` (`?biplot`).
Instead, we can plot the scores of the first two principal components using the standard `plot` command, using the scores that are stored in the `x` object of `pca.fit`:
```{r}
plot(x = pca.fit$x[, 1],
y = pca.fit$x[, 2],
xlab = "PC 1",
ylab = "PC 2")
```
Well, maybe that plot isn't so useful either. It does help a bit if we color the points by species, though. We start by creating a small vector which only contains the species names; we'll use this for the legend and for assigning colors:
```{r}
# Pull out the unique values in the 'species' column
species.names <- unique(otter$species)
species.names # a quick look at the values in the vector
```
We want a vector of colors, too, one for each species. Since there are four species, we have four colors:
```{r}
legend.cols <- c("black", "green4", "cyan3", "red3")
```
The legend.cols vector will be used for the legend, but we still need a color vector for each _sample_. We can use the indexing of the `species.names` and `legend.cols` to create a vector which has a value for each of the points we want to plot. Here we first assign all values the same color (the first color in the `legend.cols` vector). Then we compare values in the `otter$species` column to the second, third, and fourth possible values in the `species.names` vector.
```{r}
pt.cols <- rep(x = legend.cols[1], length = nrow(otter))
pt.cols[otter$species == species.names[2]] <- legend.cols[2]
pt.cols[otter$species == species.names[3]] <- legend.cols[3]
pt.cols[otter$species == species.names[4]] <- legend.cols[4]
```
> Question: Why did we not do the assignment / comparison for the first element in `species.names` and `legend.cols`?
And finally, we draw the plot, adding point colors through the `col` parameter and adding the legend after the plot is drawn.
```{r}
plot(x = pca.fit$x[, 1],
y = pca.fit$x[, 2],
xlab = "PC 1",
ylab = "PC 2",
pch = 19,
col = pt.cols)
legend("bottomleft", legend = species.names, pch = 19, col = legend.cols, cex = 0.8)
```
### A brief interpretion of PCA results (part two)
Ah, now this plot is a little more interesting. Note the first principal component scores along the x-axis, and there is actually pretty clear separation among some of species. As mentioned above, this principal component is really an index of size, which we can visualize by looking at the actual skull measurements across the four species. First note the distribution of the four species along the x-axis: _A. cinerea_ has the lowest values, _E. lutris_ and _P. brasiliensis_ have the highest values, and _L. canadensis_ has values in the middle. We can use `boxplot` to show the distributions of each measurement for each species:
```{r}
# Set-up a multi-panel graph (2 rows, 3 columns), filling row by row
par(mfrow = c(2, 3), las = 2)
# Plot each of the six measurements in a different plot
boxplot(formula = m1 ~ species, data = otter)
boxplot(formula = m2 ~ species, data = otter)
boxplot(formula = m3 ~ species, data = otter)
boxplot(formula = m4 ~ species, data = otter)
boxplot(formula = m5 ~ species, data = otter)
boxplot(formula = m6 ~ species, data = otter)
# Restore graphing defaults
par(mfrow = c(1, 1), las = 0)
```
Looking at these boxplots, we see that indeed, for all six measurements, _A. cinerea_ has the lowest values, _E. lutris_ and _P. brasiliensis_ have the highest values, and _L. canadensis_ has values in the middle.
Remember that second principal component, PC2, on the y-axis of our scatterplot:
```{r, echo = FALSE}
plot(x = pca.fit$x[, 1],
y = pca.fit$x[, 2],
xlab = "PC 1",
ylab = "PC 2",
pch = 19,
col = pt.cols)
legend("bottomleft", legend = species.names, pch = 19, col = legend.cols, cex = 0.8)
```
And the loadings for the first two components are:
```{r, echo = FALSE}
pca.summary$rotation[, 1:2]
```
In this second principal component, as mentioned before, m1 and m4 don't contribute much to the component [the magnitudes of their respective loadings (.173 and .031) are small compared to the other four skull measurements]. The remaining four loadings indicate a shape difference, with one pair of variables having positive loadings and one pair having negative loadings. This component describes variation in the four variables: high values of PC1 correspond to
high values of m2 and m5 and low values of m3 and m6, while specimens with low values of PC1 have the opposite (low
m2 and m5, high m3 and m6). You can confirm this looking again at a boxplot for these values, focusing only on those four variables of interest for the two species that are differentiated along PC2, _E. lutris_ and _P. brasiliensis_:
```{r}
# Pull out the data for the two species of interest
two.species <- c("E. lutris", "P. brasiliensis")
otter.two <- otter[otter$species %in% two.species, ]
# Drop the levels corresponding to the two species we excluded
otter.two$species <- factor(otter.two$species)
# Prepare colors
legend.cols <- c("black", "red3")
pt.cols <- rep(x = legend.cols[1], length = nrow(otter.two))
pt.cols[otter.two$species == two.species[2]] <- legend.cols[2]
# Plot m6 against m5
plot(x = otter.two$m5,
y = otter.two$m6,
xlab = "m5",
ylab = "m6",
pch = 19,
col = pt.cols)
legend("topright", legend = two.species, pch = 19, col = legend.cols, cex = 0.8)
```
Considering the scores of PC2, _P. brasilensis_ had low values, while _E. lutris_ had high values. If we consider
the loadings for just m5 and m6 (0.567 and -0.440, respectively), the plot above shows what we expect: specimens
with high values of PC2 (i.e. those of _E. lutris_), should have relatively low values of m5 and high values of
m6; this contrasts with specimens with low values of PC2 (i.e. those of _P. brasilensis_), which are characterized
by high values of m5 and low values of m6. The second principal component is thus describing variation in _relative_, not absolute, sizes of these four different measurements.
### Final script for PCA plot
Our script for performing Principal Components Analysis and graphing the first two components should look like this:
```{r, eval = FALSE}
# PCA on otter jaw measurements
# Jeff Oliver
# 2016-11-18
# Read in data
otter <- read.csv(file = "data/otter-mandible-data.csv", header = TRUE)
# Drop rows with NA
otter <- na.omit(otter)
# Renumber rows
rownames(otter) <- NULL
# Run PCA
pca.fit <- prcomp(x = otter[, -c(1:3)], scale. = TRUE)
pca.summary <- summary(pca.fit)
# Plotting results
# Pull out the unique values in the 'species' column for legend
species.names <- unique(otter$species)
# Set up a vector of colors for the *legend*
legend.cols <- c("black", "green4", "cyan3", "red3")
# Set up a vector of colors for the actual *plot*, based on values in the
# 'species' column and the legend colors vector. This vector has one element
# corresponding to each row of the otter data frame.
pt.cols <- rep(x = legend.cols[1], length = nrow(otter))
pt.cols[otter$species == species.names[2]] <- legend.cols[2]
pt.cols[otter$species == species.names[3]] <- legend.cols[3]
pt.cols[otter$species == species.names[4]] <- legend.cols[4]
# Plot the first two components
plot(x = pca.fit$x[, 1],
y = pca.fit$x[, 2],
xlab = "PC 1",
ylab = "PC 2",
pch = 19,
col = pt.cols)
legend("bottomleft", legend = species.names, pch = 19, col = legend.cols, cex = 0.8)
```
***
## Discriminant Function Analysis
Principal Components Analysis is useful to describe variation, especially when groups of samples are not known _a priori_. However, when the groups _are_ known, one can use Discriminant Function Analysis, DFA, to see if the these groups can be differentiated based on a suite of variables. We will use the same otter data to see if we can differentiate among the four species based on the jaw measurements.
### Reading data into R for DFA
```{r}
# Read in data
otter <- read.csv(file = "data/otter-mandible-data.csv", header = TRUE)
# Drop rows with NA
otter <- na.omit(otter)
# Renumber rows
rownames(otter) <- NULL
```
### Running DFA
To run a DFA, we use a function in the `MASS` library, an additional package of R functions. To load a package for use in R, we use the `library` function and include the name of the package (see below if you want to learn more about [installing packages](#installing-r-packages)):
```{r}
library("MASS")
```
And we run DFA with a call to `lda` indicating which column contains our _a priori_ groups in via the `formula` parameter. In this case, we pass `formula = species ~ m1 + m2 + m3 + m4 + m5 + m6`, which means `species` contains our grouping variable and the 'm' variables are the predictors in the analysis:
```{r}
# Run DFA, allowing equal probability assignment for each of four groups
lda.fit <- lda(formula = species ~ m1 + m2 + m3 + m4 + m5 + m6,
data = otter,
prior = c(1,1,1,1)/4)
```
We also indicate that assignment to each species is equally likely (25%) by setting the prior probabilities for group membership through the `prior` parameter.
### Plotting DFA results
We can plot DFA results in a similar fashion as we did for PCA results. In this case we plot the scores for the first and second discriminant functions.
```{r}
# Get individual values based on lda model and original measurements
plda <- predict(object = lda.fit,
newdata = otter)
# Set up colors for each point, based on a priori identifications
legend.cols <- c("black", "green4", "cyan3", "red3")
pt.cols <- rep(x = legend.cols[1], length = nrow(otter))
pt.cols[otter$species == levels(otter$species)[2]] <- legend.cols[2]
pt.cols[otter$species == levels(otter$species)[3]] <- legend.cols[3]
pt.cols[otter$species == levels(otter$species)[4]] <- legend.cols[4]
# Plot scores for first (plda$x[, 1]) and second (plda$x[, 2]) discriminant functions
plot(x = plda$x[, 1],
y = plda$x[, 2],
xlab = "LDA1",
ylab = "LDA2",
pch = 19,
col = pt.cols)
legend("topright", legend = levels(otter$species), cex = 0.8, col = legend.cols, pch = 19)
```
### A brief interpretion of DFA results (part one)
Let's take a look at how well the DFA did in classifying the specimens. In order to do this, we are going to call `lda` again, this time passing an additional argument to the `CV` parameter. By setting `CV = TRUE`, R will perform cross-validation on the model and provide posterior probabilities of group membership in the `posterior` object returned by `lda`.
```{r}
lda.fit <- lda(formula = species ~ m1 + m2 + m3 + m4 + m5 + m6,
data = otter,
prior = c(1,1,1,1)/4,
CV = TRUE)
```
To see how well the DFA does in categorizing each specimen, we use `table`.
```{r}
table(otter$species, lda.fit$class, dnn = c("a priori", "assignment"))
```
In this case, we passed `table` two vectors:
+ The _a priori_ species identification of each sample, which is stored in `otter$species`
+ The _a posteriori_ assignment of each sample from the DFA, stored in `lda.fit$class`
We also passed a value to the `dnn` parameter, which is used to label the table ("dnn" stands for _dimnames names_. Yes, that's right, _dimnames names_. Seems repetitive to me, too). The first row of the table shows how the specimens identified as _A. cinerea_ were assigned in the DFA - all were correctly assigned to _A. cinerea_. What should jump out at you in this table is that *all* specimens' DFA assignments match their _a priori_ identifications.
### A brief interpretion of DFA results (part two)
But let's take a *closer* look at the results. The first six rows show the assignment probabilities for six specimens:
```{r}
head(lda.fit$posterior)
```
The `lda.fit$posterior` matrix holds the posterior probability of assignment to each of the four species for each row of data in the original `otter` data. The first row indicates the probabilities that the first observation in `otter` is assigned to each species. For this first observation, the only non-negligible assignment probability is to _`r colnames(lda.fit$posterior)[which(lda.fit$posterior[1, ] == max(lda.fit$posterior[1, ]))]`_. For this data set, most observations are assigned with high probability to a single group.
However, some probabilities are not so trivial. Look at the values for row 88 (rounding to three decimal places):
```{r}
round(lda.fit$posterior[88, ], 3)
```
How can we identify those with ambiguous assignments? We could go row-by-row to see which rows have assignment ambiguity, but R wasn't developed so we could do things by hand. So first we define our cutoff for the posterior probability. In this case, we'll say any sample with a posterior probability less than 0.95 has an ambiguous assignment.
```{r}
# Set our cutoff value for what we call unambiguous
minimum.prob <- 0.95
```
We can then create a matrix of assignments based on this cutoff by comparing the values in `lda.fit$posterior` to our cutoff value, `minimum.prob`:
```{r}
# A new matrix of logicals, with TRUE for all cells with posterior prob > minimum.prob
unambiguous <- lda.fit$posterior > minimum.prob
```
Taking a look at the first few rows of the `unambiguous` object, we see it is filled with `TRUE` and `FALSE` values:
```{r}
head(unambiguous)
```
A row where the assignment probabilities for every group was below the threshold should only have values of FALSE. Take our example from row 88 as described above. The posterior probabilities were:
```{r}
round(lda.fit$posterior[88, ], 3)
```
And the corresponding values in our `unambiguous` object are:
```{r}
unambiguous[88, ]
```
Because we are only interested in those samples where assignment was ambiguous, we need to identify those rows where all the values are `FALSE`:
```{r}
# New vector indicating whether sample had any assignment greater than minimum.prob
unambiguous.rows <- apply(X = unambiguous, MARGIN = 1, FUN = "any")
# The converse (i.e. vector indicating samples with *no* assignment greater than minimum.prob)
ambiguous.rows <- !unambiguous.rows
# Use this to retrieve sample information for those rows and probabilities
ambiguous.results <- otter[ambiguous.rows, c(1:3)]
ambiguous.results <- cbind(ambiguous.results, lda.fit$posterior[ambiguous.rows, ])
```
This new data frame, `ambiguous.results` contains the posterior probabilities for all those samples with no assignment probabilities higher than the `minimum.prob` threshold. It also has the metadata associated with those samples, which we extracted from the original `otter` data frame. Looking at this data frame, we see there were `r nrow(ambiguous.results)` samples could not be unambiguously assigned to a group (assuming a posterior probability threshold of `r minimum.prob`):
```{r}
# Show only those samples which didn't have high assignment probability to any group
ambiguous.results
```
### Plotting DFA assignment probabilities (ADVANCED)
One way to visualize how well the analysis performed at distinguishing among the groups is to plot the posterior probabilities for each sample. These can be done with a stacked bar graph (much like the graphs used to visualize [STRUCTURE](http://pritchardlab.stanford.edu/structure.html) results). The first thing we need is a two-dimensional matrix of the posterior probabilities, with each sample corresponding to a _column_. The `lda.fit$posteriors` object has samples in rows, so we can use transpose, `t`, to exchange the rows and columns:
```{r}
posteriors <- t(lda.fit$posterior)
```
And we can plot these posteriors with `barplot`:
```{r}
barplot(posteriors,
ylab = "Posterior Probability")
```
Hmmm...that's a lot of gray. Let's start by eliminating space between bars and getting rid of the borders:
```{r}
barplot(posteriors,
ylab = "Posterior Probability",
space = 0,
border = NA)
```
And now we can add some color, making an adjustment to the x-axis (via `xlim`) so the legend shows up _outside_ the plot area:
```{r}
legend.cols <- c("black", "green4", "cyan3", "red3")
barplot(posteriors,
ylab = "Posterior Probability",
space = 0,
border = NA,
xlim = c(0, ncol(posteriors) + 45),
col = legend.cols,
legend.text = levels(otter$species))
```
The legend still overlaps the plot, so we'll need to pass some additional parameters to the legend maker.
```{r}
legend.cols <- c("black", "green4", "cyan3", "red3")
barplot(posteriors,
ylab = "Posterior Probability",
space = 0,
border = NA,
xlim = c(0, ncol(posteriors) + 45),
col = legend.cols,
legend.text = levels(otter$species),
args.legend = list(cex = 0.7, x = ncol(posteriors) + 55, y = 0.6, xpd = TRUE))
```
The last thing we want is to show the _a priori_ assignments of the samples. This requires some creativity on our part. The code below assumes that the original data being fed into the `lda` function were already sorted by species. Two new vectors, `tick.pos` and `label.pos`, are used to add the _a priori_ identification information to the x-axis.
+ `tick.pos` will draw five vertical tick marks on the x-axis, to serve as delimiters between species.
+ `label.pos` vector determines where to put the labels on the x-axis.
Because this isn't a standard way of labeling an axis, we tell R to skip labeling the x-axis in the `barplot` call (`xaxt = 'n'`), and instead, label the axis after the plot is drawn, with a pair of calls to the `axis` function. I'll leave it as an exercies for you to determin how `tick.pos` and `label.pos` are calculated (hint: documentation for `match` and `rev` will be helpful). Finally, we will need to add some more vertical space in our plot to prevent the species names on the x-axis from being cut off; we use the `par` function to temporarily change the margins of our plot, adding some space to the bottom margin.
```{r}
tick.pos <- c(0,
nrow(otter) - match(levels(otter$species)[1], rev(otter$species)) + 1, # last occurrence of first species
nrow(otter) - match(levels(otter$species)[2], rev(otter$species)) + 1, # last occurrence of second species
nrow(otter) - match(levels(otter$species)[3], rev(otter$species)) + 1, # last occurrence of third species
nrow(otter) - match(levels(otter$species)[4], rev(otter$species)) + 1) # last occurrence of fourth species
label.pos <- c(floor((tick.pos[2] - tick.pos[1]) / 2),
floor((tick.pos[3] - tick.pos[2]) / 2 + tick.pos[2]),
floor((tick.pos[4] - tick.pos[3]) / 2 + tick.pos[3]),
floor((tick.pos[5] - tick.pos[4]) / 2 + tick.pos[4]))
legend.cols <- c("black", "green4", "cyan3", "red3")
# Store graphics defaults
mar.default <- c(5, 4, 4, 2) + 1 # from par documentation
par(mar = mar.default + c(2, 0, 0, 0)) # add space to bottom margin
barplot(posteriors,
ylab = "Posterior Probability",
space = 0,
border = NA,
xaxt = 'n',
xlim = c(0, ncol(posteriors) + 45),
col = legend.cols,
legend.text = levels(otter$species),
args.legend = list(cex = 0.7, x = ncol(posteriors) + 55, y = 0.6, xpd = TRUE))
axis(side = 1, at = tick.pos, labels = FALSE) # tick marks
axis(side = 1, at = label.pos, labels = levels(otter$species), tick = FALSE, par(las = 2, cex = 0.8))
# Restore graphics defaults
par(mar = mar.default)
```
### Final script for DFA posteriors
The final script for running the DFA and creating the barplot of posterior probabilities would then be:
```{r, eval = FALSE}
# DFA on otter jaw measurements
# Jeff Oliver
# 2016-11-18
# Read in data
otter <- read.csv(file = "data/otter-mandible-data.csv", header = TRUE)
# Drop rows with NA
otter <- na.omit(otter)
# Renumber rows
rownames(otter) <- NULL
# Load MASS library
library("MASS")
# Run DFA
lda.fit <- lda(formula = species ~ m1 + m2 + m3 + m4 + m5 + m6,
data = otter,
prior = c(1,1,1,1)/4,
CV = TRUE)
# Setup vectors for labeling x-axis with a priori assignments
tick.pos <- c(0,
nrow(otter) - match(levels(otter$species)[1], rev(otter$species)) + 1, # last occurrence of first species
nrow(otter) - match(levels(otter$species)[2], rev(otter$species)) + 1, # last occurrence of second species
nrow(otter) - match(levels(otter$species)[3], rev(otter$species)) + 1, # last occurrence of third species
nrow(otter) - match(levels(otter$species)[4], rev(otter$species)) + 1) # last occurrence of fourth species
label.pos <- c(floor((tick.pos[2] - tick.pos[1]) / 2),
floor((tick.pos[3] - tick.pos[2]) / 2 + tick.pos[2]),
floor((tick.pos[4] - tick.pos[3]) / 2 + tick.pos[3]),
floor((tick.pos[5] - tick.pos[4]) / 2 + tick.pos[4]))
legend.cols <- c("black", "green4", "cyan3", "red3")
# Store graphics defaults
mar.default <- c(5, 4, 4, 2) + 1 # from par documentation
par(mar = mar.default + c(2, 0, 0, 0)) # add space to bottom margin
barplot(posteriors,
ylab = "Posterior Probability",
space = 0,
border = NA,
xaxt = 'n',
xlim = c(0, ncol(posteriors) + 45),
col = legend.cols,
legend.text = levels(otter$species),
args.legend = list(cex = 0.7, x = ncol(posteriors) + 55, y = 0.6, xpd = TRUE))
# Add x-axis labels
axis(side = 1, at = tick.pos, labels = FALSE) # tick marks
axis(side = 1, at = label.pos, labels = levels(otter$species), tick = FALSE, par(las = 2, cex = 0.8))
# Restore graphics defaults
par(mar = mar.default)
```
***
## Additional resources
### General multivariate analyses:
+ Manly, B.F.J. 2004. _Multivariate Statistical Methods: a primer_. Chapman & Hall, Boca Raton.
### PCA
+ [Quick-R page on PCA](http://www.statmethods.net/advstats/factor.html)
+ [Visualizing PCA results from r-bloggers](https://www.r-bloggers.com/computing-and-visualizing-pca-in-r/)
+ [Principal Components Regression from r-bloggers](https://www.r-bloggers.com/performing-principal-components-regression-pcr-in-r/)
+ [Why PCA? (or "how to explain PCA to your grandmother")](http://stats.stackexchange.com/questions/2691/making-sense-of-principal-component-analysis-eigenvectors-eigenvalues)
### DFA
+ [Quick-R page on DFA](http://www.statmethods.net/advstats/discriminant.html)
+ [R-bloggers DFA with visualization](https://www.r-bloggers.com/computing-and-visualizing-lda-in-r/)
***
### Installing R packages
Note that if a package is not installed on your machine, a call to `library` will throw an error, indicating the package has to be installed first. For example, if we were to load a package called `mustelid` without installing it first:
```{r error = TRUE}
library("mustelid")
```
So how do I install packages? you ask. With `install.packages`:
```{r eval = FALSE}
install.packages("mustelid")
library("mustelid")
```
<a href="index.html">Back to learn-r main page</a>
Questions? e-mail me at <a href="mailto:[email protected]">[email protected]</a>.