Problem 1

UNIVERSIDAD DE LOS ANDES Pedro Alexander Díaz Quiroga Cod: 201410616

Data

mydata <- import.csv('C:/Users/pdiazq/Desktop/TareaAI/SP500_close_price.csv')
mydata <- mydata[, -1]
date <- as.Date(mydata[, 1])
myzoo <- zoo(mydata[,-1], date )
myzoo <- na.locf(myzoo) #impute missing values
prices2returns <- function(x) 100*diff(log(x)) #function to covert from price to return
log.return.zoo <- prices2returns(myzoo)
log.return.data <- coredata(log.return.zoo) #Data
log.return.date <- time(log.return.zoo) #date

The following is a curve that represents the first 10 stock prices that the initial data frame contains (just the first of the total data frame), where some trends could be seen

plot(myzoo[, 1:5])

The following is a representaion of the input Data, after aplying a function to covert the input to “price to return” (A meaning of the return when investing in that stock. Again the same 10 first stocks below.

plot(log.return.zoo[, 1:5])

  1. Fit a PCA model to log returns derived from stock price data. The code for deriving log returns is provided in the template file. Please use data frame named log.return.data as input to your PCA model. Having built the model, please do the following:
  1. Plot a scree plot which shows the distribution of variance contained in subsequent principal components sorted by their eigenvalues.

Now we are gonna aply PCA (Principal Component Analysis) to this input data

# apply PCA - scale. = TRUE is highly 
# advisable, but default is FALSE. 
zoo.pca <- prcomp(log.return.data,
                 center = TRUE,
                 scale. = TRUE)
#print (zoo.pca)
screeplot(zoo.pca, type = "l")

  1. Create a second plot showing cumulative variance retained if top N components are kept after dimensionality reduction (i.e. the horizontal axis will show the number of components kept, the vertical axis will show the cumulative percentage of variance retained).

    The following plot shows the cumulative variance as asked before. Vertical axis shows the cumulative variance with a max value of 1. Horizontal axis shows the 471 components of PCA.

s <- summary(prcomp(log.return.data))
plot (s$importance[3, ])

summary(zoo.pca)
## Importance of components%s:
##                            PC1     PC2    PC3     PC4     PC5    PC6
## Standard deviation     13.6101 3.83252 2.9915 2.53438 2.14994 2.0241
## Proportion of Variance  0.3933 0.03119 0.0190 0.01364 0.00981 0.0087
## Cumulative Proportion   0.3933 0.42446 0.4435 0.45710 0.46691 0.4756
##                            PC7    PC8    PC9    PC10    PC11    PC12
## Standard deviation     1.89665 1.7901 1.7367 1.64954 1.61697 1.58928
## Proportion of Variance 0.00764 0.0068 0.0064 0.00578 0.00555 0.00536
## Cumulative Proportion  0.48325 0.4900 0.4965 0.50223 0.50778 0.51315
##                           PC13    PC14    PC15    PC16    PC17    PC18
## Standard deviation     1.53551 1.49585 1.46860 1.44101 1.42991 1.41189
## Proportion of Variance 0.00501 0.00475 0.00458 0.00441 0.00434 0.00423
## Cumulative Proportion  0.51815 0.52290 0.52748 0.53189 0.53623 0.54046
##                           PC19    PC20    PC21    PC22    PC23    PC24
## Standard deviation     1.38238 1.36995 1.35110 1.34027 1.32325 1.31581
## Proportion of Variance 0.00406 0.00398 0.00388 0.00381 0.00372 0.00368
## Cumulative Proportion  0.54452 0.54851 0.55238 0.55620 0.55991 0.56359
##                           PC25    PC26    PC27   PC28    PC29    PC30
## Standard deviation     1.30876 1.30059 1.28651 1.2647 1.25992 1.25226
## Proportion of Variance 0.00364 0.00359 0.00351 0.0034 0.00337 0.00333
## Cumulative Proportion  0.56723 0.57082 0.57433 0.5777 0.58110 0.58443
##                           PC31    PC32    PC33    PC34    PC35    PC36
## Standard deviation     1.24845 1.23610 1.22212 1.21833 1.20734 1.19755
## Proportion of Variance 0.00331 0.00324 0.00317 0.00315 0.00309 0.00304
## Cumulative Proportion  0.58774 0.59098 0.59415 0.59730 0.60040 0.60344
##                           PC37    PC38    PC39    PC40    PC41    PC42
## Standard deviation     1.19267 1.18708 1.18326 1.17986 1.17238 1.16458
## Proportion of Variance 0.00302 0.00299 0.00297 0.00296 0.00292 0.00288
## Cumulative Proportion  0.60646 0.60946 0.61243 0.61538 0.61830 0.62118
##                           PC43    PC44    PC45    PC46    PC47    PC48
## Standard deviation     1.16302 1.15823 1.15300 1.15048 1.13866 1.13568
## Proportion of Variance 0.00287 0.00285 0.00282 0.00281 0.00275 0.00274
## Cumulative Proportion  0.62405 0.62690 0.62972 0.63253 0.63529 0.63802
##                           PC49    PC50    PC51    PC52   PC53    PC54
## Standard deviation     1.12993 1.12555 1.11943 1.11324 1.1067 1.10216
## Proportion of Variance 0.00271 0.00269 0.00266 0.00263 0.0026 0.00258
## Cumulative Proportion  0.64074 0.64343 0.64609 0.64872 0.6513 0.65390
##                           PC55    PC56    PC57   PC58   PC59    PC60
## Standard deviation     1.09894 1.09742 1.09043 1.0856 1.0842 1.07999
## Proportion of Variance 0.00256 0.00256 0.00252 0.0025 0.0025 0.00248
## Cumulative Proportion  0.65646 0.65902 0.66154 0.6640 0.6665 0.66902
##                           PC61    PC62    PC63    PC64    PC65    PC66
## Standard deviation     1.07538 1.06879 1.06823 1.06471 1.05866 1.05426
## Proportion of Variance 0.00246 0.00243 0.00242 0.00241 0.00238 0.00236
## Cumulative Proportion  0.67147 0.67390 0.67632 0.67873 0.68111 0.68347
##                           PC67    PC68    PC69    PC70    PC71    PC72
## Standard deviation     1.05036 1.04457 1.04272 1.03745 1.03665 1.03481
## Proportion of Variance 0.00234 0.00232 0.00231 0.00229 0.00228 0.00227
## Cumulative Proportion  0.68581 0.68812 0.69043 0.69272 0.69500 0.69727
##                           PC73    PC74    PC75    PC76   PC77    PC78
## Standard deviation     1.03066 1.02981 1.02649 1.02356 1.0173 1.01147
## Proportion of Variance 0.00226 0.00225 0.00224 0.00222 0.0022 0.00217
## Cumulative Proportion  0.69953 0.70178 0.70402 0.70624 0.7084 0.71061
##                           PC79    PC80    PC81    PC82    PC83    PC84
## Standard deviation     1.00939 1.00652 1.00432 1.00093 0.99657 0.99268
## Proportion of Variance 0.00216 0.00215 0.00214 0.00213 0.00211 0.00209
## Cumulative Proportion  0.71277 0.71493 0.71707 0.71919 0.72130 0.72339
##                           PC85    PC86    PC87    PC88    PC89    PC90
## Standard deviation     0.98692 0.98489 0.98325 0.97984 0.97707 0.97291
## Proportion of Variance 0.00207 0.00206 0.00205 0.00204 0.00203 0.00201
## Cumulative Proportion  0.72546 0.72752 0.72957 0.73161 0.73364 0.73565
##                          PC91    PC92    PC93    PC94    PC95    PC96
## Standard deviation     0.9712 0.96757 0.96306 0.96026 0.95730 0.94908
## Proportion of Variance 0.0020 0.00199 0.00197 0.00196 0.00195 0.00191
## Cumulative Proportion  0.7377 0.73964 0.74161 0.74357 0.74551 0.74743
##                          PC97    PC98    PC99   PC100   PC101   PC102
## Standard deviation     0.9452 0.94462 0.93898 0.93849 0.93475 0.93215
## Proportion of Variance 0.0019 0.00189 0.00187 0.00187 0.00186 0.00184
## Cumulative Proportion  0.7493 0.75122 0.75309 0.75496 0.75681 0.75866
##                          PC103   PC104  PC105   PC106   PC107   PC108
## Standard deviation     0.92806 0.92389 0.9208 0.91923 0.91769 0.91230
## Proportion of Variance 0.00183 0.00181 0.0018 0.00179 0.00179 0.00177
## Cumulative Proportion  0.76049 0.76230 0.7641 0.76589 0.76768 0.76945
##                          PC109   PC110   PC111   PC112  PC113   PC114
## Standard deviation     0.90861 0.90709 0.90297 0.89911 0.8954 0.88815
## Proportion of Variance 0.00175 0.00175 0.00173 0.00172 0.0017 0.00167
## Cumulative Proportion  0.77120 0.77295 0.77468 0.77640 0.7781 0.77977
##                          PC115   PC116   PC117   PC118   PC119   PC120
## Standard deviation     0.88580 0.88521 0.88055 0.87628 0.87412 0.87373
## Proportion of Variance 0.00167 0.00166 0.00165 0.00163 0.00162 0.00162
## Cumulative Proportion  0.78144 0.78310 0.78475 0.78638 0.78800 0.78962
##                          PC121  PC122   PC123   PC124   PC125   PC126
## Standard deviation     0.86964 0.8674 0.86630 0.86225 0.86085 0.85852
## Proportion of Variance 0.00161 0.0016 0.00159 0.00158 0.00157 0.00156
## Cumulative Proportion  0.79123 0.7928 0.79442 0.79600 0.79757 0.79914
##                          PC127   PC128   PC129   PC130   PC131  PC132
## Standard deviation     0.85560 0.85098 0.84943 0.84492 0.84312 0.8408
## Proportion of Variance 0.00155 0.00154 0.00153 0.00152 0.00151 0.0015
## Cumulative Proportion  0.80069 0.80223 0.80376 0.80527 0.80678 0.8083
##                          PC133   PC134   PC135   PC136   PC137   PC138
## Standard deviation     0.83536 0.83196 0.83151 0.82934 0.82643 0.82422
## Proportion of Variance 0.00148 0.00147 0.00147 0.00146 0.00145 0.00144
## Cumulative Proportion  0.80977 0.81124 0.81270 0.81416 0.81561 0.81706
##                          PC139   PC140  PC141  PC142   PC143   PC144
## Standard deviation     0.81733 0.81414 0.8131 0.8125 0.80871 0.80419
## Proportion of Variance 0.00142 0.00141 0.0014 0.0014 0.00139 0.00137
## Cumulative Proportion  0.81848 0.81988 0.8213 0.8227 0.82408 0.82545
##                          PC145   PC146   PC147   PC148   PC149   PC150
## Standard deviation     0.80353 0.80235 0.79760 0.79576 0.79488 0.78854
## Proportion of Variance 0.00137 0.00137 0.00135 0.00134 0.00134 0.00132
## Cumulative Proportion  0.82682 0.82819 0.82954 0.83088 0.83222 0.83354
##                          PC151   PC152  PC153  PC154   PC155   PC156
## Standard deviation     0.78677 0.78611 0.7834 0.7810 0.77930 0.77423
## Proportion of Variance 0.00131 0.00131 0.0013 0.0013 0.00129 0.00127
## Cumulative Proportion  0.83486 0.83617 0.8375 0.8388 0.84006 0.84133
##                          PC157   PC158   PC159   PC160   PC161   PC162
## Standard deviation     0.77246 0.76961 0.76890 0.76577 0.76217 0.76070
## Proportion of Variance 0.00127 0.00126 0.00126 0.00125 0.00123 0.00123
## Cumulative Proportion  0.84260 0.84385 0.84511 0.84635 0.84759 0.84882
##                          PC163   PC164   PC165  PC166  PC167   PC168
## Standard deviation     0.75843 0.75639 0.75373 0.7515 0.7512 0.74704
## Proportion of Variance 0.00122 0.00121 0.00121 0.0012 0.0012 0.00118
## Cumulative Proportion  0.85004 0.85125 0.85246 0.8537 0.8549 0.85604
##                          PC169   PC170   PC171   PC172   PC173   PC174
## Standard deviation     0.74416 0.74219 0.73731 0.73608 0.73470 0.73248
## Proportion of Variance 0.00118 0.00117 0.00115 0.00115 0.00115 0.00114
## Cumulative Proportion  0.85722 0.85839 0.85954 0.86069 0.86184 0.86298
##                          PC175   PC176   PC177   PC178  PC179  PC180
## Standard deviation     0.73050 0.72769 0.72572 0.72396 0.7209 0.7196
## Proportion of Variance 0.00113 0.00112 0.00112 0.00111 0.0011 0.0011
## Cumulative Proportion  0.86411 0.86523 0.86635 0.86746 0.8686 0.8697
##                          PC181   PC182   PC183   PC184   PC185   PC186
## Standard deviation     0.71476 0.71254 0.71174 0.70889 0.70713 0.70384
## Proportion of Variance 0.00108 0.00108 0.00108 0.00107 0.00106 0.00105
## Cumulative Proportion  0.87075 0.87183 0.87290 0.87397 0.87503 0.87609
##                          PC187   PC188   PC189   PC190   PC191  PC192
## Standard deviation     0.69852 0.69792 0.69408 0.69111 0.68906 0.6872
## Proportion of Variance 0.00104 0.00103 0.00102 0.00101 0.00101 0.0010
## Cumulative Proportion  0.87712 0.87816 0.87918 0.88019 0.88120 0.8822
##                         PC193   PC194   PC195   PC196   PC197   PC198
## Standard deviation     0.6858 0.68398 0.68154 0.67713 0.67587 0.67271
## Proportion of Variance 0.0010 0.00099 0.00099 0.00097 0.00097 0.00096
## Cumulative Proportion  0.8832 0.88419 0.88518 0.88615 0.88712 0.88809
##                          PC199   PC200   PC201   PC202   PC203   PC204
## Standard deviation     0.67105 0.66594 0.66521 0.66219 0.66114 0.66012
## Proportion of Variance 0.00096 0.00094 0.00094 0.00093 0.00093 0.00093
## Cumulative Proportion  0.88904 0.88998 0.89092 0.89185 0.89278 0.89371
##                          PC205   PC206   PC207   PC208  PC209   PC210
## Standard deviation     0.65671 0.65443 0.65384 0.65340 0.6497 0.64834
## Proportion of Variance 0.00092 0.00091 0.00091 0.00091 0.0009 0.00089
## Cumulative Proportion  0.89462 0.89553 0.89644 0.89735 0.8982 0.89913
##                          PC211   PC212   PC213   PC214   PC215   PC216
## Standard deviation     0.64749 0.64234 0.63943 0.63736 0.63543 0.63312
## Proportion of Variance 0.00089 0.00088 0.00087 0.00086 0.00086 0.00085
## Cumulative Proportion  0.90002 0.90090 0.90177 0.90263 0.90349 0.90434
##                          PC217   PC218   PC219   PC220   PC221   PC222
## Standard deviation     0.63026 0.62824 0.62601 0.62434 0.62216 0.61631
## Proportion of Variance 0.00084 0.00084 0.00083 0.00083 0.00082 0.00081
## Cumulative Proportion  0.90518 0.90602 0.90685 0.90768 0.90850 0.90931
##                          PC223  PC224   PC225   PC226   PC227   PC228
## Standard deviation     0.61595 0.6144 0.61153 0.61057 0.60828 0.60805
## Proportion of Variance 0.00081 0.0008 0.00079 0.00079 0.00079 0.00078
## Cumulative Proportion  0.91011 0.9109 0.91171 0.91250 0.91329 0.91407
##                          PC229   PC230   PC231   PC232   PC233   PC234
## Standard deviation     0.60709 0.60445 0.60179 0.59858 0.59671 0.59519
## Proportion of Variance 0.00078 0.00078 0.00077 0.00076 0.00076 0.00075
## Cumulative Proportion  0.91485 0.91563 0.91640 0.91716 0.91792 0.91867
##                          PC235   PC236   PC237   PC238   PC239   PC240
## Standard deviation     0.59458 0.59370 0.59114 0.58868 0.58634 0.58426
## Proportion of Variance 0.00075 0.00075 0.00074 0.00074 0.00073 0.00072
## Cumulative Proportion  0.91942 0.92017 0.92091 0.92164 0.92237 0.92310
##                          PC241   PC242   PC243   PC244  PC245  PC246
## Standard deviation     0.58353 0.58199 0.57759 0.57655 0.5758 0.5735
## Proportion of Variance 0.00072 0.00072 0.00071 0.00071 0.0007 0.0007
## Cumulative Proportion  0.92382 0.92454 0.92525 0.92595 0.9267 0.9274
##                          PC247   PC248   PC249   PC250   PC251   PC252
## Standard deviation     0.57075 0.56863 0.56739 0.56609 0.56364 0.55943
## Proportion of Variance 0.00069 0.00069 0.00068 0.00068 0.00067 0.00066
## Cumulative Proportion  0.92805 0.92874 0.92942 0.93010 0.93077 0.93144
##                          PC253   PC254   PC255   PC256   PC257   PC258
## Standard deviation     0.55854 0.55681 0.55520 0.55200 0.55000 0.54788
## Proportion of Variance 0.00066 0.00066 0.00065 0.00065 0.00064 0.00064
## Cumulative Proportion  0.93210 0.93276 0.93341 0.93406 0.93470 0.93534
##                          PC259   PC260   PC261   PC262   PC263   PC264
## Standard deviation     0.54761 0.54605 0.54476 0.54391 0.54138 0.53911
## Proportion of Variance 0.00064 0.00063 0.00063 0.00063 0.00062 0.00062
## Cumulative Proportion  0.93598 0.93661 0.93724 0.93787 0.93849 0.93911
##                          PC265   PC266  PC267   PC268   PC269   PC270
## Standard deviation     0.53637 0.53434 0.5326 0.52807 0.52710 0.52491
## Proportion of Variance 0.00061 0.00061 0.0006 0.00059 0.00059 0.00058
## Cumulative Proportion  0.93972 0.94032 0.9409 0.94152 0.94211 0.94269
##                          PC271   PC272   PC273   PC274   PC275   PC276
## Standard deviation     0.52436 0.52030 0.51710 0.51656 0.51398 0.51362
## Proportion of Variance 0.00058 0.00057 0.00057 0.00057 0.00056 0.00056
## Cumulative Proportion  0.94328 0.94385 0.94442 0.94499 0.94555 0.94611
##                          PC277   PC278   PC279   PC280   PC281   PC282
## Standard deviation     0.51283 0.51181 0.50905 0.50796 0.50526 0.50400
## Proportion of Variance 0.00056 0.00056 0.00055 0.00055 0.00054 0.00054
## Cumulative Proportion  0.94667 0.94722 0.94777 0.94832 0.94886 0.94940
##                          PC283   PC284   PC285   PC286   PC287   PC288
## Standard deviation     0.50108 0.49971 0.49788 0.49672 0.49387 0.49214
## Proportion of Variance 0.00053 0.00053 0.00053 0.00052 0.00052 0.00051
## Cumulative Proportion  0.94993 0.95046 0.95099 0.95151 0.95203 0.95255
##                          PC289   PC290   PC291  PC292  PC293  PC294
## Standard deviation     0.49071 0.49007 0.48795 0.4862 0.4846 0.4834
## Proportion of Variance 0.00051 0.00051 0.00051 0.0005 0.0005 0.0005
## Cumulative Proportion  0.95306 0.95357 0.95407 0.9546 0.9551 0.9556
##                          PC295   PC296   PC297   PC298   PC299   PC300
## Standard deviation     0.48109 0.47896 0.47513 0.47505 0.47283 0.47164
## Proportion of Variance 0.00049 0.00049 0.00048 0.00048 0.00047 0.00047
## Cumulative Proportion  0.95606 0.95655 0.95703 0.95751 0.95798 0.95845
##                          PC301   PC302   PC303   PC304   PC305   PC306
## Standard deviation     0.47091 0.46637 0.46572 0.46372 0.46355 0.46068
## Proportion of Variance 0.00047 0.00046 0.00046 0.00046 0.00046 0.00045
## Cumulative Proportion  0.95892 0.95939 0.95985 0.96030 0.96076 0.96121
##                          PC307   PC308   PC309   PC310   PC311   PC312
## Standard deviation     0.45795 0.45743 0.45591 0.45288 0.45074 0.44933
## Proportion of Variance 0.00045 0.00044 0.00044 0.00044 0.00043 0.00043
## Cumulative Proportion  0.96165 0.96210 0.96254 0.96298 0.96341 0.96384
##                          PC313   PC314   PC315   PC316   PC317   PC318
## Standard deviation     0.44889 0.44730 0.44515 0.44207 0.44092 0.43991
## Proportion of Variance 0.00043 0.00042 0.00042 0.00041 0.00041 0.00041
## Cumulative Proportion  0.96426 0.96469 0.96511 0.96552 0.96594 0.96635
##                          PC319  PC320  PC321  PC322   PC323   PC324
## Standard deviation     0.43795 0.4349 0.4340 0.4331 0.43045 0.42932
## Proportion of Variance 0.00041 0.0004 0.0004 0.0004 0.00039 0.00039
## Cumulative Proportion  0.96676 0.9672 0.9676 0.9679 0.96835 0.96874
##                          PC325   PC326   PC327   PC328   PC329   PC330
## Standard deviation     0.42847 0.42618 0.42390 0.42138 0.41777 0.41750
## Proportion of Variance 0.00039 0.00039 0.00038 0.00038 0.00037 0.00037
## Cumulative Proportion  0.96913 0.96951 0.96990 0.97027 0.97064 0.97101
##                          PC331   PC332   PC333   PC334   PC335   PC336
## Standard deviation     0.41642 0.41549 0.41387 0.41342 0.41199 0.40995
## Proportion of Variance 0.00037 0.00037 0.00036 0.00036 0.00036 0.00036
## Cumulative Proportion  0.97138 0.97175 0.97211 0.97248 0.97284 0.97319
##                          PC337   PC338   PC339   PC340   PC341   PC342
## Standard deviation     0.40823 0.40765 0.40528 0.40274 0.40177 0.39976
## Proportion of Variance 0.00035 0.00035 0.00035 0.00034 0.00034 0.00034
## Cumulative Proportion  0.97355 0.97390 0.97425 0.97459 0.97493 0.97527
##                          PC343   PC344   PC345   PC346   PC347   PC348
## Standard deviation     0.39954 0.39830 0.39593 0.39403 0.39220 0.39145
## Proportion of Variance 0.00034 0.00034 0.00033 0.00033 0.00033 0.00033
## Cumulative Proportion  0.97561 0.97595 0.97628 0.97661 0.97694 0.97726
##                          PC349   PC350   PC351   PC352   PC353   PC354
## Standard deviation     0.39073 0.38906 0.38832 0.38334 0.38267 0.38146
## Proportion of Variance 0.00032 0.00032 0.00032 0.00031 0.00031 0.00031
## Cumulative Proportion  0.97759 0.97791 0.97823 0.97854 0.97885 0.97916
##                          PC355  PC356  PC357  PC358  PC359  PC360   PC361
## Standard deviation     0.37997 0.3790 0.3777 0.3769 0.3748 0.3732 0.37131
## Proportion of Variance 0.00031 0.0003 0.0003 0.0003 0.0003 0.0003 0.00029
## Cumulative Proportion  0.97947 0.9798 0.9801 0.9804 0.9807 0.9810 0.98126
##                          PC362   PC363   PC364   PC365   PC366   PC367
## Standard deviation     0.36839 0.36616 0.36535 0.36449 0.36220 0.35994
## Proportion of Variance 0.00029 0.00028 0.00028 0.00028 0.00028 0.00028
## Cumulative Proportion  0.98155 0.98184 0.98212 0.98240 0.98268 0.98296
##                          PC368   PC369   PC370   PC371   PC372   PC373
## Standard deviation     0.35800 0.35574 0.35384 0.35078 0.34927 0.34881
## Proportion of Variance 0.00027 0.00027 0.00027 0.00026 0.00026 0.00026
## Cumulative Proportion  0.98323 0.98350 0.98376 0.98402 0.98428 0.98454
##                          PC374   PC375   PC376   PC377   PC378   PC379
## Standard deviation     0.34598 0.34531 0.34303 0.34238 0.34164 0.33982
## Proportion of Variance 0.00025 0.00025 0.00025 0.00025 0.00025 0.00025
## Cumulative Proportion  0.98480 0.98505 0.98530 0.98555 0.98580 0.98604
##                          PC380   PC381   PC382   PC383   PC384   PC385
## Standard deviation     0.33804 0.33591 0.33574 0.33397 0.33291 0.33270
## Proportion of Variance 0.00024 0.00024 0.00024 0.00024 0.00024 0.00024
## Cumulative Proportion  0.98628 0.98652 0.98676 0.98700 0.98723 0.98747
##                          PC386   PC387   PC388   PC389   PC390   PC391
## Standard deviation     0.32987 0.32575 0.32541 0.32343 0.32235 0.32045
## Proportion of Variance 0.00023 0.00023 0.00022 0.00022 0.00022 0.00022
## Cumulative Proportion  0.98770 0.98793 0.98815 0.98837 0.98859 0.98881
##                          PC392   PC393   PC394   PC395   PC396   PC397
## Standard deviation     0.31985 0.31853 0.31626 0.31493 0.31394 0.31164
## Proportion of Variance 0.00022 0.00022 0.00021 0.00021 0.00021 0.00021
## Cumulative Proportion  0.98903 0.98924 0.98946 0.98967 0.98988 0.99008
##                         PC398  PC399  PC400  PC401  PC402  PC403   PC404
## Standard deviation     0.3091 0.3086 0.3074 0.3064 0.3060 0.3034 0.29902
## Proportion of Variance 0.0002 0.0002 0.0002 0.0002 0.0002 0.0002 0.00019
## Cumulative Proportion  0.9903 0.9905 0.9907 0.9909 0.9911 0.9913 0.99147
##                          PC405   PC406   PC407   PC408   PC409   PC410
## Standard deviation     0.29808 0.29743 0.29626 0.29558 0.29161 0.29098
## Proportion of Variance 0.00019 0.00019 0.00019 0.00019 0.00018 0.00018
## Cumulative Proportion  0.99166 0.99185 0.99203 0.99222 0.99240 0.99258
##                          PC411   PC412   PC413   PC414   PC415   PC416
## Standard deviation     0.28853 0.28789 0.28648 0.28603 0.28535 0.28195
## Proportion of Variance 0.00018 0.00018 0.00017 0.00017 0.00017 0.00017
## Cumulative Proportion  0.99276 0.99293 0.99311 0.99328 0.99345 0.99362
##                          PC417   PC418   PC419   PC420   PC421   PC422
## Standard deviation     0.28032 0.27909 0.27533 0.27499 0.27283 0.27187
## Proportion of Variance 0.00017 0.00017 0.00016 0.00016 0.00016 0.00016
## Cumulative Proportion  0.99379 0.99395 0.99411 0.99428 0.99443 0.99459
##                          PC423   PC424   PC425   PC426   PC427   PC428
## Standard deviation     0.27081 0.26896 0.26711 0.26549 0.26497 0.26316
## Proportion of Variance 0.00016 0.00015 0.00015 0.00015 0.00015 0.00015
## Cumulative Proportion  0.99475 0.99490 0.99505 0.99520 0.99535 0.99550
##                          PC429   PC430   PC431   PC432   PC433   PC434
## Standard deviation     0.26174 0.25937 0.25735 0.25631 0.25377 0.25262
## Proportion of Variance 0.00015 0.00014 0.00014 0.00014 0.00014 0.00014
## Cumulative Proportion  0.99564 0.99579 0.99593 0.99607 0.99620 0.99634
##                          PC435   PC436   PC437   PC438   PC439   PC440
## Standard deviation     0.24953 0.24862 0.24747 0.24452 0.24341 0.24124
## Proportion of Variance 0.00013 0.00013 0.00013 0.00013 0.00013 0.00012
## Cumulative Proportion  0.99647 0.99660 0.99673 0.99686 0.99698 0.99711
##                          PC441   PC442   PC443   PC444   PC445   PC446
## Standard deviation     0.23855 0.23738 0.23493 0.23292 0.23127 0.23049
## Proportion of Variance 0.00012 0.00012 0.00012 0.00012 0.00011 0.00011
## Cumulative Proportion  0.99723 0.99735 0.99746 0.99758 0.99769 0.99781
##                          PC447   PC448   PC449   PC450  PC451  PC452
## Standard deviation     0.22840 0.22804 0.22597 0.22265 0.2205 0.2190
## Proportion of Variance 0.00011 0.00011 0.00011 0.00011 0.0001 0.0001
## Cumulative Proportion  0.99792 0.99803 0.99814 0.99824 0.9983 0.9984
##                         PC453  PC454  PC455   PC456   PC457   PC458
## Standard deviation     0.2148 0.2146 0.2129 0.21084 0.20901 0.20628
## Proportion of Variance 0.0001 0.0001 0.0001 0.00009 0.00009 0.00009
## Cumulative Proportion  0.9985 0.9986 0.9987 0.99883 0.99893 0.99902
##                          PC459   PC460   PC461   PC462   PC463   PC464
## Standard deviation     0.20535 0.20202 0.20150 0.19910 0.19694 0.19363
## Proportion of Variance 0.00009 0.00009 0.00009 0.00008 0.00008 0.00008
## Cumulative Proportion  0.99911 0.99919 0.99928 0.99936 0.99944 0.99952
##                          PC465   PC466   PC467   PC468   PC469   PC470
## Standard deviation     0.19207 0.18565 0.18365 0.17935 0.17877 0.17375
## Proportion of Variance 0.00008 0.00007 0.00007 0.00007 0.00007 0.00006
## Cumulative Proportion  0.99960 0.99968 0.99975 0.99982 0.99988 0.99995
##                          PC471
## Standard deviation     0.15689
## Proportion of Variance 0.00005
## Cumulative Proportion  1.00000
  1. How many principal components must be retained in order to capture at least 80% of the total variance in data?

    As seen in the table where the plot was based, can be seen that 2 componets, must be taken, in order to reach 80% of the cumuled variance. (0.8042544) had been reached when those two components had been taken

  2. What is the magnitude of the estimated reconstruction error if we only retain top two of the PCA components?

    If we take just the first two PCA components, we would have a cumulative variance of 0.8042544, as seen in a previous table, so we are loosing the 19.57456% of the total variance

  1. Analysis of principal components and weights
  1. Compute and plot the time series of the 1st principal component and observe temporal patterns. Identify the date with the lowest value for this component and conduct a quick research on the Internet to see if you can identify event(s) that might explain the observed behavior.
#summary(zoo.pca)[2]

p1 <-- prcomp(log.return.data)$rotation[,"PC1"]
p1t <- zoo(p1, date )

plot (p1t)

According with the previous plot, for PC1, some events are seen.

3 drops and 2 increases can be seen. 2 of the dros apear to be bigger that the other, and bigger than the 2 increases too. So, in overall, can be say that the marked suffered more drops than increases, and that at least 2 drops were bigger than any other event.

Can say that the "drops" were 3 times greater than "increases".

Next, some events that could explain what happened on those dates:

End of 2011 https://en.wikipedia.org/wiki/August_2011_stock_markets_fall http://money.cnn.com/2011/07/08/markets/markets_newyork/index.htm

Mid 2012 https://www.usatoday.com/story/money/markets/2012/12/31/stocks-post-doubledigit-gains-in-2012/1800223/ http://theweek.com/articles/469099/why-2012-good-year-stock-market

Beging of 2013** () http://roccacharts.blogspot.com.co/2013/02/aapl-apple-comienzo-o-fin-de-la-caida.html http://money.cnn.com/2013/02/21/investing/stocks-markets/index.html http://www.businessinsider.com/two-charts-show-why-apple-stock-dropped-2013-4

Mid End of 2013 http://www.marketwatch.com/story/us-stocks-edge-lower-as-shutdown-drags-on-2013-10-08 http://money.cnn.com/2013/08/16/investing/stocks-markets/index.html

Beging of 2014 https://www.thestreet.com/story/12195485/1/stocks-begin-2014-with-inflating-bubbles.html

End of 2014** https://dealbook.nytimes.com/2014/12/31/bull-market-for-stocks-lasts-through-2014/ http://money.cnn.com/2014/10/09/investing/stocks-markets-wall-street-selloff/index.html http://www.cnbc.com/2013/12/30/why-the-market-could-see-a-17-drop-in-2014.html

Mid 2015 http://www.businessinsider.com/bank-of-america-on-mid-2015-stock-market-2015-6

  The Plot below, corresponding to the second principal component, shows events, the same dates than the principal component one, but with other magnitudes. aditionally, two events apear to be contrary to the PC1, Showing 4 increase events and 3 drop events.

In this case. the "drop" magnitude is yet greater than "increases", but the relation is much lower than in the PC1´s plot. Can say that "increases"" are a little more than a half than "drops". 
  1. Extract the weights from PCA model for 1st and 2nd principal components.
prcomp(log.return.data)$rotation[,"PC1"]
##           MMM           ACE           ABT           ANF          ADBE 
##  2.534324e-04  4.242656e-04 -7.105428e-05  2.849963e-04  5.726191e-04 
##           AMD           AES           AET           AFL             A 
## -4.833868e-05  3.913908e-04  4.584344e-04  6.233554e-04  9.667904e-04 
##           APD           ARG           AKS          AKAM            AA 
##  1.901613e-04  1.462052e-04  1.152964e-03  1.725941e-04  4.689306e-04 
##           ATI           AGN           ALL          ALTR            MO 
##  6.974859e-04 -8.322261e-04  2.847716e-04  3.456671e-04 -2.360787e-04 
##          AMZN           AEE           AEP           AXP           AIG 
## -5.682580e-06  6.038653e-04 -1.880672e-04  3.603231e-04  8.377200e-04 
##           AMT           AMP           ABC          AMGN           APH 
##  1.433001e-04  5.865390e-04 -7.481451e-05 -4.974105e-04  9.918407e-05 
##           APC           ADI           AON           APA           AIV 
##  2.943229e-04  6.501483e-05  1.488933e-04  7.827020e-04 -3.271332e-04 
##          APOL          AAPL          AMAT           ADM           AIZ 
## -1.339512e-03 -3.818817e-04  5.378829e-04  1.648637e-04  1.071177e-03 
##             T          ADSK           ADP            AN           AZO 
## -5.461744e-05 -1.243825e-04  1.187974e-04  2.479417e-04  2.030521e-04 
##           AVB           AVY           AVP           BHI           BLL 
## -2.297085e-04  9.278339e-05  1.638516e-03  5.888284e-04  1.889678e-04 
##           BAC            BK           BCR           BAX           BBT 
##  7.009029e-04  6.554195e-04 -6.857052e-05  1.949944e-04  3.935908e-04 
##           BDX          BBBY           BMS         BRK.B           BBY 
##  7.429941e-05  4.634342e-04  1.604621e-04  2.801572e-04 -8.225634e-04 
##           BIG          BIIB           BLK           HRB           BMC 
##  2.050458e-04 -3.483059e-04  5.348949e-04 -9.818444e-06  1.623359e-01 
##            BA           BXP           BSX           BMY          BRCM 
##  5.759309e-04 -1.971968e-04  5.478695e-04 -1.356917e-04  1.045098e-03 
##          BF.B          CHRW            CA           CVC           COG 
##  3.047916e-04  5.294136e-04  4.005104e-04 -1.805652e-04 -7.417198e-05 
##           CAM           CPB           COF           CAH           CFN 
##  5.771409e-04  3.079808e-05  5.268858e-04  1.204587e-04  4.917886e-04 
##           KMX           CCL           CAT           CBG           CBS 
##  6.174567e-04  1.142341e-03  1.901130e-04  7.975651e-04  2.082988e-04 
##          CELG           CNP           CTL          CERN            CF 
## -1.357923e-04  1.446233e-04  2.991568e-04  1.513650e-05  1.047728e-04 
##          SCHW           CHK           CVX            CB            CI 
##  3.851534e-04  3.029669e-04 -1.357302e-05  1.784234e-04  9.876890e-05 
##          CINF          CTAS          CSCO             C          CTXS 
##  2.582890e-04  2.351938e-04 -4.044133e-05  1.388008e-03  4.227732e-04 
##           CLF           CLX           CME           CMS           COH 
##  3.427323e-04  1.288094e-04  2.602006e-04  2.279915e-04  4.869795e-04 
##            KO           CCE          CTSH            CL         CMCSA 
## -1.615097e-04  6.025661e-05  1.282844e-04 -2.163191e-04  3.121703e-05 
##           CMA           CSC          CPWR           CAG           COP 
##  1.490352e-03  5.194014e-04  5.401609e-04  8.976231e-05  2.898812e-04 
##           CNX            ED           STZ           GLW          COST 
##  2.954764e-04  5.852028e-05  4.812036e-04  2.785013e-04  2.765143e-04 
##           CVH           COV           CSX           CMI           CVS 
## -5.022219e-05  2.133259e-04  1.408285e-04  8.637167e-04  3.107190e-04 
##           DHI           DHR           DRI           DVA            DF 
##  8.664309e-04  5.858561e-04  2.765843e-04  1.007229e-04  3.670610e-04 
##            DE           DNR          XRAY           DVN            DV 
##  6.211863e-04  3.191028e-04  2.894856e-04  3.096952e-04 -2.707384e-04 
##            DO           DTV           DFS         DISCA             D 
##  2.759605e-04  3.136356e-04  2.274549e-04  9.785847e-05 -2.569244e-05 
##           RRD           DOV           DOW           DPS           DTE 
##  1.395683e-04  4.458182e-04  6.351476e-04  5.364060e-04  2.278037e-04 
##            DD           DUK           DNB          ETFC           EMN 
##  4.511213e-04 -3.642897e-05 -3.691167e-04  6.540628e-04  6.188778e-04 
##           ETN          EBAY           ECL           EIX            EW 
##  3.461492e-04 -5.290155e-05  1.014422e-04  1.116891e-04 -5.808879e-05 
##           EMC           EMR           ETR           EOG           EQT 
##  1.161674e-04  4.785725e-04  1.671290e-04  6.387372e-04  4.235219e-05 
##           EFX           EQR            EL           EXC          EXPE 
##  3.495479e-04 -1.980086e-04  4.262165e-04  6.320307e-05 -9.589035e-05 
##          EXPD          ESRX           XOM          FFIV           FDO 
##  1.208647e-03  2.440893e-04  5.839174e-05  4.788436e-04  4.349413e-04 
##          FAST           FII           FDX           FIS          FITB 
## -1.203500e-04  8.960881e-05  6.277911e-04  5.373554e-04  4.341595e-04 
##           FHN          FSLR            FE          FISV          FLIR 
##  4.559296e-04 -3.443853e-05 -8.087565e-05  4.344965e-05 -1.729604e-04 
##           FLS           FLR           FMC           FTI             F 
##  5.621459e-04  3.516598e-04  2.649172e-04  4.606680e-04  3.149692e-04 
##           BEN           FCX           FTR           GME           GCI 
##  4.848871e-04  8.003792e-04  9.371747e-04 -9.800220e-04  5.494996e-04 
##           GPS            GD            GE           GIS           GPC 
## -4.996875e-04  3.949467e-04  6.993984e-04 -6.863891e-05  6.005897e-04 
##           GNW          GILD            GS            GT           GWW 
##  8.561931e-04  1.793392e-04  5.835058e-04  7.716771e-04  6.517108e-05 
##           HAL           HOG           HAR           HRS           HIG 
##  6.081025e-04  1.632499e-04  8.569431e-04  4.904373e-04  8.713667e-04 
##           HAS           HCP           HCN            HP           HES 
##  3.485264e-04 -8.443225e-05 -1.810017e-04  3.052713e-04  8.207880e-04 
##           HPQ            HD           HON           HRL           HSP 
##  7.870356e-04  2.152926e-05  6.684008e-04 -5.298433e-06  5.728254e-04 
##           HST          HCBK           HUM          HBAN           ITW 
##  7.071257e-04  5.396041e-04 -2.854360e-04  8.928484e-04  2.782859e-04 
##           TEG          INTC           ICE           IBM           IFF 
## -1.733966e-05  2.267524e-04  2.387202e-05  1.294587e-04  2.790186e-04 
##           IGT            IP           IPG          INTU          ISRG 
## -5.126461e-04  3.353832e-05  5.119520e-04 -6.489059e-05 -1.029112e-03 
##           IVZ           IRM           JBL           JEC           JNS 
##  6.365604e-04  3.191438e-04  5.152038e-04  2.278970e-04  4.680613e-04 
##          JDSU           JNJ           JCI           JOY           JPM 
##  1.042088e-03  3.321361e-05 -7.535505e-05  3.343512e-04  7.689847e-04 
##          JNPR             K           KEY           KMB           KIM 
##  3.657649e-04 -2.584309e-05  4.328920e-04  2.872455e-04  4.420938e-05 
##          KLAC           KSS            KR           LLL            LH 
##  5.415050e-04  5.468106e-04  2.447387e-04  3.822035e-04  2.703726e-05 
##            LM           LEG           LEN           LUK           LXK 
##  5.643040e-04  3.981523e-04  9.242679e-04  8.665601e-04  8.994855e-04 
##          LIFE           LLY           LNC          LLTC           LMT 
## -6.838541e-02  2.188366e-04  8.066168e-04  2.950819e-04  5.307708e-04 
##             L            LO           LOW           LSI           MTB 
##  3.771221e-04 -1.399158e-05  1.037929e-04 -3.622322e-01  4.112679e-04 
##             M           MRO           MAR           MMC           MAS 
##  3.126668e-04  5.968281e-04  7.325125e-04  2.542295e-04  1.397782e-03 
##           ANR            MA           MAT           MKC           MCD 
##  7.645512e-04  6.195544e-05  4.482492e-04 -9.067261e-05 -2.899881e-06 
##           MCK           MJN           MWV           MDT           MRK 
##  3.821569e-04 -2.063936e-05 -5.528586e-04  4.087448e-04  1.811289e-05 
##           MET          MCHP            MU          MSFT           TAP 
##  6.774920e-04  2.617979e-04  2.569161e-04  2.338751e-05  1.679785e-04 
##           MON           MWW           MCO            MS           MOS 
##  8.262517e-05  1.138598e-03 -3.323342e-04  1.131601e-03  9.697552e-05 
##           MSI           MUR           MYL           NBR          NDAQ 
##  3.299168e-04  3.156953e-04 -2.687713e-04  5.520914e-04  1.668972e-04 
##           NOV          NTAP          NFLX           NWL           NFX 
##  3.040679e-04  2.294618e-04 -1.210307e-03  5.681230e-04  7.389664e-04 
##           NEM           NEE           GAS           NKE            NI 
##  5.497974e-04  6.203608e-05  1.396842e-04  3.873973e-05  1.812197e-04 
##            NE           NBL           JWN           NSC          NTRS 
##  7.344806e-04  2.688900e-04  1.606000e-04  1.664055e-05  5.553686e-04 
##           NOC            NU           CMG           NRG           NUE 
##  3.464999e-04 -8.060485e-05  5.305021e-05  7.392803e-04  5.956970e-04 
##          NVDA          ORLY           OXY           OMC           OKE 
##  4.745464e-04 -1.469616e-04  5.670207e-04  3.523485e-04  2.672166e-05 
##          ORCL            OI          PCAR            IR           PLL 
##  1.384632e-04  5.267291e-04  3.372452e-04  5.289276e-04  4.514679e-04 
##            PH          PDCO          PAYX           BTU           JCP 
##  6.655111e-04  7.792427e-04  9.961078e-05  6.311034e-04  3.279949e-04 
##          PBCT           POM           PEP           PKI           PFE 
##  2.884807e-04 -8.772861e-05  1.644952e-04  7.969510e-04 -3.292777e-04 
##           PCG            PM           PNW           PXD           PBI 
##  1.747971e-04 -8.217181e-05  2.288306e-04  2.209336e-04  2.227779e-04 
##           PCL           PNC            RL           PPG           PPL 
##  1.338242e-04  5.506001e-04  2.963280e-04  2.665912e-04  4.126669e-05 
##            PX           PCP          PCLN           PFG            PG 
##  2.367000e-04  2.294239e-04 -1.110228e-04  5.967621e-04  2.207787e-06 
##           PGR           PLD           PRU           PEG           PSA 
##  2.865017e-04  2.213957e-04  5.348873e-04  7.013661e-05 -7.649952e-05 
##           PHM           QEP           PWR          QCOM           DGX 
##  1.301629e-03  4.254149e-04  5.796318e-04 -1.321818e-04  2.891386e-04 
##           RSH           RRC           RTN           RHT            RF 
##  1.010883e-03  4.324359e-04  3.634016e-04  6.357033e-04  9.047762e-04 
##           RSG           RAI           RHI           ROK           COL 
##  9.882288e-05 -1.595315e-04  2.137068e-04  6.327958e-04  3.820866e-04 
##           ROP          ROST           RDC             R           SWY 
##  3.229179e-04  1.279903e-04  5.638156e-04  1.071464e-03  2.807512e-04 
##           CRM          SNDK           SCG           SLB           SNI 
##  2.656848e-04 -3.664020e-04  1.385089e-04  5.010225e-04  3.655808e-05 
##           SEE          SHLD           SRE           SHW          SIAL 
##  6.356978e-05  9.422288e-04  2.187726e-04 -2.885802e-04  2.011169e-04 
##           SPG           SLM           SJM           SNA            SO 
## -2.510472e-04  5.779145e-04  2.211857e-04  5.414249e-04 -1.296433e-04 
##           LUV           SWN            SE             S           STJ 
##  5.841912e-04  1.093869e-04  2.943860e-04  7.262986e-04  3.339923e-04 
##           SWK          SPLS          SBUX           HOT           STT 
##  4.643908e-04 -1.517736e-05  7.442419e-06  5.550948e-04  6.810282e-04 
##          SRCL           SYK           STI           SVU          SYMC 
##  3.480863e-05  3.113414e-04  2.370521e-04  1.588505e-03  2.419580e-04 
##           SYY          TROW           TGT           TEL            TE 
##  2.039014e-04  6.108138e-04  4.484784e-04  4.208151e-04  2.249846e-04 
##           THC           TDC           TER           TSO           TXN 
##  2.616260e-04 -1.775019e-04  6.290688e-04 -3.018830e-04  1.877158e-04 
##           TXT           HSY           TRV           TMO           TIF 
##  4.613845e-04 -4.330898e-05  2.666602e-04  3.470052e-04  1.313013e-04 
##           TWX           TWC           TIE           TJX           TMK 
##  4.899614e-04  2.627623e-04  9.152295e-01  7.444240e-06  2.918255e-03 
##           TSS           TSN           TYC           USB           UNP 
## -2.254876e-04  1.002386e-04  2.692626e-04  5.637878e-04  2.383150e-04 
##           UNH           UPS             X           UTX           UNM 
##  2.457504e-05  2.355397e-04  7.784340e-04  2.348077e-04  3.084274e-04 
##          URBN           VFC           VLO           VAR           VTR 
##  6.550752e-05 -5.142984e-04  5.886625e-05  3.134079e-04 -2.440609e-04 
##          VRSN            VZ          VIAB             V           VNO 
## -4.041238e-04  3.340993e-04  3.324852e-04  1.179457e-04 -1.256593e-04 
##           VMC           WMT           WAG           DIS            WM 
## -2.722100e-05  4.366925e-04  1.546493e-04  2.457738e-04  2.441896e-04 
##           WAT           WLP           WFC           WDC            WU 
##  4.378060e-05  6.461576e-04  5.348426e-04  1.536538e-04  6.190430e-04 
##            WY           WHR           WFM           WMB           WIN 
##  3.900421e-04  8.823346e-04 -2.816601e-04  4.352886e-04  5.691894e-04 
##           WEC           WYN          WYNN           XEL           XRX 
## -1.793161e-05  5.784501e-04 -3.797323e-04 -4.328238e-06  6.358336e-04 
##          XLNX            XL          YHOO           YUM           ZMH 
##  1.463972e-04  4.743176e-04 -1.087533e-04  3.755188e-05  5.227760e-04 
##          ZION 
##  8.494489e-04
prcomp(log.return.data)$rotation[,"PC2"]
##           MMM           ACE           ABT           ANF          ADBE 
##  4.994089e-04  9.121001e-04  7.565907e-05 -1.853760e-04  1.876898e-04 
##           AMD           AES           AET           AFL             A 
## -5.637528e-04 -1.294423e-04  3.244355e-04  1.430304e-03 -4.172662e-04 
##           APD           ARG           AKS          AKAM            AA 
##  2.868371e-04 -1.988899e-05 -1.876764e-03  4.010166e-04 -1.204128e-03 
##           ATI           AGN           ALL          ALTR            MO 
##  9.515009e-04  2.037110e-03  6.920145e-04 -2.045232e-04  5.802674e-04 
##          AMZN           AEE           AEP           AXP           AIG 
## -7.349528e-04  8.256540e-04  5.810936e-05  7.658600e-04  5.471844e-04 
##           AMT           AMP           ABC          AMGN           APH 
##  1.015344e-03  5.833571e-05  3.455009e-04  7.809938e-04 -1.975566e-05 
##           APC           ADI           AON           APA           AIV 
## -7.430365e-04  8.001353e-04  1.633419e-04 -6.721624e-04  1.310647e-03 
##          APOL          AAPL          AMAT           ADM           AIZ 
##  1.525445e-03 -4.307999e-04  6.388617e-04 -1.258778e-04 -2.374278e-04 
##             T          ADSK           ADP            AN           AZO 
##  1.026062e-03 -1.095118e-04  1.117368e-03  6.845288e-04 -7.701284e-05 
##           AVB           AVY           AVP           BHI           BLL 
##  8.687496e-04  1.264430e-03 -1.956491e-03 -6.117378e-04  4.202781e-04 
##           BAC            BK           BCR           BAX           BBT 
##  9.155963e-04  2.848073e-04  1.181830e-03 -7.338909e-05  3.606447e-04 
##           BDX          BBBY           BMS         BRK.B           BBY 
##  6.661814e-04  5.970730e-05  1.672362e-04  7.219306e-04  9.517075e-04 
##           BIG          BIIB           BLK           HRB           BMC 
## -4.796890e-04  5.560308e-04 -2.495117e-04 -5.849871e-04  8.607344e-01 
##            BA           BXP           BSX           BMY          BRCM 
##  3.477746e-04  3.554540e-04  2.715840e-04  3.012525e-04  7.635772e-04 
##          BF.B          CHRW            CA           CVC           COG 
##  5.713840e-04  9.033629e-04  6.389082e-05 -4.467520e-04 -7.140290e-04 
##           CAM           CPB           COF           CAH           CFN 
## -1.101399e-03  5.902496e-04  8.757477e-04  1.613713e-03  4.355460e-04 
##           KMX           CCL           CAT           CBG           CBS 
##  2.004082e-03  3.746067e-04  4.516379e-04  1.171742e-03  7.203779e-04 
##          CELG           CNP           CTL          CERN            CF 
##  2.499355e-04 -5.734997e-04  8.049602e-04  5.684118e-04  3.202557e-04 
##          SCHW           CHK           CVX            CB            CI 
## -1.085331e-04 -2.767117e-03 -8.496511e-04  6.157345e-04  6.053669e-04 
##          CINF          CTAS          CSCO             C          CTXS 
##  1.907991e-04  9.054468e-04  8.685544e-04  2.551496e-04  1.053205e-03 
##           CLF           CLX           CME           CMS           COH 
## -7.061109e-04 -7.563573e-04  5.505492e-04  3.903649e-04  1.832064e-03 
##            KO           CCE          CTSH            CL         CMCSA 
##  5.212668e-04 -4.952500e-04  8.750434e-04 -4.839767e-04  6.328517e-04 
##           CMA           CSC          CPWR           CAG           COP 
##  2.512958e-04  1.628453e-03  1.464475e-04 -2.056483e-04 -1.040763e-03 
##           CNX            ED           STZ           GLW          COST 
## -6.759569e-04 -3.144431e-05  4.018383e-04  3.513779e-05  2.009319e-05 
##           CVH           COV           CSX           CMI           CVS 
## -4.405538e-06  1.096626e-03  2.725508e-04  1.331639e-03 -7.969454e-05 
##           DHI           DHR           DRI           DVA            DF 
##  1.094115e-04  1.375689e-04  1.029635e-03  2.065702e-04 -1.830858e-04 
##            DE           DNR          XRAY           DVN            DV 
##  1.523322e-03 -1.271845e-03  6.628221e-04  8.044312e-06  1.088980e-03 
##            DO           DTV           DFS         DISCA             D 
##  2.723378e-04  6.817591e-04  1.810410e-03 -6.473501e-04  2.908818e-06 
##           RRD           DOV           DOW           DPS           DTE 
## -1.806191e-04 -2.828398e-04 -3.449187e-04 -9.710470e-06  4.930493e-04 
##            DD           DUK           DNB          ETFC           EMN 
##  3.424421e-04 -9.318162e-05  1.215287e-03  3.064468e-04  1.349885e-03 
##           ETN          EBAY           ECL           EIX            EW 
## -3.986821e-05  4.793333e-04 -6.308135e-04  5.459267e-04  5.285639e-04 
##           EMC           EMR           ETR           EOG           EQT 
##  9.451584e-04  1.202089e-04  5.488036e-04 -1.297868e-03 -5.520982e-04 
##           EFX           EQR            EL           EXC          EXPE 
##  3.830883e-04  4.828506e-04 -2.238028e-04 -2.669927e-04  7.464812e-05 
##          EXPD          ESRX           XOM          FFIV           FDO 
##  1.572991e-03  8.769377e-05 -1.350066e-04  6.668839e-04  1.198428e-03 
##          FAST           FII           FDX           FIS          FITB 
##  1.790914e-04  2.717300e-04  7.694134e-04  9.430135e-04 -3.159547e-05 
##           FHN          FSLR            FE          FISV          FLIR 
##  7.474240e-04 -6.435458e-04 -1.572617e-04  1.145315e-03 -4.415454e-04 
##           FLS           FLR           FMC           FTI             F 
##  6.638396e-04  1.801691e-04  3.992752e-04 -8.977586e-04  5.126500e-04 
##           BEN           FCX           FTR           GME           GCI 
##  9.181936e-04 -1.720362e-04 -5.724679e-04 -4.373195e-04 -1.089928e-04 
##           GPS            GD            GE           GIS           GPC 
##  1.090688e-03 -4.075841e-04  9.195268e-04  5.754276e-05  2.317202e-04 
##           GNW          GILD            GS            GT           GWW 
##  2.894774e-03 -1.761789e-03  4.242078e-04  3.380319e-04  1.261472e-04 
##           HAL           HOG           HAR           HRS           HIG 
## -6.461778e-04  1.236025e-03  9.113230e-04  3.362443e-04  8.679953e-04 
##           HAS           HCP           HCN            HP           HES 
##  3.794128e-04  1.041982e-03  2.783344e-04 -2.908529e-04  3.806302e-05 
##           HPQ            HD           HON           HRL           HSP 
## -1.463892e-03  6.249965e-04  3.305640e-05  5.977666e-04  4.905132e-04 
##           HST          HCBK           HUM          HBAN           ITW 
##  1.583053e-03  9.106673e-04  3.596698e-04  5.763169e-04  6.281151e-04 
##           TEG          INTC           ICE           IBM           IFF 
## -1.253601e-03  8.224257e-04  4.326793e-04  6.219692e-04  7.553525e-04 
##           IGT            IP           IPG          INTU          ISRG 
##  2.566567e-03 -1.528122e-03  1.567118e-03  7.082690e-04  1.603251e-03 
##           IVZ           IRM           JBL           JEC           JNS 
##  1.755550e-05  2.384667e-04  1.162418e-03  3.349622e-04  8.010222e-04 
##          JDSU           JNJ           JCI           JOY           JPM 
##  5.422494e-04  4.333422e-05  6.222524e-04  6.109138e-04  9.214787e-04 
##          JNPR             K           KEY           KMB           KIM 
##  1.433820e-03  2.360502e-04  1.359491e-03 -8.168672e-04  1.172614e-03 
##          KLAC           KSS            KR           LLL            LH 
##  5.468667e-04 -6.182168e-04  5.182829e-04  4.943828e-04 -5.638959e-04 
##            LM           LEG           LEN           LUK           LXK 
##  4.501496e-04  2.171362e-04 -4.994333e-04  3.009207e-04 -1.293504e-03 
##          LIFE           LLY           LNC          LLTC           LMT 
## -4.704115e-02 -2.612942e-05  1.142118e-03  6.431292e-04  1.425199e-04 
##             L            LO           LOW           LSI           MTB 
##  3.763582e-04  1.333295e-03  1.863100e-04 -3.977719e-01  3.456091e-04 
##             M           MRO           MAR           MMC           MAS 
##  3.784148e-04 -6.338293e-04  1.860794e-03  7.436837e-04 -1.733605e-03 
##           ANR            MA           MAT           MKC           MCD 
## -3.328587e-03  2.755307e-04 -8.393263e-04  6.399191e-04  4.571696e-04 
##           MCK           MJN           MWV           MDT           MRK 
##  4.598714e-04  1.728943e-03  1.161270e-03  4.505784e-04  9.015928e-04 
##           MET          MCHP            MU          MSFT           TAP 
##  7.926219e-04  1.705738e-04 -1.138886e-04  1.375000e-03  1.081973e-03 
##           MON           MWW           MCO            MS           MOS 
##  3.308739e-04  9.780295e-04  6.626453e-04  5.243007e-04 -1.948333e-04 
##           MSI           MUR           MYL           NBR          NDAQ 
##  7.424499e-04 -4.092733e-04  2.269038e-04 -9.578654e-04  1.180500e-03 
##           NOV          NTAP          NFLX           NWL           NFX 
## -8.238651e-04  6.171110e-04  2.896492e-03  7.423029e-04 -1.081080e-03 
##           NEM           NEE           GAS           NKE            NI 
## -4.959459e-04  2.657241e-05 -1.554627e-05  6.293653e-04 -6.778012e-04 
##            NE           NBL           JWN           NSC          NTRS 
## -6.681765e-04  1.634225e-04  7.295255e-05 -3.202018e-04  4.395185e-04 
##           NOC            NU           CMG           NRG           NUE 
##  7.867680e-04  8.774813e-06  1.100196e-03 -5.248907e-04  1.810124e-04 
##          NVDA          ORLY           OXY           OMC           OKE 
##  1.242081e-03  2.246318e-04 -5.294373e-04  5.568116e-04 -8.529922e-04 
##          ORCL            OI          PCAR            IR           PLL 
##  1.647449e-04  9.434716e-04  6.923646e-04 -5.947089e-04  6.186959e-04 
##            PH          PDCO          PAYX           BTU           JCP 
##  8.400328e-04  1.093224e-03  1.127383e-03 -1.665656e-03 -1.208141e-03 
##          PBCT           POM           PEP           PKI           PFE 
##  6.279448e-04 -1.068111e-04  8.107985e-06  4.972008e-04  5.010735e-04 
##           PCG            PM           PNW           PXD           PBI 
##  1.301413e-04 -6.257807e-05 -1.454459e-04  4.100596e-04  1.470650e-04 
##           PCL           PNC            RL           PPG           PPL 
##  4.212717e-04  7.907004e-04  1.772839e-04  5.742643e-04 -4.029033e-04 
##            PX           PCP          PCLN           PFG            PG 
## -8.905026e-05  1.071538e-03  1.084433e-03  9.251154e-04 -1.996977e-04 
##           PGR           PLD           PRU           PEG           PSA 
##  1.012091e-03  5.488574e-04  8.592297e-04  1.539525e-04  3.372947e-05 
##           PHM           QEP           PWR          QCOM           DGX 
##  1.334435e-04 -1.927195e-03 -1.835849e-03 -2.198370e-05 -2.973636e-04 
##           RSH           RRC           RTN           RHT            RF 
## -3.958857e-04 -1.404813e-03  1.121241e-03  1.132675e-04  2.057091e-03 
##           RSG           RAI           RHI           ROK           COL 
##  7.426386e-04  1.008177e-03  8.996440e-04  5.680032e-04  6.153671e-04 
##           ROP          ROST           RDC             R           SWY 
##  8.113956e-04  9.737628e-04 -1.196449e-03 -1.164123e-04 -1.941718e-04 
##           CRM          SNDK           SCG           SLB           SNI 
##  6.637868e-04  1.334726e-03  4.347224e-05 -1.078531e-03  6.233985e-04 
##           SEE          SHLD           SRE           SHW          SIAL 
## -8.419709e-05  2.412348e-03 -2.713943e-04 -3.812535e-05 -6.302930e-05 
##           SPG           SLM           SJM           SNA            SO 
##  2.912570e-04  8.001919e-04 -4.508871e-04  8.441387e-04  1.584969e-05 
##           LUV           SWN            SE             S           STJ 
##  2.057005e-03 -1.482339e-03 -9.637667e-04  1.372658e-03  1.024661e-03 
##           SWK          SPLS          SBUX           HOT           STT 
##  6.210982e-04  3.513127e-04  6.422518e-04  1.630993e-03  6.725812e-04 
##          SRCL           SYK           STI           SVU          SYMC 
##  4.696345e-04 -3.673209e-04  7.790156e-04 -2.228410e-03  9.244253e-04 
##           SYY          TROW           TGT           TEL            TE 
##  5.438047e-04  2.575607e-04  5.705541e-04  1.013044e-03 -3.344185e-04 
##           THC           TDC           TER           TSO           TXN 
## -4.617426e-04  9.600376e-04  7.790887e-04 -5.214392e-04  1.114228e-04 
##           TXT           HSY           TRV           TMO           TIF 
##  1.502176e-03 -6.081409e-04  3.100453e-04  2.932616e-04  4.138642e-04 
##           TWX           TWC           TIE           TJX           TMK 
## -4.618701e-04  4.175481e-04 -3.136359e-01  9.260773e-04 -6.053002e-04 
##           TSS           TSN           TYC           USB           UNP 
##  4.914771e-04 -4.942492e-05 -7.618297e-04  7.157696e-04  1.346411e-04 
##           UNH           UPS             X           UTX           UNM 
##  1.814545e-04  1.053562e-03 -9.644693e-04  9.723152e-04  1.059740e-03 
##          URBN           VFC           VLO           VAR           VTR 
##  3.735884e-04 -3.693484e-04  6.455580e-04  9.195541e-04  4.633680e-04 
##          VRSN            VZ          VIAB             V           VNO 
## -1.197605e-04  5.024612e-04  2.975724e-04  5.383766e-04  5.757358e-04 
##           VMC           WMT           WAG           DIS            WM 
## -1.991789e-03  2.641982e-04 -1.519736e-04  1.124950e-03  5.208963e-05 
##           WAT           WLP           WFC           WDC            WU 
##  1.019392e-03  6.201881e-04  3.037750e-04 -8.551960e-05  5.488627e-04 
##            WY           WHR           WFM           WMB           WIN 
##  1.580056e-04 -4.736885e-04 -1.526429e-03 -1.572810e-03  8.166013e-04 
##           WEC           WYN          WYNN           XEL           XRX 
##  5.443529e-04  1.423680e-03  8.746912e-04  7.316964e-04  4.110637e-04 
##          XLNX            XL          YHOO           YUM           ZMH 
## -3.598142e-04  1.695696e-04 -3.305579e-04  1.038662e-03 -1.818347e-04 
##          ZION 
##  6.285299e-04
  1. Create a plot to show weights of the 1st principal component grouped by the industry sector (for example, you may draw a bar plot of mean weight per sector). Observe the distribution of weights (magnitudes, signs). Based on your observation, what kind of information do you think the 1st principal component might have captured?
library(knitr)
library(ggplot2)
p1 <- import.csv('C:/Users/pdiazq/Desktop/TareaAI/p1.csv')
df <- data.frame(p1, qty=p1[, 2], category=p1[ ,3])

ggplot(df, aes(x=category, y=qty)) + geom_bar(stat="identity")+ coord_flip()

The plot below, shows the main industries that contribute to the PCA principal component one.Stocks related with “Materials”" and “Information technologies”, were the most important for the numbers of the marked of stocks. As concluded in the temporal analysis of the Principal Component one, 5 drop events for the market were seen (two of them of bigger magnitude), and two events increasing the market value.

So I can conclude that the main reason for the total marked drop seen in those 4 years was    because the materials markets, but information technology contributed negatively to this      drop.
  1. Make a similar plot for the 2nd principal component. What kind of information do you think does this component reveal? (Hint: look at the signs and magnitudes.
library(knitr)
p2 <- import.csv('C:/Users/pdiazq/Desktop/TareaAI/p2.csv')

df2 <- data.frame(p2, qty=p2[, 2], category=p2[ ,3])

ggplot(df2, aes(x=category, y=qty)) + geom_bar(stat="identity")+ coord_flip()

The plot below, shows again the main contribution of Materials and Information Technology stocks, but almost inverted (according to the PC(one)). In fact, the temporal plot shows some other aditional information, compared with the aported by Component one. 3 main bigger drops and 4 increases in the market.

  1. Suppose we wanted to construct a new stock index using one principal component to track the overall market tendencies. Which of the two components would you prefer to use for this purpose, the 1st or the 2nd? Why?

    Two components could be complementary. Some information is a little similar but some other are a little different.

    Component one shows: More pronuncied drop, due to Materials and inhibited in a little proportion by information technology

    Component two shows: Not so pronuncied drop, due to information technology and inhibited in a big proportion by materials

    So in both cases, can be say that materiasl has a big impact in “drops”, and information technology has a not so notorious impact on “increasing”

    The PC1 shows this conclussion and I would choose this component, because has more weight.