I am using quantmod and Performance Analytics to complete this...
maxDate <-"2000-01-01"
> AMZN.prices<-Ad(getSymbols("AMZN",auto.assign = FALSE, from=maxDate))
> AMZN.rets<-dailyReturn(AMZN.prices)
> VaR(AMZN.rets,p=0.95,method = "historical")
daily.returns
VaR -0.04416111
> VaR(AMZN.rets, p=0.99, method="historical")
daily.returns
VaR -0.08969521
> ES(AMZN.rets, p=0.99, method = "historical")
daily.returns
ES -0.1225124
> tickers <-c("AMZN","FB","MSFT")
> weights <- c(.50,.10,.40)
> getSymbols(tickers,from=maxDate)
[1] "AMZN" "FB" "MSFT"
> Port.prices <- na.omit(merge(Ad(AMZN), Ad(FB),Ad(MSFT)))
> Port.returns <- ROC(Port.prices, type = "discrete")[-1,]
> colnames(Port.returns)<- tickers
> head(Port.returns)
AMZN FB MSFT
2012-05-21 0.019920481 -0.10986139 0.0163992312
2012-05-22 -0.012745858 -0.08903906 0.0003357195
2012-05-23 0.009055854 0.03225806 -0.0218411670
2012-05-24 -0.009388779 0.03218747 -0.0013741794
2012-05-25 -0.010918073 -0.03390854 -0.0003442763
2012-05-29 0.008736911 -0.09620809 0.0172060378
> VaR(Port.returns, p=0.95, weights = weights, portfolio_method = "component", method = "modified")
$MVaR
[1] 0.02098371
$contribution
AMZN FB MSFT
0.011423717 0.002670831 0.006889165
$pct_contrib_MVaR
AMZN FB MSFT
0.5444088 0.1272811 0.3283101
> VAR.Hist <- VaR(Port.returns, p=0.95, weights = NULL, portfolio_method = "single", method = "historical")
> head(VAR.Hist)
AMZN FB MSFT
VaR -0.02650218 -0.03285992 -0.02232063
> VAR.Gaus <- VaR(Port.returns, p=0.95, weights = NULL, portfolio_method = "single", method = "gaussian")
> head(VAR.Gaus)
AMZN FB MSFT
VaR -0.029776 -0.03751721 -0.02566986
> VAR.MOD <- VaR(Port.returns, p= 0.95, weights = NULL, portfolio_method = "single", method ="modified")
> head(VAR.MOD)
AMZN FB MSFT
VaR -0.02439567 -0.02107829 -0.02154536
> ALL.VARS <- data.frame(rbind(VAR.Hist, VAR.Gaus, VAR.MOD))
> rownames(ALL.VAR) <- c("Hist", "Gaus", "MOD")
> head(ALL.VAR)
AMZN FB MSFT
Hist -0.02650218 -0.03285992 -0.02232063
Gaus -0.02977600 -0.03751721 -0.02566986
MOD -0.02439567 -0.02107829 -0.02154536
> **PortVAR.Hist <- VaR(Port.returns, p=0.95, weights = weights, portfolio_method = "component", method = "historical")[1]**
**> head(PortVAR.Hist)**
**$hVaR** I need to get this to just the number and I have no idea how to
** hVaR 95% ** do it. I was able to figure it out for the Gaus and Modified. I just
**0.02330049** cannot get this one corrected.
> PortVar.Gaus <- VaR(Port.returns, p=0.95, weights = weights, portfolio_method = "component", method = "gaussian")$VaR[1]
> head(PortVar.Gaus)
[1] 0.02402974
> PortVar.Mod <- VaR(Port.returns, p=0.95, weights = weights, portfolio_method = "component", method = "modified")$MVaR[1]
> head(PortVar.Mod)
[1] 0.02098371
> PortVAR.Hist <- VaR(Port.returns, p=0.95, weights = weights, portfolio_method = "component", method = "historical")
> head(PortVAR.Hist)
$hVaR
hVaR 95%
0.02330049
$contribution
AMZN FB MSFT
-0.0076427240 -0.0009305948 -0.0030769242
$pct_contrib_hVaR
AMZN FB MSFT
0.65601412 0.07987772 0.26410816
> PortVar.Gaus <- VaR(Port.returns, p=0.95, weights = weights, portfolio_method = "component", method = "gaussian")$VaR[1]
> head(PortVar.Gaus)
[1] 0.02402974
> PortVar.Mod <- VaR(Port.returns, p=0.95, weights = weights, portfolio_method = "component", method = "modified")$MVaR[1]
> head(PortVar.Mod)
[1] 0.02098371
> ALL.VARS$Portfolio <- c(PortVAR.Hist,PortVar.Gaus,PortVar.Mod)
Error in `$<-.data.frame`(`*tmp*`, Portfolio, value = list(hVaR = c(`hVaR 95%` = 0.0233004861003799), :
replacement has 5 rows, data has 3