Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. # function
  2. # 'screen' is numeric vector of scores from the screening tool
  3. # 'gs' is a logical vector showing gold standard disease status
  4. # 'vals' is a numeric vector of screening tool values to test (including all values in 'screen')
  5. # returns a ROC plot and a list where the first value is the sensitivity and specificity for all values of 'val', and the second is the area under ROC
  6.  
  7. easyRoc <- function(screen, gs, vals) {
  8. pos <- screen >= rep(vals, each = length(screen))
  9. pos <- matrix(pos, ncol = length(vals))
  10. sens <- colSums(gs & pos) / sum(gs)
  11. spec <- colSums(!gs & !pos) / sum(!gs)
  12. plot(0, ylim = c(0, 1), xlim = c(0, 1), type = 'n', xlab = 'Specificity', ylab = 'Sensitivity', axes = F)
  13. lines(1-spec, sens, lwd = 1.5)
  14. aurx <- c(1, 1, 1-spec)
  15. aury <- c(0, 1, sens)
  16. polygon(aurx, aury, col = 'grey93')
  17. segments(0, 0, 1, 1, lty = 3)
  18. rect(0, 0, 1, 1)
  19. axis(1, seq(0, 1, 0.2), seq(1, 0, -0.2), pos = 0)
  20. axis(2, seq(0, 1, 0.2), pos = 0, las = 2)
  21. n <- length(aurx)
  22. a <- sum(aurx[1:(n - 1)] * aury[2:n]) + aurx[n] * aury[1]
  23. b <- sum(aurx[2:n] * aury[1:(n - 1)]) + aurx[1] * aury[n]
  24. aur <- 0.5 * (a - b)
  25. text(0.8, 0.2, paste0('AUR=',round(aur, 2)))
  26. return(list(res = cbind(cutoff = vals, sens, spec), AUR = aur))
  27. }
  28.  
  29. # example
  30.  
  31. dat <- data.frame(screen = c(rbinom(100, 50, 0.2), rbinom(100, 50, 0.5)),
  32. disease = c(rbinom(100, 1, 0.1), rbinom(100, 1, 0.6)))
  33. boxplot(screen ~ disease, dat)
  34.  
  35. par(mar = c(4, 4, 1, 1))
  36. y <- easyRoc(dat$screen, dat$disease, 0:50)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement