---
# System prepended metadata

title: 'Sensitivity, Specificity, and ROC Curve'
tags: [Predictive Value, R, Statistics, Sensitivity, Specificity, ROC curve]

---

# Sensitivity, Specificity, and ROC Curve

###### tags: `R` `Statistics` `Sensitivity` `Specificity` `Predictive Value` `ROC curve`

## Dataset
**Data Set Information:**
** This dataset contains the medical records of **229 geriatric patients who visited WF ER**. Each patient profile has **22 clinical variables**.
<br/>

**Attribute Information:**
* Age: year	
* Sex: male = 1
* Admission: admitted = 1	
* ICU: ICU admission; admitted = 1	
* LOS:	length of stay; day
* DEMENTIA: dementia (+) = 1	
* CVA: CVA (+) =1
* Liver_d: liver disease (+) = 1
* DM: DM (+) =1
* CKD: CKD (+) = 1
* tumor: any cancer (+) = 1 
* ISAR: Identification of Seniors At Risk (ISAR) * score
* CCI: The Charlson Comorbidity Index	
* Katz: Katz Index of Independence
* AD8: Dementia Screening Interview
* SOF: The study of osteoporotic fractures (SOF) index	
* MNA: The Mini Nutritional Assessment (MNA)	
* BSRS-5: The 5-item Brief Symptom Rating Scale
* x72hrs_return: 72 hours ER return
* x30D: 30 days mortality
* x30R: 30 days ER return
* x30A: 30 days hospital admission

## Sensitivity, Specificity, and Predictive Value

```r=
install.packages("caret")
install.packages("pROC")

# Load library
library(caret)
library(pROC)
library("PerformanceAnalytics")

# import geriatric.txt file and name it data
data <- read.table("data/geriatric_2.txt", header = T, sep = "\t")
str(data)
# Transform data
data$Sex <- as.factor(data$Sex)
data$Admission <- as.factor(data$Admission)
data$x72hrs_return <- as.factor(data$x72hrs_return)
data$x30D <- as.factor(data$x30D)
data$x30R <- as.factor(data$x30R)
data$x30A <- as.factor(data$x30A)
str(data)

all_index <- data %>% select(ISAR, CCI, Katz, SPSMQ, SOF, MNA, BSRS.5)
# Correlation MAtrix
chart.Correlation(all_index, histogram=TRUE, pch=40)

# use MNA < 7 as a cut off for admission prediction
data$predit_adm <- as.integer(data$MNA < 7)

### make a two way table for analysis
### table(vertical, Horizontal)

table <- table(data$predit_adm,data$Admission)
table
table <- table(factor(data$predit_adm,levels = c("1","0")),factor(data$Admission,levels = c("1","0")))
table

# rename the column and row
colnames(table) <- c("ADM","Non-ADM") #predit
rownames(table) <- c("ADM","Non-ADM") # true
table

# calculate the Sensitivity, Specificity, and Predictive Value
confusionMatrix(table)

```

##ROC Curve and AUC 
```
# Create ROC model
myroc <- roc(data$Admission, data$MNA,levels=c( "1","0"),
             percent = TRUE, 
             ci = TRUE, 
             of = "se", ci.type="shape" )
myroc

# Plot ROC curve
plot.roc(myroc, print.thres="best", print.thres.best.method="youden", print.auc=TRUE )
plot.roc(myroc, print.thres="best", print.thres.best.method="youden", print.auc=TRUE, legacy.axes= T )

# change the setting
myroc1 <- roc(data$Admission, data$MNA,levels=c( "1","0"),
             of = "se", ci.type="shape" )

myroc1

plot.roc(myroc1, print.thres="best", print.thres.best.method="youden", print.auc=TRUE, legacy.axes= T )


#Use cutoff value = 10

data$predit_adm <- as.integer(data$MNA < 10)
table2 <- table(factor(data$predit_adm,levels = c("1","0")),factor(data$Admission,levels = c("1","0")))
colnames(table2) <- c("ADM","Non-ADM") #predit
rownames(table2) <- c("ADM","Non-ADM") # true
confusionMatrix(table2) 

```

