2012-06-21 25 views
6

Tôi muốn biểu đồ SPX sử dụng quantmod :: chart_Series() và dưới đây rút ra những thay đổi về GDP và 12 tháng SMA thay đổi GDP. Không có vấn đề làm thế nào tôi cố gắng để làm điều đó (những gì kết hợp tôi sử dụng) eithe lỗi xảy ra hoặc quantmod :: chart_Series() chỉ hiển thị một phần cốt truyện.quantmod :: chart_Series() bug?

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: Trên thực tế, có vẻ như với tôi rằng đây là một quantmod :: chart_series() vấn đề khi sử dụng dữ liệu quý:

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 

này sản xuất SPX âm mưu trên bảng điều khiển chính, nhưng lá rỗng bảng thứ hai và thứ ba. Sau đó, tôi đã cố gắng để chơi xung quanh với việc có cùng một chỉ mục trên dữ liệu, cùng một độ dài, vv

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

Và kết quả là lỗi trên tất cả:

> 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 

Sử dụng

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

tất cả có vẻ tốt với tôi.

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 

Bất cứ ý tưởng gì có thể là giải pháp cho những vấn đề này?

EDIT: Đây có vẻ là lỗi quantmod :: chart_Series(). Nếu tôi làm điều này:

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

Mọi ý tưởng về cách khắc phục sự cố này?

Trả lời

3

Tôi vừa viết một câu trả lời "dài" xác nhận các vấn đề của bạn, ngay cả sau khi một số dữ liệu xoa bóp, và thậm chí sử dụng chức năng cũ hơn chartSeries. Sau đó, tôi nhận ra rằng add_TA() có lẽ là sai chức năng. Cách tiếp cận này hoạt động:

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

(Xem R/quantmod: multiple charts all using the same y-axis cho một cách tiếp cận khác bằng cách sử dụng lệnh layout.)

Hoặc với các tập hợp con:.

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

(NB hai bộ dữ liệu kết thúc ở vị trí khác nhau , do đó, không hoàn toàn xếp hàng.)

Ngẫu nhiên, có một lỗi nhất định trong biểu đồ_Series với dữ liệu hàng quý: nhãn x trục ok như "% n% b% n2010".

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

Làm việc tốt xung quanh.Re: lỗi định dạng nhãn trục, vấn đề là 'zoo ::: format.yearqtr' không hỗ trợ'% n' _conversion specification_. 'chart_Series' sử dụng' xts ::: axTicksByTime' sử dụng 'định dạng' chung. Vì 'to.quarterly' cho chỉ mục một lớp' yearqtr', 'format' gửi' format.yearqtr' bằng một chuỗi bao gồm '% n' (nhưng khác nhau tùy thuộc vào hệ điều hành của bạn). Một, thừa nhận không phải là tuyệt vời, cách để làm việc xung quanh điều này ở cấp độ người dùng là thay đổi lớp chỉ mục: 'indexClass (q.SPX) <-" Date "; chart_Series (q.SPX) ' – GSee

5

Tôi đã gặp lỗi tương tự vài ngày trước. Tôi thấy rằng vấn đề là ở add_TA với dòng:

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

na.approx sử dụng xấp xỉ với sự cai trị = 1 theo mặc định, mà lá NA trailing trong danh sách nếu timestamp cuối cùng trong dữ liệu gốc là trước khi người cuối cùng dấu thời gian trong dữ liệu TA. Thay đổi dòng đó để đặt quy tắc = 2 đã khắc phục được sự cố.

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

Áp dụng cho SVN trong [r581] (https://r-forge.r-project.org/scm/viewvc.php/pkg/R/chart_Series.R?view=markup&revision=581&root= quantmod). Cảm ơn @ Michael741. –

+0

Chào mừng bạn đến với SO, Maddogg! :) – GSee

+0

+1, Thật không may, nó vẫn không hoạt động với ví dụ đầu tiên của @ Samo khi SPX có tần suất cao hơn GDPC96. Tuy nhiên, chuyển đổi cả hai 'to.quarterly' là một công việc khá đơn giản. Ngoài ra, mã được vá vẫn còn ném rất nhiều cảnh báo từ 'as_numeric (H) 'xuất phát từ một hàm' parse.side' nằm trong '.parseISO8601'. (vâng, vâng, tôi biết, họ chỉ là cảnh báo) – GSee

Các vấn đề liên quan