[R] scatterplotting stock returns using quantmod and pairs()
alan lapedes
asl87501 at earthlink.net
Wed Dec 7 01:36:35 CET 2011
I want to get data for a set of ticker symbols and compute the daily return of the adjusted close using quantmod, and then scatterplot returns using pairs().
The following gets data for the list of tickers:
tickers <- c("SHY","TLT","SPY","IWM","GLD","IEV","ILF","EWJ","EPP","SAF","ASA")
AdjClosePrices <- do.call(merge, lapply(tickers, function(x) Ad(get(x)))) #get adjusted close prices
First try at getting returns and plotting:
AdjCloseReturns <- do.call(merge, lapply(tickers, function(x) dailyReturn(Ad(get(x))) ))
But the resulting pairs plot
pairs(data.matrix(AdjCloseReturns),cex=0.01)
has uninformative text "daily.returns" down the diag on the plot.
Second try at getting returns and plotting: using lapply instead
AdjCloseReturns <- lapply(AdjClosePrices,dailyReturn)
head(AdjCloseReturns)
this prints long columns of numbers (not what I expected) but at least I see a ticker symbol e.g:
$IEV.Adjusted
daily.returns
2001-03-01 0.0000000000
2001-03-02 0.0037147103
2001-03-05 0.0088823094
However trying to plot:
pairs(data.matrix(AdjCloseReturns),cex=0.01)
gives error:
Error in pairs.default(data.matrix(AdjCloseReturns), cex = 0.01) :
non-numeric argument to 'pairs'
Any insight would be appreciated: I want to scatterplot the returns for a set of tickers using pairs() with the ticker symbol info down the diag (and would be nice to also have scatterplot on lower panel and the numerical correlation on upper panel)
Thanks!
More information about the R-help
mailing list