Escriba aquí los objetivos de la clase.
vinos <- read.table(header = TRUE, text = "
Cultivar Alcohol AcMalico FenTot Flavon IntColor Prolina
1 14.23 1.71 2.8 3.06 5.64 1065
1 13.2 1.78 2.65 2.76 4.38 1050
1 13.16 2.36 2.8 3.24 5.68 1185
1 14.37 1.95 3.85 3.49 7.8 1480
1 13.24 2.59 2.8 2.69 4.32 735
1 14.2 1.76 3.27 3.39 6.75 1450
1 14.39 1.87 2.5 2.52 5.25 1290
1 14.06 2.15 2.6 2.51 5.05 1295
1 14.83 1.64 2.8 2.98 5.2 1045
1 13.86 1.35 2.98 3.15 7.22 1045
1 14.1 2.16 2.95 3.32 5.75 1510
1 14.12 1.48 2.2 2.43 5 1280
1 13.75 1.73 2.6 2.76 5.6 1320
1 14.75 1.73 3.1 3.69 5.4 1150
1 14.38 1.87 3.3 3.64 7.5 1547
1 13.63 1.81 2.85 2.91 7.3 1310
1 14.3 1.92 2.8 3.14 6.2 1280
1 13.83 1.57 2.95 3.4 6.6 1130
1 14.19 1.59 3.3 3.93 8.7 1680
1 13.64 3.1 2.7 3.03 5.1 845
1 14.06 1.63 3 3.17 5.65 780
1 12.93 3.8 2.41 2.41 4.5 770
1 13.71 1.86 2.61 2.88 3.8 1035
1 12.85 1.6 2.48 2.37 3.93 1015
1 13.5 1.81 2.53 2.61 3.52 845
1 13.05 2.05 2.63 2.68 3.58 830
1 13.39 1.77 2.85 2.94 4.8 1195
1 13.3 1.72 2.4 2.19 3.95 1285
1 13.87 1.9 2.95 2.97 4.5 915
1 14.02 1.68 2.65 2.33 4.7 1035
1 13.73 1.5 3 3.25 5.7 1285
1 13.58 1.66 2.86 3.19 6.9 1515
1 13.68 1.83 2.42 2.69 3.84 990
1 13.76 1.53 2.95 2.74 5.4 1235
1 13.51 1.8 2.35 2.53 4.2 1095
1 13.48 1.81 2.7 2.98 5.1 920
1 13.28 1.64 2.6 2.68 4.6 880
1 13.05 1.65 2.45 2.43 4.25 1105
1 13.07 1.5 2.4 2.64 3.7 1020
1 14.22 3.99 3 3.04 5.1 760
1 13.56 1.71 3.15 3.29 6.13 795
1 13.41 3.84 2.45 2.68 4.28 1035
1 13.88 1.89 3.25 3.56 5.43 1095
1 13.24 3.98 2.64 2.63 4.36 680
1 13.05 1.77 3 3 5.04 885
1 14.21 4.04 2.85 2.65 5.24 1080
1 14.38 3.59 3.25 3.17 4.9 1065
1 13.9 1.68 3.1 3.39 6.1 985
1 14.1 2.02 2.75 2.92 6.2 1060
1 13.94 1.73 2.88 3.54 8.9 1260
1 13.05 1.73 2.72 3.27 7.2 1150
1 13.83 1.65 2.45 2.99 5.6 1265
1 13.82 1.75 3.88 3.74 7.05 1190
1 13.77 1.9 3 2.79 6.3 1375
1 13.74 1.67 2.6 2.9 5.85 1060
1 13.56 1.73 2.96 2.78 6.25 1120
1 14.22 1.7 3.2 3 6.38 970
1 13.29 1.97 3 3.23 6 1270
1 13.72 1.43 3.4 3.67 6.8 1285
2 12.37 0.94 1.98 0.57 1.95 520
2 12.33 1.1 2.05 1.09 3.27 680
2 12.64 1.36 2.02 1.41 5.75 450
2 13.67 1.25 2.1 1.79 3.8 630
2 12.37 1.13 3.5 3.1 4.45 420
2 12.17 1.45 1.89 1.75 2.95 355
2 12.37 1.21 2.42 2.65 4.6 678
2 13.11 1.01 2.98 3.18 5.3 502
2 12.37 1.17 2.11 2 4.68 510
2 13.34 0.94 2.53 1.3 3.17 750
2 12.21 1.19 1.85 1.28 2.85 718
2 12.29 1.61 1.1 1.02 3.05 870
2 13.86 1.51 2.95 2.86 3.38 410
2 13.49 1.66 1.88 1.84 3.74 472
2 12.99 1.67 3.3 2.89 3.35 985
2 11.96 1.09 3.38 2.14 3.21 886
2 11.66 1.88 1.61 1.57 3.8 428
2 13.03 0.9 1.95 2.03 4.6 392
2 11.84 2.89 1.72 1.32 2.65 500
2 12.33 0.99 1.9 1.85 3.4 750
2 12.7 3.87 2.83 2.55 2.57 463
2 12 0.92 2.42 2.26 2.5 278
2 12.72 1.81 2.2 2.53 3.9 714
2 12.08 1.13 2 1.58 2.2 630
2 13.05 3.86 1.65 1.59 4.8 515
2 11.84 0.89 2.2 2.21 3.05 520
2 12.67 0.98 2.2 1.94 2.62 450
2 12.16 1.61 1.78 1.69 2.45 495
2 11.65 1.67 1.92 1.61 2.6 562
2 11.64 2.06 1.95 1.69 2.8 680
2 12.08 1.33 2.2 1.59 1.74 625
2 12.08 1.83 1.6 1.5 2.4 480
2 12 1.51 1.45 1.25 3.6 450
2 12.69 1.53 1.38 1.46 3.05 495
2 12.29 2.83 2.45 2.25 2.15 290
2 11.62 1.99 3.02 2.26 3.25 345
2 12.47 1.52 2.5 2.27 2.6 937
2 11.81 2.12 1.6 0.99 2.5 625
2 12.29 1.41 2.55 2.5 2.9 428
2 12.37 1.07 3.52 3.75 4.5 660
2 12.29 3.17 2.85 2.99 2.3 406
2 12.08 2.08 2.23 2.17 3.3 710
2 12.6 1.34 1.45 1.36 2.45 562
2 12.34 2.45 2.56 2.11 2.8 438
2 11.82 1.72 2.5 1.64 2.06 415
2 12.51 1.73 2.2 1.92 2.94 672
2 12.42 2.55 1.68 1.84 2.7 315
2 12.25 1.73 1.65 2.03 3.4 510
2 12.72 1.75 1.38 1.76 3.3 488
2 12.22 1.29 2.36 2.04 2.7 312
2 11.61 1.35 2.74 2.92 2.65 680
2 11.46 3.74 3.18 2.58 2.9 562
2 12.52 2.43 2.55 2.27 2 325
2 11.76 2.68 1.75 2.03 3.8 607
2 11.41 0.74 2.48 2.01 3.08 434
2 12.08 1.39 2.56 2.29 2.9 385
2 11.03 1.51 2.46 2.17 1.9 407
2 11.82 1.47 1.98 1.6 1.95 495
2 12.42 1.61 2 2.09 2.06 345
2 12.77 3.43 1.63 1.25 3.4 372
2 12 3.43 2 1.64 1.28 564
2 11.45 2.4 2.9 2.79 3.25 625
2 11.56 2.05 3.18 5.08 6 465
2 12.42 4.43 2.2 2.13 2.08 365
2 13.05 5.8 2.62 2.65 2.6 380
2 11.87 4.31 2.86 3.03 2.8 380
2 12.07 2.16 2.6 2.65 2.76 378
2 12.43 1.53 2.74 3.15 3.94 352
2 11.79 2.13 2.13 2.24 3 466
2 12.37 1.63 2.22 2.45 2.12 342
2 12.04 4.3 2.1 1.75 2.6 580
3 12.86 1.35 1.51 1.25 4.1 630
3 12.88 2.99 1.3 1.22 5.4 530
3 12.81 2.31 1.15 1.09 5.7 560
3 12.7 3.55 1.7 1.2 5 600
3 12.51 1.24 2 0.58 5.45 650
3 12.6 2.46 1.62 0.66 7.1 695
3 12.25 4.72 1.38 0.47 3.85 720
3 12.53 5.51 1.79 0.6 5 515
3 13.49 3.59 1.62 0.48 5.7 580
3 12.84 2.96 2.32 0.6 4.92 590
3 12.93 2.81 1.54 0.5 4.6 600
3 13.36 2.56 1.4 0.5 5.6 780
3 13.52 3.17 1.55 0.52 4.35 520
3 13.62 4.95 2 0.8 4.4 550
3 12.25 3.88 1.38 0.78 8.21 855
3 13.16 3.57 1.5 0.55 4 830
3 13.88 5.04 0.98 0.34 4.9 415
3 12.87 4.61 1.7 0.65 7.65 625
3 13.32 3.24 1.93 0.76 8.42 650
3 13.08 3.9 1.41 1.39 9.4 550
3 13.5 3.12 1.4 1.57 8.6 500
3 12.79 2.67 1.48 1.36 10.8 480
3 13.11 1.9 2.2 1.28 7.1 425
3 13.23 3.3 1.8 0.83 10.52 675
3 12.58 1.29 1.48 0.58 7.6 640
3 13.17 5.19 1.74 0.63 7.9 725
3 13.84 4.12 1.8 0.83 9.01 480
3 12.45 3.03 1.9 0.58 7.5 880
3 14.34 1.68 2.8 1.31 13 660
3 13.48 1.67 2.6 1.1 11.75 620
3 12.36 3.83 2.3 0.92 7.65 520
3 13.69 3.26 1.83 0.56 5.88 680
3 12.85 3.27 1.65 0.6 5.58 570
3 12.96 3.45 1.39 0.7 5.28 675
3 13.78 2.76 1.35 0.68 9.58 615
3 13.73 4.36 1.28 0.47 6.62 520
3 13.45 3.7 1.7 0.92 10.68 695
3 12.82 3.37 1.48 0.66 10.26 685
3 13.58 2.58 1.55 0.84 8.66 750
3 13.4 4.6 1.98 0.96 8.5 630
3 12.2 3.03 1.25 0.49 5.5 510
3 12.77 2.39 1.39 0.51 9.899999 470
3 14.16 2.51 1.68 0.7 9.7 660
3 13.71 5.65 1.68 0.61 7.7 740
3 13.4 3.91 1.8 0.75 7.3 750
3 13.27 4.28 1.59 0.69 10.2 835
3 13.17 2.59 1.65 0.68 9.3 840
3 14.13 4.1 2.05 0.76 9.2 560
")
vinos$Cultivar <- factor(vinos$Cultivar, levels = 1:3,
labels = c("Barolo", "Grignolino", "Barbera"))
X <- vinos[, -1] # solo las variables químicas
str(vinos)
## 'data.frame': 178 obs. of 7 variables:
## $ Cultivar: Factor w/ 3 levels "Barolo","Grignolino",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ Alcohol : num 14.2 13.2 13.2 14.4 13.2 ...
## $ AcMalico: num 1.71 1.78 2.36 1.95 2.59 1.76 1.87 2.15 1.64 1.35 ...
## $ FenTot : num 2.8 2.65 2.8 3.85 2.8 3.27 2.5 2.6 2.8 2.98 ...
## $ Flavon : num 3.06 2.76 3.24 3.49 2.69 3.39 2.52 2.51 2.98 3.15 ...
## $ IntColor: num 5.64 4.38 5.68 7.8 4.32 6.75 5.25 5.05 5.2 7.22 ...
## $ Prolina : int 1065 1050 1185 1480 735 1450 1290 1295 1045 1045 ...
summary(X)
## Alcohol AcMalico FenTot Flavon
## Min. :11.03 Min. :0.740 Min. :0.980 Min. :0.340
## 1st Qu.:12.36 1st Qu.:1.603 1st Qu.:1.742 1st Qu.:1.205
## Median :13.05 Median :1.865 Median :2.355 Median :2.135
## Mean :13.00 Mean :2.336 Mean :2.295 Mean :2.029
## 3rd Qu.:13.68 3rd Qu.:3.083 3rd Qu.:2.800 3rd Qu.:2.875
## Max. :14.83 Max. :5.800 Max. :3.880 Max. :5.080
## IntColor Prolina
## Min. : 1.280 Min. : 278.0
## 1st Qu.: 3.220 1st Qu.: 500.5
## Median : 4.690 Median : 673.5
## Mean : 5.058 Mean : 746.9
## 3rd Qu.: 6.200 3rd Qu.: 985.0
## Max. :13.000 Max. :1680.0
apply(X, 2, sd)
## Alcohol AcMalico FenTot Flavon IntColor Prolina
## 0.8118265 1.1171461 0.6258510 0.9988587 2.3182859 314.9074743
round(cor(X),2)
## Alcohol AcMalico FenTot Flavon IntColor Prolina
## Alcohol 1.00 0.09 0.29 0.24 0.55 0.64
## AcMalico 0.09 1.00 -0.34 -0.41 0.25 -0.19
## FenTot 0.29 -0.34 1.00 0.86 -0.06 0.50
## Flavon 0.24 -0.41 0.86 1.00 -0.17 0.49
## IntColor 0.55 0.25 -0.06 -0.17 1.00 0.32
## Prolina 0.64 -0.19 0.50 0.49 0.32 1.00
par(mfrow = c(1, 2))
boxplot(X, las = 2, main = "Datos originales", col = "grey85")
boxplot(scale(X), las = 2, main = "Datos estandarizados", col = "lightblue")
par(mfrow = c(1, 1))
pca <- prcomp(X, center = TRUE, scale. = TRUE)
pca$rotation[,1]=-pca$rotation[,1]
pca$x[, 1] <- -pca$x[, 1]
summary(pca)
## Importance of components:
## PC1 PC2 PC3 PC4 PC5 PC6
## Standard deviation 1.6384 1.3466 0.8266 0.64624 0.52713 0.35128
## Proportion of Variance 0.4474 0.3022 0.1139 0.06961 0.04631 0.02057
## Cumulative Proportion 0.4474 0.7496 0.8635 0.93312 0.97943 1.00000
varianza <- pca$sdev^2
varianza
## [1] 2.6843994 1.8133893 0.6833120 0.4176318 0.2778675 0.1234000
porc <- 100 * varianza / sum(varianza)
porc
## [1] 44.739991 30.223154 11.388534 6.960530 4.631125 2.056666
round(data.frame(ValorPropio = varianza, Porcentaje = porc,
Acumulado = cumsum(porc)), 2)
## ValorPropio Porcentaje Acumulado
## 1 2.68 44.74 44.74
## 2 1.81 30.22 74.96
## 3 0.68 11.39 86.35
## 4 0.42 6.96 93.31
## 5 0.28 4.63 97.94
## 6 0.12 2.06 100.00
barplot(porc, names.arg = paste0("PC", 1:6), col = "steelblue",
ylim = c(0, 50), ylab = "% de varianza explicada",
main = "Gráfico de sedimentación")
lines(seq(0.7, by = 1.2, length.out = 6), porc, type = "b", pch = 19)
cargas <- pca$rotation[, 1:2]
round(cargas, 3)
## PC1 PC2
## Alcohol 0.373 -0.494
## AcMalico -0.250 -0.428
## FenTot 0.522 0.210
## Flavon 0.516 0.289
## IntColor 0.107 -0.627
## Prolina 0.497 -0.229
par(mfrow=c(1,2))
barplot(cargas[, 1], las = 2, col = ifelse(cargas[, 1] > 0, "firebrick", "steelblue"),
main = "Cargas de PC1", ylim = c(-0.7, 0.7))
barplot(cargas[, 2], las = 2, col = ifelse(cargas[, 2] > 0, "firebrick", "steelblue"),
main = "Cargas de PC2", ylim = c(-0.7, 0.7))
colores <- c("darkred", "darkorange", "purple")
col_vino <- colores[vinos$Cultivar]
plot(pca$x[, 1], pca$x[, 2], col = col_vino, pch = 19,
xlab = sprintf("PC1 (%.1f %%)", porc[1]),
ylab = sprintf("PC2 (%.1f %%)", porc[2]),
main = "Vinos en el plano de las dos primeras componentes")
abline(v=0,h=0,col="#0000dd",lty=2)
El segundo ejemplo trata sobre química forense: la composición de 214 fragmentos de vidrio, medida como el contenido de 8 óxidos en % en masa (Na₂O, MgO, Al₂O₃, SiO₂, K₂O, CaO, BaO y Fe₂O₃). Los fragmentos se agrupan en 4 tipos: ventana, faro de vehículo, envase y vajilla.
Fuentes de los datos. Evett y Spiehler (1987), distribuidos en el repositorio UCI (Glass Identification, DOI 10.24432/C5WW2P) y en Venables y Ripley (2002). La documentación del conjunto fgl de R dice por error que “Mg” es manganeso; en realidad es MgO
vidrio <- read.table(header = TRUE, text = "
Tipo Na Mg Al Si K Ca Ba Fe
Ventana 13.64 4.49 1.1 71.78 0.06 8.75 0 0
Ventana 13.89 3.6 1.36 72.73 0.48 7.83 0 0
Ventana 13.53 3.55 1.54 72.99 0.39 7.78 0 0
Ventana 13.21 3.69 1.29 72.61 0.57 8.22 0 0
Ventana 13.27 3.62 1.24 73.08 0.55 8.07 0 0
Ventana 12.79 3.61 1.62 72.97 0.64 8.07 0 0.26
Ventana 13.3 3.6 1.14 73.09 0.58 8.17 0 0
Ventana 13.15 3.61 1.05 73.24 0.57 8.24 0 0
Ventana 14.04 3.58 1.37 72.08 0.56 8.3 0 0
Ventana 13 3.6 1.36 72.99 0.57 8.4 0 0.11
Ventana 12.72 3.46 1.56 73.2 0.67 8.09 0 0.24
Ventana 12.8 3.66 1.27 73.01 0.6 8.56 0 0
Ventana 12.88 3.43 1.4 73.28 0.69 8.05 0 0.24
Ventana 12.86 3.56 1.27 73.21 0.54 8.38 0 0.17
Ventana 12.61 3.59 1.31 73.29 0.58 8.5 0 0
Ventana 12.81 3.54 1.23 73.24 0.58 8.39 0 0
Ventana 12.68 3.67 1.16 73.11 0.61 8.7 0 0
Ventana 14.36 3.85 0.89 71.36 0.15 9.15 0 0
Ventana 13.9 3.73 1.18 72.12 0.06 8.89 0 0
Ventana 13.02 3.54 1.69 72.73 0.54 8.44 0 0.07
Ventana 12.82 3.55 1.49 72.75 0.54 8.52 0 0.19
Ventana 14.77 3.75 0.29 72.02 0.03 9 0 0
Ventana 12.78 3.62 1.29 72.79 0.59 8.7 0 0
Ventana 12.81 3.57 1.35 73.02 0.62 8.59 0 0
Ventana 13.38 3.5 1.15 72.85 0.5 8.43 0 0
Ventana 12.98 3.54 1.21 73 0.65 8.53 0 0
Ventana 13.21 3.48 1.41 72.64 0.59 8.43 0 0
Ventana 12.87 3.48 1.33 73.04 0.56 8.43 0 0
Ventana 12.56 3.52 1.43 73.15 0.57 8.54 0 0
Ventana 13.08 3.49 1.28 72.86 0.6 8.49 0 0
Ventana 12.65 3.56 1.3 73.08 0.61 8.69 0 0.14
Ventana 12.84 3.5 1.14 73.27 0.56 8.55 0 0
Ventana 12.85 3.48 1.23 72.97 0.61 8.56 0.09 0.22
Ventana 12.57 3.47 1.38 73.39 0.6 8.55 0 0.06
Ventana 12.69 3.54 1.34 72.95 0.57 8.75 0 0
Ventana 13.29 3.45 1.21 72.74 0.56 8.57 0 0
Ventana 13.89 3.53 1.32 71.81 0.51 8.78 0.11 0
Ventana 12.74 3.48 1.35 72.96 0.64 8.68 0 0
Ventana 14.21 3.82 0.47 71.77 0.11 9.57 0 0
Ventana 14.21 3.82 0.47 71.77 0.11 9.57 0 0
Ventana 12.79 3.5 1.12 73.03 0.64 8.77 0 0
Ventana 12.71 3.42 1.2 73.2 0.59 8.64 0 0
Ventana 13.21 3.39 1.33 72.76 0.59 8.59 0 0
Ventana 13.73 3.84 0.72 71.76 0.17 9.74 0 0
Ventana 12.73 3.43 1.19 72.95 0.62 8.76 0 0.3
Ventana 13.49 3.48 1.35 71.95 0.55 9 0 0
Ventana 13.19 3.37 1.18 72.72 0.57 8.83 0 0.16
Ventana 13.99 3.7 0.71 71.57 0.02 9.82 0 0.1
Ventana 13.21 3.77 0.79 71.99 0.13 10.02 0 0
Ventana 13.58 3.35 1.23 72.08 0.59 8.91 0 0
Ventana 13.72 3.72 0.51 71.75 0.09 10.06 0 0.16
Ventana 13.2 3.33 1.28 72.36 0.6 9.14 0 0.11
Ventana 13.43 2.87 1.19 72.84 0.55 9.03 0 0
Ventana 13.14 2.84 1.28 72.85 0.55 9.07 0 0
Ventana 13.21 2.81 1.29 72.98 0.51 9.02 0 0.09
Ventana 12.45 2.71 1.29 73.7 0.56 9.06 0 0.24
Ventana 12.99 3.47 1.12 72.98 0.62 8.35 0 0.31
Ventana 12.87 3.48 1.29 72.95 0.6 8.43 0 0
Ventana 13.48 3.74 1.17 72.99 0.59 8.03 0 0
Ventana 13.39 3.66 1.19 72.79 0.57 8.27 0 0.11
Ventana 13.6 3.62 1.11 72.64 0.14 8.76 0 0
Ventana 13.81 3.58 1.32 71.72 0.12 8.67 0.69 0
Ventana 13.51 3.86 0.88 71.79 0.23 9.54 0 0.11
Ventana 14.17 3.81 0.78 71.35 0 9.69 0 0
Ventana 13.48 3.74 0.9 72.01 0.18 9.61 0 0.07
Ventana 13.69 3.59 1.12 71.96 0.09 9.4 0 0
Ventana 13.05 3.65 0.87 72.22 0.19 9.85 0 0.17
Ventana 13.05 3.65 0.87 72.32 0.19 9.85 0 0.17
Ventana 13.12 3.58 0.9 72.2 0.23 9.82 0 0.16
Ventana 13.31 3.58 0.82 71.99 0.12 10.17 0 0.03
Ventana 14.86 3.67 1.74 71.87 0.16 7.36 0 0.12
Ventana 13.64 3.87 1.27 71.96 0.54 8.32 0 0.32
Ventana 13.09 3.59 1.52 73.1 0.67 7.83 0 0
Ventana 13.34 3.57 1.57 72.87 0.61 7.89 0 0
Ventana 13.02 3.56 1.54 73.11 0.72 7.9 0 0
Ventana 13.02 3.58 1.51 73.12 0.69 7.96 0 0
Ventana 13.44 3.61 1.54 72.39 0.66 8.03 0 0
Ventana 13 3.58 1.54 72.83 0.61 8.04 0 0
Ventana 13.92 3.52 1.25 72.88 0.37 7.94 0 0.14
Ventana 12.82 3.52 1.9 72.86 0.69 7.97 0 0
Ventana 12.86 3.52 2.12 72.66 0.69 7.97 0 0
Ventana 13.25 3.45 1.43 73.17 0.61 7.86 0 0
Ventana 13.41 3.55 1.25 72.81 0.68 8.1 0 0
Ventana 13.09 3.52 1.55 72.87 0.68 8.05 0 0.09
Ventana 14.25 3.09 2.08 72.28 1.1 7.08 0 0
Ventana 13.36 3.58 1.49 72.72 0.45 8.21 0 0
Ventana 13.24 3.49 1.47 73.25 0.38 8.03 0 0
Ventana 13.4 3.49 1.52 72.65 0.67 8.08 0 0.1
Ventana 13.01 3.5 1.48 72.89 0.6 8.12 0 0
Ventana 12.55 3.48 1.87 73.23 0.63 8.08 0 0.09
Ventana 12.93 3.74 1.11 72.28 0.64 8.96 0 0.22
Ventana 12.9 3.44 1.45 73.06 0.44 8.27 0 0
Ventana 13.12 3.41 1.58 73.26 0.07 8.39 0 0.19
Ventana 13.24 3.34 1.47 73.1 0.39 8.22 0 0
Ventana 12.71 3.33 1.49 73.28 0.67 8.24 0 0
Ventana 13.36 3.43 1.43 72.26 0.51 8.6 0 0
Ventana 13.02 3.62 1.06 72.34 0.64 9.13 0 0.15
Ventana 12.2 3.25 1.16 73.55 0.62 8.9 0 0.24
Ventana 12.67 2.88 1.71 73.21 0.73 8.54 0 0
Ventana 12.96 2.96 1.43 72.92 0.6 8.79 0.14 0
Ventana 12.75 2.85 1.44 73.27 0.57 8.79 0.11 0.22
Ventana 12.35 2.72 1.63 72.87 0.7 9.23 0 0
Ventana 12.62 2.76 0.83 73.81 0.35 9.42 0 0.2
Ventana 13.8 3.15 0.66 70.57 0.08 11.64 0 0
Ventana 13.83 2.9 1.17 71.15 0.08 10.79 0 0
Ventana 11.45 0 1.88 72.19 0.81 13.24 0 0.34
Ventana 10.73 0 2.1 69.81 0.58 13.3 3.15 0.28
Ventana 12.3 0 1 70.16 0.12 16.19 0 0.24
Ventana 14.43 0 1 72.67 0.1 11.52 0 0.08
Ventana 13.72 0 0.56 74.45 0 10.99 0 0
Ventana 11.23 0 0.77 73.21 0 14.68 0 0
Ventana 11.02 0 0.75 73.08 0 14.96 0 0
Ventana 12.64 0 0.67 72.02 0.06 14.4 0 0
Ventana 13.46 3.83 1.26 72.55 0.57 8.21 0 0.14
Ventana 13.1 3.97 1.19 72.44 0.6 8.43 0 0
Ventana 13.41 3.89 1.33 72.38 0.51 8.28 0 0
Ventana 13.24 3.9 1.41 72.33 0.55 8.31 0 0.1
Ventana 13.72 3.68 1.81 72.06 0.64 7.88 0 0
Ventana 13.3 3.64 1.53 72.53 0.65 8.03 0 0.29
Ventana 13.56 3.57 1.47 72.45 0.64 7.96 0 0
Ventana 13.25 3.76 1.32 72.4 0.58 8.42 0 0
Ventana 12.93 3.54 1.62 72.96 0.64 8.03 0 0.21
Ventana 13.23 3.54 1.48 72.84 0.56 8.1 0 0
Ventana 13.48 3.48 1.71 72.52 0.62 7.99 0 0
Ventana 13.2 3.68 1.15 72.75 0.54 8.52 0 0
Ventana 12.93 3.66 1.56 72.51 0.58 8.55 0 0.12
Ventana 12.94 3.61 1.26 72.75 0.56 8.6 0 0
Ventana 13.78 2.28 1.43 71.99 0.49 9.85 0 0.17
Ventana 13.55 2.09 1.67 72.18 0.53 9.57 0.27 0.17
Ventana 13.98 1.35 1.63 71.76 0.39 10.56 0 0.18
Ventana 13.75 1.01 1.36 72.19 0.33 11.14 0 0
Ventana 13.7 0 1.36 71.24 0.19 13.44 0 0.1
Ventana 13.43 3.98 1.18 72.49 0.58 8.15 0 0
Ventana 13.71 3.93 1.54 71.81 0.54 8.21 0 0.15
Ventana 13.33 3.85 1.25 72.78 0.52 8.12 0 0
Ventana 13.19 3.9 1.3 72.33 0.55 8.44 0 0.28
Ventana 13 3.8 1.08 73.07 0.56 8.38 0 0.12
Ventana 12.89 3.62 1.57 72.96 0.61 8.11 0 0
Ventana 12.79 3.52 1.54 73.36 0.66 7.9 0 0
Ventana 12.87 3.56 1.64 73.14 0.65 7.99 0 0
Ventana 13.33 3.54 1.61 72.54 0.68 8.11 0 0
Ventana 13.2 3.63 1.07 72.83 0.57 8.41 0.09 0.17
Ventana 12.85 3.51 1.44 73.01 0.68 8.23 0.06 0.25
Ventana 13 3.47 1.79 72.72 0.66 8.18 0 0
Ventana 12.99 3.18 1.23 72.97 0.58 8.81 0 0.24
Ventana 12.85 3.67 1.24 72.57 0.62 8.68 0 0.35
Ventana 13.65 3.66 1.11 72.77 0.11 8.6 0 0
Ventana 13.33 3.53 1.34 72.67 0.56 8.33 0 0
Ventana 13.24 3.57 1.38 72.7 0.56 8.44 0 0.1
Ventana 12.16 3.52 1.35 72.89 0.57 8.53 0 0
Ventana 13.14 3.45 1.76 72.48 0.6 8.38 0 0.17
Ventana 14.32 3.9 0.83 71.5 0 9.49 0 0
Ventana 13.64 3.65 0.65 73 0.06 8.93 0 0
Ventana 13.42 3.4 1.22 72.69 0.59 8.32 0 0
Ventana 12.86 3.58 1.31 72.61 0.61 8.79 0 0
Ventana 13.04 3.4 1.26 73.01 0.52 8.58 0 0
Ventana 13.41 3.39 1.28 72.64 0.52 8.65 0 0
Ventana 14.03 3.76 0.58 71.79 0.11 9.65 0 0
Ventana 13.53 3.41 1.52 72.04 0.58 8.79 0 0
Ventana 13.5 3.36 1.63 71.94 0.57 8.81 0 0.09
Ventana 13.33 3.34 1.54 72.14 0.56 8.99 0 0
Ventana 13.64 3.54 0.75 72.65 0.16 8.89 0.15 0.24
Ventana 14.19 3.78 0.91 71.36 0.23 9.14 0 0.37
Envase 14.01 2.68 3.5 69.89 1.68 5.87 2.2 0
Envase 12.73 1.85 1.86 72.69 0.6 10.09 0 0
Envase 11.56 1.88 1.56 72.86 0.47 11.41 0 0
Envase 11.03 1.71 1.56 73.44 0.58 11.62 0 0
Envase 12.64 0 1.65 73.75 0.38 11.53 0 0
Envase 12.86 0 1.83 73.88 0.97 10.17 0 0
Envase 13.27 0 1.76 73.03 0.47 11.32 0 0
Envase 13.44 0 1.58 72.22 0.32 12.24 0 0
Envase 13.02 0 3.04 70.48 6.21 6.96 0 0
Envase 13 0 3.02 70.7 6.21 6.93 0 0
Envase 13.38 0 1.4 72.25 0.33 12.5 0 0
Envase 12.85 1.61 2.17 72.18 0.76 9.7 0.24 0.51
Envase 12.97 0.33 1.51 73.39 0.13 11.27 0 0.28
Vajilla 14 2.39 1.56 72.37 0 9.57 0 0
Vajilla 13.79 2.41 1.19 72.76 0 9.77 0 0
Vajilla 14.46 2.24 1.62 72.38 0 9.26 0 0
Vajilla 14.09 2.19 1.66 72.67 0 9.32 0 0
Vajilla 14.4 1.74 1.54 74.55 0 7.59 0 0
Vajilla 14.99 0.78 1.74 72.5 0 9.95 0 0
Vajilla 14.15 0 2.09 72.74 0 10.88 0 0
Vajilla 14.56 0 0.56 73.48 0 11.22 0 0
Vajilla 17.38 0 0.34 75.41 0 6.65 0 0
Faro 13.69 3.2 1.81 72.81 1.76 5.43 1.19 0
Faro 14.32 3.26 2.22 71.25 1.46 5.79 1.63 0
Faro 13.44 3.34 1.23 72.38 0.6 8.83 0 0
Faro 14.86 2.2 2.06 70.26 0.76 9.76 0 0
Faro 15.79 1.83 1.31 70.43 0.31 8.61 1.68 0
Faro 13.88 1.78 1.79 73.1 0 8.67 0.76 0
Faro 14.85 0 2.38 73.28 0 8.76 0.64 0.09
Faro 14.2 0 2.79 73.46 0.04 9.04 0.4 0.09
Faro 14.75 0 2 73.02 0 8.53 1.59 0.08
Faro 14.56 0 1.98 73.29 0 8.52 1.57 0.07
Faro 14.14 0 2.68 73.39 0.08 9.07 0.61 0.05
Faro 13.87 0 2.54 73.23 0.14 9.41 0.81 0.01
Faro 14.7 0 2.34 73.28 0 8.95 0.66 0
Faro 14.38 0 2.66 73.1 0.04 9.08 0.64 0
Faro 15.01 0 2.51 73.05 0.05 8.83 0.53 0
Faro 15.15 0 2.25 73.5 0 8.34 0.63 0
Faro 11.95 0 1.19 75.18 2.7 8.93 0 0
Faro 14.85 0 2.42 73.72 0 8.39 0.56 0
Faro 14.8 0 1.99 73.11 0 8.28 1.71 0
Faro 14.95 0 2.27 73.3 0 8.71 0.67 0
Faro 14.95 0 1.8 72.99 0 8.61 1.55 0
Faro 14.94 0 1.87 73.11 0 8.67 1.38 0
Faro 14.39 0 1.82 72.86 1.41 6.47 2.88 0
Faro 14.37 0 2.74 72.85 0 9.45 0.54 0
Faro 14.14 0 2.88 72.61 0.08 9.18 1.06 0
Faro 14.92 0 1.99 73.06 0 8.4 1.59 0
Faro 14.36 0 2.02 73.42 0 8.44 1.64 0
Faro 14.38 0 1.94 73.61 0 8.48 1.57 0
Faro 14.23 0 2.08 73.36 0 8.62 1.67 0
")
vidrio$Tipo <- factor(vidrio$Tipo,
levels = c("Ventana", "Faro",
"Envase", "Vajilla"))
X <- vidrio[, -1] # Solo las variables químicas
str(vidrio)
## 'data.frame': 214 obs. of 9 variables:
## $ Tipo: Factor w/ 4 levels "Ventana","Faro",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ Na : num 13.6 13.9 13.5 13.2 13.3 ...
## $ Mg : num 4.49 3.6 3.55 3.69 3.62 3.61 3.6 3.61 3.58 3.6 ...
## $ Al : num 1.1 1.36 1.54 1.29 1.24 1.62 1.14 1.05 1.37 1.36 ...
## $ Si : num 71.8 72.7 73 72.6 73.1 ...
## $ K : num 0.06 0.48 0.39 0.57 0.55 0.64 0.58 0.57 0.56 0.57 ...
## $ Ca : num 8.75 7.83 7.78 8.22 8.07 8.07 8.17 8.24 8.3 8.4 ...
## $ Ba : num 0 0 0 0 0 0 0 0 0 0 ...
## $ Fe : num 0 0 0 0 0 0.26 0 0 0 0.11 ...
summary(X)
## Na Mg Al Si
## Min. :10.73 Min. :0.000 Min. :0.290 Min. :69.81
## 1st Qu.:12.91 1st Qu.:2.115 1st Qu.:1.190 1st Qu.:72.28
## Median :13.30 Median :3.480 Median :1.360 Median :72.79
## Mean :13.41 Mean :2.685 Mean :1.445 Mean :72.65
## 3rd Qu.:13.82 3rd Qu.:3.600 3rd Qu.:1.630 3rd Qu.:73.09
## Max. :17.38 Max. :4.490 Max. :3.500 Max. :75.41
## K Ca Ba Fe
## Min. :0.0000 Min. : 5.430 Min. :0.000 Min. :0.00000
## 1st Qu.:0.1225 1st Qu.: 8.240 1st Qu.:0.000 1st Qu.:0.00000
## Median :0.5550 Median : 8.600 Median :0.000 Median :0.00000
## Mean :0.4971 Mean : 8.957 Mean :0.175 Mean :0.05701
## 3rd Qu.:0.6100 3rd Qu.: 9.172 3rd Qu.:0.000 3rd Qu.:0.10000
## Max. :6.2100 Max. :16.190 Max. :3.150 Max. :0.51000
apply(X, 2, sd)
## Na Mg Al Si K Ca Ba Fe
## 0.8166036 1.4424078 0.4992696 0.7745458 0.6521918 1.4231535 0.4972193 0.0974387
round(cor(X), 2)
## Na Mg Al Si K Ca Ba Fe
## Na 1.00 -0.27 0.16 -0.07 -0.27 -0.28 0.33 -0.24
## Mg -0.27 1.00 -0.48 -0.17 0.01 -0.44 -0.49 0.08
## Al 0.16 -0.48 1.00 -0.01 0.33 -0.26 0.48 -0.07
## Si -0.07 -0.17 -0.01 1.00 -0.19 -0.21 -0.10 -0.09
## K -0.27 0.01 0.33 -0.19 1.00 -0.32 -0.04 -0.01
## Ca -0.28 -0.44 -0.26 -0.21 -0.32 1.00 -0.11 0.12
## Ba 0.33 -0.49 0.48 -0.10 -0.04 -0.11 1.00 -0.06
## Fe -0.24 0.08 -0.07 -0.09 -0.01 0.12 -0.06 1.00
par(mfrow = c(1, 2))
boxplot(X, las = 2,
main = "Datos originales",
col = "grey85")
boxplot(scale(X), las = 2,
main = "Datos estandarizados",
col = "lightblue")
par(mfrow = c(1, 1))
pca <- prcomp(X, center = TRUE, scale. = TRUE)
summary(pca)
## Importance of components:
## PC1 PC2 PC3 PC4 PC5 PC6 PC7
## Standard deviation 1.4785 1.2575 1.1688 1.0421 0.9484 0.71757 0.60389
## Proportion of Variance 0.2732 0.1976 0.1708 0.1357 0.1124 0.06436 0.04559
## Cumulative Proportion 0.2732 0.4709 0.6417 0.7774 0.8898 0.95420 0.99979
## PC8
## Standard deviation 0.04098
## Proportion of Variance 0.00021
## Cumulative Proportion 1.00000
varianza <- pca$sdev^2
varianza
## [1] 2.186007845 1.581236266 1.366097636 1.085928028 0.899459877 0.514901832
## [7] 0.364688751 0.001679765
porc <- 100 * varianza / sum(varianza)
porc
## [1] 27.32509806 19.76545333 17.07622045 13.57410035 11.24324846 6.43627290
## [7] 4.55860938 0.02099706
round(data.frame(
ValorPropio = varianza,
Porcentaje = porc,
Acumulado = cumsum(porc)
), 2)
## ValorPropio Porcentaje Acumulado
## 1 2.19 27.33 27.33
## 2 1.58 19.77 47.09
## 3 1.37 17.08 64.17
## 4 1.09 13.57 77.74
## 5 0.90 11.24 88.98
## 6 0.51 6.44 95.42
## 7 0.36 4.56 99.98
## 8 0.00 0.02 100.00
barplot(porc,
names.arg = paste0("PC", 1:8),
col = "steelblue",
ylim = c(0, max(porc) + 5),
ylab = "% de varianza explicada",
main = "Gráfico de sedimentación")
lines(seq(0.7, by = 1.2, length.out = 8),
porc, type = "b", pch = 19)
cargas <- pca$rotation[, 1:2]
round(cargas, 3)
## PC1 PC2
## Na -0.378 0.036
## Mg 0.497 -0.402
## Al -0.521 -0.230
## Si -0.022 0.015
## K -0.063 -0.539
## Ca 0.107 0.692
## Ba -0.536 0.063
## Fe 0.185 0.104
par(mfrow = c(1, 2))
barplot(cargas[, 1], las = 2,
col = ifelse(cargas[, 1] > 0,
"firebrick", "steelblue"),
main = "Cargas de PC1",
ylim = c(-0.7, 0.7))
barplot(cargas[, 2], las = 2,
col = ifelse(cargas[, 2] > 0,
"firebrick", "steelblue"),
main = "Cargas de PC2",
ylim = c(-0.7, 0.7))
par(mfrow = c(1, 1))
colores <- c("darkred", "darkorange",
"purple", "darkgreen")
col_vidrio <- colores[vidrio$Tipo]
plot(pca$x[, 1], pca$x[, 2],
col = col_vidrio,
pch = 19,
xlab = sprintf("PC1 (%.1f %%)", porc[1]),
ylab = sprintf("PC2 (%.1f %%)", porc[2]),
main = "Vidrios en el plano de las dos primeras componentes")
abline(v = 0, h = 0, col = "#0000dd", lty = 2)
legend("topright",
legend = levels(vidrio$Tipo),
col = colores,
pch = 19)