library(readstata13)
thuan = read.dta13("D:\\OneDrive - UMP\\Desktop\\HUONG201022lamsach.dta")
attach(thuan)
# Gọi thư viện
library(pROC)
## Type 'citation("pROC")' for a citation.
## 
## Attaching package: 'pROC'
## The following objects are masked from 'package:stats':
## 
##     cov, smooth, var
  roc(songcon, Dim1)
## Setting levels: control = 0, case = 1
## Setting direction: controls < cases
## 
## Call:
## roc.default(response = songcon, predictor = Dim1)
## 
## Data: Dim1 in 225 controls (songcon 0) < 8 cases (songcon 1).
## Area under the curve: 0.7967
  roc.Dim = roc(songcon ~ Dim1, 
              col="blue",
              percent = T, 
              partial.auc.correct = T, 
              partial.auc.focus="sens",
              ci= T, 
              boot.n = 1000, 
              ci.alpha = 0.95, 
              stratified =T, 
              plot = T, 
              auc.polygon = T, 
              max.auc.polygon = T, grid = T, 
              print.auc = T, show.thres = T) 
## Setting levels: control = 0, case = 1
## Setting direction: controls < cases
plot.roc(smooth(roc.Dim),add = TRUE, col ="red")

# dán nhãn legend(“bottomright”,legend=c(“Điển cắt”, “Đường cong”), col = c(par(“fg”), “blue”), lwd=“3”) ## Tìm điểm ngưỡng cắt tốt nhất



```r
 coords(roc.Dim, "best", ret=c("threshold", "sens", "specificity"))
##   threshold sensitivity specificity
## 1      1042        87.5    78.22222
# Các điểm ngưỡng cắt tốt
coords(roc.Dim, "local maximas",ret=c("threshold", "sens", "spec", "ppv", "npv"))
##    threshold sensitivity specificity       ppv       npv
## 1      198.0       100.0    12.00000  3.883495 100.00000
## 2     1042.0        87.5    78.22222 12.500000  99.43503
## 3     1212.5        75.0    82.66667 13.333333  98.93617
## 4     1536.0        50.0    88.88889 13.793103  98.03922
## 5     4161.5        37.5    96.88889 30.000000  97.75785
## 6     6410.0        25.0    97.33333 25.000000  97.33333
## 7     8940.5        12.5    98.66667 25.000000  96.94323
## 8    41327.5         0.0    99.11111  0.000000  96.53680
## 9    58329.0         0.0    99.55556  0.000000  96.55172
## 10       Inf         0.0   100.00000       NaN  96.56652
coords(roc.Dim,"best",ret=c("threshold", "sens", "spec", "ppv", "npv", "1-npv"))
##           threshold sensitivity specificity  ppv      npv     1-npv
## threshold      1042        87.5    78.22222 12.5 99.43503 0.5649718
# KTC của ROC     
ci(roc.Dim)
## 95% CI: 59.78%-99.55% (DeLong)