预测r不工作中的3个依赖结果

时间:2017-03-13 14:09:32

标签: r machine-learning r-caret quantmod

我有一个相当基本的模型,试图在第二天预测一只股票的数量。但是,我想预测所有三只股票。因此,有三个结果,而不是一个结果。

outcomeSymbol <- cbind('AAPL.Volume','ADBE.Volume','ADI.Volume')

以下是结果的结果(按随机顺序排列日期):

enter image description here

以下是适用于一个结果变量(outcomeSymbol <- 'AAPL.Volume')的培训:

bst <- train(train[,predictorNames],  as.factor(train$outcome),
             method='gbm'
)

但是当用3个结果变量运行时,我得到:
 Error: nrow(x) == n is not TRUE

如果有多个结果,是否必须使用不同的参数或不同的模型?

整个代码,所以你可以看到所有内容并自己运行: https://gist.github.com/alteredorange/b97481ed7e00b33bab0d28dcdd7d0e4a

1 个答案:

答案 0 :(得分:1)

您需要按以下方式更改代码(从第63行到第78行):

set.seed(1234)
split <- sample(nrow(nasdaq100), floor(0.7*nrow(nasdaq100)))

# process the outcome variables for the entire data 
nasdaq100$outcome <- ifelse(nasdaq100$outcome==1,'yes','nope')
nasdaq100$outcome <- sapply(as.data.frame(nasdaq100$outcome), function(x) as.factor(x)) 

train <-nasdaq100[split,]
test <- nasdaq100[-split,]

# learn 3 different models, one for each outcome variable
bst <- lapply(1:3, function(i) train(train[,predictorNames],train$outcome[,i],method='gbm'))

# compute ROC separately for 3 of the models
library(pROC)
auc <- lapply(1:3, function(i) {
  predictions <- predict(object=bst[[i]], test[,predictorNames], type='prob')
  auc(test$outcome[,i],predictions[,2])
})

# auc scores for 3 models
print(paste('AUC score:', auc)) 
# [1] "AUC score: 0.662664263875109" "AUC score: 0.698058147615867" "AUC score: 0.719709083058406"