An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20110922/b053165d/attachment.pl>
problem with chart.TimeSeries()
3 messages · Eric Zivot, Brian G. Peterson, Gabor Grothendieck
On Thu, 2011-09-22 at 09:38 -0700, Eric Zivot wrote:
I think there is a problem with the yearmon class in zoo when running R 2.13 and higher. For a class project, I wanted students to make a "growth of a $1" plot. Here is some sample code that didn't work in R 2.13.1 (works in R 2.12.2, however)
This works for me in R 2.13.1:
######
require(PerformanceAnalytics)
require(quantmod)
getSymbols('vfinx',from="2006-05-01",to="2011-05-30")
head(VFINX)
ReturnsSimple.z = Return.calculate(
prices= VFINX$VFINX.Adjusted ,
method='simple'
)
chart.CumReturns(
ReturnsSimple.z,
wealth.index=TRUE,
legend.loc='topleft',
lwd=2,main='Growth of $1'
)
#######
Make sure you have the latest version of zoo/xts, there was some
ugliness lately in as.Date between zoo/xts and R-base.
Also, you'll note that my example uses the newer getSymbols rather than
get.hist.quote, and returns an xts object directly, avoiding one
conversion step, so skipping your time()->Date() index conversion
entirely.
Regards,
- Brian
Brian G. Peterson http://braverock.com/brian/ Ph: 773-459-4973 IM: bgpbraverock
On Thu, Sep 22, 2011 at 12:38 PM, Eric Zivot <ezivot at u.washington.edu> wrote:
I think there is a problem with the yearmon class in zoo when running R 2.13 and higher. For a class project, I wanted students to make a "growth of a $1" plot. Here is some sample code that didn't work in R 2.13.1 (works in R 2.12.2, however)
options(digits=4, width=70)
# load packages
library("PerformanceAnalytics")
library("tseries")
start.date = "2006-05-01"
end.date = "2011-05-30"
vfinx.prices = get.hist.quote(instrument="vfinx", start=start.date,
+ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?end=end.date, quote="AdjClose", + ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?provider="yahoo", origin="1970-01-01", + ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?compression="m", retclass="zoo") trying URL 'http://chart.yahoo.com/table.csv?s=vfinx&a=4&b=01&c=2006&d=4&e=30&f=2011&g= m&q=q&y=0&z=vfinx&x=.csv' Content type 'text/csv' length unknown opened URL downloaded 3007 bytes time series ends ? 2011-05-02
# change time indices to class yearmon, which is most appropriate for
monthly data
index(vfinx.prices) = as.yearmon(index(vfinx.prices))
ReturnsCC.z = diff(log(vfinx.prices))
ReturnsSimple.z = exp(ReturnsCC.z) - 1
chart.CumReturns(ReturnsSimple.z, wealth.index=TRUE, legend.loc="topleft",
+ ? ? ? ? ? ? ? ? ?lwd=2, main="growth of $1") Error in as.Date.default(time(y)) : ?do not know how to convert 'time(y)' to class "Date"
traceback()
5: stop(gettextf("do not know how to convert '%s' to class \"Date\"",
? ? ? deparse(substitute(x))))
4: as.Date.default(time(y))
3: as.Date(time(y))
2: chart.TimeSeries(Return.cumulative, col = colorset, legend.loc =
legend.loc,
? ? ? ...)
1: chart.CumReturns(ReturnsSimple.z, wealth.index = TRUE, legend.loc =
"topleft",
? ? ? lwd = 2, main = "growth of $1")
I am able to reproduce this bug using zoo 1.7-4. Its now fixed this
in the development version so you can either use the development
version of zoo (svn version 897 or later) or else just add this
statement to the above code for now:
source("http://r-forge.r-project.org/scm/viewvc.php/*checkout*/pkg/zoo/R/yearmon.R?revision=897&root=zoo")
Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com