如何在R语言中为随机森林设置PPV?

我想创建一个优化PPV的模型。我已经创建了一个RF模型(如下所示),它输出了一个混淆矩阵,然后我手动计算了敏感性、特异性、PPV、NPV和F1。现在我知道准确性被优化了,但我愿意牺牲敏感性和特异性来获得更高的PPV。

data_ctrl_null <- trainControl(method="cv", number = 5, classProbs = TRUE, summaryFunction=twoClassSummary, savePredictions=T, sampling=NULL)set.seed(5368)model_htn_df <- train(outcome ~ ., data=htn_df, ntree = 1000, tuneGrid = data.frame(mtry = 38), trControl = data_ctrl_null, method= "rf",                            preProc=c("center","scale"),metric="ROC", importance=TRUE)model_htn_df$finalModel #provides confusion matrix

结果如下:

Call:  randomForest(x = x, y = y, ntree = 1000, mtry = param$mtry, importance = TRUE)            Type of random forest: classification                 Number of trees: 1000  No. of variables tried at each split: 38    OOB estimate of  error rate: 16.2%    Confusion matrix:      no yes class.error no  274  19  0.06484642 yes  45  57  0.44117647

我的手动计算结果:敏感性 = 55.9%,特异性 = 93.5%,PPV = 75.0%,NPV = 85.9%(混淆矩阵将我的“no”和“yes”作为结果进行了交换,所以我在计算性能指标时也交换了数字。)

那么,我该怎么做才能使PPV达到90%呢?

这是一个类似的问题,但我不是很理解。


回答:

我们定义一个函数来计算PPV并以名称返回结果:

PPV <- function (data,lev = NULL,model = NULL) {   value <- posPredValue(data$pred,data$obs, positive = lev[1])   c(PPV=value)}

假设我们有以下数据:

library(randomForest)library(caret)data=irisdata$Species = ifelse(data$Species == "versicolor","versi","others")trn = sample(nrow(iris),100)

然后我们通过指定PPV为度量指标来进行训练:

mdl <- train(Species ~ ., data = data[trn,],             method = "rf",             metric = "PPV",             trControl = trainControl(summaryFunction = PPV,                                       classProbs = TRUE))Random Forest 100 samples  4 predictor  2 classes: 'others', 'versi' No pre-processingResampling: Bootstrapped (25 reps) Summary of sample sizes: 100, 100, 100, 100, 100, 100, ... Resampling results across tuning parameters:  mtry  PPV        2     0.9682811  3     0.9681759  4     0.9648426PPV was used to select the optimal model using the largest value.The final value used for the model was mtry = 2.

现在你可以看到它是基于PPV进行训练的。然而,你不能强制训练达到0.9的PPV。这真的取决于数据,如果你的独立变量没有预测能力,无论你如何训练,它都不会改善,对吗?

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注