Perform PCA

require(quantmod)
## Loading required package: quantmod
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## Version 0.4-0 included new data defaults. See ?getSymbols.
getSymbols(c("AMZN","GOOG","CSCO","FB","AAPL","IBM","MSFT","MMM","WMT","DD","V"))
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
## 
## This message is shown once per session and may be disabled by setting 
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
## pausing 1 second between requests for more than 5 symbols
##  [1] "AMZN" "GOOG" "CSCO" "FB"   "AAPL" "IBM"  "MSFT" "MMM"  "WMT"  "DD"  
## [11] "V"
db<-cbind(dailyReturn(AMZN),
dailyReturn(GOOG),
dailyReturn(CSCO),
dailyReturn(FB),
dailyReturn(AAPL),
dailyReturn(IBM),
dailyReturn(MSFT),
dailyReturn(MMM),
dailyReturn(WMT),
dailyReturn(DD),
dailyReturn(V))

names(db)<-c("AMZN","GOOG","CSCO","FB","AAPL","IBM","MSFT","MMM","WMT","DD","V")

db<-na.trim(db)

a<-data.frame(db)
r<-prcomp(a)
summary(r)
## Importance of components:
##                           PC1     PC2     PC3     PC4     PC5     PC6     PC7
## Standard deviation     0.0392 0.01987 0.01583 0.01355 0.01233 0.01097 0.01074
## Proportion of Variance 0.4948 0.12718 0.08069 0.05912 0.04893 0.03878 0.03714
## Cumulative Proportion  0.4948 0.62202 0.70271 0.76183 0.81076 0.84953 0.88667
##                             PC8      PC9     PC10     PC11
## Standard deviation     0.009917 0.009551 0.009379 0.008624
## Proportion of Variance 0.031670 0.029380 0.028330 0.023950
## Cumulative Proportion  0.918340 0.947720 0.976050 1.000000
plot(r)

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.