Logistic Regression on a CSV
Operator: Abdulla Al Fahad. How tools are built and checked
Of 240 loan applicants in the sample, 31 defaulted. A model on income, debt-to-income ratio, years employed and housing finds that each extra point of debt-to-income multiplies the odds of default by 1.15 (95% interval 1.08 to 1.22), while each extra year in the job multiplies them by 0.86 (0.75 to 0.97).
The overall fit is moderate: the likelihood-ratio test gives chi-square 46.2 on 4 degrees of freedom, McFadden R-squared is 0.250 and the area under the ROC curve is 0.833. What one accuracy figure hides is the threshold. At 0.5 the model catches 7 of the 31 defaults (22.6%) while being right on 88.8% of all cases; at the Youden threshold of 0.228 it catches 71.0% of defaults and clears 90.0% of the loans that were repaid.
The page fits the model in your browser by iteratively reweighted least squares. For a numeric outcome use Linear Regression for CSV; this tool is for outcomes with exactly two values.
Key features
- Coefficients with standard errors, Wald z and p, odds ratios and 95% intervals
- Likelihood-ratio test, null and residual deviance, AIC and BIC
- McFadden, Cox-Snell and Nagelkerke pseudo R-squared
- Confusion matrix with accuracy, sensitivity, specificity, precision and F1 at a threshold you type, and a one-click Youden threshold
- ROC curve with the chosen point marked, AUC and a Hanley-McNeil interval
- Hosmer-Lemeshow calibration test with the observed and expected events for each group, and a warning when the fit shows signs of separation
- Predicted probabilities for every row as CSV, a form for predicting a new case, and an XLSX with odds-ratio formulas
How the model is fitted, and how the threshold changes every classification number
Pick an outcome column with exactly two distinct values (0 and 1, yes and no, or any two labels), choose which value counts as the event, and mark each other column as a number, a category or not used. Categories are dummy coded against a reference category, the most frequent one unless you choose another, with at most 50 levels per column. Rows with a blank or unusable value in the outcome or any used predictor are left out and counted by column. At least 20 complete rows are needed, with at least 5 of the rarer outcome, up to 200,000 rows.
How it works
Starting from the log-odds of the overall event rate, each iteration builds working weights p(1 - p) and a working response, solves the weighted least-squares normal equations, and halves the step if the deviance would increase. It stops when the relative deviance change is under 1e-10, or after 100 iterations. Columns that are exact linear combinations of others are found with a QR step and dropped before fitting, as lm does, and named in a warning.
Standard errors come from the inverse of the information matrix X'WX at the solution. The z statistic is the estimate over its standard error, p is two-sided from the normal distribution, and odds-ratio intervals exponentiate the estimate plus or minus 1.96 standard errors. The likelihood-ratio statistic is null deviance minus residual deviance, referred to chi-square with one degree of freedom per slope. McFadden is 1 - logL / logL0, Cox-Snell is 1 - exp(2 (logL0 - logL) / n) and Nagelkerke divides that by its maximum, 1 - exp(2 logL0 / n).
The ROC curve is traced over every distinct fitted probability and the AUC is its trapezoid area, with tied scores counted half as in the Mann-Whitney statistic. The Youden threshold maximises sensitivity plus specificity minus 1. Hosmer-Lemeshow cuts the fitted probabilities at their deciles (fewer cuts for small samples), puts tied probabilities in the same group so the result does not depend on row order, and compares observed with expected events; its statistic has groups minus 2 degrees of freedom, and the test is left out if ties leave fewer than 4 groups.
Common use cases
- A lender checking which application fields move the odds of default, and reading the interval on housing (0.18 to 1.11, which includes 1) as no clear difference.
- A subscription team modelling churn from plan, tenure and support tickets, then choosing a threshold that flags the accounts worth a retention call.
- A clinical dataset with a 0/1 outcome and a mixture of lab values and categories, with the Hosmer-Lemeshow groups used to check calibration.
- A student assignment: replicate a textbook logistic regression, checking deviance and AIC against a statistics package.
How to use this tool
- Open a table with a two-valued outcome column, or press Load example for the 240 sample applicants.
- Choose the outcome column and the value that counts as the event.
- Mark each predictor as a number, a category (with a reference) or not used.
- Read the coefficients and odds ratios, then the fit statistics and any warnings.
- Type a threshold, or use the Youden threshold, and read the confusion matrix and ROC curve.
- Download the predictions, coefficients, ROC points or the XLSX.
Example
Excerpt of the example (240 applicants)
defaulted,income_k,debt_to_income,years_employed,housing
0,64,39.6,8,own
0,33,23.4,5,own
0,29,18.8,10,own
...Odds ratios and fit
debt_to_income OR 1.148 (1.080 to 1.220) p < 0.0001
years_employed OR 0.855 (0.754 to 0.969) p = 0.0142
income_k OR 0.963 (0.934 to 0.994) p = 0.0185
housing[own] OR 0.442 (0.177 to 1.108) p = 0.0818 (reference: rent)
Deviance 184.70 to 138.53, AIC 148.53, McFadden 0.250, Nagelkerke 0.326, AUC 0.833 (0.743 to 0.924)
Threshold 0.5: TP 7, FN 24, FP 3, TN 206. Threshold 0.25: TP 21, FN 10, FP 18, TN 191Common pitfalls
Perfect or near-perfect separation, where a predictor splits the outcomes cleanly, makes coefficients run off towards infinity. The page flags it, and the odds ratios it shows in that case should not be quoted.
An odds ratio is not a risk ratio. At a 13% event rate the two are close; at 40% an odds ratio of 2 is nowhere near double the probability.
Accuracy, AUC and the confusion matrix are computed on the same rows the model was fitted to, so they are optimistic. Fit on part of the data and check the held-out rows with the new-case form before relying on them.
The Hosmer-Lemeshow test has little power in small samples and flags trivial misfit in very large ones; read the group table, not only the p-value.
- With a rare outcome such as 31 events in 240, a 0.5 threshold is rarely the useful one. Start from the Youden threshold, then move it according to what a missed event costs compared with a false alarm.
Common errors and how to fix them
- "status" has 3 different values (active, churned, paused)
- The outcome must have exactly two values. Recode it, for example churned versus everything else, in the source file.
- Only 3 rows have the rarer outcome. At least 5 are needed for a stable fit
- Too few events to estimate coefficients. Collect more events, widen the definition of the event or use fewer predictors.
- The predictors are too collinear for the fit to be stable
- Two predictors carry almost the same information. Drop one of them, or merge categories that overlap.
Best practices
- Check events per predictor before trusting an individual coefficient. A common guide is about 10 events of the rarer outcome per estimated slope; the example has 31 events for 4 slopes, so its intervals are wide.
- Choose the reference category on purpose. Every dummy coefficient is a comparison with it, so pick the baseline your reader thinks in, such as renting, rather than whichever level is most frequent.
Reading an odds ratio
Odds are the probability of the event divided by the probability of not having it. A 13% chance is odds of about 0.15 to 1. An odds ratio of 1.15 for debt-to-income means that one more point multiplies those odds by 1.15, whatever the starting level, which moves a 13% chance to roughly 14.6%, and the same point at a 50% chance to about 53.5%.
Intervals that include 1 mean the data cannot tell the effect from none. The housing coefficient in the example has an odds ratio of 0.44 for owning against renting, but its interval of 0.18 to 1.11 includes 1, so the page does not claim a difference.
Frequently asked questions
Why does a 0.5 threshold give such low sensitivity?
The model's probabilities are centred on the base rate. With 13% defaults, few applicants reach a fitted probability of 0.5, so a 0.5 cut-off labels almost everyone as a non-default. A lower threshold trades false alarms for caught events, which the confusion matrix shows.
Can the outcome have more than two values?
Not in one run. Recode it to one value against the rest and run it once per value of interest; each run models the probability of that value.