[R] scatterplotting stock returns using quantmod and pairs()
R. Michael Weylandt
michael.weylandt at gmail.com
Wed Dec 7 16:29:51 CET 2011
The names of the list entries are not the same as the column names of
the elements of the list (if that makes any sense)...you need to add
them manually after the merge: this works
library(quantmod)
tickers <- c("SHY","TLT","SPY","IWM","GLD","IEV")
getSymbols(tickers)
AdjCloseReturns <- do.call(merge, lapply(tickers, function(x)
dailyReturn(Ad(get(x)))))
names(AdjCloseReturns) <- tickers
pairs(as.matrix(AdjCloseReturns))
Michael
On Tue, Dec 6, 2011 at 7:36 PM, alan lapedes <asl87501 at earthlink.net> wrote:
> 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!
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list