R数据挖掘语法

时间:2012-04-19 20:39:32

标签: r statistics data-mining xts quantmod

我正在尝试运行Data Mining With R book提供的代码。它基本上采用SP500指数(GSPC)的报价数据,并建立预测函数(T.ind)来预测接下来n天的报价。

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.ind函数调用调用newTA()。如何将选定时段的引号值传递给T.ind函数。请告诉我。

1 个答案:

答案 0 :(得分:3)

这有点像格子或ggolot2图,但没有“+”符号。但是,相当于“特征添加”的操作是通过副作用传递的。该图不仅是2D显示,还是工作区中的对象。当您调用addT.ind()时,其效果将应用于当前活动的图表对象,该对象具有HLC()在隐含访问candleChart()产品结果的上下文中收集的数据。