分类分析--选择预测效果最好的解

分类分析--选择预测效果最好的解

预测准确性度量

 

下面给出计算这几个统计量的函数

评估二分类准确性

performance <- function(table, n=2){

 if(!all(dim(table) == c(2,2)))

stop("Must be a 2 x 2 table")

1)第一步:得到频数

tn = table[1,1]

fp = table[1,2]

fn = table[2,1]

tp = table[2,2]

2)第二步:计算统计量

 

sensitivity = tp/(tp+fn)

 specificity = tn/(tn+fp)

 ppp = tp/(tp+fp)

 npp = tn/(tn+fn)

 hitrate = (tp+tn)/(tp+tn+fp+fn)

3)第三步:输出结果

 

result <- paste("Sensitivity = ", round(sensitivity, n) ,

 "\nSpecificity = ", round(specificity, n),

 "\nPositive Predictive Value = ", round(ppp, n),

 "\nNegative Predictive Value = ", round(npp, n),

 "\nAccuracy = ", round(hitrate, n), "\n", sep="")

 cat(result)

}

以下代码清单将performance()函数用于上述提到的五个分类器

逻辑回归

 

传统决策树

 

条件推断树

 

随机森林(决策树)

 

随机森林(条件推断树)

 

支持向量机(无调和参数)

 

支持向量机(有调和参数)

 

posted @ 2021-08-17 16:50  zhang-X  阅读(113)  评论(0)    收藏  举报