quantmod::chart_Series()错误?

quantmod::chart_Series()错误?,r,finance,xts,quantmod,R,Finance,Xts,Quantmod,我想使用quantmod::chart_Series()和下面的图表绘制SPX,绘制GDP变化和12个月GDP变化的SMA。无论我如何尝试(使用何种组合),无论是发生错误还是quantmod::chart_Series()只显示部分绘图 require(quantmod) FRED.symbols <- c("GDPC96") getSymbols(FRED.symbols, src="FRED") SPX <- getSymbols("^GSPC", auto.assign=F

我想使用quantmod::chart_Series()和下面的图表绘制SPX,绘制GDP变化和12个月GDP变化的SMA。无论我如何尝试(使用何种组合),无论是发生错误还是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")
结果是到处都是错误:

> 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
使用

我觉得一切都很好

My sessionInfo():

有什么办法可以解决这些问题吗

编辑:这似乎是一个quantmod::chart_Series()错误。如果我这样做:

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>)
子集回溯()
14:停止(“x”和“y”长度不同”)在图表_系列。R#510
13:xy.coords(x,y)在chart_系列。R#510
12:chart_Series.R#510上的plot.xy(xy.coords(x,y),type=type,…)
11:chart_Series.R#510处的lines.default(ta.x,as.numeric(ta.y[,i]),col=col,…)
10:chart_Series.R#510处的线条(ta.x,as.numeric(ta.y[,i]),col=col,…)
9:plot_ta(x=current.chob(),ta=get(“x”),on=NA,taType=NULL,
col=1)在回复件R#238处
8:replot.R#238处的评估(expr、envir、enclose)
7:回复R#238处的评估(aob、环境)
6:replot.R#230上的乐趣(X[[12L]],…)
5:lappy(x$Env$动作,功能(aob){
如果(属性(aob,“帧”)>0){
x$set_frame(attr(aob,“frame”)、attr(aob,“clip”))
env我刚刚写了一个很长的“答案”来确认您的问题,即使在经过一些数据处理之后,甚至使用了较旧的
chartSeries
函数。然后我意识到
add_TA()
可能是错误的函数。这种方法有效:

par(mfrow=c(2,1))
chart_Series(SPX)
chart_Series(GDPC96)
(有关使用
布局
命令的替代方法,请参阅。)

或使用子集:

par(mfrow=c(2,1))
chart_Series(SPX,subset="2000/")
chart_Series(GDPC96,subset="2000/")
(注意,这两个数据集在不同的地方结束,所以不要排成一行。)

顺便说一句,季度数据图表_系列中有一个明确的错误:x轴标签看起来像“%n%b%n2010”


几天前我遇到了类似的错误。我发现问题出在以下行的附加部分:

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

ta.x应用于SVN in.。感谢@Michael741.+1,不幸的是,它仍然不适用于@Samo的第一个示例,其中SPX的频率高于GDPC96。但是,将这两个
转换为.quarterly
是一个非常简单的解决方法。此外,修补后的代码仍然以数字(H)的形式从
发出大量警告
来自
parse.side
函数,该函数是
.parseISO8601
的本地函数(是的,是的,我知道,它们只是警告)很好的解决方法。Re:axis标签格式错误,问题是
zoo:::format.yearqtr
不支持
%n
转换规范。
chart\u Series
使用
xts::axTicksByTime
使用通用
格式。因为
to.quarterly
给索引一个
yearq>类tr
format
使用包含
%n
的字符串分派
format.yearqtr
(但根据您的操作系统而有所不同)。在用户级别解决此问题的一种方法是更改索引类:
indexClass(q.SPX)
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>)
par(mfrow=c(2,1))
chart_Series(SPX)
chart_Series(GDPC96)
par(mfrow=c(2,1))
chart_Series(SPX,subset="2000/")
chart_Series(GDPC96,subset="2000/")
q.SPX=to.quarterly(SPX)
chart_Series(q.SPX)
ta.x <- as.numeric(na.approx(ta.adj[, 1]))
ta.x <- as.numeric(na.approx(ta.adj[, 1], rule=2))