R language Access Menu

Title Text Both  

Evaluating a new test: Sensitivity, specificity, ROC curve

If TP is true positives, TN is true negatives, FP is false positives and FN is false negatives, then senstitivity etc are determined using following custom-built function: 

Code:

> mysens_etc = function(TP, FN, FP, TN, Prevalence=-1){
    SENS=TP / (TP+FN) 
    SPEC=TN / (FP+TN) 
    if(Prevalence==-1) Prevalence=(TP+FN) / (TP+FN+FP+TN)
    
    Positive_predictive_value=(SENS*Prevalence)/ (SENS*Prevalence + (1-SPEC)*(1-Prevalence))
    Negative_predictive_value=(SPEC*(1-Prevalence))/ ((1-SENS)*Prevalence + SPEC*(1-Prevalence))

    Positive_Likelihood_Ratio=SENS / (1-SPEC) 
    Negative_Likelihood_Ratio=(1-SENS) / SPEC

    Pretest_Prob=Prevalence 
    Pretest_Odds=Pretest_Prob / (1 - Pretest_Prob) 
    Positive_Posttest_Odds=Pretest_Odds * Positive_Likelihood_Ratio 
    Positive_Posttest_Probability=Positive_Posttest_Odds / (1 + Positive_Posttest_Odds)

    Negative_Posttest_Odds=Pretest_Odds * Negative_Likelihood_Ratio 
    Negative_Posttest_Probability=Negative_Posttest_Odds / (1 + Negative_Posttest_Odds)

    ddf=data.frame(1:17,c("True_positives", "False_positives", "True_negatives", "False_negatives", "SENS","SPEC","Prevalence","Positive_predictive_value","Negative_predictive_value",
    "Positive_Likelihood_Ratio", "Negative_Likelihood_Ratio","Pretest_Prob", "Pretest_Odds",
    "Positive_Posttest_Odds", "Positive_Posttest_Probability", "Negative_Posttest_Odds", "Negative_Posttest_Probability"),
    c(TP, FP, TN, FN, SENS,SPEC,Prevalence,Positive_predictive_value,Negative_predictive_value,
    Positive_Likelihood_Ratio, Negative_Likelihood_Ratio,Pretest_Prob, Pretest_Odds,
    Positive_Posttest_Odds, Positive_Posttest_Probability, Negative_Posttest_Odds, Negative_Posttest_Probability))
    names(ddf)=c("SNo","parameter","value")
    ddf$value=round(ddf$value,3)
    ddf

The function can be used as follows: 

Code:

    > rnsens_etc(250, 10, 25, 1000 )

The output is shown on the next page: 
   SNo                     parameter    value
1    1                True_positives  250.000
2    2               False_positives   25.000
3    3                True_negatives 1000.000
4    4               False_negatives   10.000
5    5                          SENS    0.962
6    6                          SPEC    0.976
7    7                    Prevalence    0.202
8    8     Positive_predictive_value    0.909
9    9     Negative_predictive_value    0.990
10  10     Positive_Likelihood_Ratio   39.423
11  11     Negative_Likelihood_Ratio    0.039
12  12                  Pretest_Prob    0.202
13  13                  Pretest_Odds    0.254
14  14        Positive_Posttest_Odds   10.000
15  15 Positive_Posttest_Probability    0.909
16  16        Negative_Posttest_Odds    0.010
17  17 Negative_Posttest_Probability    0.010

ROC curves
The Deducer package makes ROC curve plotting very easy: 

Code:
    > mod = glm(low~., data=bwdf, family=binomial)
    > library(Deducer)
    > rocplot(mod)

Output graph:
                  

References:
Ian Fellows (2012). Deducer: A Data Analysis GUI for R. Journal of Statistical Software, 49(8), 1-15. URL http://www.jstatsoft.org/v49/i08/.
 


    Comments & Feedback