π― Trigger: binary target β glm(β¦, family = binomial) β predict(β¦, type = "response") on test β my.pred.stats β step(k = β¦) β re-score.
β‘ Key Constraint:predict() returns log-odds by default β without type = "response" every threshold, AUC and log-loss is computed on the wrong scale.
Doctorβs reading β PLAS, AGE and PED (family history) canβt really be changed; BMI can β log-odds of diabetes fall by 3.157 per unit decrease in log(BMI).
BIC vs KIC β KIC predicts best but keeps 20 terms incl. interactions, with a log(BMI) coefficient of 52.1 beside BMI and BMI2 β likely overestimated; trust the simpler modelβs coefficients for explanation, the KIC model when prediction is all that matters.
π Variations
Transformations in the formula β . + log(BMI) Β· . + I(PLAS^2) (I() protects ^) Β· . + SKIN*AGE (adds the product term SKIN:AGE) Β· . + .*. = all pairwise interactions Β· log(PREG+1) guards log0.
Added to ~ .
Termβs p-value
Deviance drop from 618.08
Verdict
log(BMI)
0.0013
10.76
nonlinear BMI effect likely
I(PLAS^2)
0.704
0.14
no evidence of curvature
SKIN*AGE
0.015
5.16
interaction probably associated, though SKIN alone is not
Criterion via k β k = 2 AIC (default) Β· k = 3 KIC Β· k = log(nrow(df)) BIC β least to most conservative.
p close to n (gene data) β n=200, p=100 binary SNP factors, 10,000 balanced test rows:
Model
Params
CA
Sens
Spec
AUC
Log-loss
full
101
0.535
0.562
0.508
0.552
18112
AIC step
26
0.544
0.551
0.537
0.567
10962
BIC step
3
0.623
0.324
0.923
0.622
6678
Gene reading β full fit: null deviance 277.26 β residual 154.23, AIC =154.23+2(101)=356.23, yet test AUC β chance βΉ overfitting. BIC keeps only Ξ·^β=β0.112β1.134SNP12+1.460SNP56 β a SNP12 mutation lowers the log-odds of disease by 1.134; a SNP56 mutation raises them by 1.460.
Solution-comment correction β the studio notebook says BIC βgreatly improved sensitivityβ; the rerun shows the reverse β sensitivity 0.56β0.32, specificity 0.51β0.92 β while accuracy, AUC and log-loss all improved.
βοΈ Practice
Practice 1: From df (factor target y, levels N/Y) and newdf, fit the full logistic model, prune it by KIC, and print only the test AUC.
Reference solution
full = glm(y ~ ., data = df, family = binomial)kic = step(full, k = 3, direction = "both", trace = 0)p = predict(kic, newdf, type = "response")roc(response = newdf$y, p)$auc
Key move:type = "response" before roc; k = 3 is the whole AIC β KIC change.
Practice 2: A logistic model has 100 predictors and residual deviance 154.23. Compute R's AIC, then the lecture-scale NLL and AIC.
Reference solution
R scale β AICRβ=deviance+2k=154.23+2(101)=356.23.
Key move: columns are the truth, so sensitivity divides by the Y column; my.pred.stats hard-codes the 21β threshold.
β οΈ Common Mistakes
π‘ Omitting family = binomial β glm silently fits a Gaussian linear model.
π‘ Target not a factor β read with stringsAsFactors = TRUE; R treats the second level as success, and my.pred.stats hard-codes the labels "N"/"Y".
π‘ Studio 10βs my.pred.stats is a new version β display = Freturnslist(ca, sens, spec, auc, log.loss) instead of printing β needed to feed a statistic to boot() β Resampling Inference in R (boot, boot.ci, perm.log.reg).
π‘ Reading my.pred.stats log-loss as an average β it is a sum over the test set (18112 over 10,000 rows) β compare only on the same test data.
π‘ Judging a pβn model in-sample β deviance 277β154 looks excellent; test AUC 0.55 says otherwise.