igor-millis~/work/dengue-logistic-regression
EN/PT
← all work · statistics · generalized linear models

Dengue Risk Model

Who becomes a dengue case? A logistic regression built step by step: which variables earn a place, whether age acts as a straight line, which link to use — ending with two defensible models and the trade-off between them.

RGLMlogistic regressionDHARMapROCR Markdown
196
individuals · 57 cases (29.1%)
0.0045
p-value of the age-curvature test
6.06
AIC drop when age gets a quadratic term
2
candidate models, choice left open
data
course dataset · 196 individuals · age, level, sector, case (yes/no)
model
binomial GLM · logit link · profile-likelihood intervals
tools
R 4.1 · DHARMa · pROC · car · ggplot2 · R Markdown
checks
likelihood-ratio tests · link comparison · simulated residuals · Cook · AUC
model card
huggingface.co/igorlimamillis/dengue-logistic-regression
status
course project · final model choice left open on purpose
01 · data

196 people, one yes/no outcome

Each row is one individual: age, socioeconomic level (1 high, 2 medium, 3 low), city sector (1 or 2) and whether they became a case. Of the 196 individuals, 57 (29.1%) are cases. The charts show raw shares, before any model.

Two things stand out. The share of cases is much higher in sector 2 than in sector 1, while the three levels look alike. By age, the share jumps after the first decade and then wanders — but only 36 people are 40 or older, so the intervals there are wide.

observed share of casesIC 95% Wilson
0.000.100.200.300.400.500.60sector 1sector 1: 0.188 [0.128, 0.268]0.188sector 2sector 2: 0.443 [0.339, 0.553]0.443level 1level 1: 0.312 [0.219, 0.422]0.312level 2level 2: 0.286 [0.178, 0.424]0.286level 3level 3: 0.271 [0.181, 0.385]0.271
Wilson 95% intervals. Raw data, no model.
observed share of cases by ageIC 95% Wilson
0.000.200.400.600.800–9 (n = 48)0–9 (n = 48): 0.062 [0.021, 0.168]0.06210–19 (n = 44)10–19 (n = 44): 0.295 [0.182, 0.442]0.29520–29 (n = 36)20–29 (n = 36): 0.361 [0.225, 0.524]0.36130–39 (n = 32)30–39 (n = 32): 0.375 [0.229, 0.547]0.37540–49 (n = 11)40–49 (n = 11): 0.455 [0.213, 0.720]0.45550–59 (n = 8)50–59 (n = 8): 0.375 [0.137, 0.694]0.37560+ (n = 17)60+ (n = 17): 0.471 [0.262, 0.690]0.471
Age bands in years; n = people in the band.
02 · selecting

Which variables earn a place

The starting point is caso ~ nivel + idade + setor (case, level, age, sector — the variable names of the dataset) with a logit link. Age and sector are clearly associated with the outcome; the two level coefficients cannot be told apart from zero. Coefficients are shown as odds ratios, holding the other variables fixed.

initial model · odds ratiosOR · IC 95%
0.501248level 2 vs 1OR 1.05 [0.44, 2.44]1.05level 3 vs 1OR 1.29 [0.58, 2.88]1.29age (per year)OR 1.03 [1.01, 1.05]1.03sector 2 vs 1OR 3.47 [1.76, 7.03]3.47
Log axis; intervals by profile likelihood.

Those p-values compare each level with level 1. Whether the whole factor helps — and whether any interaction does — is a question for likelihood-ratio tests between nested models:

added termΔ devianced.f.p-value
level, with linear age0.41920.811
level, with quadratic age1.31720.518
age × sector, with linear age0.26310.608
age × sector, with quadratic age0.29910.584
age and age² × sector1.88420.390

None improves the fit, so the candidate models drop level and keep the effects additive.

03 · age

Age is not a straight line

A single OR per year assumes every extra year adds the same amount to the log-odds. Adding an age² term tests that assumption: the deviance drops by 8.06 with one degree of freedom (p = 0.0045) and the AIC goes from 217.64 to 211.58. The fitted curve peaks near 47.5 years (95% CI 36 to 59) — wide enough that the exact position of the peak is uncertain.

The quadratic term never enters without the linear one: dropping it would force the vertex to age zero. The figure compares the two age models by sector.

fitted probability · sector 1P(case)
A · linear ageB · quadratic age
0.000.250.500.751.000204060800–19 · n = 54 · 7%20–39 · n = 47 · 32%40–59 · n = 9 · 22%60+ · n = 7 · 14%
fitted probability · sector 2P(case)
0.000.250.500.751.000204060800–19 · n = 38 · 32%20–39 · n = 21 · 48%40–59 · n = 10 · 60%60+ · n = 10 · 70%

