Tôi cố gắng để (lại) xây dựng một mô hình cơ bản dự báo chỉ số S & P 500 INDEX (orignates dữ liệu từ Yahoo Finance)R: Lỗi trong XTS - order.by
tôi chạy vào một số khó khăn với " đặt hàng "của tập dữ liệu của tôi.
Trong vóc dáng người data.model lỗi sau xảy ra
Lỗi trong XTS (new.x, x.index): NROW (x) phải phù hợp với chiều dài (order.by)
Sau khi một số nghiên cứu tôi nhận ra rằng vấn đề là với việc đặt hàng, và nó dường như thiếu thứ tự như là cần thiết cho gói sở thú cơ bản.
Có cách nào thanh lịch để giải quyết vấn đề này không ?! Cảm ơn trước
library(xts)
library(tseries)
library(quantmod)
GSPC <- as.xts(get.hist.quote("^GSPC",start="1970-01-02",
quote=c("Open", "High", "Low", "Close","Volume","AdjClose")))
head(GSPC)
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
}
myATR <- function(x) ATR(HLC(x))[, "atr"]
mySMI <- function(x) SMI(HLC(x))[, "SMI"]
myADX <- function(x) ADX(HLC(x))[, "ADX"]
myAroon <- function(x) aroon(x[, c("High", "Low")])$oscillator
myBB <- function(x) BBands(HLC(x))[, "pctB"]
myChaikinVol <- function(x) Delt(chaikinVolatility(x[, c("High", "Low")]))[, 1]
myCLV <- function(x) EMA(CLV(HLC(x)))[, 1]
myEMV <- function(x) EMV(x[, c("High", "Low")], x[, "Volume"])[, 2]
myMACD <- function(x) MACD(Cl(x))[, 2]
myMFI <- function(x) MFI(x[, c("High", "Low", "Close")], x[, "Volume"])
mySAR <- function(x) SAR(x[, c("High", "Close")])[, 1]
myVolat <- function(x) volatility(OHLC(x), calc = "garman")[, 1]
library(randomForest)
data.model <- specifyModel(T.ind(GSPC) ~ Delt(Cl(GSPC),k=1:10) +
myATR(GSPC) + mySMI(GSPC) + myADX(GSPC) + myAroon(GSPC) +
myBB(GSPC) + myChaikinVol(GSPC) + myCLV(GSPC) +
CMO(Cl(GSPC)) + EMA(Delt(Cl(GSPC))) + myEMV(GSPC) +
myVolat(GSPC) + myMACD(GSPC) + myMFI(GSPC) + RSI(Cl(GSPC)) +
mySAR(GSPC) + runMean(Cl(GSPC)) + runSD(Cl(GSPC)))
Cảm ơn rất nhiều, nó đã hoạt động! – Val