简体   繁体   English

quantmod :: chart_Series()bug?

[英]quantmod::chart_Series() bug?

I would like to chart SPX using quantmod::chart_Series() and below draw changes in GDP and 12 month SMA of changes of GDP. 我想使用quantmod :: chart_Series()绘制SPX图表,并在下面绘制GDP的变化和GDP变化的12个月SMA。 No matter how I try to do it (what combinations I use) eithe errors occur or quantmod::chart_Series() displays just partial plot. 无论我如何尝试(我使用什么组合)发生错误或quantmod :: chart_Series()只显示部分图。

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: 编辑:实际上,在我看来,这是使用季度数据时的quantmod :: chart_series()问题:

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 does produce SPX plot on main panel, but leaves empty second and third panel. 这确实在主面板上产生SPX图,但是留下空的第二和第三面板。 Then I tried to play around with having same index on data, 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 result is errors all over: 结果是错误:

> 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))

all looks good to me. 一切看起来都不错。

My sessionInfo(): 我的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  

Any ideas what might be the solution for these issues? 任何想法可能是这些问题的解决方案?

EDIT: This seems to be a quantmod::chart_Series() bug. 编辑:这似乎是一个quantmod :: chart_Series()错误。 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#510
13: xy.coords(x, y) at chart_Series.R#510
12: plot.xy(xy.coords(x, y), type = type, ...) at chart_Series.R#510
11: lines.default(ta.x, as.numeric(ta.y[, i]), col = col, ...) at chart_Series.R#510
10: lines(ta.x, as.numeric(ta.y[, i]), col = col, ...) at chart_Series.R#510
9: plot_ta(x = current.chob(), ta = get("x"), on = NA, taType = NULL, 
       col = 1) at replot.R#238
8: eval(expr, envir, enclos) at replot.R#238
7: eval(aob, env) at replot.R#238
6: FUN(X[[12L]], ...) at replot.R#230
5: lapply(x$Env$actions, function(aob) {
       if (attr(aob, "frame") > 0) {
           x$set_frame(attr(aob, "frame"), attr(aob, "clip"))
           env <- attr(aob, "env")
           if (is.list(env)) {
               env <- unlist(lapply(env, function(x) eapply(x, eval)), 
                   recursive = FALSE)
           }
           eval(aob, env)
       }
   }) at replot.R#230
4: plot.replot(x, ...)
3: plot(x, ...)
2: print.replot(<environment>)
1: print(<environment>)

Any ideas on how to get this fixed? 关于如何解决这个问题的任何想法?

I had a similar error several days ago. 几天前我有类似的错误。 I found that the problem was in add_TA with the line: 我发现问题出在add_TA的行中:

ta.x <- as.numeric(na.approx(ta.adj[, 1]))

na.approx uses approx with rule = 1 by default, which leaves trailing NAs in the list if the last timestamp in the original data is before the last timestamp in the TA data. na.approx默认使用约为rule = 1,如果原始数据中的最后一个时间戳位于TA数据中的最后一个时间戳之前,则会在列表中留下尾随的NA。 Changing that line to set rule = 2 fixed the problem. 更改该行以设置rule = 2可修复问题。

ta.x <- as.numeric(na.approx(ta.adj[, 1], rule=2))

I just wrote a long "answer" confirming your problems, even after some data massaging, and even using the older chartSeries function. 我写了一个很长的“答案”,确认你的问题,即使在一些数据按摩后,甚至使用旧的chartSeries功能。 Then I realized that add_TA() is perhaps the wrong function. 然后我意识到add_TA()可能是错误的功能。 This approach works: 这种方法有效:

par(mfrow=c(2,1))
chart_Series(SPX)
chart_Series(GDPC96)

(See R/quantmod: multiple charts all using the same y-axis for an alternative approach using the layout command.) (参见R / quantmod:使用相同的y轴的多个图表,使用layout命令替代方法。)

Or with the subset: 或者使用子集:

par(mfrow=c(2,1))
chart_Series(SPX,subset="2000/")
chart_Series(GDPC96,subset="2000/")

(NB. the two datasets end at different place, so don't quite line up.) (注意:两个数据集在不同的地方结束,所以不要排成一行。)

Incidentally, there is one definite bug in chart_Series with quarterly data: the x-axis labels look like "%n%b%n2010". 顺便提一下,chart_Series中有一个明确的错误,包含季度数据:x轴标签看起来像“%n%b%n2010”。

q.SPX=to.quarterly(SPX)
chart_Series(q.SPX)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM