这个
df
在列车和测试数据帧中拆分。列车数据帧在训练和测试数据帧中被分割。因变量
Y
是值为0和1的二进制(因子)。我试图用这段代码(神经网络,caret软件包)预测概率:
library(caret)
model_nn <- train(
Y ~ ., training,
method = "nnet",
metric="ROC",
trControl = trainControl(
method = "cv", number = 10,
verboseIter = TRUE,
classProbs=TRUE
)
)
model_nn_v2 <- model_nn
nnprediction <- predict(model_nn, testing, type="prob")
cmnn <-confusionMatrix(nnprediction,testing$Y)
print(cmnn) # The confusion matrix is to assess/compare the model
但是,它给了我以下错误:
Error: At least one of the class levels is not a valid R variable name;
This will cause errors when class probabilities are generated because the
variables names will be converted to X0, X1 . Please use factor levels
that can be used as valid R variable names (see ?make.names for help).
我不明白“使用可以用作有效R变量名的因子级别”是什么意思。因变量
Y
已是因子,但不是有效的R变量名?。
附言:如果你删除代码,代码会很好地工作
classProbs=TRUE
在里面
trainControl()
和
metric="ROC"
在里面
train()
.然而
"ROC"
metric是我的最佳模型的比较指标,因此我尝试使用“ROC”指标创建一个模型。
编辑
:代码示例:
# You have to run all of this BEFORE running the model
classes <- c("a","b","b","c","c")
floats <- c(1.5,2.3,6.4,2.3,12)
dummy <- c(1,0,1,1,0)
chr <- c("1","2","2,","3","4")
Y <- c("1","0","1","1","0")
df <- cbind(classes, floats, dummy, chr, Y)
df <- as.data.frame(df)
df$floats <- as.numeric(df$floats)
df$dummy <- as.numeric(df$dummy)
classes <- c("a","a","a","b","c")
floats <- c(5.5,2.6,7.3,54,2.1)
dummy <- c(0,0,0,1,1)
chr <- c("3","3","3,","2","1")
Y <- c("1","1","1","0","0")
df <- cbind(classes, floats, dummy, chr, Y)
df <- as.data.frame(df)
df$floats <- as.numeric(df$floats)
df$dummy <- as.numeric(df$dummy)