2012-04-19 147 views
1

我試圖運行Data Mining With R book提供的代碼。它基本上採用SP500指數(GSPC)的報價數據,並建立一個預測函數(T.ind)來預測未來n天的報價。R數據挖掘語法

library(DMwR) 
#load S&P500 Dataset 
data(GSPC) 

# Create a Prediction function T based on which Buy/Sell/Hold decision 
# will be taken. target variation margin is 2.5% 
T.ind <- function(quotes,tgt.margin=0.025,n.days=10) { 
    v <- apply(HLC(quotes),1,mean) 
    r <- matrix(NA,ncol=n.days,nrow=NROW(quotes)) 

    for(x in 1:n.days) { 
    r[,x] <- Next(Delt(v,k=x),x) 
    } 

    x <- apply(r,1,function(x) sum(x[x > tgt.margin | x < -tgt.margin])) 

    if (is.xts(quotes)) 
    xts(x,time(quotes)) 
    else 
    x 
} 

#Plot candle chart for 3 months of Index with Avg. price and Parameter T. 
candleChart(last(GSPC,'3 months'),theme='white',TA=NULL) 

addAvgPrice <- newTA(FUN=avgPrice,col=1,legend='AvgPrice') 
addT.ind <- newTA(FUN=T.ind,col='red',legend='tgtRet') 
addT.ind() 

我的問題是如何T.indnewTA()函數調用調用。如何將選定期間的報價值傳遞給T.ind函數。請告訴我。

+1

在R提示符處輸入'addT.ind'並查看該函數。第三行顯示瞭如何調用T.ind。 – BenBarnes 2012-04-19 20:51:26

回答

3

這有點像格或ggolot2情節,但沒有「+」號。但是,等同於「特徵添加」的操作正在通過副作用傳遞。該圖不僅是一個2D顯示,而且也是工作區中的一個對象。當您調用addT.ind()時,其影響正在應用於當前活動的圖表對象,該圖表對象具有由HLC()在隱式訪問candleChart()產品結果的上下文中收集的數據。

+0

謝謝你的幫助 – Amey 2012-05-01 20:40:28