Melt Function in Reshape 2

How do I get AMZN to show up in my melted database? I am using Ticker as the variable and VaR as the value name.

ALL.VAR$Portfolio <-c(PortVAR.Hist,PortVAR.Gaus,PortVAR.Mod)

ALL.VAR$Type <-c("Hist","Gaus","Mod")

head(ALL.VAR)
MSFT AAPL AMZN Portfolio Type
Hist -0.02270796 -0.02512269 -0.02805707 0.02327364 Hist
Gaus -0.02617325 -0.02759804 -0.03064730 0.02426414 Gaus
Mod -0.02134944 -0.02498954 -0.02516078 0.02067018 Mod

plotVar <- melt(ALL.VAR, variable.names("Ticker"), value.name = "VaR")

variable VaR
1 MSFT -0.0227079622823321
2 MSFT -0.0261732481640011
3 MSFT -0.021349444240091
4 AAPL -0.0251226908723899
5 AAPL -0.0275980440901959
6 AAPL -0.0249895390663036
AMZN should be here

Can you provide a reproducible example? Perhaps you should use pivot_longer in the tidyr package instead.

How much of the code do you think need to reproduce it? It is rather long and I was told too confusing for helpers on here if I post the entire code. I was told to edit it. Not being a jerk I am new to R and I am confused on how much code to post.

Just enough data to reproduce it. Maybe slightly more than what you had in your original post, but in a format that means that it can be worked on (as described in the link).

This makes it easier for others to help you with your answer and more likely that you will get an answer.

Every single step shows AMZN until the melt is used (look at the previous post). I hope I did this correctly...

library("quantmod")
library("PerformanceAnalytics")
library("ggplot2")
library("reshape2")

maxDate <-"2000-01-01"

MSFT.prices<-Ad(getSymbols("MSFT",auto.assign = FALSE, from=maxDate))
MSFT.rets<-dailyReturn(MSFT.prices)

VaR(MSFT.rets,p=0.95,method = "historical")
VaR(MSFT.rets, p=0.99, method="historical")
ES(MSFT.rets, p=0.99, method = "historical")

tickers <-c("MSFT","AAPL","AMZN")
weights <- c(.50,.10,.40)
getSymbols(tickers,from=maxDate)

Port.prices <- na.omit(merge(Ad(MSFT), Ad(AAPL),Ad(AMZN)))
Port.returns <- ROC(Port.prices, type = "discrete")[-1,]
colnames(Port.returns)<-tickers

Port.prices <- na.omit(merge(Ad(MSFT), Ad(AAPL),Ad(AMZN)))
Port.returns <- ROC(Port.prices, type = "discrete")[-1,]
colnames(Port.returns)<-tickers
VAR.Hist <- VaR(Port.returns, p=0.95, weights = NULL, portfolio_method = "single", method = "historical")

VAR.Gaus <- VaR(Port.returns, p=0.95, weights = NULL, portfolio_method = "single", method = "gaussian")
VAR.Mod <- VaR(Port.returns, p= 0.95, weights = NULL, portfolio_method = "single", method ="modified")

ALL.VARS <- data.frame(rbind(VAR.Hist, VAR.Gaus, VAR.MOD))
rownames(ALL.VARS)<-c("Hist","Gaus","Mod")

PortVAR.Hist <- VaR(Port.returns, p=0.95, weights = weights, portfolio_method = "component", method = "historical")$hVaR
PortVar.Gaus <- VaR(Port.returns, p=0.95, weights = weights, portfolio_method = "component", method = "gaussian")$VaR[1]
PortVar.Mod <- VaR(Port.returns, p=0.95, weights = weights, portfolio_method = "component", method = "modified")$MVaR[1]

ALL.VARS$Portfolio <- c(PortVAR.Hist,PortVar.Gaus,PortVar.Mod)
head(ALL.VARS$Portfolio)

ALL.VARS$Type <-c("Hist","Gaus","Mod")
head(ALL.VARS)

plotVar <-melt(ALL.VARS, variable.name = "Ticker", value.name = "VaR")
ggplot(plotVar, aes(x=Type,y=VaR, fill= Ticker))+geom_bar(stat="identity", position = "dodge")

`type or paste code here`

VAR.MOD This is a typographical error of Var.Mod which was defined on the previous line.

1 Like

Thank you so much! I really appreciate your assistance and apologize for my atrocious behavior the other day. It has been a very long and trying semester. Thank you again.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.