R - 计算神经网络的 AUC

问题描述

我正在处理一个分类问题并想尝试一种更复杂的算法,我让它正常工作并且能够在之后构建一个混淆矩阵,但我正在努力计算 AUC。

这是我用来获取这里的代码

library(caret)
dummy <- dummyVars(" ~ .",data=df)
newdata <- data.frame(predict(dummy,newdata = df))
table(newdata$LC_NS)

# Training and Test Data
set.seed(2396)
#Use 70% of dataset as training set and remaining 30% as testing set
ind <- sample(2,nrow(newdata),replace = T,prob = c(0.7,0.3))
training <- newdata[ind == 1,]
testing <- newdata[ind == 2,]

#Verify data separation
dim(training); dim(testing)

#Neural Network
library(neuralnet)
nn.model <- neuralnet(LC_NS~ .,data=training,hidden=c(2,1),linear.output=FALSE,threshold=0.01)
plot(nn.model)

nn.model$result.matrix

#Test the resulting output
temp_test <- subset(testing[,-80])
head(temp_test)
nn.results <- compute(nn.model,temp_test)
results <- data.frame(actual = testing$LC_NS,prediction = nn.results$net.result)

roundedresults<-sapply(results,round,digits=0)
roundedresultsdf=data.frame(roundedresults)
attach(roundedresultsdf)
table(actual,prediction)

我与其他模型一起使用的东西不起作用。我知道其中很大一部分原因是我的自变量和预测采用了不同的格式,而且我很难让它们变得同理。

我尝试了这段代码,结果是:

#predict appointment adherence
LC_NS.predict.prob <- predict(nn.model,testing,type="prob")

LC_NS.predict <- predict(nn.model,testing)

#Prediction and confusion Matrix
caret::confusionMatrix(LC_NS.predict,testing$LC_NS,positive = 1)

Error: `data` and `reference` should be factors with the same levels.

然后我尝试走这条路,但在第一步出错:

# need to create prediction object from ROCR
pr <- prediction(LC_NS.predict.prob[,2],testing$LC_NS)

LC_NS.predict.prob

# plotting ROC curve
prf <- performance(pr,measure = "tpr",x.measure = "fpr")
plot(prf)

# AUC value
auc <- performance(pr,measure = "auc")
auc <- [email protected][[1]]
auc

plot(varImp(nn.model))

Error in LC_NS.predict.prob[,2] : subscript out of bounds

我知道这与数据格式有关,但我似乎无法弄清楚。我很乐意按要求提供其他信息。

谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)