Data Preparation

> file <- "C:/Cauldron/garage/R/soulcraft/Volatility/Learn/Wolfgang_Multivariate/Data/food.dat"
> x <- as.data.frame(read.table(file))
> y <- as.matrix(x[, -1])
> colnames(y) <- c("bread", "vegetables", "fruits", "meat", "poultry",
+     "milk", "wine")
> rownames(y) <- paste(c("MA", "EM", "CA"), c(rep("2", 3), rep("3",
+     3), rep("4", 3), rep("5", 3)), sep = "")
> print(y)
    bread vegetables fruits meat poultry milk wine
MA2   332        428    354 1437     526  247  427
EM2   293        559    388 1527     567  239  258
CA2   372        767    562 1948     927  235  433
MA3   406        563    341 1507     544  324  407
EM3   386        608    396 1501     558  319  363
CA3   438        843    689 2345    1148  243  341
MA4   534        660    367 1620     638  414  407
EM4   460        699    484 1856     762  400  416
CA4   385        789    621 2366    1149  304  282
MA5   655        776    423 1848     759  495  486
EM5   584        995    548 2056     893  518  319
CA5   515       1097    887 2630    1167  561  284
> n <- dim(y)[1]
> p <- dim(y)[2]
> z <- y - matrix(rep(apply(y, 2, mean), n), byrow = T, nrow = n)
> for (i in 1:p) {
+     z[, i] <- z[, i]/apply(y, 2, sd)[i]
+ }
> print(z)
          bread vegetables     fruits        meat    poultry       milk
MA2 -1.07017495 -1.6069348 -0.9146401 -1.13644860 -1.1106185 -0.9498231
EM2 -1.43415887 -0.9144728 -0.7086946 -0.90903254 -0.9463298 -1.0181250
CA2 -0.69685811  0.1850089  0.3452615  0.15476927  0.4962054 -1.0522760
MA3 -0.37953879 -0.8933289 -0.9933839 -0.95956944 -1.0384917 -0.2924175
EM3 -0.56619721 -0.6554602 -0.6602369 -0.97473051 -0.9823931 -0.3351061
CA3 -0.08088532  0.5867426  1.1145283  1.15792679  1.3817617 -0.9839741
MA4  0.81507511 -0.3805898 -0.8358962 -0.67403594 -0.6618298  0.4759788
EM4  0.12443895 -0.1744370 -0.1272016 -0.07770049 -0.1649566  0.3564505
CA4 -0.57553013  0.3013003  0.7026374  1.21099054  1.3857687 -0.4631722
MA5  1.94435855  0.2325827 -0.4966920 -0.09791525 -0.1769777  1.1675354
EM5  1.28172116  1.3902100  0.2604604  0.42766854  0.3599659  1.3639033
CA5  0.63774961  1.9293789  2.3138577  1.87807765  1.4578955  1.7310260
          wine
MA2  0.8138087
EM2 -1.5405481
CA2  0.8973954
MA3  0.5351866
EM3 -0.0777820
CA3 -0.3842663
MA4  0.5351866
EM4  0.6605666
CA4 -1.2062015
MA5  1.6357439
EM5 -0.6907506
CA5 -1.1783393
> cov.mat <- cov(z)
> Xt <- z %*% eigen(cov.mat)$vectors
> lambdas <- eigen(cov.mat)$values
> par(mfrow = c(1, 2))
> plot(Xt[, 1], Xt[, 2], pch = 19, main = "p dimensions")
> text(Xt[, 1], Xt[, 2], col = "blue", labels = rownames(y), adj = c(1,
+     1), cex = 0.7)
> abline(v = 0, col = "grey")
> abline(h = 0, col = "grey")
> Vt <- Xt
> for (i in 1:p) {
+     Vt[, i] <- Xt[, i]/sqrt(lambdas[i])
+ }
> Wt <- t(z) %*% Vt
> plot(Wt[, 1], Wt[, 2], pch = 19, main = "n dimensions")
> text(Wt[, 1], Wt[, 2], col = "blue", labels = colnames(y), adj = c(0,
+     0), cex = 0.7)
> abline(v = 0, col = "grey")
> abline(h = 0, col = "grey")

C8_1-002.jpg