-
Notifications
You must be signed in to change notification settings - Fork 0
/
MFA in R Slides.Rmd
540 lines (302 loc) · 11.5 KB
/
MFA in R Slides.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
---
title: "Multiple Factor Analysis in R"
author: "Si Wei Wu, Renjie Wu, Daniel Lee"
date: "December 2, 2016"
output: ioslides_presentation
---
##mfaR Package
- Uses S4 object-oriented programming
- Main purpose: perform Multiple Factor Analysis
- Requires the user to load the following packages:
- MFAg
- ggplot2
- png
- gridGraphics
- gridExtra
##mfaR Package
The following is a list of functions and their purposes in the package.
##mfa_const()
- Main function in the package
- `mfa_const` takes the following arguments:
- `mfa_const(data, sets, ncomps = NULL, center = TRUE, scale = TRUE)`
##mfa_const()
The argumets for `mfa_const()` are:
- **data**: data set (matrix or data frame).
- **sets**: list of vectors indicating the sets of variables (i.e. the blocks). Sets must be a list. It can be a list of character vectors or numeric vectors.
- **ncomps**: integer indicating how many number of components (i.e. factors) are to be extracted. By default, `ncomps = NULL`, which means that all possible components will be extracted.
##mfa_const()
- **center**: either a logical value or a numeric vector of length equal to the number of active variables in the analysis. The value of `center` determines how column centering is performed. If `center` is a numeric vector with length equal to the number of active variables, then each variable has the corresponding value from `center` subtracted from it. If `center = TRUE`, then centering is done by subtracting the column means, and if `center = FALSE`, no centering is done.
##mfa_const()
- **scale**: either a logical value or a numeric vector of length equal to the number of active variables in the analysis. The value of `scale` determines how the scaling of active variables is performed (after centering). If `scale` is a numeric vector, then each active variable is divided by the corresponding value from `scale`. If `scale = TRUE`, then scaling is done by dividing the (centered) variables by their standard deviations if `center = TRUE`, and the root mean square otherwise. If `scale = FALSE`, no scaling is done.
##mfa_const()
`mfa_const()` returns an object of class `"mfa"` with the following elements:
- vector containing the eigenvalues, computed from the generalized singular value decomposition of $\boldsymbol{X} = \boldsymbol{P \Delta Q^T}$.
- matrix of common factor scores (cfs) (also known as compromise factor scores)
- list of matrices of partial factor scores (pfs)
- matrix of loadings (also known as factor loadings) (mol). This is the matrix $\boldsymbol{Q}$ in $\boldsymbol{X} = \boldsymbol{P \Delta Q^T}$, i.e. the right singular values.
- matrix of $\alpha$ weights
##mfa_const()
Example of `mfa_const()` function and its outputs using a fictitious wine tasting experiment
- 12 wines made from Sauvignon Blanc grapes coming from three wine regions (4 wines from each region): New Zealand, France, and Canada
- Ten expert assessors evaluate these wines.
##mfa_const()
The assessors were asked:
1. to evaluate the wines on 9-point rating scales, using four variables considered as standard for the evaluation of these wines: cat-pee, passion-fruit, green pepper, and mineral.
2. if they felt the need, they were able to add some variables of their own.
##mfa_const()
Perform the multiple factor analysis on this dataset
1. Load the wine data.
2. Perform multiple factor analysis on the data. Even though we can use all the possible number of components, for illustrative purposes, we will only use 3. That is, `ncomps = 3`.
```{r include = FALSE}
library(mfaR)
library(MFAg)
# Read data set
wines <- read.csv("wines.csv")
# Add row names
row.names(wines) <- wines[,1]
wines <- read.csv("wines.csv")
row.names(wines) <- wines[,1]
# Separate the grand table into individual tables (create the set list)
col_ind = grep("V2", colnames(wines))
sets = list()
for (i in 1:(length(col_ind))){
# First 9 tables
if (i < 10){
sets[[i]]= (col_ind[i]-1):(col_ind[i+1]-2)
}
# Last table
else{
sets[[i]]= (col_ind[i]-1):(col_ind[i]+2)
}
}
```
```{r}
# Construct an object of class 'mfa'
mfa_wine <- mfa_const(data = wines, sets = sets, ncomps = 3)
```
##print()
- `mfa_wine` is an object of class `mfa`
- The output of `mfa_wine` is too long
- Use the `print()` method to display the basic information:
+ eigenvalues
+ compromise factor scores (cfs)
##print() {.smaller}
```{r}
print(mfa_wine)
```
##plot_compromise()
- Takes two dimensions of compromise of tables and returns a plot of the two dimensions.
```{r include = FALSE}
library(ggplot2)
library(png)
library(gridGraphics)
```
##plot_compromise()
```{r fig.width = 8.5, fig.height = 4.25, include = FALSE}
NZ <- readPNG("nz.png")
FR <- readPNG("fr.png")
CA <- readPNG("ca.png")
cfs = mfa_wine@cfs
```
```{r}
plot_compromise(cfs[, 1], cfs[, 2], rownames_vec = row.names(wines), NZ, FR, CA)
```
##plot_pfs()
This function takes two dimensions of partial factor scores and returns a plot of the two dimensions.
```{r include = FALSE}
library(gridExtra)
```
```{r eval = FALSE}
library(gridExtra)
```
##plot_pfs()
Assessor 1
```{r, echo = FALSE}
plot_pfs(mfa_wine@pfs[[1]][, 1], mfa_wine@pfs[[1]][, 2], rownames(wines), NZ, FR, CA)
```
##plot_pfs()
Assessor 2
```{r, echo = FALSE}
plot_pfs(mfa_wine@pfs[[2]][, 1], mfa_wine@pfs[[2]][, 2], rownames(wines), NZ, FR, CA)
```
##plot_pfs()
Assessor 3
```{r, echo = FALSE}
plot_pfs(mfa_wine@pfs[[3]][, 1], mfa_wine@pfs[[3]][, 2], rownames(wines), NZ, FR, CA)
```
##plot_pfs()
Assessor 4
```{r, echo = FALSE}
plot_pfs(mfa_wine@pfs[[4]][, 1], mfa_wine@pfs[[4]][, 2], rownames(wines), NZ, FR, CA)
```
##plot_pfs()
Assessor 5
```{r, echo = FALSE}
plot_pfs(mfa_wine@pfs[[5]][, 1], mfa_wine@pfs[[5]][, 2], rownames(wines), NZ, FR, CA)
```
##plot_pfs()
Assessor 6
```{r, echo = FALSE}
plot_pfs(mfa_wine@pfs[[6]][, 1], mfa_wine@pfs[[6]][, 2], rownames(wines), NZ, FR, CA)
```
##plot_pfs()
Assessor 7
```{r, echo = FALSE}
plot_pfs(mfa_wine@pfs[[7]][, 1], mfa_wine@pfs[[7]][, 2], rownames(wines), NZ, FR, CA)
```
##plot_pfs()
Assessor 8
```{r, echo = FALSE}
plot_pfs(mfa_wine@pfs[[8]][, 1], mfa_wine@pfs[[8]][, 2], rownames(wines), NZ, FR, CA)
```
##plot_pfs()
Assessor 9
```{r, echo = FALSE}
plot_pfs(mfa_wine@pfs[[9]][, 1], mfa_wine@pfs[[9]][, 2], rownames(wines), NZ, FR, CA)
```
##plot_pfs()
Assessor 10
```{r, echo = FALSE}
plot_pfs(mfa_wine@pfs[[10]][, 1], mfa_wine@pfs[[10]][, 2], rownames(wines), NZ, FR, CA)
```
##plot_vload()
This function takes two dimensions of variable loadings and returns a plot of the two dimensions. An example using the wine data is below.
```{r, echo = FALSE}
col_ind = grep("V15", colnames(wines))
colnames(wines)[col_ind] <- "Peach"
col_ind = grep("V14", colnames(wines))
colnames(wines)[col_ind] <- "Grass"
col_ind = grep("V13", colnames(wines))
colnames(wines)[col_ind] <- "Melon"
col_ind = grep("V12", colnames(wines))
colnames(wines)[col_ind] <- "Hay"
col_ind = grep("V11", colnames(wines))
colnames(wines)[col_ind] <- "Vegetal"
col_ind = grep("V10", colnames(wines))
colnames(wines)[col_ind] <- "Flinty"
col_ind = grep("V9", colnames(wines))
colnames(wines)[col_ind] <- "Grassy"
col_ind = grep("V8", colnames(wines))
colnames(wines)[col_ind] <- "Leafy"
col_ind = grep("V7", colnames(wines))
colnames(wines)[col_ind] <- "Tropical"
col_ind = grep("V6", colnames(wines))
colnames(wines)[col_ind] <- "Citrus"
col_ind = grep("V5", colnames(wines))
colnames(wines)[col_ind] <- "Smoky"
col_ind = grep("V4", colnames(wines))
colnames(wines)[col_ind] <- "Mineral"
col_ind = grep("V3", colnames(wines))
colnames(wines)[col_ind] <- "Green Pepper"
col_ind = grep("V2", colnames(wines))
colnames(wines)[col_ind] <- "Passion Fruit"
col_ind = grep("V1", colnames(wines))
colnames(wines)[col_ind] <- "Cat Pee"
```
##plot_vload()
Assessor 1
```{r echo = FALSE}
plot_vload(mfa_wine@mol[[1]][,1], mfa_wine@mol[[1]][,2], colnames(wines)[sets[[1]]])
```
##plot_vload()
Assessor 2
```{r echo = FALSE}
plot_vload(mfa_wine@mol[[2]][,1], mfa_wine@mol[[2]][,2], colnames(wines)[sets[[2]]])
```
##plot_vload()
Assessor 3
```{r echo = FALSE}
plot_vload(mfa_wine@mol[[3]][,1], mfa_wine@mol[[3]][,2], colnames(wines)[sets[[3]]])
```
##plot_vload()
Assessor 4
```{r echo = FALSE}
plot_vload(mfa_wine@mol[[4]][,1], mfa_wine@mol[[4]][,2], colnames(wines)[sets[[4]]])
```
##plot_vload()
Assessor 5
```{r echo = FALSE}
plot_vload(mfa_wine@mol[[5]][,1], mfa_wine@mol[[5]][,2], colnames(wines)[sets[[5]]])
```
##plot_vload()
Assessor 6
```{r echo = FALSE}
plot_vload(mfa_wine@mol[[6]][,1], mfa_wine@mol[[6]][,2], colnames(wines)[sets[[6]]])
```
##plot_vload()
Assessor 7
```{r echo = FALSE}
plot_vload(mfa_wine@mol[[7]][,1], mfa_wine@mol[[7]][,2], colnames(wines)[sets[[7]]])
```
##plot_vload()
Assessor 8
```{r echo = FALSE}
plot_vload(mfa_wine@mol[[8]][,1], mfa_wine@mol[[8]][,2], colnames(wines)[sets[[8]]])
```
##plot_vload()
Assessor 9
```{r echo = FALSE}
plot_vload(mfa_wine@mol[[9]][,1], mfa_wine@mol[[9]][,2], colnames(wines)[sets[[9]]])
```
##plot_vload()
Assessor 10
```{r echo = FALSE}
plot_vload(mfa_wine@mol[[10]][,1], mfa_wine@mol[[10]][,2], colnames(wines)[sets[[10]]])
```
##eigen_table()
- Takes the `mfa` object and returns a table with singluar values, the eigenvalues, and cumulative, percentage of intertia, and cumulative percentage of inertia for all the extracted components.
```{r}
eigen_table(mfa_wine)
```
##COD() {.smaller}
This function takes the `mfa` object and returns a matrix of contributions of an observation to a dimension. The outputs values are helpful in interpreting how the observations contribute to a dimension.
```{r}
COD(mfa_wine)
```
##CVD() {.smaller}
- This function takes the `mfa` object and returns a list of output values that help interpret how the variables contribute to a dimension.
- Below are the first two elements of the list.
```{r, echo = FALSE}
CVD(mfa_wine)[[1]]
CVD(mfa_wine)[[2]]
```
##CTD() {.smaller}
- This function takes the `mfa` object and returns a list of output values that help interpret how the tables contribute to a dimension.
- The contribution of a table reflects the proportion of the variance of a dimension that can be attributed to this table.
```{r}
CTD(mfa_wine)
```
##RV()
- This function takes two tables with same number of rows and returns the $R_V$ coefficient.
- The $R_V$ coefficient is used to evaluate the similarity between two tables.
- Below is an example using simulated data.
```{r}
table1 <- matrix(rnorm(100), nrow = 10)
table2 <- matrix(rnorm(50), nrow = 10)
RV(table1, table2)
```
##RV_table()
- This function takes a dataset of class `matrix` or `data.frame`, and a list of sets of how to divide up the dataset into separate blocks.
- Then, it outputs a table of $R_V$ coefficients. RV coefficient table contains information of how similar the individual tables are to each other pairwise.
##RV_table()
```{r}
RV_table(wines, sets)
```
##Lg()
- This function calculates the $L_g$ coefficient between two tables.
- It takes in two tables of class `matrix` and with same rows, and returns the $L_g$ coefficient.
- Below is an example using simulated data.
```{r}
table1 <- matrix(rnorm(100), nrow = 10)
table2 <- matrix(rnorm(50), nrow = 10)
Lg(table1, table2)
```
##Lg_table()
- This function computes a table of $L_g$ coefficients from a dataset (either class `matrix` or class `data.frame`) along with `sets` of class `list`.
- It then returns a matrix containing $L_g$ coefficients.
##Lg_table() {.smaller}
```{r}
Lg_table(wines, sets)
```
## Thank You {.flexbox .vcenter}
![For a Great Semester!](Glass-versus-crystal.png)