Bands are 95% confidence intervals; circles are the observed shares in the bands 0–20, 20–40, 40–60 and 60+. The models agree up to about 40 years; beyond that the data are scarce and the bands of the quadratic model widen a lot.

No age × sector interaction was found, so the sector effect is the same at every age: the two panels differ only by a vertical shift on the logit scale.

04 · reading it

One odds ratio per interval, not per year

For ages a₁ and a₂ the log-odds difference is b₁(a₂−a₁) + b₂(a₂²−a₁²); the intercept and the sector term cancel. Model A gives the same OR for any 10-year interval, model B does not. Only 36 individuals are 40 or older (17 are 60 or older), so the intervals in that region are wide and lean on the assumed quadratic shape.

10-year intervalmodel A · OR (95% CI)model B · OR (95% CI)
20 → 301.31 (1.10 to 1.55)1.71 (1.30 to 2.24)
30 → 401.31 (1.10 to 1.55)1.35 (1.12 to 1.62)
40 → 501.31 (1.10 to 1.55)1.06 (0.84 to 1.34)
50 → 601.31 (1.10 to 1.55)0.84 (0.58 to 1.21)
60 → 701.31 (1.10 to 1.55)0.66 (0.39 to 1.12)
model B · OR per 10 yearsOR · IC 95%
0.501220 → 30OR 1.71 [1.30, 2.24]1.7130 → 40OR 1.35 [1.12, 1.62]1.3540 → 50OR 1.06 [0.84, 1.34]1.0650 → 60OR 0.84 [0.58, 1.21]0.8460 → 70OR 0.66 [0.39, 1.12]0.66
Wald intervals; the covariance between the two age coefficients is taken into account.

In model B the OR is clearly above 1 for 20–30 and 30–40 years. From 40 on every interval includes 1, so the data do not show whether risk plateaus or falls.

05 · link & checks

Link function and diagnostics

The logit link was compared with probit and complementary log-log in both models. They have the same number of parameters, so AIC compares them directly; the differences are below 2, meaning the data do not tell them apart. The logit stays because its coefficients are odds ratios. A link test (adding the squared linear predictor) gives p = 0.441 for model A and p = 0.188 for model B: no sign of a misspecified link.

link function (AIC)model Amodel B
logit217.64211.58
probit217.34210.85
cloglog218.11212.56

Lower is better; differences under 2 are not meaningful.

check (p-value unless noted)model Amodel B
uniformity of simulated residuals (KS)0.9770.976
dispersion0.8400.812
outliers1.0001.000
largest Cook's distance0.0530.037
AUC0.7250.745

Simulated residuals (DHARMa, 500 simulations). Usual Cook's distance cut-offs are 0.5 and 1. The AUC is computed on the same data used to fit.

No check flags a problem, which is weaker evidence than it sounds: with a binary outcome and n = 196, residual-based tests have little power to detect curvature. Model A passes them, yet the likelihood-ratio test of section 03 found the curvature — a reminder to test the specific structure you care about.

06 · candidates

Two defensible models, one open decision

The analysis ends with two candidates that differ only in how age enters.

A · linear ageB · quadratic age
formulacaso ~ idade + setorcaso ~ idade + idade² + setor
AIC217.64211.58
BIC227.47224.69
AUC0.7250.745
R² McFadden0.1040.139
OR, sector 2 vs 1 (95% CI)3.26 [1.70, 6.39]3.59 [1.83, 7.23]
effect of ageone OR per year (1.027)a curve: one OR per interval
Choice left open on purpose. B fits better by AIC, BIC and the likelihood-ratio test; A has a single, directly interpretable OR for age, and the sector OR is almost the same in both. If age is only an adjustment variable, the sector OR is the main result and the choice matters little; if age itself is the exposure of interest, the curvature should not be ignored. If A is chosen, the write-up should say that the curvature test was significant and that the choice was made for parsimony.
07 · limits

What this does not settle

  • Association, not cause. Observational data: odds ratios describe associations.
  • Same data to choose and to judge. The model was selected and assessed on the same sample; p-values ignore the selection and the AUC is apparent, with no external validation.
  • Few older people. Only 36 individuals are 40 or older, so the right end of the age curve is poorly estimated and the quadratic shape extrapolates there.
  • Low-power checks. Residual diagnostics and the link test detect little with a binary outcome and 196 observations.
  • Data. The raw file comes from a course and is not redistributed here; this page shows only aggregated results.
  • Model card. Hugging Face ↗ — coefficients of both models, the full R report and the analysis code (no raw data).
Next steps. With more data, check whether sector 1 and sector 2 should be modeled separately. No age × sector interaction was detected here, but with 196 individuals those tests have little power; a larger sample could show that the age curve or the other effects differ between sectors.