Chapter 8 Principal Components Applied Multivariate Analysis
Clearing the global environment
rm(list = ls())
R libraries
install.packages("gganimate")
Installing package into ‘C:/Users/bitle/AppData/Local/R/win-library/4.4’
(as ‘lib’ is unspecified)
also installing the dependencies ‘wk’, ‘classInt’, ‘s2’, ‘units’, ‘sf’, ‘lpSolve’, ‘transformr’, ‘tweenr’
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.4/wk_0.9.4.zip'
Content type 'application/zip' length 2046645 bytes (2.0 MB)
downloaded 2.0 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.4/classInt_0.4-10.zip'
Content type 'application/zip' length 505442 bytes (493 KB)
downloaded 493 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.4/s2_1.1.7.zip'
Content type 'application/zip' length 4099178 bytes (3.9 MB)
downloaded 3.9 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.4/units_0.8-5.zip'
Content type 'application/zip' length 788158 bytes (769 KB)
downloaded 769 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.4/sf_1.0-19.zip'
Content type 'application/zip' length 40986239 bytes (39.1 MB)
downloaded 39.1 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.4/lpSolve_5.6.22.zip'
Content type 'application/zip' length 362000 bytes (353 KB)
downloaded 353 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.4/transformr_0.1.5.zip'
Content type 'application/zip' length 248373 bytes (242 KB)
downloaded 242 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.4/tweenr_2.0.3.zip'
Content type 'application/zip' length 544294 bytes (531 KB)
downloaded 531 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.4/gganimate_1.0.9.zip'
Content type 'application/zip' length 2090359 bytes (2.0 MB)
downloaded 2.0 MB
package ‘wk’ successfully unpacked and MD5 sums checked
package ‘classInt’ successfully unpacked and MD5 sums checked
package ‘s2’ successfully unpacked and MD5 sums checked
package ‘units’ successfully unpacked and MD5 sums checked
package ‘sf’ successfully unpacked and MD5 sums checked
package ‘lpSolve’ successfully unpacked and MD5 sums checked
package ‘transformr’ successfully unpacked and MD5 sums checked
package ‘tweenr’ successfully unpacked and MD5 sums checked
package ‘gganimate’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\bitle\AppData\Local\Temp\RtmpysDxES\downloaded_packages
Fetching standard & poor’s index data from January 1, 2014 to November 30, 2024
GSPC <- getSymbols("^GSPC", src = "yahoo",from = "2014-01-01", to = "2024-11-30", periodicity = "weekly", auto.assign = FALSE)
head(GSPC)
Fetching JP Morgan stock data from January 1, 2014 to November 30, 2024
JPM <- getSymbols("JPM", src = "yahoo",from = "2014-01-01", to = "2024-11-30", periodicity = "weekly", auto.assign = FALSE)
head(JPM)
Fetching Citibank stock data from January 1, 2014 to November 30, 2024
C <- getSymbols("C", src = "yahoo",from = "2014-01-01", to = "2024-11-30", periodicity = "weekly", auto.assign = FALSE)
head(C)
C.Open C.High C.Low C.Close C.Volume C.Adjusted
2014-01-01 52.03 54.72 51.81 54.18 100708100 41.43935
2014-01-08 54.29 55.28 53.44 53.95 109160700 41.26343
2014-01-15 54.52 55.17 51.56 51.85 158136800 39.65726
2014-01-22 52.01 52.13 48.36 49.60 179186600 37.93634
2014-01-29 48.92 49.36 46.19 46.78 201648400 35.77948
2014-02-05 46.76 49.86 46.31 49.66 146929700 37.99014
Fetching Wells Fargo stock data from January 1, 2014 to November 30, 2024
WFC <- getSymbols("WFC", src = "yahoo",from = "2014-01-01", to = "2024-11-30", periodicity = "weekly", auto.assign = FALSE)
head(WFC)
WFC.Open WFC.High WFC.Low WFC.Close WFC.Volume WFC.Adjusted
2014-01-01 45.15 45.81 44.91 45.40 67362800 33.13436
2014-01-08 45.40 46.20 44.92 45.59 99905700 33.27303
2014-01-15 45.94 46.84 45.76 46.50 70989200 33.93718
2014-01-22 46.74 46.83 45.23 45.96 89198800 33.54307
2014-01-29 45.63 46.21 44.23 44.77 110588600 32.67457
2014-02-05 44.60 46.09 44.17 45.97 85877500 33.55037
Fetching Royal Dutch Shell stock data from January 1, 2014 to November 30, 2024
SHEL <- getSymbols("SHEL", src = "yahoo",from = "2014-01-01", to = "2024-11-30", periodicity = "weekly", auto.assign = FALSE)
head(SHEL)
SHEL.Open SHEL.High SHEL.Low SHEL.Close SHEL.Volume SHEL.Adjusted
2014-01-01 71.27 71.27 70.28 70.92 7760962 40.10404
2014-01-08 70.65 72.05 70.28 71.49 16969417 40.42635
2014-01-15 71.17 72.06 70.52 71.28 16378657 40.30762
2014-01-22 72.07 72.91 70.41 70.54 26645059 39.88916
2014-01-29 69.95 71.64 67.75 67.86 19409128 38.37366
2014-02-05 67.98 70.80 67.93 70.70 9690678 39.97964
Fetching Exxon Mobil stock data from January 1, 2014 to November 30, 2024
XOM <- getSymbols("XOM", src = "yahoo", from = "2014-01-01", to = "2024-11-30", periodicity = "weekly", auto.assign = FALSE)
head(XOM)
XOM.Open XOM.High XOM.Low XOM.Close XOM.Volume XOM.Adjusted
2014-01-01 100.60 101.22 98.89 101.07 43359200 63.56967
2014-01-08 101.19 101.19 98.27 99.12 54766000 62.34320
2014-01-15 99.16 100.34 98.04 98.50 48692500 61.95324
2014-01-22 98.88 98.95 94.75 95.65 64868500 60.16069
2014-01-29 94.93 96.20 89.39 90.02 82973600 56.61957
2014-02-05 90.31 91.00 89.25 90.84 68274000 57.13535
Extracting the adjusted close prices
GSPC_adj <- Ad(GSPC)
JPM_adj <- Ad(JPM)
WFC_adj <- Ad(WFC)
C_adj <- Ad(C)
SHEL_adj <- Ad(SHEL)
XOM_adj <- Ad(XOM)
head(GSPC_adj)
GSPC.Adjusted
2014-01-01 1837.88
2014-01-08 1838.88
2014-01-15 1843.80
2014-01-22 1792.50
2014-01-29 1755.20
2014-02-05 1819.75
Merging the adjusted close for each stock ticket
combined_xts <- merge(GSPC_adj, JPM_adj, WFC_adj, C_adj, SHEL_adj, XOM_adj)
head(combined_xts)
GSPC.Adjusted JPM.Adjusted WFC.Adjusted C.Adjusted SHEL.Adjusted
2014-01-01 1837.88 43.08535 33.13436 41.43935 40.10404
2014-01-08 1838.88 42.93586 33.27303 41.26343 40.42635
2014-01-15 1843.80 43.25562 33.93718 39.65726 40.30762
2014-01-22 1792.50 41.44865 33.54307 37.93634 39.88916
2014-01-29 1755.20 40.86119 32.67457 35.77948 38.37366
2014-02-05 1819.75 42.70535 33.55037 37.99014 39.97964
XOM.Adjusted
2014-01-01 63.56967
2014-01-08 62.34320
2014-01-15 61.95324
2014-01-22 60.16069
2014-01-29 56.61957
2014-02-05 57.13535
colnames(combined_xts) <- c("GSPC", "JPM", "C", "WFC", "SHEL", "XOM")
head(combined_xts)
GSPC JPM C WFC SHEL XOM
2014-01-01 1837.88 43.08535 33.13436 41.43935 40.10404 63.56967
2014-01-08 1838.88 42.93586 33.27303 41.26343 40.42635 62.34320
2014-01-15 1843.80 43.25562 33.93718 39.65726 40.30762 61.95324
2014-01-22 1792.50 41.44865 33.54307 37.93634 39.88916 60.16069
2014-01-29 1755.20 40.86119 32.67457 35.77948 38.37366 56.61957
2014-02-05 1819.75 42.70535 33.55037 37.99014 39.97964 57.13535
stock_return_data <- diff(combined_xts)/ lag(combined_xts, k = 1)
head(stock_return_data)
GSPC JPM C WFC SHEL
2014-01-01 NA NA NA NA NA
2014-01-08 0.0005441052 -0.003469721 0.004185254 -0.004245206 0.008036881
2014-01-15 0.0026755655 0.007447373 0.019960616 -0.038924707 -0.002937101
2014-01-22 -0.0278230000 -0.041774107 -0.011613087 -0.043394842 -0.010381682
2014-01-29 -0.0208089533 -0.014173280 -0.025891965 -0.056854830 -0.037992664
2014-02-05 0.0367764646 0.045132360 0.026803634 0.061785887 0.041851100
XOM
2014-01-01 NA
2014-01-08 -0.019293395
2014-01-15 -0.006255073
2014-01-22 -0.028933907
2014-01-29 -0.058860944
2014-02-05 0.009109528
tail(stock_return_data)
GSPC JPM C WFC SHEL
2024-10-23 -0.003124192 -0.005443518 0.007560641 0.017778539 -0.0258170109
2024-10-30 -0.008599493 -0.006325655 -0.018836148 -0.015767648 0.0450367797
2024-11-06 0.034798345 0.081583781 0.133135649 0.093639936 -0.0388449705
2024-11-13 -0.011198256 0.014735343 0.017260399 -0.006085166 0.0036601734
2024-11-20 0.017686371 0.028302295 0.053384150 0.016763871 -0.0050948710
2024-11-27 0.001785231 -0.001000120 -0.015255337 0.016057387 -0.0007717711
XOM
2024-10-23 -2.833466e-02
2024-10-30 1.432467e-02
2024-11-06 1.168459e-02
2024-11-13 -1.429162e-02
2024-11-20 2.607904e-03
2024-11-27 -8.478542e-05
stock_return_data <- stock_return_data[-1,]
head(stock_return_data)
GSPC JPM C WFC SHEL
2014-01-15 0.002675566 0.007447373 0.019960616 -0.038924707 -0.002937101
2014-01-22 -0.027823000 -0.041774107 -0.011613087 -0.043394842 -0.010381682
2014-01-29 -0.020808953 -0.014173280 -0.025891965 -0.056854830 -0.037992664
2014-02-05 0.036776465 0.045132360 0.026803634 0.061785887 0.041851100
2014-02-12 0.011545547 0.018456980 0.010250109 -0.005638381 0.025883688
2014-02-19 0.002368579 -0.024961547 -0.001084165 -0.019846030 0.023507735
XOM
2014-01-15 -0.006255073
2014-01-22 -0.028933907
2014-01-29 -0.058860944
2014-02-05 0.009109528
2014-02-12 0.042891530
2014-02-19 0.023811940
Calculation of the covariance matrix
sigma <- as.matrix(cov(stock_return_data))
sigma
GSPC JPM C WFC SHEL XOM
GSPC 0.0004301921 0.0004628620 0.0004969935 0.0005764125 0.0004170972 0.0003616392
JPM 0.0004628620 0.0011541665 0.0010366875 0.0011389756 0.0006111657 0.0005799267
C 0.0004969935 0.0010366875 0.0015657350 0.0012303941 0.0007393359 0.0006568361
WFC 0.0005764125 0.0011389756 0.0012303941 0.0016955539 0.0008922839 0.0007716886
SHEL 0.0004170972 0.0006111657 0.0007393359 0.0008922839 0.0015920984 0.0009691596
XOM 0.0003616392 0.0005799267 0.0006568361 0.0007716886 0.0009691596 0.0012221730
Calculation of eigenvalues
lambda <- eigen(sigma)$values
lambda
[1] 0.0051936608 0.0012128457 0.0004218487 0.0004017819 0.0002384050 0.0001913768
str(lambda)
num [1:6] 0.005194 0.001213 0.000422 0.000402 0.000238 ...
Calculation of eigenvectors
e <- eigen(sigma)$vectors
e
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] -0.2146230 0.04116499 0.001446222 0.12894513 0.25943254 0.93183059
[2,] -0.4054321 0.31970338 0.101930862 0.17973594 0.75689536 -0.34326239
[3,] -0.4702264 0.37007639 -0.065410840 -0.77245536 -0.19885468 0.03770294
[4,] -0.5176339 0.27058984 -0.086164380 0.59001754 -0.54740943 -0.06028394
[5,] -0.4148524 -0.66822668 -0.598602744 -0.05011394 0.11202438 -0.08935575
[6,] -0.3583315 -0.48927794 0.787133968 -0.06122709 -0.08974653 -0.02868044
total_variance <- sum(lambda)
total_variance
[1] 0.007659919
lambda_proportions <- numeric(length(lambda))
for (i in seq_along(lambda)){
lambda_proportions[i] <- lambda[i] /total_variance
}
print(lambda_proportions)
[1] 0.67803078 0.15833662 0.05507222 0.05245250 0.03112369 0.02498418
lambda_cumsum <- cumsum(lambda_proportions)
lambda_cumsum
[1] 0.6780308 0.8363674 0.8914396 0.9438921 0.9750158 1.0000000
plot(lambda_cumsum, type = "o", pch = 16, col ="darkblue",
main = "Cumulative Variance",
xlab = "Components",
ylab = "Cumulative Proportion of Variance")
lambda_df <- data.frame(index = seq_along(lambda_proportions),
proportion = lambda_proportions)
lambda_df$cumsum <- cumsum(lambda_df$proportion)
lambda_df
NA
NA
ggplot(lambda_df, aes(x = index, y = proportion )) +
geom_line(color = "darkblue") +
geom_point(color = "darkblue", size = 3) +
labs(title = "Proportion of Variance Explained",
x = "Eigenvalue Index",
y = "proportion of Variance") +
theme_minimal()
NA
ggplot(lambda_df, aes(x = index, y = cumsum )) +
geom_line(color = "darkblue") +
geom_point(color = "darkblue", size = 3) +
labs(title = "Proportion of Variance Explained",
x = "Eigenvalue Index",
y = "proportion of Variance") +
theme_minimal()
stock_return_data_df <- as.data.frame(stock_return_data)
head(stock_return_data_df)
?prcomp()
pca_result<- prcomp(stock_return_data_df)
pca_result
Standard deviations (1, .., p=6):
[1] 0.07206706 0.03482593 0.02053896 0.02004450 0.01544037 0.01383390
Rotation (n x k) = (6 x 6):
PC1 PC2 PC3 PC4 PC5 PC6
GSPC -0.2146230 -0.04116499 0.001446222 0.12894513 -0.25943254 0.93183059
JPM -0.4054321 -0.31970338 0.101930862 0.17973594 -0.75689536 -0.34326239
C -0.4702264 -0.37007639 -0.065410840 -0.77245536 0.19885468 0.03770294
WFC -0.5176339 -0.27058984 -0.086164380 0.59001754 0.54740943 -0.06028394
SHEL -0.4148524 0.66822668 -0.598602744 -0.05011394 -0.11202438 -0.08935575
XOM -0.3583315 0.48927794 0.787133968 -0.06122709 0.08974653 -0.02868044
biplot(pca_result, scale = 0)
library(ggplot2)
# Extract scores (projections of observations)
scores <- as.data.frame(pca_result$x)
scores$Observation <- rownames(scores)
# Scatterplot of first two PCs
ggplot(scores, aes(x = PC1, y = PC2)) +
geom_point(color = "blue", size = 3) +
geom_text(aes(label = Observation), vjust = -1, hjust = -0.5) +
labs(title = "PCA: Scores Plot", x = "PC1", y = "PC2") +
theme_minimal()
# Extract loadings (rotations)
loadings <- as.data.frame(pca_result$rotation)
loadings$Variable <- rownames(loadings)
# Scatterplot of loadings
ggplot(loadings, aes(x = PC1, y = PC2)) +
geom_segment(aes(xend = 0, yend = 0), arrow = arrow(length = unit(0.2, "cm")), color = "red") +
geom_text(aes(label = Variable), vjust = 1.5, hjust = 1.5) +
labs(title = "PCA: Loadings Plot", x = "PC1", y = "PC2") +
theme_minimal()
install.packages("factoextra")
Installing package into ‘C:/Users/bitle/AppData/Local/R/win-library/4.4’
(as ‘lib’ is unspecified)
also installing the dependencies ‘estimability’, ‘corrplot’, ‘viridis’, ‘DT’, ‘emmeans’, ‘flashClust’, ‘leaps’, ‘multcompView’, ‘ggsci’, ‘ggsignif’, ‘rstatix’, ‘dendextend’, ‘FactoMineR’, ‘ggpubr’, ‘ggrepel’
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.4/estimability_1.5.1.zip'
Content type 'application/zip' length 54292 bytes (53 KB)
downloaded 53 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.4/corrplot_0.95.zip'
Content type 'application/zip' length 3826107 bytes (3.6 MB)
downloaded 3.6 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.4/viridis_0.6.5.zip'
Content type 'application/zip' length 3014779 bytes (2.9 MB)
downloaded 2.9 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.4/DT_0.33.zip'
Content type 'application/zip' length 2027972 bytes (1.9 MB)
downloaded 1.9 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.4/emmeans_1.10.5.zip'
Content type 'application/zip' length 2256803 bytes (2.2 MB)
downloaded 2.2 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.4/flashClust_1.01-2.zip'
Content type 'application/zip' length 28514 bytes (27 KB)
downloaded 27 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.4/leaps_3.2.zip'
Content type 'application/zip' length 88536 bytes (86 KB)
downloaded 86 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.4/multcompView_0.1-10.zip'
Content type 'application/zip' length 117711 bytes (114 KB)
downloaded 114 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.4/ggsci_3.2.0.zip'
Content type 'application/zip' length 2403003 bytes (2.3 MB)
downloaded 2.3 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.4/ggsignif_0.6.4.zip'
Content type 'application/zip' length 602241 bytes (588 KB)
downloaded 588 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.4/rstatix_0.7.2.zip'
Content type 'application/zip' length 621163 bytes (606 KB)
downloaded 606 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.4/dendextend_1.19.0.zip'
Content type 'application/zip' length 5057124 bytes (4.8 MB)
downloaded 4.8 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.4/FactoMineR_2.11.zip'
Content type 'application/zip' length 3812248 bytes (3.6 MB)
downloaded 3.6 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.4/ggpubr_0.6.0.zip'
Content type 'application/zip' length 2093708 bytes (2.0 MB)
downloaded 2.0 MB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.4/ggrepel_0.9.6.zip'
Content type 'application/zip' length 603889 bytes (589 KB)
downloaded 589 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.4/factoextra_1.0.7.zip'
Content type 'application/zip' length 424794 bytes (414 KB)
downloaded 414 KB
package ‘estimability’ successfully unpacked and MD5 sums checked
package ‘corrplot’ successfully unpacked and MD5 sums checked
package ‘viridis’ successfully unpacked and MD5 sums checked
package ‘DT’ successfully unpacked and MD5 sums checked
package ‘emmeans’ successfully unpacked and MD5 sums checked
package ‘flashClust’ successfully unpacked and MD5 sums checked
package ‘leaps’ successfully unpacked and MD5 sums checked
package ‘multcompView’ successfully unpacked and MD5 sums checked
package ‘ggsci’ successfully unpacked and MD5 sums checked
package ‘ggsignif’ successfully unpacked and MD5 sums checked
package ‘rstatix’ successfully unpacked and MD5 sums checked
package ‘dendextend’ successfully unpacked and MD5 sums checked
package ‘FactoMineR’ successfully unpacked and MD5 sums checked
package ‘ggpubr’ successfully unpacked and MD5 sums checked
package ‘ggrepel’ successfully unpacked and MD5 sums checked
package ‘factoextra’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\bitle\AppData\Local\Temp\RtmpysDxES\downloaded_packages
library(factoextra)
Warning: package ‘factoextra’ was built under R version 4.4.2Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
# Biplot
fviz_pca_biplot(pca_result, repel = TRUE,
title = "PCA Biplot")