@@ -34,23 +34,19 @@ nonParametricPV <- function(outcome, score) {
34
34
ppv <- vapply(
35
35
thresh.predictions ,
36
36
function (x ) {
37
- yardstick :: ppv_vec(
38
- truth = factor (outcome , levels = c(" 1" , " 0" )),
39
- estimate = factor (x , levels = c(" 1" , " 0" )),
40
- event_level = " first"
41
- )
37
+ tp <- sum(outcome == 1 & x == 1 )
38
+ fp <- sum(outcome == 0 & x == 1 )
39
+ tp / (tp + fp )
42
40
},
43
41
numeric (1 )
44
42
)
45
43
46
44
npv <- vapply(
47
45
thresh.predictions ,
48
46
function (x ) {
49
- yardstick :: npv_vec(
50
- truth = factor (outcome , levels = c(" 1" , " 0" )),
51
- estimate = factor (x , levels = c(" 1" , " 0" )),
52
- event_level = " first"
53
- )
47
+ tn <- sum(outcome == 0 & x == 0 )
48
+ fn <- sum(outcome == 1 & x == 0 )
49
+ tn / (tn + fn )
54
50
},
55
51
numeric (1 )
56
52
)
@@ -136,35 +132,27 @@ nonParametricTR <- function(outcome, score) {
136
132
137
133
# Calc sensitivities and specificities at each risk percentile threshold
138
134
senses <- vapply(
139
- thresh.predictions [ 1 : (length( score ) - 1 )] ,
135
+ thresh.predictions ,
140
136
function (x ) {
141
- yardstick :: sens_vec(
142
- truth = factor (outcome , levels = c(" 1" , " 0" )),
143
- estimate = factor (x , levels = c(" 1" , " 0" )),
144
- event_level = " first"
145
- )
137
+ sum(outcome == 1 & x == 1 ) / sum(outcome == 1 )
146
138
},
147
139
numeric (1 )
148
140
)
149
141
150
142
specs <- vapply(
151
- thresh.predictions [ 1 : (length( score ) - 1 )] ,
143
+ thresh.predictions ,
152
144
function (x ) {
153
- yardstick :: spec_vec(
154
- truth = factor (outcome , levels = c(" 1" , " 0" )),
155
- estimate = factor (x , levels = c(" 1" , " 0" )),
156
- event_level = " first"
157
- )
145
+ sum(outcome == 0 & x == 0 ) / sum(outcome == 0 )
158
146
},
159
147
numeric (1 )
160
148
)
161
149
162
150
# Create a data.frame
163
151
dat <- data.frame (
164
- score = c(min( score ), score ) ,
165
- percentile = c( 0 , ecdf(score )(score ) ),
166
- Sensitivity = c( 1 , senses , 0 ) ,
167
- Specificity = c( 0 , specs , 1 )
152
+ score = score ,
153
+ percentile = ecdf(score )(score ),
154
+ Sensitivity = senses ,
155
+ Specificity = specs
168
156
) %> %
169
157
tidyr :: pivot_longer(
170
158
cols = c(" Sensitivity" , " Specificity" ),
0 commit comments