对于一些背景知识,我使用构建简单神经网络的nnet
包。
我的数据集有许多因子和连续变量功能。为了处理连续变量,我应用scale
和center
,每个变量按其均值减去,并除以其SD。
我正在努力制作一套ROC& AUC图来自神经网络模型的结果。
以下是用于构建我的基本神经网络模型的代码:
model1 <- nnet(Cohort ~ .-Cohort,
data = train.sample,
size = 1)
为了得到一些预测,我调用以下函数:
train.predictions <- predict(model1, train.sample)
现在,这将train.predictions
对象分配给由0和0组成的大矩阵。 1个值。我想要做的是获得每个预测的类概率,以便我可以使用pROC
包绘制ROC曲线。
所以,我尝试将以下参数添加到我的预测函数中:
train.predictions <- predict(model1, train.sample, type="prob")
但是我收到了一个错误:
match.arg(type)中的错误:'arg'应该是“raw”,“class”之一
如何从输出中获取课程概率?
答案 0 :(得分:2)
假设您的测试/验证数据集在train.predictions <- predict(model1, train.test, type="raw")
## This might not be necessary:
detach(package:nnet,unload = T)
library(ROCR)
## train.labels:= A vector, matrix, list, or data frame containing the true
## class labels. Must have the same dimensions as 'predictions'.
## computing a simple ROC curve (x-axis: fpr, y-axis: tpr)
pred = prediction(train.predictions, train.labels)
perf = performance(pred, "tpr", "fpr")
plot(perf, lwd=2, col="blue", main="ROC - Title")
abline(a=0, b=1)
中,并且train.labels包含真正的类标签:
{{1}}