File: SensSpec.demo.R

package info (click to toggle)
r-cran-teachingdemos 2.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,168 kB
  • sloc: makefile: 2
file content (47 lines) | stat: -rw-r--r-- 1,288 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
SensSpec.demo <- function(sens, spec, prev, n=100000, step=11) {
    mat <- matrix(NA, ncol=4, nrow=4)
    dimnames(mat) <- list( Test=c('Positive','Negative','','Total'),
                           Disease=c('   Yes','    No','  ','  Total') )
    pplines <- c(' ',
                 'PPV =',
                 'NPV =')
    mat[4,4] <- n
    if(step>1){
        mat[4,1] <- round(n*prev)
    }
    if(step>2){
        mat[4,2] <- n-mat[4,1]
    }
    if(step>3){
        mat[1,1] <- round( sens*mat[4,1] )
    }
    if(step>4){
        mat[2,1] <- mat[4,1] - mat[1,1]
    }
    if(step>5){
        mat[2,2] <- round( spec*mat[4,2] )
    }
    if(step>6){
        mat[1,2] <- mat[4,2]-mat[2,2]
    }
    if(step>7){
        mat[1,4] <- mat[1,1]+mat[1,2]
    }
    if(step>8){
        mat[2,4] <- mat[2,1]+mat[2,2]
    }
    if(step>9){
        pplines[2] <- paste( 'PPV = ', mat[1,1], '/', mat[1,4],
                             ' = ', round(mat[1,1]/mat[1,4], 4), sep='')
    }
    if(step>10){
        pplines[3] <- paste( 'NPV = ',mat[2,2], '/', mat[2,4],
                             ' = ', round(mat[2,2]/mat[2,4], 4), sep='')
    }

    print(mat, na.print='')
    cat(paste(pplines, collapse='\n'),"\n\n")

    invisible(mat[-3,-3])
}