作为背景,我正在使用nnet软件包构建一个简单的神经网络。
我的数据集具有许多因素和连续变量特性。为了处理连续变量,我应用了scale和center,它们将每个变量的平均值缩小,并除以其SD。
我试图根据神经网络模型的结果生成一个ROC & AUC图。
下面是用于构建我的基本神经网络模型的代码:
model1 <- nnet(Cohort ~ .-Cohort,
data = train.sample,
size = 1)为了得到一些预测,我调用以下函数:
train.predictions <- predict(model1, train.sample)现在,将train.predictions对象分配给一个由0&1值组成的大型矩阵。我想要做的是,得到每个预测的类概率,这样我就可以使用pROC包绘制一个ROC曲线。
因此,我尝试在我的预测函数中添加以下参数:
train.predictions <- predict(model1, train.sample, type="prob")但我发现了一个错误:
match.arg(type)中的错误:'arg‘应该是“raw”、“class”之一
如何从输出中获取类概率?
发布于 2017-10-23 14:35:20
假设您的测试/验证数据集在train.test中,并且train.labels包含真正的类标签:
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)https://stackoverflow.com/questions/46891681
复制相似问题