我有一个代码,可以预测未来返回的标志的变化。
library(quantmod)
library(PerformanceAnalytics)
library(forecast)
library(e1071)
library(caret)
library(kernlab)
library(dplyr)
library(roll)
# get data yahoo finance
getSymbols("^GSPC", from = "1990-01-01", to = "2017-12-01")
# take logreturns
rnull <- CalculateReturns(prices = GSPC$GSPC.Adjusted ,method ="log")
# lags 1, 2, 3, 4, 5 as features
feat <- merge(na.trim(lag(rnull,1)),na.trim(lag(rnull,2)),na.trim(lag(rnull,3)),na.trim(lag(rnull,4)),na.trim(lag(rnull,5)),all=FALSE)
# create dataset. 6th column is actural. Previous is lagged
dataset <- merge(feat,rnull,all=FALSE)
# set columns' names
colnames(dataset) = c("lag.1", "lag.2", "lag.3","lag.4","lag.5","TARGET")
# get signs and make a data.frame
x <- sign(dataset)%>%as.data.frame
# exclude 0 sign and assume that these values are positive
x[x==0] <- 1
# for svm purposes we need to set dependent variable as factor and make levels to interpretation
x$TARGET <- as.factor(as.character(x$TARGET))
levels(x$TARGET) <- list(positive = "1", negative = "-1")
# divide sample to training and test subsamples
trainindex <- x[1:5792,]
testindex <- x[5792:7030,]
# run svm
svmFit <- ksvm(TARGET~.,data=trainindex,type="C-svc",kernel= "rbfdot")
# prediction
predsvm <- predict(svmFit, newdata=testindex)
# results
confusionMatrix(predsvm, testindex$TARGET)
我要做的接下来的事情是在我的模型中添加一个滚动窗口(1步预测(。但是,基本方法作为rollapply不适用于数据框架。时间序列的一个步骤预测的孔隙方法也不适用于data。在e1071软件包中使用的帧。我写了以下功能:
svm_next_day_prediction <- function(x){
svmFit <- svm(TARGET~., data=x)
prediction <- predict(object = svmFit, newdata = tail(x,1) )
return(prediction)
}
apl = rollapplyr(data = x, width = 180, FUN = svm_next_day_prediction, by.column = TRUE)
但收到错误,因为Rollapply不了解数据。
terms.formula中的错误(公式,data = data(:'。'在公式和 没有"数据"参数
您可以解释如何使用数据框应用SVM分类模型的滚动窗口?
几点
-
rollapply
与可以将矩阵胁迫的数据帧配合使用,因此请确保您的输入完全是数字 - 而不是数字和因子的混合。例如,这使用具有两个数字列的内置数据框架BOD
进行工作。请注意,x
传递给pred
是矩阵。pred <- function(x) predict(svm(demand ~ Time, x)) rollapplyr(BOD, 3, FUN = pred, by.column = FALSE)
给予
## 1 2 3 ## [1,] 8.868888 10.86889 17.25474 ## [2,] 11.661666 17.24870 16.00000 ## [3,] 18.328435 16.18583 15.78583 ## [4,] 16.230474 15.83247 19.56886
-
我无法复制您遇到的错误。我有不同的错误。
-
问题中的代码具有
by.column = TRUE
(无论如何,这是默认值(但这是由于仅将一个向量传递给函数的结果不是你想要的。您想要by.column = FALSE
。
尝试以下操作:
x0 <- data.matrix(x)
rollapplyr(data = x0, width = 180, FUN = svm_next_day_prediction, by.column = FALSE)
您可以使用单个数据帧创建列表,然后应用您的功能。我将x重命名为df以避免混乱:
df=x
rowwindow=179
dfList=lapply(1:(nrow(df)-rowwindow),function(x) df[x:(rowwindow+x),])
result=sapply(dfList,svm_next_day_prediction)