0
votes

My data, ret, includes 3 variables and 208 rows for each variable.

dim(ret)
[1] 208   3

I do the plot one by one for different variables.

hist(ret$AAPL,breaks
     freq=F,
     main="The probaility distribution of AAPL",
     xlab="AAPL")
hist(ret$GOOGL,breaks
     freq=F,
     main="The probaility distribution of GOOGL",
     xlab="GOOGL")
hist(ret$WMT,breaks
     freq=F,
     main="The probaility distribution of WMT",
     xlab="WMT")

Now,I try to use sapply function to plot variables separately in one time.

sapply(ret, function(x) hist(x,breaks=100,
                             freq=F,
                             main="the probaility distribution of x",
                             xlab="x"))

However, "main="the probaility distribution of x"" and "xlab=x" do not work. I try to put the colnames in x.

In addition, I also try to put the line in the plot together with variables. The function I use is

lines(density(,main="",lty=1,lwd=1) 

If I plot separately with variables, I do

hist(ret$AAPL,breaks
     freq=F,
     main="the probaility distribution of AAPL",
     xlab="AAPL")
lines(density(ret$AAPL),main="AAPL",lty=1,lwd=1)

But how to use sapply function to do it together? Could someone please tell me how to solve the problems: using sapply function to plot the probability distribution with the line of probability density for different variables.

2

2 Answers

0
votes

I would use some quantmod and ggplot2 packages for this task. Just ignore this if your question is really specific to hist and sapply.

library(ggplot2) # to access round_any
library(quantmod)

getSymbols(c("AAPL","GOOGL","WMT"))

A=data.frame('aapl',AAPL$AAPL.Close)
G=data.frame('goog',GOOGL$GOOGL.Close)
M=data.frame('wmt',WMT$WMT.Close)
names(A)=names(G)=names(M)=c('symbol','close')
d=rbind(A,G,M)

m <- ggplot(d, aes(x=close, colour=symbol, group=symbol))
m + geom_density(fill=NA)

enter image description here

or if you want a histogram

m <- ggplot(d, aes(x=close, colour=symbol, group=symbol))
m + geom_histogram(fill=NA,position='dodge')

enter image description here

0
votes

Something like this? I'm assuming your data.frame ret has returns, not prices, but the basic methodology should work in either case.

# set up example - you have this already...
library(quantmod)    # for getSymbols
symbols <- c("AAPL", "GOOG", "WMT")
ret     <- do.call(merge,lapply(symbols,function(s)dailyReturn(Cl(getSymbols(s,src="google",auto.assign=FALSE)))))
names(ret) <- symbols
ret     <- data.frame(date=index(ret), ret)
# you start here...
plot.hist <- function(x,s) {
  hist(x,breaks=100,freq=F,xlab=s,main=paste("Histogram of",s), xlim=0.1*c(-1,1))
  lines(density(x, na.rm=TRUE),lty=1,lwd=1)
}
par(mfrow=c(3,1))
mapply(plot.hist, ret[,2:4], symbols)

There are a few nuances here.

First, you want the stock symbol in the title, not the character string "x". To do that you need to use paste(...) as above.

Second, when using sapply(...) on a data.frame the columns are indeed passed to the function, but the column names are not. So we have to pass both. The easiest way to do this is with mapply(...) (read the docs).

Finally, as pointed out in the other answer, you can indeed use ggplot for this, which I also recommend:

library(ggplot2)
library(reshape2)    # for melt(...)
gg.df <- melt(ret, id="date", variable.name="Stock", value.name="Return")
ggplot(gg.df, aes(x=Return))+
  geom_histogram(aes(y=..density.., fill=Stock),color="grey80",binwidth=0.005)+
  stat_density(geom="line")+
  facet_grid(Stock~.)