`Ballesta-Castillejos A, Gómez-Salgado J, Rodríguez-Almagro J, Hernández-Martínez A. Development and validation of a predictive model of exclusive breastfeeding at hospital discharge: Retrospective cohort study. Int J Nurs Stud. 2021 May;117:103898. doi: 10.1016/j.ijnurstu.2021.103898. Epub 2021 Feb 7. PMID: 33636452.`

## 理论铺垫

Sensitivity: A/(A+C) × 100

Specificity: D/(D+B) × 100

Positive Predictive Value: A/(A+B) × 100

Negative Predictive Value: D/(D+C) × 100

(positive likelihood ratio, LR+)，似然比的概念请参考下一段英文描述：

`Likelihood ratio (LR) is the ratio of two probabilities: (i) probability that a given test result may be expected in a diseased individual and (ii) probability that the same result will occur in a healthy subject.`

(LR-) = (1 – sensitivity) / specificity，意思就是假阴性率与真阴性率之比。表示错误判断阴性的可能性是正确判断阴性可能性的倍数。其比值越小，试验结果阴性时为真阴性的可能性越大。

## 在实战中理解ROC曲线

`model_glm = glm(default ~ balance, data = default_trn, family = "binomial")`

```test_pred_10 = get_logistic_pred(model_glm, data = default_tst, res = "default",
pos = "Yes", neg = "No", cut = 0.1)
test_pred_50 = get_logistic_pred(model_glm, data = default_tst, res = "default",
pos = "Yes", neg = "No", cut = 0.5)
test_pred_90 = get_logistic_pred(model_glm, data = default_tst, res = "default",
pos = "Yes", neg = "No", cut = 0.9)
test_tab_10 = table(predicted = test_pred_10, actual = default_tst\$default)
test_tab_50 = table(predicted = test_pred_50, actual = default_tst\$default)
test_tab_90 = table(predicted = test_pred_90, actual = default_tst\$default)
test_con_mat_10 = confusionMatrix(test_tab_10, positive = "Yes")
test_con_mat_50 = confusionMatrix(test_tab_50, positive = "Yes")
test_con_mat_90 = confusionMatrix(test_tab_90, positive = "Yes")
metrics = rbind(
c(test_con_mat_10\$overall["Accuracy"],
test_con_mat_10\$byClass["Sensitivity"],
test_con_mat_10\$byClass["Specificity"]),
c(test_con_mat_50\$overall["Accuracy"],
test_con_mat_50\$byClass["Sensitivity"],
test_con_mat_50\$byClass["Specificity"]),
c(test_con_mat_90\$overall["Accuracy"],
test_con_mat_90\$byClass["Sensitivity"],
test_con_mat_90\$byClass["Specificity"])
)
rownames(metrics) = c("c = 0.10", "c = 0.50", "c = 0.90")
metrics```

## 预测模型R语言实操

`model_glm = glm(default ~ balance, data = default_trn, family = "binomial")`

`exp(cbind(OR = coef(model_glm), confint(model_glm)))`

`model_glm_pred = ifelse(predict(model_glm, type = "response") > 0.5, "Yes", "No")`

`test_roc = roc(default_tst\$default ~ test_prob, plot = TRUE, print.auc = TRUE)`