Sensitivity, specificity, positive and negative predictive values, and other 2x2 table statistics


There are many common statistics defined for 2×2 tables. Some statistics are available in PROC FREQ in SAS/STAT® and in PROC FREQTAB in SAS® Viya®. Others can be computed as discussed and illustrated below.

The following hypothetical data assume that subjects were observed to exhibit the response (such as a disease) or not. Subjects also tested either positive (Test=1) or negative (Test=0) on a prognostic test for the response. Results from all subjects can be summarized in a 2×2 table.

These statements read in the cell counts of the table and use PROC FREQ to display the table. PROC FREQTAB could also be used. PROC SORT orders the row and column variables so that 1 appears before 0. The ORDER=DATA option in PROC FREQ orders the table according to the order found in the sorted data set. As a result, the 1 levels appear before the 0 levels, putting Test=1, Response=1 in the upper-left (1,1) cell of the table:

data FatComp;
   input Test Response Count;
   datalines;
   0 0  6
   0 1  2
   1 0  4
   1 1 11
   ;
proc sort data=FatComp;
   by descending Test descending Response;
   run;
proc freq data=FatComp order=data;
   weight Count;
   tables Test*Response / senspec;
   run;

Point estimates for sensitivity, specificity, positive predictive value (PPV), negative predictive value (NPV), false positive probability, and false negative probability are row or column percentages of the 2×2 tableNote. Beginning in SAS® 9.4M6, point estimates and confidence intervals for sensitivity, specificity, PPV, and NPV are available in PROC FREQ (and in PROC SURVEYFREQ) with the SENSPEC option in the TABLES statement as shown above. In earlier releases, estimates, confidence intervals, and tests of the above statistics can be obtained either by using PROC FREQ on subtables or by using a modeling procedure to estimate the statistics. This is illustrated below.

A common estimate (meta-analysis) of any of these four statistics, computed using the data from multiple studies, can be obtained as shown in SAS KB0058661.

Below are the results from PROC FREQ, with sensitivity, specificity, positive predictive value, negative predictive value, false positive probability, and false negative probability indicated by matching colors:

The estimates highlighted above are repeated in the results from the SENSPEC option along with their standard error estimates and confidence intervals:

All statistics discussed in this article are defined as follows, assuming that the table is arranged as shown with Response levels as the columns and Test levels as the rows and with Test=1, Response=1 in the (1,1) cell of the table:

Using PROC NLMIXED to Estimate and Test All Statistics 

One way to obtain estimates of all of the above statistics, along with their standard errors (computed using the delta method) and large-sample confidence intervals, is to use PROC NLMIXED. This is done by fitting a saturated Poisson model that has one parameter in the model for each cell of the table. Each statistic is then estimated by specifying its formula in an ESTIMATE statement. The standard errors are estimated using the delta method.

This is illustrated in the following NLMIXED step that produces the estimates shown above. A similar approach can be used to compare statistics computed in two or more independent tables as discussed in SAS KB0044428. Because of the DF=1E8 option, each ESTIMATE statement provides a large-sample test of the null hypothesis that the specified function equals 0. For ratios, such as the lift and likelihood ratio statistics, where it is of interest to test their equality to 1 rather than 0, a second ESTIMATE statement is used that subtracts 1 from the function to test that the ratio equals 1:

proc nlmixed data=FatComp df=1e8;
   mu=exp(t0r0*(test=0 and response=0) + t0r1*(test=0 and response=1) + 
          t1r0*(test=1 and response=0) + t1r1*(test=1 and response=1));
   model count ~ poisson(mu);
   
   /* Table Cell Counts */
   n11=exp(t1r1); n10=exp(t1r0); n01=exp(t0r1); n00=exp(t0r0);
   /* Table Margins For Test and Response */
   t1=n11+n10; t0=n01+n00;
   r1=n11+n01; r0=n10+n00;
   total=n11+n10+n01+n00;
   
   /* Statistics */
   estimate 'Sensitivity/TPR/Recall' n11/r1;
   estimate 'Specificity/TNR' n00/r0;
   estimate 'PPV/Precision' n11/t1;
   estimate 'NPV' n00/t0;
   estimate 'FPR/FDR' n10/t1;
   estimate 'FPR' n10/r0;
   estimate 'FNR/FOR' n01/t0;
   estimate 'FNR' n01/r1;
   estimate 'Accuracy' (n00+n11)/total;
   estimate 'Estimate Lift+' (n11/t1)/(r1/total);
   estimate 'Test Lift+ = 1' (n11/t1)/(r1/total)-1;
   estimate 'Estimate Lift-' (n01/t0)/(r1/total);
   estimate 'Test Lift- = 1' (n01/t0)/(r1/total)-1;
   estimate 'Estimate LR+' (n11/r1)/(1-n00/r0);
   estimate 'Test LR+ = 1' (n11/r1)/(1-n00/r0)-1;
   estimate 'Estimate LR-' (1-n11/r1)/(n00/r0);
   estimate 'Test LR- = 1' (1-n11/r1)/(n00/r0)-1;
   estimate 'AR' ((n11/t1)-(n01/t0))/(n11/t1);
   estimate 'PAR' ((r1/total)-(n01/t0))/(r1/total);
   estimate 'NNT' 1/((n11/t1)-(n01/t0));
   estimate 'F1' 2*(((n11/t1)*(n11/r1))/((n11/t1)+(n11/r1)));
   run;

