[英]How to plot ROC-curve for logistic regression (LASSO) in R?
I am fitting a logistic regression model to a training data set in R, more specifically a LASSO regression with an L1 penalty.我正在将逻辑回归模型拟合到 R 中的训练数据集,更具体地说是带有 L1 惩罚的 LASSO 回归。 I used the
glmnet
package for that.我
glmnet
使用了glmnet
包。 The code for the model looks like this.该模型的代码如下所示。
t1 <- Sys.time()
glmnet_classifier <- cv.glmnet(x = dtm_train_tfidf,
y = tweets_train[['sentiment']],
family = 'binomial',
# L1 penalty
alpha = 1,
# interested in the area under ROC curve
type.measure = "auc",
# 5-fold cross-validation
nfolds = 5,
# high value is less accurate, but has faster training
thresh = 1e-3,
# again lower number of iterations for faster training
maxit = 1e3)
print(difftime(Sys.time(), t1, units = 'mins'))
preds <- predict(glmnet_classifier, dtm_test_tfidf, type = 'response')[ ,1]
Now I would like to plot the ROC-curve.现在我想绘制 ROC 曲线。 However, I cannot figure out how to accurately plot it.
但是,我无法弄清楚如何准确地绘制它。
When I plot(glmnet_classifier)
this is what I receive:当我
plot(glmnet_classifier)
这就是我收到的:
Since this is not the Roc-curve, I would like to know if anybody knows how to plot it in R?由于这不是 Roc 曲线,我想知道是否有人知道如何在 R 中绘制它? I already referred to the
ROCR
package, but it gives me an error:我已经提到了
ROCR
包,但它给了我一个错误:
roc.perf = performance(preds, measure = "tpr", x.measure = "fpr")
Can anybody help?有人可以帮忙吗? Thank you very much!
非常感谢!
library(pROC)
data("aSAH")
fit <- glm(outcome ~ gender + age + wfns + s100b , data = aSAH, family = binomial)
roc(aSAH$outcome, as.vector(fitted.values(fit)), percent=F, boot.n=1000, ci.alpha=0.9, stratified=FALSE, plot=TRUE, grid=TRUE, show.thres=TRUE, legacy.axes = TRUE, reuse.auc = TRUE,
# print.thres = c(0.30,0.35, 0.40, 0.45,0.48, 0.50,0.55, 0.60),#
print.auc = TRUE, print.thres.col = "blue", ci=TRUE, ci.type="bars", print.thres.cex = 0.7, main = paste("ROC curve using","(N = ",nrow(aSAH),")") )
I hope it help ;)我希望它有所帮助;)
The problem you have with ROCR
is that you are using performance
directly on the prediction and not on a standardized prediction object. ROCR
的问题在于您直接在预测上使用performance
,而不是在标准化预测对象上使用。 Here is an example of how to plot the ROC curve这是如何绘制 ROC 曲线的示例
library(ggplot2) # For diamonds data
library(ROCR) # For ROC curves
library(glmnet) # For regularized GLMs
# Classification problem
class <- diamonds$price > median(diamonds$price) # The top 50% valued diamonds
X <- as.matrix(diamonds[, c('carat', 'depth', 'x', 'y', 'z')]) # Predictor variables
# L1 regularized logistic regression
fit <- cv.glmnet(x = X, y = class, family = 'binomial', type.measure = 'class', alpha = 1)
# Predict from model
preds <- predict(fit, newx = X, type = 'response')
# ROCR for ROC curve
library(ROCR)
# Calculate true positive rate and false positive rate on the prediction object
perf <- performance(prediction(preds, class), 'tpr', 'fpr')
plot(perf)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.