如何解决此错误:“ auc3 _((实际,预测,排名)错误:与请求的类型不兼容:[type = character; target = double]。”

时间:2019-02-07 09:43:40

标签: r machine-learning r-caret

我当前正在使用混合类型数据的数据集。 我正在使用R软件中的caret软件包来训练SVM,LogitBoost,RF .....

我的数据集的尺寸为124x28,正如我所指出的那样,预测变量是数值变量或因子(在某些情况下为有序因子)。

我试图根据从重要性最低的变量的数据集中消除预测变量的方法:每次迭代预测变量的数量减少一。我使用插入符号包中的varImp函数来评估变量的重要性。 要分类的变量是第一个称为State的变量,它是2级的因子。

我遇到的问题是varImp不适用于特定的一组预测变量,但可以用于该特定组的一个或多个预测变量集。 特别是,对于运行varImp之后的那组特定的预测变量,控制台将显示以下错误:

  

auc3_(实际,预测,等级)中的错误:与不兼容   要求的类型:[type = character; target = double]。

代码如下:

超出设置

rm(list = ls())
library(readxl)
library(caret)
library(ROCR)
library(kernlab)
library(e1071)
library(openxlsx)

tutti <- read_excel("C:/Users/Matteo/Tesi/data set.xlsx")
tutti<-data.frame(tutti)
tutti$State = as.character(tutti$State)
tutti$State = factor(tutti$State)
tutti$T_Anxiety_40fl2 = factor(tutti$T_Anxiety_40fl2)
tutti$SmkAroundLMP2 = factor(tutti$SmkAroundLMP2)
tutti$SmkTrimester1_overall = factor(tutti$SmkTrimester1_overall)
tutti$SmkTrimester2_overall = factor(tutti$SmkTrimester2_overall)
tutti$AnyDrugs2 = factor(tutti$AnyDrugs2)
tutti$ACC_G = factor(tutti$ACC_G,ordered=TRUE)
tutti$ACC_P = factor(tutti$ACC_P,ordered=TRUE)
tutti$DEC = factor(tutti$DEC,ordered=TRUE)

tutti<-tutti[-c(2,11,4,16,8,19,6,5,20,26,23,7,10,18,27,9)]


train.index <- sample.int(nrow(tutti),ceiling(nrow(tutti)*0.7));
trainset <- tutti[train.index,] 
testset <- tutti[-train.index,]
testX<-testset[-1]

ctrl<-trainControl(method="cv",number=10,     summaryFunction=twoClassSummary,classProbs=TRUE)
grid <- expand.grid(C=(2))
svm.tune <- train(State ~ .,data=trainset,method = "svmLinear",tuneGrid =  grid,trControl=ctrl)
all <- varImp(svm.tune)

######################

特定集

rm(list = ls())
library(readxl)
library(caret)
library(ROCR)
library(kernlab)
library(e1071)
library(openxlsx)

tutti <- read_excel("C:/Users/Matteo/Tesi/data set.xlsx")
tutti<-data.frame(tutti)
tutti$State = as.character(tutti$State)
tutti$State = factor(tutti$State)
tutti$T_Anxiety_40fl2 = factor(tutti$T_Anxiety_40fl2)
tutti$SmkAroundLMP2 = factor(tutti$SmkAroundLMP2)
tutti$SmkTrimester1_overall = factor(tutti$SmkTrimester1_overall)
tutti$SmkTrimester2_overall = factor(tutti$SmkTrimester2_overall)
tutti$AnyDrugs2 = factor(tutti$AnyDrugs2)
tutti$ACC_G = factor(tutti$ACC_G,ordered=TRUE)
tutti$ACC_P = factor(tutti$ACC_P,ordered=TRUE)
tutti$DEC = factor(tutti$DEC,ordered=TRUE)

tutti<-tutti[-c(2,11,4,16,8,19,6,5,20,26,23,7,10,18,27,9,25)]

train.index <- sample.int(nrow(tutti),ceiling(nrow(tutti)*0.7));
trainset <- tutti[train.index,] 
testset <- tutti[-train.index,]
testX<-testset[-1]

ctrl<-trainControl(method="cv",number=10,     summaryFunction=twoClassSummary,classProbs=TRUE)
grid <- expand.grid(C=(2))
svm.tune <- train(State ~ .,data=trainset,method = "svmLinear",tuneGrid =  grid,trControl=ctrl)
all <- varImp(svm.tune)

#############################

子集

rm(list = ls())
library(readxl)
library(caret)
library(ROCR)
library(kernlab)
library(e1071)
library(openxlsx)

tutti <- read_excel("C:/Users/Matteo/Tesi/data set.xlsx")
tutti<-data.frame(tutti)
tutti$State = as.character(tutti$State)
tutti$State = factor(tutti$State)
tutti$T_Anxiety_40fl2 = factor(tutti$T_Anxiety_40fl2)
tutti$SmkAroundLMP2 = factor(tutti$SmkAroundLMP2)
tutti$SmkTrimester1_overall = factor(tutti$SmkTrimester1_overall)
tutti$SmkTrimester2_overall = factor(tutti$SmkTrimester2_overall)
tutti$AnyDrugs2 = factor(tutti$AnyDrugs2)
tutti$ACC_G = factor(tutti$ACC_G,ordered=TRUE)
tutti$ACC_P = factor(tutti$ACC_P,ordered=TRUE)
tutti$DEC = factor(tutti$DEC,ordered=TRUE)

tutti<-tutti[-c(2,11,4,16,8,19,6,5,20,26,23,7,10,18,27,9,25,24)]

train.index <- sample.int(nrow(tutti),ceiling(nrow(tutti)*0.7));
trainset <- tutti[train.index,] 
testset <- tutti[-train.index,]
testX<-testset[-1]

ctrl<-trainControl(method="cv",number=10,     summaryFunction=twoClassSummary,classProbs=TRUE)
grid <- expand.grid(C=(2))
svm.tune <- train(State ~ .,data=trainset,method = "svmLinear",tuneGrid =  grid,trControl=ctrl)
all <- varImp(svm.tune)

实际上,如果一组预测变量仅包含一个因子变量,则varImp返回该错误。 (除了要预测的变量)

您能帮我解决这个问题吗?

我已经更新了R的每个软件包。

快速查看源代码后,我认为这是源代码(C ++)问题。我说的对吗?

0 个答案:

没有答案