Here are the results from the ESTIMATE statements in PROC NLMIXED, including point estimates, tests, and confidence intervals for the statistics. For each of the lift and LR statistics, the results from the first ESTIMATE statement provide the point estimate and confidence interval. The results from the second ESTIMATE statement provide a test that the ratio function defining the statistic equals 1 – the Estimate and confidence limit values can be ignored:

Other Methods to Estimate and Test the Statistics 

Alternatively, the BINOMIAL option in the TABLES statement of PROC FREQ can be used to obtain asymptotic and exact confidence intervals and an asymptotic test that the proportion equals 0.5 (by default). The BINOMIAL option in the EXACT statement provides all of this plus an exact test of the proportion. You can test against a null value other than 0.5 by specifying P=value in parentheses after the BINOMIAL option. For example, BINOMIAL(P=0.75) tests against the null value of 0.75.

The following statements estimate and test each of the first six statistics as indicated in the TITLE statements. The WHERE statement is used to select the proper row or column for the statistic in each case. The use of LEVEL= in the BINOMIAL option selects the level of TEST or RESPONSE whose probability is estimated:

title 'Sensitivity/TPR/Recall';
proc freq data=FatComp;
   where Response=1;
   weight Count;
   tables Test / binomial(level="1");
   exact binomial;
   run;

title 'Specificity/TNR';
proc freq data=FatComp;
   where Response=0;
   weight Count;
   tables Test / binomial(level="0");
   exact binomial;
   run;

title 'Positive predictive value/Precision';
proc freq data=FatComp;
   where Test=1;
   weight Count;
   tables Response / binomial(level="1");
   exact binomial;
   run;

title 'Negative predictive value';
proc freq data=FatComp;
   where Test=0;
   weight Count;
   tables Response / binomial(level="0");
   exact binomial;
   run;

title 'False Positive Probability/FDR (Col)';
proc freq data=FatComp;
   where Response=0;
   weight Count;
   tables Test / binomial(level="1");
   exact binomial;
   run;

title 'False Positive Probability (Row)';
proc freq data=FatComp;
   where Test=1;
   weight Count;
   tables Response / binomial(level="0");
   exact binomial;
   run;

title 'False Negative Probability/FOR (Col)';
proc freq data=FatComp;
   where Response=1;
   weight Count;
   tables Test / binomial(level="0");
   exact binomial;
   run;

title 'False Negative Probability (Row)';
proc freq data=FatComp;
   where Test=0;
   weight Count;
   tables Response / binomial(level="1");
   exact binomial;
   run;

Below are the results for sensitivity. Note that the estimate, 0.8462, is the same as shown above. An asymptotic confidence interval (0.65, 1) and an exact confidence interval (0.55, 0.98) for sensitivity are given. Also provided are asymptotic and exact one- and two-sided tests of the null hypothesis that sensitivity = 0.5:

The accuracy can be computed by creating a binary variable (ACC), indicating whether test and response agree in each observation. As above, the BINOMIAL option in the TABLES and EXACT statements can be used to obtain asymptotic and exact tests and confidence intervals:

data acc; 
   set FatComp;
   acc = (test = response); 
   run;
proc freq;
   weight count;
   tables acc / binomial(level="1");
   exact binomial;
   run;

The accuracy is again found to be 0.7391 with asymptotic confidence interval (0.56, 0.92) and exact confidence interval (0.52, 0.90). Asymptotic and exact tests of the null hypothesis that accuracy = 0.5 are similar and significant:

The lift values can be estimated in PROC GENMOD by fitting a log-linked binomial model to the data. This models the log of the positive response probabilities in the Test levels. By using the log of the overall probability of positive response as the offset, the log of the lift is modeled. The LSMEANS statement with the ILINK and CL options estimates the lift and provides a confidence interval and a test that the lift equals one:

data lift;
   set FatComp;
   off=log(13/23);
   run;
proc genmod data=lift descending;
   freq count; 
   class test;
   model response=test / dist=binomial link=log offset=off;
   lsmeans test / ilink cl;
   run;

In the results from the LSMEANS statement, the Estimate column contains the log lift estimates. The lift estimates appear in the Mean column and the confidence limits are in the Lower Mean and Upper Mean columns. The p-value for the test that the lift equals one is in the Pr>|z| column:

The likelihood ratios, LR+ and LR-, can be computed from the sensitivity and specificity as described above. Since they can also be seen as nonlinear functions (ratios) of model parameters, they can be computed using the NLEST macro (SAS Note 58775), which provides a large sample test and confidence interval for each. PROC GENMOD is used to fit this linear probability model with TEST as the response and RESPONSE as a categorical predictor:

Pr(TEST=1) = β0RESPONSE0 + β1RESPONSE1 ,

