gpt4 book ai didi

r - 在 r 中预测 3 个相关结果不起作用

转载 作者:行者123 更新时间:2023-11-30 09:09:43 25 4
gpt4 key购买 nike

我有一个相当基本的模型,它将尝试预测第二天一只股票的交易量。不过,我想预测所有三只股票。因此,结果不是一种,而是三种。

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

最佳答案

您需要按以下方式更改代码(从第 #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"

关于r - 在 r 中预测 3 个相关结果不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42765688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com