[英]CODE FOR 95% confidence intervals for sensitivity and specificity using confusion matrix
I have a 5 variable data set called EYETESTS.我有一个名为 EYETESTS 的 5 变量数据集。 The variables are MAD, SAD, RED, BLUE, LEVEL.变量是 MAD、SAD、RED、BLUE、LEVEL。
MAD, SAD, RED AND BLUE AND LEVEL are all factor variables with 2 factors that represent yes(1) or no(0). MAD、SAD、RED AND BLUE 和 LEVEL 都是因子变量,有 2 个因子表示是(1)或否(0)。
Example:例子:
MAD疯狂的 | SAD伤心 | RED红色的 | BLUE蓝色的 | LEVEL等级 |
---|---|---|---|---|
0 0 | 0 0 | 0 0 | 1 1个 | 1 1个 |
0 0 | 1 1个 | 1 1个 | 0 0 | 0 0 |
1 1个 | 0 0 | 0 0 | 1 1个 | 0 0 |
0 0 | 1 1个 | 0 0 | 0 0 | 0 0 |
0 0 | 0 0 | 1 1个 | 0 0 | 0 0 |
1 1个 | 0 0 | 0 0 | 0 0 | 1 1个 |
I am trying to create a confusion matrix of MAD against LEVEL.我正在尝试创建 MAD 与 LEVEL 的混淆矩阵。 My Reference variable is LEVEL.我的参考变量是 LEVEL。 The other variables are all predictor/test variables.其他变量都是预测变量/测试变量。
Then a separate confusion matrix of SAD against LEVEL.然后是 SAD 与 LEVEL 的单独混淆矩阵。 Then a separate confusion matrix of RED against LEVEL.然后是 RED 与 LEVEL 的单独混淆矩阵。 Then a separate confusion matrix of BLUE against LEVEL.然后是一个单独的 BLUE 与 LEVEL 的混淆矩阵。
The issue that I am having trouble with is calculating the 95% Confidence Intervals for the sensitivity and specificity alongside the others.我遇到的问题是与其他问题一起计算灵敏度和特异性的 95% 置信区间。
I can get the output in the form I want using the caret library.我可以使用插入符号库以我想要的形式获得 output。
library(caret)
confusionMatrix(as.factor(SAD), as.factor(LEVEL))
This gives me the output I want in terms of sensitivity, specificity and accuracy but I want the 95% Confidence Intervals for the sensitivity and specificity.这给了我 output 我想要的灵敏度、特异性和准确性,但我想要灵敏度和特异性的 95% 置信区间。
Would be incredibly grateful for help with this.非常感谢您的帮助。 I have tried using the conf
package and the epiR
package but they do not give the confidence intervals for the sensitivity and specificity.我试过使用conf
package 和epiR
package,但它们没有给出灵敏度和特异性的置信区间。
Sensitivity and specificity are proportions and their confidence intervals are binomial proportion intervals.灵敏度和特异性是比例,它们的置信区间是二项式比例区间。
Here is a way with package binom
, that gives 11 different confidence intervals for the binomial proportion.这是 package 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
prop.test(mat[1,1], sum(mat[,1]))
#> Error in is.table(x): object 'mat' not found
prop.test(mat[2,2], sum(mat[,2]))
#> Error in is.table(x): object 'mat' not found
Created on 2023-01-08 with reprex v2.0.2创建于 2023-01-08,使用reprex v2.0.2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.