[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
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)
this prints long columns of numbers (not what I expected) but at least I see a ticker symbol e.g:
2001-03-01  0.0000000000
2001-03-02  0.0037147103
2001-03-05  0.0088823094

However trying to plot:
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)

More information about the R-help mailing list