I would like to draw SPX using quantmod :: chart_Series () and below make changes in GDP and 12 months of SMA GDP changes. Regardless of how I try to do this (which combinations I use), errors occur or a quantum :: chart_Series () displays only a partial graph.
require(quantmod) FRED.symbols <- c("GDPC96") getSymbols(FRED.symbols, src="FRED") SPX <- getSymbols("^GSPC", auto.assign=FALSE, from="1900-01-01") subset="2000/" chart_Series(SPX, subset=subset) add_TA(GDPC96) add_TA(ROC(GDPC96, type="discrete")) add_TA(SMA(ROC(GDPC96, type="discrete"), n=4), on=3, col="blue")
EDIT: Actually, it seems to me that this is a quantmod :: chart_series () problem when using quarterly data:
subset <- "2000/" chart_Series(to.quarterly(SPX, drop.time=TRUE), subset=subset) add_TA(SMA(Cl(to.quarterly(SPX, drop.time=TRUE)))) > subset <- "2000/" > chart_Series(to.quarterly(SPX, drop.time=TRUE), subset=subset) > add_TA(SMA(Cl(to.quarterly(SPX, drop.time=TRUE)))) Error in xy.coords(x, y) : 'x' and 'y' lengths differ In addition: Warning messages: 1: In as_numeric(H) : NAs introduced by coercion 2: In as_numeric(H) : NAs introduced by coercion 3: In as_numeric(H) : NAs introduced by coercion
This creates an SPX graph on the main panel, but leaves the second and third panels blank. Then I tried to play with the same index on the data, the same lengths, etc.
chart_Series(head(to.quarterly(SPX, drop.time="TRUE"), -1), subset=subset) add_TA(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE)) add_TA(ROC(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE), type="discrete")) add_TA(SMA(ROC(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE), type="discrete"), n=4), on=3, col="blue")
And the result is errors in everything:
> chart_Series(head(to.quarterly(SPX, drop.time="TRUE"), -1), subset=subset) > add_TA(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE)) Error in xy.coords(x, y) : 'x' and 'y' lengths differ In addition: Warning messages: 1: In as_numeric(H) : NAs introduced by coercion 2: In as_numeric(H) : NAs introduced by coercion 3: In as_numeric(H) : NAs introduced by coercion > add_TA(ROC(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE), type="discrete")) Error in xy.coords(x, y) : 'x' and 'y' lengths differ In addition: Warning messages: 1: In as_numeric(H) : NAs introduced by coercion 2: In as_numeric(H) : NAs introduced by coercion 3: In as_numeric(H) : NAs introduced by coercion > add_TA(SMA(ROC(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE), type="discrete"), n=4), on=3, col="blue") Error in xy.coords(x, y) : 'x' and 'y' lengths differ In addition: Warning messages: 1: In as_numeric(H) : NAs introduced by coercion 2: In as_numeric(H) : NAs introduced by coercion 3: In as_numeric(H) : NAs introduced by coercion
Using
tail(to.quarterly(SPX, drop.time="TRUE")) tail(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE)) tail(ROC(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE), type="discrete")) tail(SMA(ROC(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE), type="discrete"), n=4)) dput(to.quarterly(SPX, drop.time="TRUE")) dput(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE)) dput(ROC(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE), type="discrete")) dput(SMA(ROC(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE), type="discrete"), n=4))
everything looks good to me.
My sessionInfo ():
> sessionInfo() R version 2.15.0 (2012-03-30) Platform: x86_64-pc-linux-gnu (64-bit) locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=en_US.UTF-8 LC_NAME=en_US.UTF-8 [9] LC_ADDRESS=en_US.UTF-8 LC_TELEPHONE=en_US.UTF-8 [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] quantmod_0.3-18 TTR_0.21-0 xts_0.8-7 zoo_1.7-7 [5] Defaults_1.1-1 rj_1.1.0-4 loaded via a namespace (and not attached): [1] grid_2.15.0 lattice_0.20-0 tools_2.15.0
What ideas can solve these problems?
EDIT: This seems to be a quantmod :: chart_Series () error. If I do this:
subset <- "1990/" test <- cbind(head(to.quarterly(SPX, drop.time="TRUE"), -1)[subset], to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE)[subset], ROC(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE), type="discrete")[subset], SMA(ROC(to.quarterly(GDPC96, drop.time="TRUE", OHLC=FALSE), type="discrete"), n=4)[subset]) test$test <- 1 subset <- "2000/" chart_Series(OHLC(test), subset=subset) add_TA(test$test) add_TA(test$GDPC96) > test$test <- 1 > subset <- "2000/" > chart_Series(OHLC(test), subset=subset) > add_TA(test$test) Error in xy.coords(x, y) : 'x' and 'y' lengths differ In addition: Warning messages: 1: In as_numeric(H) : NAs introduced by coercion 2: In as_numeric(H) : NAs introduced by coercion 3: In as_numeric(H) : NAs introduced by coercion > add_TA(test$GDPC96) Error in xy.coords(x, y) : 'x' and 'y' lengths differ In addition: Warning messages: 1: In as_numeric(H) : NAs introduced by coercion 2: In as_numeric(H) : NAs introduced by coercion 3: In as_numeric(H) : NAs introduced by coercion > traceback() 14: stop("'x' and 'y' lengths differ") at chart_Series.R
Any ideas on how to get this fix?