使用混淆矩阵计算灵敏度和特异度的95%置信区间的代码。

3

我有一个名为EYETESTS的5个变量数据集。这些变量是MAD,SAD,RED,BLUE和LEVEL。

MAD,SAD,RED,BLUE和LEVEL都是具有2个因子的因子变量,表示是(1)或否(0)。

示例:

MAD SAD RED BLUE LEVEL
0 0 0 1 1
0 1 1 0 0
1 0 0 1 0
0 1 0 0 0
0 0 1 0 0
1 0 0 0 1

该程序相关,返回翻译文本:
我正在尝试创建一个MAD对LEVEL的混淆矩阵。我的参考变量是LEVEL。其他变量都是预测/测试变量。
然后,创建一个单独的SAD对LEVEL的混淆矩阵。 然后,创建一个单独的RED对LEVEL的混淆矩阵。 然后,创建一个单独的BLUE对LEVEL的混淆矩阵。
我遇到的问题是计算灵敏度和特异度的95%置信区间以及其他内容。
我可以使用caret库以我想要的形式获取输出。
library(caret)
confusionMatrix(as.factor(SAD), as.factor(LEVEL))

这个程序可以输出我想要的灵敏度、特异性和准确率,但我还想得到灵敏度和特异性的95%置信区间。

非常感谢您的帮助。我已经尝试使用conf包和epiR包,但它们没有给出灵敏度和特异性的置信区间。


这个能帮忙吗? - Rui Barradas
1个回答

1
敏感性和特异性是比例,它们的置信区间是二项比例区间。 这里有一种使用binom包的方法,可以给出二项比例的11个不同置信区间。
df1 <- "MAD     SAD     RED     BLUE    LEVEL
0   0   0   1   1
0   1   1   0   0
1   0   0   1   0
0   1   0   0   0
0   0   1   0   0
1   0   0   0   1"
df1 <- read.table(text = df1, header = TRUE)

confusionMatrixCI <- function(x, ...) {
  y <- x$table
  Se <- binom::binom.confint(y[1,1], sum(y[,1]), ...)
  Sp <- binom::binom.confint(y[2,2], sum(y[,2]), ...)
  list(sensitivity = Se, specificity = Sp)
}

res <- caret::confusionMatrix(factor(df1$MAD), factor(df1$LEVEL), 
                              positive = "1")

ci95 <- confusionMatrixCI(res)
#> Warning in stats::prop.test(x[i], n[i]): Chi-squared approximation may be
#> incorrect

#> Warning in stats::prop.test(x[i], n[i]): Chi-squared approximation may be
#> incorrect
ci95$sensitivity
#>           method x n mean     lower     upper
#> 1  agresti-coull 3 4 0.75 0.2891407 0.9659139
#> 2     asymptotic 3 4 0.75 0.3256553 1.1743447
#> 3          bayes 3 4 0.70 0.3470720 0.9966562
#> 4        cloglog 3 4 0.75 0.1279469 0.9605486
#> 5          exact 3 4 0.75 0.1941204 0.9936905
#> 6          logit 3 4 0.75 0.2378398 0.9664886
#> 7         probit 3 4 0.75 0.2543493 0.9777762
#> 8        profile 3 4 0.75 0.2772218 0.9800582
#> 9            lrt 3 4 0.75 0.2775912 0.9837676
#> 10     prop.test 3 4 0.75 0.2194265 0.9868088
#> 11        wilson 3 4 0.75 0.3006418 0.9544127
ci95$specificity
#>           method x n mean        lower     upper
#> 1  agresti-coull 1 2  0.5  0.094531206 0.9054688
#> 2     asymptotic 1 2  0.5 -0.192951912 1.1929519
#> 3          bayes 1 2  0.5  0.060830276 0.9391697
#> 4        cloglog 1 2  0.5  0.005983088 0.9104101
#> 5          exact 1 2  0.5  0.012579117 0.9874209
#> 6          logit 1 2  0.5  0.058866787 0.9411332
#> 7         probit 1 2  0.5  0.041195981 0.9588040
#> 8        profile 1 2  0.5  0.038416621 0.9615834
#> 9            lrt 1 2  0.5  0.038100934 0.9618991
#> 10     prop.test 1 2  0.5  0.094531206 0.9054688
#> 11        wilson 1 2  0.5  0.094531206 0.9054688
ci95
#> $sensitivity
#>           method x n mean     lower     upper
#> 1  agresti-coull 3 4 0.75 0.2891407 0.9659139
#> 2     asymptotic 3 4 0.75 0.3256553 1.1743447
#> 3          bayes 3 4 0.70 0.3470720 0.9966562
#> 4        cloglog 3 4 0.75 0.1279469 0.9605486
#> 5          exact 3 4 0.75 0.1941204 0.9936905
#> 6          logit 3 4 0.75 0.2378398 0.9664886
#> 7         probit 3 4 0.75 0.2543493 0.9777762
#> 8        profile 3 4 0.75 0.2772218 0.9800582
#> 9            lrt 3 4 0.75 0.2775912 0.9837676
#> 10     prop.test 3 4 0.75 0.2194265 0.9868088
#> 11        wilson 3 4 0.75 0.3006418 0.9544127
#> 
#> $specificity
#>           method x n mean        lower     upper
#> 1  agresti-coull 1 2  0.5  0.094531206 0.9054688
#> 2     asymptotic 1 2  0.5 -0.192951912 1.1929519
#> 3          bayes 1 2  0.5  0.060830276 0.9391697
#> 4        cloglog 1 2  0.5  0.005983088 0.9104101
#> 5          exact 1 2  0.5  0.012579117 0.9874209
#> 6          logit 1 2  0.5  0.058866787 0.9411332
#> 7         probit 1 2  0.5  0.041195981 0.9588040
#> 8        profile 1 2  0.5  0.038416621 0.9615834
#> 9            lrt 1 2  0.5  0.038100934 0.9618991
#> 10     prop.test 1 2  0.5  0.094531206 0.9054688
#> 11        wilson 1 2  0.5  0.094531206 0.9054688

2023-01-08创建,使用reprex v2.0.2


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接