where RESPONSE0 equals 1 if RESPONSE=0, and equals 0 otherwise, and RESPONSE1 equals 1 if RESPONSE=1, and equals 0 otherwise. Under this model, β1 is the sensitivity and β0 is 1-specificity. When fitting the model in PROC GENMOD, include the STORE statement to save the model. Create a data set to be specified in fdata= in the NLEST macro call. The data set should have an observation for each function to be estimated with the function expressions in a character variable named F. Specify a label for the result of each function in a character variable named LABEL. In each function, you refer to the model parameters using names B_P1, B_P2, and so on, in the order shown in the GENMOD results. To test that each estimated function equals 1 rather than 0 (the default), specify null=1 in the macro call. See the description of the NLEST macro for details.

proc genmod data=FatComp descending;
   freq count;
   class response test;
   model test = response / dist=binomial link=identity noint;
   store genfit;
   run;
data fd; 
   length label f $32767; 
   infile datalines delimiter=',';
   input label f; 
   datalines;
LR+, b_p2/b_p1
LR-, (1-b_p2)/(1-b_p1)
;
%nlest(instore=genfit, fdata=fd, null=1, title=Likelihood Ratios)

The point estimates of LR+ and LR- agree with the computations above (2.1154 and 0.2564 respectively). The 95% large sample confidence interval for LR+ is (0.4364, 3.7943) and for LR- is (-0.0926, 0.6081). A test that each ratio equals 1 is provided by specifying null=1 in the NLEST macro call. These results match those from the PROC NLMIXED analysis above:

Computation of the attributable risk and population attributable risk (PAR) requires a data set of event counts and total counts for each population. In the above table, the Test levels are the populations and Response=1 is the event of interest. The TestCnts data set below contains the event counts (Count) and total counts (Total) for each Test population. Note that the population representing presence of the risk factor (Test=1) appears first. PROC STDRATE estimates the two risks by specifying the METHOD=MH(AF) and STAT=RISK options. In the POPULATION statement, the Test variable is identified as the GROUP= variable indicating the populations. The GROUP(EXPOSED="1")=Test option specifies that the Test=1 group is the exposed group. The event and total count variables are specified in the EVENT= and TOTAL= options:

data TestCnts;
   input Test Count Total;
   datalines;
1 11 15
0  2  8
;
proc stdrate data=TestCnts method=mh(af) stat=risk;
   population group(exposed="1")=Test event=Count total=Total;
   run;

The final table from PROC STDRATE presents the two risk estimates and their confidence intervals:

See also the example titled "Computing Attributable Fraction Estimates" in the STDRATE documentation and SAS KB0058201, which discusses adjusting the estimates for covariates. 

The number needed to treat (NNT) can be estimated in various ways. One way is shown above using PROC NLMIXED. Since NNT is equal to the reciprocal of the risk difference, another approach is to fit a logistic model, obtain the individual risk estimates, and then use the NLMeans macro to estimate the appropriate nonlinear function (inverse of the difference) of the risks. See SAS KB0057272 for more on estimating the risk difference.

The following statements fit a logistic model to the Response with Test as the predictor. The LSMEANS statement with the ILINK option estimates the risk at each Test level. The number needed to treat is estimated by specifying the inverse of the risk difference in f= in the NLMeans macro call using the LSMEANS estimates, which it refers by the names MU1 and MU2:

proc logistic data=FatComp;
   freq Count;
   class Test / param=glm;
   model Response(event="1")=Test;
   lsmeans test/ilink e;
   ods output coef=c;
   store Log;
   run;
%nlmeans(instore=Log, coef=c, link=logit, f=1/(mu2-mu1), title=NNT)

The results show that a little over two subjects (2.0690) need to be treated, on average, to obtain one more positive response. A 95% large sample confidence interval for the NNT is (0.4666, 3.6713):

________

Note: Many of these statistics are used to evaluate the performance of a model or classifier on a binary (event/nonevent) response, which assigns a probability of being the event to each observation in the input data set. By selecting a cutoff (or threshold) between 0 and 1 and comparing it to the predicted event probabilities, each observation can be classified as either a predicted event or a predicted nonevent by the model or classifier. A 2×2 table of predicted versus actual response levels can then be constructed and these statistics can be computed. In this way, the statistics can be computed for each cutoff over a range of cutoff values. Using this method, the sensitivity and 1-specificity pairs associated with the various selected cutoffs can be plotted to produce the ROC (Receiver Operating Characteristic) curve. The area under the ROC curve is commonly used to assess the predictive ability of the model.

Similarly, the precision and recall pairs can be plotted to produce the precision-recall (PR) curve. The ROC curve, and the area under it, can be produced by PROC LOGISTIC. See "ROC (Receiver Operating Characteristic) curve" in FASTats (SAS KB0057103). The PR curve, and the area under it, can be produced by the PRcurve macro (SAS Note 68077) and, in SAS Viya, by PROC LOGISTIC.

Another model assessment plots the lift statistic after binning the predicted event probabilities and computing the lift (and other statistics such as gain) in each bin. See the GainLift macro and, for SAS Viya, SAS KB0060503.