2012-06-21 3 views
6

quantmod :: chart_Series()를 사용하여 SPX를 차트로 작성하려고합니다. 아래에서는 GDP의 변화와 12 개월 SMA의 GDP 변화를 보여줍니다. 아무리 노력해도 (어떤 조합을 사용합니까?) 오류가 발생하거나 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() 문제가 있음을 날 것으로 보인다 :이 메인 패널의 SPX 플롯을 생산 않습니다

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 

을하지만, 빈 잎 두 번째 및 세 번째 패널.

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

사용 : 가 그럼 난 같은 등 데이터에 대한 인덱스, 같은 길이

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

그리고 결과를 갖는 주위를 재생하려고 온통 오류입니다 나에게.

내 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

멋진 해결.다시 : 축 레이블 서식 버그, 문제는'zoo ::: format.yearqtr'는'% n' _conversion specification_을 지원하지 않는다는 것입니다. 'chart_Series'는 일반적인'format'을 사용하는'xts ::: axTicksByTime'을 사용합니다. 'to.quarterly'는 인덱스에'yearqtr'의 클래스를 부여했기 때문에'format'은'% n'을 포함하는 문자열로 format.yearqtr을 전달합니다 (OS에 따라 다름). 사용자 수준에서이 문제를 해결하는 방법은 인덱스 클래스를 변경하는 것입니다.'indexClass (q.SPX) <- "Date"; chart_Series (q.SPX)' – GSee

5

나는 몇일 전에 비슷한 오류가 발생했습니다.

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

na.approx이 = 약 규칙을 사용하여 기본적으로 1, 원본 데이터의 마지막 타임 스탬프가 마지막 전의 경우는리스트 대열의 NA 잎 : 나는 문제가 라인 add_TA 있다는 발견 TA 데이터의 타임 스탬프. rule = 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

에 오신 것을 환영합니다, Maddogg! :) – GSee

+0

+1, 불행히도, SPX가 GDPC96보다 높은 주파수 인 @ Samo의 첫 번째 예제에서는 여전히 작동하지 않습니다. 그러나'to.quarterly '를 변환하는 것은 매우 간단합니다. 또한, 패치 된 코드는 여전히'.parseISO8601'에 국한된'parse.side' 함수에서 오는'as_numeric (H)'로부터 많은 경고를 던집니다. (예, 예, 알겠습니다. 단지 경고 일뿐입니다) – GSee