我有一个像这样组织的数据集:
library(lubridate)
library(e1071)
library(rpart)
library(pROC)
library(rpart.plot)
library(RColorBrewer)
library(ada)
library(maboost)
library(adabag)
library(ROCR)
library(data.table)
> head(crypto_data)
time btc_price eth_price block_size difficulty estimated_btc_sent estimated_transaction_volume_usd hash_rate
1: 2017-09-02 21:54:00 4537.834 330.727 142521291 8.88e+11 2.04e+13 923315360 7417412092
2: 2017-09-02 22:29:00 4577.605 337.804 136524566 8.88e+11 2.03e+13 918188067 7152504517
3: 2017-09-02 23:04:00 4566.360 336.938 134845546 8.88e+11 2.01e+13 910440916 7240807042
4: 2017-09-02 23:39:00 4590.031 342.929 133910638 8.88e+11 1.99e+13 901565930 7284958305
5: 2017-09-03 00:14:00 4676.193 354.171 130678099 8.88e+11 2.01e+13 922422228 7152504517
6: 2017-09-03 00:49:00 4699.936 352.299 127557140 8.88e+11 1.99e+13 910457430 7064201992
miners_revenue_btc miners_revenue_usd minutes_between_blocks n_blocks_mined n_blocks_total n_btc_mined n_tx nextretarget total_btc_sent
1: 2395 10839520 8.00 168 483207 2.10e+11 241558 483839 1.62e+14
2: 2317 10482320 8.33 162 483208 2.03e+11 236661 483839 1.60e+14
3: 2342 10596900 8.22 164 483216 2.05e+11 238682 483839 1.60e+14
4: 2352 10642439 8.14 165 483220 2.06e+11 237159 483839 1.58e+14
5: 2316 10611798 8.38 162 483223 2.03e+11 237464 483839 1.58e+14
6: 2288 10481960 8.41 160 483226 2.00e+11 234472 483839 1.57e+14
total_fees_btc totalbtc trade_volume_btc trade_volume_usd targetVar
1: 29597881711 1.65e+15 102451.92 463497285 buy
2: 29202300823 1.65e+15 102451.92 463497285 buy
3: 29234981721 1.65e+15 102451.92 463497285 buy
4: 28991577368 1.65e+15 102451.92 463497285 buy
5: 29179041967 1.65e+15 96216.78 440710136 hold
6: 28844391629 1.65e+15 96216.78 440710136 hold
然后我创建了一个函数:
classification_error <- function(conf_mat) {
conf_mat = as.matrix(conf_mat)
error = 1-sum(diag(conf_mat))/sum(conf_mat)
return (list(conf_mat = conf_mat,
error = error))
}
predFunc <- function(inData,trainPct){
trainP <- trainPct * .6
valP <- trainPct * .2
testP <- trainPct * .2
#SplitData
trainObs <- sample(nrow(inData), trainP * nrow(inData), replace = FALSE)
valObs <- sample(nrow(inData), valP * nrow(inData), replace = FALSE)
testObs <- sample(nrow(inData), testP * nrow(inData), replace = FALSE)
# Create the training/va/test datasets
trainDS <- inData[trainObs,]
valDS <- inData[valObs,]
testDS <- inData[testObs,]
# SVM- linear kernel
linearSVM <- svm(targetVar ~ ., data = trainDS, method = "C-classification", kernel = "linear")
# linear SVM Predictions
predSVMlin <- predict(linearSVM, trainDS[,-c("targetVar")])
valSVMlin <- predict(linearSVM, valDS[,-c("targetVar")])
testSVMlin <- predict(linearSVM, testDS[,-c("targetVar")])
# SVM Confusion matrix
trainConfusion <- table(true = trainDS[,c("targetVar")], pred = predSVMlin)
valConfusion <- table(true = trainDS[,c("targetVar")], pred = valSVMlin)
testConfusion <- table(true = trainDS[,c("targetVar")], pred = testSVMlin)
# Linear SVM Classification error
trainClassificationError <- classification_error(trainConfusion)
valClassificationError <- classification_error(valConfusion)
testClassificationError <- classification_error(testConfusion)
return(list(trainError = trainClassificationError,
valError = valClassificationError,
testError = testClassificationError,
))
}
然后我调用了函数:crypt <- predFunc(crypto_data, .7)
但是当我运行此代码时出现以下错误:
sort.list(y)出错:&#39; x&#39;对于&#39; sort.list&#39;必须是原子的有你 叫做排序&#39;在名单上? 5.停止(&#34;&#39; x&#39;必须是原子的&#39; sort.list&#39; \ n您是否在列表中打电话&#39;排序?&#39; ) 4. sort.list(y) 3.因素(a,排除=排除) 2. table(true = trainDs [,c(&#34; targetVar&#34;)],pred = predSVM) 1. predFunc(crypto_data,0.7)
基本上,我认为在创建混淆矩阵时函数会遇到问题,但我无法理解为什么或如何解决它。建议?
答案 0 :(得分:0)
当你进行线性SVM预测时,你应该使用这样的smth:
System.InvalidCastException: Unable to cast a COM object of type 'Microsoft.Office.Interop.Word.ApplicationClass' to interface type 'Microsoft.Office.Interop.Word._Application'.
在predSVMlin <- predict(linearSVM, trainDS %>% select(-targetVar))
中,您可以使用# SVM Confusion matrix
制作数字向量,而不是数据框列。
这取决于你计算的指标,但最后两个步骤在我看来还不清楚,你能解释一下吗?
我找不到trainDS$targetVar
函数,请写,找到它或写源代码。
答案 1 :(得分:0)
在您发表评论后,我可以为您提供解决方案:
classification_error <- function(conf_mat) {
conf_mat = as.matrix(conf_mat)
error = 1-sum(diag(conf_mat))/sum(conf_mat)
return (list(conf_mat = conf_mat,
error = error))
}
predFunc <- function(inData,trainPct){
trainP <- trainPct * .6
valP <- trainPct * .2
testP <- trainPct * .2
#SplitData
trainObs <- sample(nrow(inData), trainP * nrow(inData), replace = FALSE)
valObs <- sample(nrow(inData), valP * nrow(inData), replace = FALSE)
testObs <- sample(nrow(inData), testP * nrow(inData), replace = FALSE)
# Create the training/va/test datasets
trainDS <- inData[trainObs,]
valDS <- inData[valObs,]
testDS <- inData[testObs,]
# SVM- linear kernel
linearSVM <- svm(targetVar ~ ., data = trainDS, method = "C-classification", kernel = "linear")
# linear SVM Predictions
predSVMlin <- predict(linearSVM, trainDS %>% select(-targetVar))
valSVMlin <- predict(linearSVM, valDS %>% select(-targetVar))
testSVMlin <- predict(linearSVM, testDS %>% select(-targetVar))
# SVM Confusion matrix
trainConfusion <- table(true = trainDS$targetVar, pred = predSVMlin)
valConfusion <- table(true = valDS$targetVar, pred = valSVMlin)
testConfusion <- table(true = testDS$targetVar, pred = testSVMlin)
# Linear SVM Classification error
trainClassificationError <- classification_error(trainConfusion)
valClassificationError <- classification_error(valConfusion)
testClassificationError <- classification_error(testConfusion)
return(list(trainError = trainClassificationError,
valError = valClassificationError,
testError = testClassificationError))
}