2012-06-21 45 views
6

我想使用quantmod :: chart_Series()及以下繪製SPX圖表繪製GDP的變化和12個月的GDP變化的SMA變化。不管我怎麼做(我使用什麼組合)都會出現錯誤,或者quantmod :: chart_Series()只顯示部分圖。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") 

編輯:其實,在我看來,這個使用季度數據時是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 

這不會產生主面板上的SPX的情節,但保留空第二和第三小組。 然後我試圖玩弄其上的數據相同的指數,相同長度等

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

而且結果是錯誤遍:

> 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 

使用

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

一切看起來不錯對我來說。

我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 

任何想法可能是這些問題的解決?

編輯:這似乎是一個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>) 

任何想法如何得到這個固定?

回答

3

我只是寫了長長的「答案」,確認您的問題,甚至一些數據後按摩,甚至使用舊chartSeries功能。然後我意識到add_TA()也許是錯誤的功能。這種方法的工作原理:

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

(使用layout命令的替代方法見R/quantmod: multiple charts all using the same y-axis。)

或者與該亞羣:

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

(NB兩個數據集在不同的地方結束,所以不太排隊)

順便說一句,不存在與季報數據chart_Series一個明確的錯誤:x軸標籤LO好像「%n%b%n2010」。

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

不錯的工作。Re:軸標籤格式化錯誤,問題是'zoo ::: format.yearqtr'不支持'%n' _conversion specification_。 'chart_Series'使用'xts ::: axTicksByTime',它使用通用的'格式'。由於'to.quarterly'給索引提供了一個'yearqtr'的類,所以'format'使用一個包含'%n'的字符串(但根據您的操作系統不同而不同)來調度'format.yearqtr'。其中一個原因不是很好,在用戶級別解決此問題的方法是更改​​索引類:'indexClass(q.SPX)< - 「Date」; chart_Series(q.SPX)' – GSee

5

幾天前我有類似的錯誤。我發現這個問題是add_TA與線:

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

na.approx使用約與規則= 1默認情況下,這讓尾隨的NA在列表中,如果在原始數據最後的時間戳是之前的最後一個時間戳記在TA數據中。更改該行以設置規則= 2解決了該問題。

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

在[r581]中應用於SVN(https://r-forge.r-project.org/scm/viewvc.php/pkg/R/chart_Series.R?view=markup&revision=581&root= quantmod)。謝謝@ Michael741。 –

+0

歡迎來到SO,Maddogg! :) – GSee

+0

+1,不幸的是,它仍然不適用於@Samo的第一個例子,其中SPX的頻率高於GDPC96。但是,將'to.quarterly'轉換爲相當簡單的解決方法。此外,修補後的代碼仍然會拋出'as_numeric(H)'的很多警告,該警告來自'.parseISO8601'本地的'parse.side'函數。 (是的,是的,我知道,他們只是警告) – GSee