- SEIR outbreak in a partly vaccinated population
- From early growth to R and to the final attack rate
- Weekly incidence report with exact Poisson limits
- Hospital admissions: delay from cases and short-term forecast
- Case-control study: odds ratio and attributable fraction
- Diagnostic test: Wilson interval, likelihood ratios, predictive values
- Incubation period and quarantine length
- Occupational cohort: SMR with exact Poisson test
- Vaccine effectiveness from a test-negative study
- Direct age standardization of mortality
- Odds ratios of a case-control study
- Incidence rate ratio with a Poisson regression
- Clusters and super-spreaders of a contact network
- Detect a rare disease with SMOTE
- Rare symptoms that come together
- Survival in a clinical trial
- Meta-analysis of 13 vaccine trials
- Hospital admissions, cold and heat (Poisson GAM)
- Did the hygiene program reduce the infections? (Poisson mixed model)
- Relapse when death competes: cumulative incidence and Fine-Gray
- Observational study: effect of a treatment with a propensity score
- A treatment received during the follow-up: Cox model with a time-varying covariate
- Dengue incidence by town: spatial autocorrelation and hot spots
- Effect of a treatment in observational data: doubly robust estimate and E-value
SEIR outbreak in a partly vaccinated population
A measles-like virus (R0 = 12, latent 8 days, infectious 5 days) enters a population with 85 % coverage of a 95 % effective vaccine. The script computes the herd immunity threshold 1 - 1/R0 (91.7 %), starts the immune fraction in R and integrates the SEIR model over one year to see the outbreak that still happens.
#1. Measles-like SEIR: R0 = 12, latent 8 days, infectious 5 days; -> "[R0]" 12; -> "[sigma]" (math calc "1/8"); -> "[gamma]" (math calc "1/5"); -> "[beta]" (math calc "[R0]*[gamma]"); #2. Herd immunity threshold 1 - 1/R0, coverage 85 % with a 95 % effective vaccine; -> "[pc]" (math calc "1-1/[R0]"); -> "[v]" (math calc "0.85*0.95"); #3. SEIR over one year starting from 10 cases per 100 000; plot ode (num ode "{\"S\": \"-[beta]*S*I\", \"E\": \"[beta]*S*I-[sigma]*E\", \"I\": \"[sigma]*E-[gamma]*I\", \"R\": \"[gamma]*I\"}" (concat "{\"S\": " (math calc "1-[v]-0.0001") ", \"E\": 0, \"I\": 0.0001, \"R\": " [v] "}") 0 365 "{\"points\": 366}")
The chart is shown in the Plot tab of the editor.
From early growth to R and to the final attack rate
Fit the exponential growth rate of the first two weeks of daily cases, turn it into a reproduction number with a gamma generation interval (Wallinga-Lipsitch: R = 1 / M(-r), integral computed with the gamma function) and solve the final size equation z = 1 - exp(-R z).
#1. Daily confirmed cases during the first two weeks of the outbreak; -> "[d]" "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]"; -> "[c]" "[12, 15, 19, 23, 30, 37, 45, 57, 70, 88, 108, 135, 166, 205]"; #2. Exponential growth rate r and doubling time; -> "[r]" (json load select "rx_gr" (stat fit_model "a*exp(r*x)" "x" [d] [c] "{\"a\": 10, \"r\": 0.2}") "/parameters/r"); -> "[td]" (math calc "ln(2)/[r]"); #3. Generation interval: gamma law, mean 5.2 days, sd 1.7 days; -> "[k]" (math calc "(5.2/1.7)^2"); -> "[th]" (math calc "1.7^2/5.2"); #4. Wallinga-Lipsitch: R = 1 / integral of exp - r t times g of t; -> "[M]" (num integrate (concat "exp(-" [r] "*t)*t^(" [k] "-1)*exp(-t/" [th] ")/(gamma(" [k] ")*" [th] "^" [k] ")") "t" 0 "inf"); -> "[R]" (math calc "1/[M]"); #5. Final size without control: z = 1 - exp - R z, and 7-day growth factor; -> "[z]" (num solve (concat "1-exp(-" [R] "*x)-x") "x" 0.01 0.999); -> "[g7]" (bigdec pow (math exp [r]) 7 6); concat "r = " (math round [r] 4) " per day, doubling time " (math round [td] 2) " days, R = " (math round [R] 3) " (closed form " (math round (math calc "(1+[r]*[th])^[k]") 3) "), expected attack rate " (math round (math calc "100*[z]") 1) " %, growth factor over 7 days x" [g7]
r = 0.2149 per day, doubling time 3.22 days, R = 2.874 (closed form 2.874), expected attack rate 93.1 %, growth factor over 7 days x4.50232
- Get the uncertainty of r from the field
errorsofstat fit_modeland propagate it to R withnum uncertainty - Compare with an exponential generation interval: R = 1 + r Tg
- Draw the fit with
plot fit
Weekly incidence report with exact Poisson limits
Four weeks of daily notifications show a weekend under-reporting artefact. The script measures the weekly cycle (autocorrelation at lag 7 and spectrum at 1/7 per day on the detrended series), smooths with a 7-day moving average, and reports the 7-day incidence per 100 000 with exact Poisson limits (Garwood) from the regularized incomplete gamma function.
#1. Daily notified cases over 4 weeks: low on weekends; -> "[cases]" "[40, 46, 51, 48, 44, 21, 18, 47, 55, 58, 53, 50, 24, 20, 56, 62, 66, 61, 57, 27, 23, 64, 70, 73, 69, 63, 30, 26]"; #2. 7-day moving average and weekly periodicity of the detrended series; -> "[ma]" (dsp moving_average [cases] 7); -> "[dt]" (dsp detrend [cases]); -> "[r7]" (json load select "rx_ac" (dsp autocorr [dt] 7) "/[7]"); -> "[a7]" (json load select "rx_sp" (dsp spectrum [dt] 1) "/amplitude[4]"); #3. Last 7 days and incidence per 100 000 for a district of 245 000 people; -> "[k]" (bigdec sum "[64, 70, 73, 69, 63, 30, 26]"); -> "[inc]" (bigdec div (math calc "[k]*100000") 245000 4); #4. Exact Poisson 95 % limits with the regularized incomplete gamma function; -> "[lo]" (num solve (concat "gamma_inc(" [k] ", x)-0.025") "x" 1 [k]); -> "[hi]" (num solve (concat "1-gamma_inc(" [k] "+1, x)-0.025") "x" [k] (math calc "3*[k]")); concat "weekly autocorrelation r7 = " (math round [r7] 3) ", amplitude of the 1/7 per day component " (math round [a7] 1) " cases" ", last 7-day average " (math round (json load select "rx_ma" [ma] "/[21]") 1) ", " [k] " cases = " [inc] " per 100 000, " (if (> (bigdec compare [inc] 150) 0) {"above"} {"below"}) " the alert threshold of 150 (95 % CI " (math round (math calc "[lo]*100000/245000") 1) " to " (math round (math calc "[hi]*100000/245000") 1) ")"
weekly autocorrelation r7 = 0.721, amplitude of the 1/7 per day component 20.3 cases, last 7-day average 56.4, 395 cases = 161.2 per 100 000, above the alert threshold of 150 (95 % CI 145.7 to 177.9)
- Compare two districts with a rate ratio and its exact interval
- Look for the outbreak signal with
dsp peakson the moving average - Draw the daily cases and the average with
plot chart
Hospital admissions: delay from cases and short-term forecast
Admissions follow cases with a delay. The script measures the lag of the maximum cross-correlation between the two centered series, discretizes a gamma delay distribution (mean 6 days, sd 2.5 days) with the regularized incomplete gamma function, and convolves the cases with it (8 % of the cases are hospitalized) to fit today's admissions and forecast the next days from the cases already reported.
#1. Daily cases and hospital admissions of a region over 24 days; -> "[cases]" "[100, 130, 165, 210, 265, 330, 400, 475, 550, 610, 650, 665, 650, 610, 550, 480, 410, 345, 285, 230, 185, 145, 115, 90]"; -> "[adm]" "[0, 0, 0, 2, 4, 5, 7, 14, 15, 20, 27, 30, 37, 40, 44, 45, 48, 49, 45, 43, 39, 33, 31, 26]"; #2. Lag between the two curves: maximum of the cross-correlation of the centered series; -> "[cc]" (dsp correlate (dsp detrend [adm] "constant") (dsp detrend [cases] "constant")); -> "[lag]" (- (json load select "rx_cc" (dsp peaks [cc] (concat "{\"height\": " (math calc (concat (stat max [cc]) "*0.999")) "}")) "/indices[0]") 23); #3. Delay from case to admission: gamma law mean 6 days, sd 2.5 days, discretized with gamma_inc; -> "[k]" (math calc "(6/2.5)^2"); -> "[th]" (math calc "2.5^2/6"); -> "[pmf]" "["; -> "[sep]" ""; for (-> "[d]" 0) (< [d] 15) (++ "[d]") { -> "[pmf]" (concat [pmf] [sep] (math calc "gamma_inc([k], ([d]+1)/[th])-gamma_inc([k], [d]/[th])")); -> "[sep]" ","; }; -> "[pmf]" (concat [pmf] "]"); #4. Expected admissions = 8 % of the cases convolved with the delay: fit of today, forecast of tomorrow; json load "rx_ex" (dsp convolve [cases] [pmf]); concat "lag of the maximum correlation " [lag] " days, sum of the delay probabilities " (math round (stat sum [pmf]) 4) ", expected admissions today " (math round (math calc (concat "0.08*" (json select "rx_ex" "/[23]"))) 1) " (observed 26), tomorrow " (math round (math calc (concat "0.08*" (json select "rx_ex" "/[24]"))) 1) ", in 3 days " (math round (math calc (concat "0.08*" (json select "rx_ex" "/[26]"))) 1) " at least"
lag of the maximum correlation 4 days, sum of the delay probabilities 0.9967, expected admissions today 25.7 (observed 26), tomorrow 21.6, in 3 days 14.1 at least
- Fit the hospitalized fraction by least squares between the observed and the convolved series with
stat fit_model - Add the cases forecast by the growth rate to complete the admissions of the coming days
- Draw observed and expected admissions with
plot chart
Case-control study: odds ratio and attributable fraction
Smoking among 100 lung cancer cases and 100 controls: odds ratio with the Woolf 95 % confidence interval, chi2 and Fisher exact tests, attributable fraction in the exposed and population attributable fraction (Miettinen).
#1. Case-control study: smokers among 100 lung cancer cases and 100 hospital controls; -> "[a]" 45; -> "[b]" 55; -> "[c]" 25; -> "[d]" 75; -> "[tab]" (concat "[[" [a] ", " [b] "], [" [c] ", " [d] "]]"); #2. Odds ratio with Woolf 95 % confidence interval; -> "[or]" (math calc "[a]*[d]/([b]*[c])"); -> "[z]" (stat dist_inv "normal" "[0, 1]" 0.975); -> "[se]" (math calc "sqrt(1/[a]+1/[b]+1/[c]+1/[d])"); #3. Tests of association: Pearson chi2 and Fisher exact; -> "[chi]" (json load select "rx_tu1_cc" (stat chi2_independence [tab]) "/p_value"); -> "[fis]" (json load select "rx_tu1_cf" (stat fisher_exact [tab]) "/p_value"); #4. Attributable fraction in the exposed and population attributable fraction, Miettinen formula; -> "[afe]" (math calc "([or]-1)/[or]"); -> "[paf]" (math calc "[a]/([a]+[b])*[afe]"); concat "OR = " (math round [or] 3) ", 95 % CI " (math round (math calc "exp(ln([or])-[z]*[se])") 2) " to " (math round (math calc "exp(ln([or])+[z]*[se])") 2) "; chi2 p = " (math round_sig [chi] 3) ", Fisher p = " (math round_sig [fis] 3) "; AF exposed = " (math round (* 100 [afe]) 1) " %, PAF = " (math round (* 100 [paf]) 1) " %"
OR = 2.455, 95 % CI 1.35 to 4.47; chi2 p = 0.00303, Fisher p = 0.00468; AF exposed = 59.3 %, PAF = 26.7 %
- Adjust for age with a stratified table and the Mantel-Haenszel odds ratio
- Compute the sample size needed to detect OR = 2 with a power of 80 %
Diagnostic test: Wilson interval, likelihood ratios, predictive values
A rapid antigen test is validated against PCR. The script gives the sensitivity with its Wilson score interval (z from the inverse error function), the specificity, both likelihood ratios and the predictive values in a 2 % prevalence screening.
#1. Validation of a rapid antigen test against PCR on 1000 people; -> "[tp]" 92; -> "[fn]" 8; -> "[fp]" 45; -> "[tn]" 855; -> "[se]" (/ [tp] (+ [tp] [fn])); -> "[sp]" (/ [tn] (+ [tn] [fp])); #2. z of a 95 % interval from the inverse error function: sqrt 2 . erfinv 0.95; -> "[z]" (* (math sqrt 2) (math erf_inv 0.95)); #3. Wilson score interval of the sensitivity; -> "[n]" (+ [tp] [fn]); -> "[c]" (/ (+ [se] (/ (* [z] [z]) (* 2 [n]))) (+ 1 (/ (* [z] [z]) [n]))); -> "[h]" (/ (* [z] (math sqrt (+ (/ (* [se] (- 1 [se])) [n]) (/ (* [z] [z]) (* 4 (* [n] [n])))))) (+ 1 (/ (* [z] [z]) [n]))); #4. Likelihood ratios and predictive values at a 2 % prevalence in screening; -> "[lrp]" (/ [se] (- 1 [sp])); -> "[lrn]" (/ (- 1 [se]) [sp]); -> "[ppv]" (math calc "[se]*0.02/([se]*0.02+(1-[sp])*0.98)"); -> "[npv]" (math calc "[sp]*0.98/([sp]*0.98+(1-[se])*0.02)"); concat "Se = " [se] " [" (math round (- [c] [h]) 3) "; " (math round (+ [c] [h]) 3) "] with z = " (math round [z] 4) ", Sp = " (math round [sp] 4) ", LR+ = " (math round [lrp] 1) ", LR- = " (math round [lrn] 3) ", PPV at 2 % = " (math round (* 100 [ppv]) 1) " %, NPV = " (math round (* 100 [npv]) 2) " %"
Se = 0.92 [0.85; 0.959] with z = 1.96, Sp = 0.95, LR+ = 18.4, LR- = 0.084, PPV at 2 % = 27.3 %, NPV = 99.83 %
- Draw the PPV against the prevalence with
plot function. - Compare two tests on the same patients with the McNemar test (
stat chi2_test).
Incubation period and quarantine length
From a lognormal incubation period fitted on contact-tracing data, the script gives the median and the mean, the probability that symptoms start after a 14 day quarantine and the quarantine length that covers 99 % of the cases.
#1. Incubation period lognormal: mean and SD of ln days from a contact-tracing study; -> "[mu]" 1.63; -> "[sg]" 0.50; #2. Median e^mu and mean e^mu+sigma2/2; -> "[med]" (math power (math e) [mu]); -> "[mean]" (math exp (+ [mu] (/ (* [sg] [sg]) 2))); #3. Probability that symptoms appear after a 14 day quarantine, with erfc; -> "[p14]" (* 0.5 (math erfc (/ (- (math log 14) [mu]) (* [sg] (math sqrt 2))))); #4. Quarantine length covering 99 % of the cases: quantile with erf_inv; -> "[q99]" (math exp (+ [mu] (* [sg] (* (math sqrt 2) (math erf_inv 0.98))))); concat "median = " (math round [med] 2) " d, mean = " (math round [mean] 2) " d, P incubation > 14 d = " (math round_sig (* 100 [p14]) 3) " %, 99th percentile = " (math round [q99] 1) " d, so a quarantine of " (math ceil [q99]) " days"
median = 5.1 d, mean = 5.78 d, P incubation > 14 d = 2.18 %, 99th percentile = 16.3 d, so a quarantine of 17 days
- Fit mu and sigma on observed exposure-onset pairs with
stat fit_modelon the log values. - Check the quantile with
stat dist_invand the lognormal law.
Occupational cohort: SMR with exact Poisson test
27 lung cancer deaths are observed in a cohort where national rates predict 18.4. The script sums the Poisson terms in a loop with log-gamma to get the exact one-sided p-value, and gives the SMR with its exact interval from the chi2 quantiles.
#1. Occupational cohort: 27 lung cancer deaths observed, 18.4 expected from national rates; -> "[o]" 27; -> "[e]" 18.4; #2. Exact Poisson probability P of X >= 27, terms exp of k.ln e - e - lngamma k+1; -> "[cum]" 0; for (-> "[k]" 0) (< [k] [o]) (++ "[k]") { -> "[cum]" (+ [cum] (math exp (- (- (* [k] (math log [e])) [e]) (math lngamma (+ [k] 1))))); }; -> "[p]" (- 1 [cum]); #3. SMR and its exact 95 % interval from the chi2 quantiles; -> "[lo]" (/ (/ (stat dist_inv "chi2" (concat "[" (* 2 [o]) "]") 0.025) 2) [e]); -> "[hi]" (/ (/ (stat dist_inv "chi2" (concat "[" (+ (* 2 [o]) 2) "]") 0.975) 2) [e]); concat "SMR = " (math round (/ [o] [e]) 2) " [" (math round [lo] 2) "; " (math round [hi] 2) "], one-sided exact p = P X>=27 = " (math round_sig [p] 3) ", check with the Poisson law: " (math round_sig (- 1 (stat dist_cdf "poisson" (concat "[" [e] "]") 26)) 3)
SMR = 1.47 [0.97; 2.13], one-sided exact p = P X>=27 = 0.0354, check with the Poisson law: 0.0354
- Stratify by age and period and add the expected deaths with
vector dot. - Use the Byar approximation and compare with the exact limits.
Vaccine effectiveness from a test-negative study
Influenza vaccine effectiveness is estimated as 1 - OR from a test-negative design, with the Woolf interval, and the length of hospital stay of breakthrough cases is compared with the unvaccinated cases by a Mann-Whitney test.
#1. Test-negative design: vaccinated among influenza-positive cases and test-negative controls; -> "[a]" 48; -> "[b]" 312; -> "[c]" 151; -> "[d]" 489; #2. Vaccine effectiveness VE = 1 - OR with the Woolf interval; -> "[or]" (/ (* [a] [d]) (* [b] [c])); -> "[se]" (math sqrt (+ (/ 1 [a]) (+ (/ 1 [b]) (+ (/ 1 [c]) (/ 1 [d]))))); -> "[z]" (stat dist_inv "normal" "[0, 1]" 0.975); -> "[vel]" (- 1 (math exp (+ (math log [or]) (* [z] [se])))); -> "[veh]" (- 1 (math exp (- (math log [or]) (* [z] [se])))); #3. Severity among breakthrough cases: hospital length of stay in days, vaccinated vs unvaccinated; -> "[vac]" "[2, 3, 2, 4, 3, 2, 5, 3]"; -> "[unv]" "[4, 6, 3, 8, 5, 7, 4, 9, 6]"; -> "[mw]" (json load select "rx_p4d_ve" (stat mann_whitney [vac] [unv]) "/p_value"); concat "OR = " (math round [or] 3) ", VE = " (math round (* 100 (- 1 [or])) 1) " % [" (math round (* 100 [vel]) 1) "; " (math round (* 100 [veh]) 1) "], length of stay median " (stat median [vac]) " vs " (stat median [unv]) " days, Mann-Whitney p = " (math round_sig [mw] 3)
OR = 0.498, VE = 50.2 % [29; 65], length of stay median 3 vs 6 days, Mann-Whitney p = 0.00549
- Adjust the OR on age with a Mantel-Haenszel estimate over age strata.
- Repeat by week of onset to see the waning of the protection.
Direct age standardization of mortality
Cardiovascular mortality of two regions with different age structures is standardized on the European Standard Population: one matrix product gives both standardized rates, a diagonal matrix gives the weight of each age group, then the comparative mortality figure.
#1. Cardiovascular mortality per 100 000 by age group 45-54 ... 85+, two regions, rows = regions; -> "[r]" "[[85, 210, 540, 1450, 4100], [70, 190, 560, 1600, 4600]]"; #2. Weights of the 2013 European Standard Population for these ages, rescaled to 1; -> "[w]" "[0.2824, 0.2588, 0.2235, 0.1647, 0.0706]"; #3. Direct standardization: R.w, then the contribution of each age group with diag w; -> "[asr]" (matrix mul [r] [w]); -> "[contrib]" (matrix mul [r] (matrix diag [w])); -> "[old]" (matrix get [contrib] 1 5); #4. Comparative mortality figure of region 2 against region 1; -> "[a1]" (json load select "rx_p4d_asr" [asr] "/[0]"); -> "[a2]" (json select "rx_p4d_asr" "/[1]"); concat "age-standardized rates: " (math round [a1] 1) " and " (math round [a2] 1) " per 100 000, CMF = " (math round (/ [a2] [a1]) 3) ", unweighted mean of the region 1 age rates: " (math avg (concat "[" (string replace (string replace (matrix row [r] 1) "[" "") "]" "") "]")) ", the 85+ group makes " (math round (* 100 (/ [old] [a1])) 1) " % of the region 1 rate"
age-standardized rates: 727.3 and 782.4 per 100 000, CMF = 1.076, unweighted mean of the region 1 age rates: 1277.0, the 85+ group makes 39.8 % of the region 1 rate
- Add the 95 % interval of each ASR with the variance sum of w^2.r/n (
vector dot). - Indirect standardization: expected deaths = population x reference rates.
Odds ratios of a case-control study
Fit a logistic regression of the disease on smoking and age, then report the odds ratio of each factor with its 95 % confidence interval and p-value.
#1. Subjects: smoker 1 or 0, age in decades - case 1 or control 0; -> "[X]" "[[1, 4], [1, 5], [1, 6], [0, 4], [0, 5], [0, 6], [1, 3], [0, 3], [1, 7], [0, 7], [1, 5], [0, 5], [1, 6], [0, 6], [0, 4], [1, 4], [0, 7], [1, 3], [0, 3], [1, 7]]"; -> "[y]" "[1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1]"; json load "rx_lr" (ml train "rx_lr" "logistic" [X] [y]); #2. Odds ratio, interval and p-value of each factor; -> "[res]" ""; for (-> "[i]" 1) (<= [i] 2) (++ "[i]") { -> "[c]" (concat "/coefficients/[" [i] "]"); -> "[res]" (concat [res] "x" (- [i] 1) ": OR " (math round (math exp (json select "rx_lr" (concat [c] "/estimate"))) 2) " CI " (math round (math exp (json select "rx_lr" (concat [c] "/ci_low"))) 2) " - " (math round (math exp (json select "rx_lr" (concat [c] "/ci_high"))) 2) ", p = " (math round (json select "rx_lr" (concat [c] "/p_value")) 4) "; "); }; -> "[res]" (concat "x0 = smoking, x1 = age per decade | " [res] "McFadden R2 " (math round (json select "rx_lr" "/mcfadden_r2") 3)); json unload "rx_lr"; ml model delete "rx_lr"; [res]
x0 = smoking, x1 = age per decade | x0: OR 17.14 CI 1.24 - 237.03, p = 0.034; x1: OR 2.24 CI 0.87 - 5.74, p = 0.0932; McFadden R2 0.326
- The key odds_ratios of
ml trainalready contains exp of the coefficients. - Add the parameter alpha to stabilize the estimates when a group has no case.
Incidence rate ratio with a Poisson regression
Model the number of cases per district with a Poisson regression on the exposure index and the population density, then report the incidence rate ratio of one exposure unit, its p-value and the dispersion.
#1. Districts: exposure index, density in thousands per km2 - number of cases; -> "[X]" "[[0.5, 1.2], [1.0, 0.8], [1.5, 1.5], [2.0, 1.1], [2.5, 2.0], [3.0, 1.4], [3.5, 2.2], [4.0, 1.8], [4.5, 2.5], [5.0, 2.1]]"; -> "[y]" "[2, 3, 5, 4, 9, 8, 14, 13, 21, 22]"; json load "rx_p" (ml train "rx_p" "poisson" [X] [y]); -> "[res]" (concat "IRR exposure " (math round (json select "rx_p" "/rate_ratios/[0]") 3) " p = " (math round (json select "rx_p" "/coefficients/[1]/p_value") 5) " | IRR density " (math round (json select "rx_p" "/rate_ratios/[1]") 3) " p = " (math round (json select "rx_p" "/coefficients/[2]/p_value") 4) " | dispersion " (math round (json select "rx_p" "/dispersion") 2) " | pseudo R2 " (math round (json select "rx_p" "/pseudo_r2") 3)); json unload "rx_p"; ml model delete "rx_p"; [res]
IRR exposure 1.437 p = 0.00316 | IRR density 1.616 p = 0.1573 | dispersion 0.07 | pseudo R2 0.989
- A dispersion far above 1 means overdispersion: the p-values are then too optimistic.
- Predict the expected cases of a new district with
ml model predict.
Clusters and super-spreaders of a contact network
From the contacts between 10 cases, find the transmission clusters with the Louvain method and the most central case with the betweenness centrality.
#1. Contacts between cases; -> "[e]" "[[\"P1\", \"P2\"], [\"P1\", \"P3\"], [\"P2\", \"P3\"], [\"P3\", \"P4\"], [\"P4\", \"P5\"], [\"P5\", \"P6\"], [\"P4\", \"P6\"], [\"P6\", \"P7\"], [\"P7\", \"P8\"], [\"P8\", \"P9\"], [\"P7\", \"P9\"], [\"P9\", \"P10\"], [\"P8\", \"P10\"]]"; json load "rx_c" (ml graph communities [e] "louvain"); json load "rx_b" (ml graph centrality [e] "betweenness"); #2. The case with the highest betweenness; -> "[top]" ""; -> "[v]" -1; for (-> "[i]" 1) (<= [i] 10) (++ "[i]") { -> "[b]" (json select "rx_b" (concat "/P" [i])); if (> [b] [v]) {-> "[v]" [b]; -> "[top]" (concat "P" [i]);}; }; -> "[res]" (concat (json select "rx_c" "/n_communities") " clusters, sizes " (json select "rx_c" "/sizes") ", modularity " (math round (json select "rx_c" "/modularity") 3) " | most central case " [top] " betweenness " (math round [v] 3)); json unload "rx_c"; json unload "rx_b"; [res]
3 clusters, sizes [ 3, 3, 4 ], modularity 0.5 | most central case P6 betweenness 0.556
ml graph pagerankon directed infector - infectee links ranks the sources.- Weights on the edges, ex: [a, b, hours of contact], change the communities.
Detect a rare disease with SMOTE
Only 1 patient in 10 is sick: a classifier trained on the raw data misses most cases. SMOTE creates synthetic sick patients in the training set and raises the recall of the test set.
#1. Training and test sets: biomarker and age-adjusted index, 1 = sick; -> "[X]" "[[5.42, 30.3], [5.87, 18.5], [5.06, 18.7], [6.3, 31.0], [6.56, 27.5], [5.6, 28.6], [4.46, 30.7], [3.94, 28.2], [5.9, 30.3], [4.79, 40.9], [6.66, 33.1], [5.36, 27.4], [4.81, 28.2], [7.23, 30.1], [5.38, 33.4], [7.22, 28.9], [4.58, 42.4], [3.74, 28.2], [5.87, 41.4], [4.25, 17.9], [7.26, 33.4], [4.81, 32.3], [5.42, 31.5], [4.77, 36.4], [6.7, 30.2], [4.75, 33.7], [5.68, 24.8], [4.74, 35.2], [5.1, 28.4], [5.41, 29.8], [6.6, 26.3], [6.95, 31.0], [4.32, 34.9], [5.51, 30.1], [6.25, 41.3], [5.75, 37.9], [7.36, 24.9], [4.68, 33.9], [3.49, 28.7], [6.49, 35.2], [7.18, 26.5], [7.55, 32.8], [5.97, 32.5], [3.48, 22.8], [3.98, 39.6], [4.37, 28.8], [4.95, 32.9], [5.7, 31.3], [3.94, 14.3], [5.37, 31.3], [7.83, 35.5], [4.59, 32.1], [3.65, 24.8], [4.19, 31.4], [7.29, 33.8], [6.34, 31.2], [5.48, 27.8], [4.92, 17.9], [4.0, 23.6], [6.04, 33.3], [7.74, 43.8], [4.89, 35.3], [7.16, 30.0], [6.29, 29.2], [3.59, 30.6], [4.38, 30.5], [4.74, 31.6], [3.06, 40.4], [6.56, 28.2], [4.29, 30.0], [7.28, 38.1], [5.6, 20.2], [4.07, 36.7], [5.44, 22.4], [4.33, 22.8], [4.18, 35.9], [4.98, 35.7], [5.39, 22.4], [4.54, 41.6], [4.42, 24.7]]"; -> "[y]" "[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]"; -> "[Xt]" "[[7.86, 36.5], [5.19, 37.5], [3.41, 32.1], [4.13, 22.9], [4.37, 35.9], [5.06, 20.6], [4.07, 37.8], [6.24, 24.4], [7.68, 28.1], [3.98, 29.3], [7.56, 44.2], [6.25, 27.8], [4.63, 22.6], [5.12, 27.0], [5.45, 23.5], [4.67, 25.5], [3.97, 23.9], [4.16, 24.0], [5.78, 30.5], [3.85, 32.6], [6.89, 43.5], [5.0, 28.9], [4.5, 30.6], [5.7, 30.9], [4.94, 32.9], [5.29, 31.3], [4.24, 31.1], [4.18, 25.7], [5.36, 40.8], [6.22, 30.0], [6.42, 41.1], [6.61, 36.2], [5.01, 31.0], [4.86, 28.5], [4.33, 28.7], [3.0, 23.9], [5.64, 25.3], [5.75, 23.4], [4.82, 32.0], [4.14, 26.3]]"; -> "[yt]" "[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]"; ml train "rx_raw" "knn_classifier" [X] [y] "{\"k\": 5, \"scale\": true}"; json load "rx_s" (ml prep smote [X] [y] "{\"seed\": 1}"); ml train "rx_bal" "knn_classifier" (json select "rx_s" "/X") (json select "rx_s" "/y") "{\"k\": 5, \"scale\": true}"; -> "[r1]" (ml metrics recall [yt] (ml model predict "rx_raw" [Xt]) 1); -> "[r2]" (ml metrics recall [yt] (ml model predict "rx_bal" [Xt]) 1); -> "[res]" (concat "synthetic sick patients: " (json select "rx_s" "/added/1") " | recall raw " (math round [r1] 3) " | recall with SMOTE " (math round [r2] 3)); ml model delete "rx_raw"; ml model delete "rx_bal"; json unload "rx_s"; [res]
synthetic sick patients: 64 | recall raw 0.5 | recall with SMOTE 0.75
- Apply SMOTE to the training set only: the test set must keep the real proportion.
- Check the precision too with
ml metrics report: more cases found also means more false alarms.
Rare symptoms that come together
Among 120 consultations, find the perfectly rare itemsets: each symptom is rare, below 15 % of the patients, but they appear together at least 5 times; a signal for an emerging disease that the frequent patterns hide.
#1. Symptoms of each consultation; -> "[c]" "[[\"cough\", \"fever\", \"joint_pain\", \"rash\"], [\"cough\", \"fatigue\", \"headache\"], [\"cough\", \"headache\"], [\"cough\", \"fever\", \"headache\"], [\"fever\"], [\"fatigue\", \"headache\", \"jaundice\"], [\"cough\", \"fatigue\", \"fever\"], [\"cough\", \"rash\"], [\"cough\", \"fatigue\", \"fever\"], [\"fatigue\"], [\"cough\", \"fatigue\", \"headache\"], [\"cough\", \"fatigue\", \"fever\"], [\"fatigue\"], [\"fatigue\"], [\"cough\", \"fatigue\", \"headache\"], [\"cough\", \"joint_pain\", \"rash\"], [\"cough\", \"fatigue\", \"fever\"], [\"cough\", \"fatigue\", \"fever\"], [\"cough\", \"fatigue\", \"fever\"], [\"cough\", \"fever\"], [\"cough\", \"headache\"], [\"cough\", \"fatigue\", \"fever\"], [\"cough\", \"fatigue\", \"headache\"], [\"cough\", \"headache\"], [\"cough\"], [\"cough\", \"fever\", \"headache\"], [\"cough\", \"fatigue\", \"headache\"], [\"fatigue\"], [\"cough\", \"fatigue\", \"headache\"], [\"cough\"], [\"fatigue\", \"joint_pain\", \"rash\"], [\"cough\", \"fever\"], [\"cough\", \"fever\"], [\"cough\"], [\"cough\", \"fatigue\", \"headache\"], [\"fatigue\"], [\"fatigue\", \"fever\"], [\"headache\"], [\"fatigue\", \"jaundice\"], [\"fatigue\"], [\"cough\"], [\"headache\"], [\"headache\"], [\"fatigue\", \"fever\", \"headache\"], [\"fatigue\", \"fever\", \"headache\"], [\"fatigue\", \"fever\", \"headache\", \"joint_pain\", \"rash\"], [\"cough\", \"fatigue\", \"fever\"], [\"fever\", \"headache\", \"rash\"], [\"fatigue\"], [\"fatigue\"], [\"cough\"], [\"fatigue\", \"fever\", \"headache\"], [\"cough\", \"fever\", \"headache\"], [\"headache\"], [\"cough\", \"fatigue\"], [\"fatigue\", \"fever\", \"headache\"], [\"fever\"], [\"fever\", \"headache\"], [\"cough\", \"fever\"], [\"fatigue\", \"fever\", \"headache\"], [\"fatigue\", \"joint_pain\", \"rash\"], [\"cough\", \"headache\"], [\"fever\"], [\"cough\", \"fatigue\", \"headache\"], [\"fatigue\", \"fever\", \"headache\"], [\"cough\", \"fatigue\", \"fever\"], [\"cough\", \"fever\", \"headache\"], [\"cough\", \"fatigue\", \"fever\"], [\"cough\", \"fatigue\", \"headache\"], [\"cough\", \"fatigue\"], [\"cough\", \"fever\", \"headache\"], [\"fatigue\", \"headache\", \"jaundice\"], [\"fever\", \"headache\"], [\"cough\", \"fever\", \"headache\"], [\"cough\", \"fever\"], [\"cough\", \"fatigue\", \"fever\", \"joint_pain\", \"rash\"], [\"cough\", \"headache\"], [\"cough\", \"fatigue\", \"headache\"], [\"fever\", \"headache\"], [\"fatigue\", \"fever\", \"headache\"], [\"fatigue\", \"headache\"], [\"cough\", \"fatigue\"], [\"fatigue\"], [\"cough\"], [\"cough\", \"fatigue\"], [\"cough\", \"fatigue\"], [\"cough\", \"fatigue\", \"fever\"], [\"cough\", \"fatigue\", \"headache\", \"rash\"], [\"cough\", \"fatigue\", \"headache\"], [\"cough\"], [\"cough\", \"fatigue\", \"fever\", \"joint_pain\", \"rash\"], [\"fever\", \"headache\"], [\"headache\"], [\"headache\"], [\"cough\"], [\"cough\", \"fatigue\", \"fever\"], [\"headache\"], [\"fatigue\", \"headache\"], [\"fatigue\", \"fever\"], [\"fever\", \"headache\"], [\"fatigue\", \"headache\"], [\"cough\"], [\"cough\", \"fatigue\", \"headache\"], [\"cough\", \"fatigue\", \"fever\"], [\"cough\", \"headache\", \"jaundice\"], [\"fever\", \"headache\", \"joint_pain\", \"rash\"], [\"cough\", \"fever\", \"headache\"], [\"fever\", \"headache\"], [\"cough\", \"fever\"], [\"fatigue\", \"fever\", \"headache\"], [\"cough\", \"fatigue\", \"headache\"], [\"fatigue\", \"fever\", \"headache\"], [\"cough\", \"fever\", \"headache\"], [\"cough\", \"headache\"], [\"cough\", \"fever\", \"headache\"], [\"cough\", \"fatigue\", \"headache\"], [\"fatigue\"], [\"cough\"], [\"fatigue\", \"fever\", \"headache\"], [\"cough\"]]"; json load "rx_r" (ml assoc rare [c] 0.04 0.15); json load "rx_b" (ml assoc rare_correlated [c] 0.15 0.5); -> "[res]" "perfectly rare: "; for (-> "[i]" 0) (< [i] (json select "rx_r" "/count")) (++ "[i]") { -> "[p]" (concat "/itemsets/[" [i] "]"); -> "[a]" ""; for (-> "[k]" 0) (< [k] (json count "rx_r" (concat [p] "/items"))) (++ "[k]") {-> "[a]" (concat [a] (if (> [k] 0) {" + "} {""}) (json select "rx_r" (concat [p] "/items/[" [k] "]")));}; -> "[res]" (concat [res] [a] " x" (json select "rx_r" (concat [p] "/support")) ", "); }; -> "[res]" (concat [res] "| strongest bond: " (json select "rx_b" "/itemsets/[0]/items/[0]") " + " (json select "rx_b" "/itemsets/[0]/items/[1]") " " (math round (json select "rx_b" "/itemsets/[0]/bond") 2)); json unload "rx_r"; json unload "rx_b"; [res]
perfectly rare: joint_pain + rash x8, joint_pain x8, rash x11, | strongest bond: joint_pain + rash 0.73
- bond = consultations with all the symptoms / consultations with at least one of them: 1 = they never appear alone.
ml assoc rarewith one threshold gives the minimal rare itemsets.
Survival in a clinical trial
Compare the survival of 60 patients under a drug or a placebo: Kaplan-Meier median of each arm and log-rank test, then a Cox model adjusted for the age: the age hides part of the drug effect, the adjusted hazard ratio reveals it.
#1. Follow-up in months, 1 = death, 0 = censored, arm, age; -> "[t]" "[3.1, 20.3, 20.8, 31.1, 20.9, 20.1, 13.8, 17.7, 12.1, 28.8, 28.4, 13.9, 24.5, 18.7, 13.2, 21.8, 28.5, 18.0, 4.0, 4.5, 15.3, 31.7, 38.9, 23.5, 34.2, 10.1, 5.0, 23.5, 15.5, 14.4, 12.7, 12.4, 12.3, 23.1, 29.1, 32.6, 29.8, 9.4, 2.0, 19.7, 4.6, 26.9, 24.7, 19.3, 8.2, 14.0, 18.1, 4.9, 2.2, 14.2, 2.2, 37.9, 0.7, 10.9, 3.7, 27.9, 5.2, 10.1, 5.6, 23.2]"; -> "[e]" "[1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0]"; -> "[g]" "[\"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\", \"placebo\", \"drug\"]"; -> "[X]" "[[0, 76], [1, 65], [0, 46], [1, 70], [0, 66], [1, 50], [0, 72], [1, 70], [0, 66], [1, 68], [0, 56], [1, 71], [0, 48], [1, 79], [0, 45], [1, 67], [0, 52], [1, 47], [0, 70], [1, 76], [0, 76], [1, 68], [0, 55], [1, 77], [0, 51], [1, 56], [0, 70], [1, 74], [0, 61], [1, 55], [0, 54], [1, 54], [0, 55], [1, 62], [0, 69], [1, 78], [0, 53], [1, 76], [0, 47], [1, 73], [0, 79], [1, 63], [0, 62], [1, 53], [0, 56], [1, 52], [0, 54], [1, 73], [0, 68], [1, 75], [0, 74], [1, 50], [0, 78], [1, 80], [0, 62], [1, 46], [0, 65], [1, 65], [0, 77], [1, 62]]"; json load "rx_k" (stat km [t] [e] [g]); json load "rx_c" (stat cox [X] [t] [e]); -> "[res]" (concat "median survival: placebo " (json select "rx_k" "/groups/placebo/median") ", drug " (json select "rx_k" "/groups/drug/median") " months | log-rank p = " (math round (json select "rx_k" "/logrank/p_value") 4)); -> "[res]" (concat [res] " | Cox: drug HR " (math round (json select "rx_c" "/coefficients/[0]/hazard_ratio") 2) " [" (math round (json select "rx_c" "/coefficients/[0]/hr_lower") 2) ", " (math round (json select "rx_c" "/coefficients/[0]/hr_upper") 2) "], age HR per year " (math round (json select "rx_c" "/coefficients/[1]/hazard_ratio") 3) " | C index " (math round (json select "rx_c" "/concordance") 2)); json unload "rx_k"; json unload "rx_c"; [res]
median survival: placebo 20.9, drug 31.1 months | log-rank p = 0.0902 | Cox: drug HR 0.29 [0.13, 0.65], age HR per year 1.093 | C index 0.74
- A hazard ratio below 1 means a lower risk of death at any time.
- The table of each Kaplan-Meier curve gives the survival and its interval at each time, ready for a plot.
Meta-analysis of 13 vaccine trials
Combine the 13 BCG vaccine trials against tuberculosis: risk ratio of each trial, pooled risk ratio with random effects (DerSimonian-Laird), heterogeneity (I2, tau2) and the prediction interval of a new trial, as metafor in R.
#1. Cases and size of the vaccinated and control groups of each trial; -> "[e1]" "[4, 6, 3, 62, 33, 180, 8, 505, 29, 17, 186, 5, 27]"; -> "[n1]" "[123, 306, 231, 13598, 5069, 1541, 2545, 88391, 7499, 1716, 50634, 2498, 16913]"; -> "[e2]" "[11, 29, 11, 248, 47, 372, 10, 499, 45, 65, 141, 3, 29]"; -> "[n2]" "[139, 303, 220, 12867, 5808, 1451, 629, 88391, 7277, 1665, 27338, 2341, 17854]"; json load "rx_m" (stat meta_binary [e1] [n1] [e2] [n2] "rr"); -> "[res]" (concat "pooled RR " (math round (json select "rx_m" "/estimate") 3) " [" (math round (json select "rx_m" "/lower") 3) ", " (math round (json select "rx_m" "/upper") 3) "], p = " (math round (json select "rx_m" "/p_value") 6) " | I2 " (math round (* 100 (json select "rx_m" "/i2")) 1) " % | tau2 " (math round (json select "rx_m" "/tau2") 3) " | a new trial: RR between " (math round (json select "rx_m" "/prediction_lower") 2) " and " (math round (json select "rx_m" "/prediction_upper") 2)); json unload "rx_m"; [res]
pooled RR 0.49 [0.345, 0.695], p = 6.5E-5 | I2 92.1 % | tau2 0.309 | a new trial: RR between 0.14 and 1.77
- The forest field gives the interval and the weight of each trial, ready for a forest plot.
- A high I2 calls for an explanation of the heterogeneity (latitude of the trials, for BCG).
Hospital admissions, cold and heat (Poisson GAM)
Daily hospital admissions against the daily temperature: the risk rises on cold days and on hot days. A Poisson GAM fits a smooth curve on the log scale without choosing its shape; the explained deviance and the effective degrees of freedom summarize the fit, the curve gives the relative risk of each temperature against the comfort zone.
#1. Temperature (C) and admissions of 150 days; -> "[X]" "[[14.7], [18.4], [25.5], [8.4], [15.3], [35.9], [31.4], [25.9], [25.8], [22.6], [26.8], [29.2], [16.4], [32.2], [28.1], [28.0], [19.1], [20.4], [32.6], [11.8], [35.0], [25.5], [22.2], [17.8], [24.4], [27.1], [32.0], [26.8], [32.1], [33.3], [28.0], [31.3], [16.0], [31.9], [10.5], [19.5], [16.2], [32.4], [25.2], [28.1], [32.7], [22.2], [16.7], [24.8], [13.5], [25.1], [9.2], [16.8], [33.1], [20.9], [26.0], [23.7], [34.3], [20.1], [14.7], [35.4], [23.4], [19.6], [8.6], [25.7], [25.6], [27.0], [27.8], [8.6], [26.9], [15.0], [24.6], [18.2], [18.3], [16.4], [29.6], [23.9], [16.7], [30.5], [13.2], [27.5], [17.0], [31.3], [32.0], [17.4], [32.8], [14.3], [22.8], [30.6], [13.1], [30.6], [30.6], [11.6], [30.2], [17.7], [19.8], [33.8], [8.1], [32.6], [20.2], [34.0], [28.9], [26.6], [16.1], [14.4], [24.5], [30.7], [33.3], [33.9], [33.2], [8.4], [12.8], [26.6], [19.6], [25.1], [15.1], [21.4], [17.9], [23.0], [12.8], [33.8], [31.1], [25.6], [9.4], [15.5], [19.8], [29.7], [9.5], [11.5], [35.3], [10.4], [16.8], [17.8], [24.4], [13.4], [11.5], [28.0], [10.2], [18.5], [29.9], [30.4], [20.1], [21.9], [19.8], [20.9], [23.0], [10.0], [19.9], [34.2], [33.1], [15.3], [11.4], [26.5], [30.2], [28.5]]"; -> "[y]" "[17, 14, 8, 42, 13, 38, 20, 9, 17, 14, 8, 17, 8, 22, 20, 20, 16, 18, 17, 19, 32, 11, 11, 14, 17, 20, 34, 10, 31, 27, 12, 21, 9, 33, 31, 9, 17, 15, 7, 13, 34, 23, 9, 6, 17, 9, 38, 21, 23, 12, 14, 13, 30, 14, 14, 35, 5, 13, 37, 8, 12, 12, 17, 26, 21, 15, 11, 11, 14, 13, 10, 14, 11, 15, 18, 10, 12, 19, 17, 15, 23, 12, 9, 23, 17, 20, 17, 20, 15, 12, 11, 22, 48, 35, 18, 24, 20, 14, 13, 11, 10, 12, 28, 35, 26, 40, 17, 14, 18, 11, 20, 15, 10, 15, 24, 32, 10, 17, 28, 16, 12, 6, 24, 16, 41, 27, 12, 15, 11, 17, 24, 14, 23, 14, 16, 20, 11, 14, 14, 10, 14, 27, 16, 27, 29, 15, 28, 18, 20, 16]"; json load "rx_g" (stat gam [X] [y] "{\"family\": \"poisson\", \"points\": 5}"); #2. Effects at the 5 points (log scale): relative risk against the middle point; -> "[mid]" (json select "rx_g" "/terms/[0]/effect/[2]/[1]"); -> "[res]" (concat "edf " (math round (json select "rx_g" "/edf") 1) ", explained deviance " (math round (* 100 (json select "rx_g" "/explained_deviance")) 1) " % | RR at " (math round (json select "rx_g" "/terms/[0]/effect/[0]/[0]") 1) " C: " (math round (math exp (- (json select "rx_g" "/terms/[0]/effect/[0]/[1]") [mid])) 2) ", at " (math round (json select "rx_g" "/terms/[0]/effect/[2]/[0]") 1) " C: 1, at " (math round (json select "rx_g" "/terms/[0]/effect/[4]/[0]") 1) " C: " (math round (math exp (- (json select "rx_g" "/terms/[0]/effect/[4]/[1]") [mid])) 2)); json unload "rx_g"; [res]
edf 6.2, explained deviance 71.2 % | RR at 8.1 C: 3.16, at 22 C: 1, at 35.9 C: 3.05
- The binomial family does the same for a 0 / 1 outcome (logit link), ex: the probability of an asthma attack against the pollution.
- Add the day of the week or the humidity as other columns: each one gets its own smooth curve.
Did the hygiene program reduce the infections? (Poisson mixed model)
Twelve hospitals count their catheter infections each quarter, with the number of catheter-days as exposure; the program started after 3 quarters. The hospitals differ a lot: a Poisson mixed model with a random intercept per hospital and the log of the exposure as offset gives the rate ratio of the program, corrected for these differences.
#1. Program (0 before, 1 after), infections, hospital, log(catheter-days / 1000); -> "[X]" "[[0], [0], [0], [1], [1], [1], [0], [0], [0], [1], [1], [1], [0], [0], [0], [1], [1], [1], [0], [0], [0], [1], [1], [1], [0], [0], [0], [1], [1], [1], [0], [0], [0], [1], [1], [1], [0], [0], [0], [1], [1], [1], [0], [0], [0], [1], [1], [1], [0], [0], [0], [1], [1], [1], [0], [0], [0], [1], [1], [1], [0], [0], [0], [1], [1], [1], [0], [0], [0], [1], [1], [1]]"; -> "[y]" "[0, 0, 0, 0, 0, 0, 2, 1, 2, 4, 2, 3, 3, 2, 3, 0, 3, 0, 1, 1, 2, 1, 1, 2, 1, 3, 1, 0, 0, 0, 1, 1, 3, 0, 1, 2, 1, 0, 2, 1, 3, 0, 1, 1, 2, 1, 1, 0, 1, 1, 4, 2, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 2, 0, 1]"; -> "[g]" "[\"H01\", \"H01\", \"H01\", \"H01\", \"H01\", \"H01\", \"H02\", \"H02\", \"H02\", \"H02\", \"H02\", \"H02\", \"H03\", \"H03\", \"H03\", \"H03\", \"H03\", \"H03\", \"H04\", \"H04\", \"H04\", \"H04\", \"H04\", \"H04\", \"H05\", \"H05\", \"H05\", \"H05\", \"H05\", \"H05\", \"H06\", \"H06\", \"H06\", \"H06\", \"H06\", \"H06\", \"H07\", \"H07\", \"H07\", \"H07\", \"H07\", \"H07\", \"H08\", \"H08\", \"H08\", \"H08\", \"H08\", \"H08\", \"H09\", \"H09\", \"H09\", \"H09\", \"H09\", \"H09\", \"H10\", \"H10\", \"H10\", \"H10\", \"H10\", \"H10\", \"H11\", \"H11\", \"H11\", \"H11\", \"H11\", \"H11\", \"H12\", \"H12\", \"H12\", \"H12\", \"H12\", \"H12\"]"; -> "[off]" "[-1.0527, -0.1649, -0.1098, -0.1997, -0.9467, -0.9916, -0.1462, -0.129, -0.6387, -0.1098, -0.1165, -1.0498, -0.5175, -0.159, -0.4861, -0.7236, -0.1233, -0.3842, -1.0106, -0.6733, -0.1696, -0.4764, -0.2692, -0.5906, -0.1188, -0.2157, -0.2758, -0.9835, -0.3175, -0.4308, -0.2231, -0.9702, -0.121, -0.4764, -0.4186, -0.1132, -0.5516, -1.0051, -0.4829, -0.2797, -0.3638, -0.4231, -0.2588, -0.8699, -0.6482, -0.8393, -0.3467, -0.2132, -0.821, -0.1473, -0.3216, -0.3725, -0.7897, -0.7897, -0.6199, -0.7215, -1.1907, -0.1661, -0.129, -0.8486, -1.0356, -0.137, -0.3453, -0.2319, -1.0134, -0.6675, -0.8867, -1.0413, -0.1278, -0.9088, -1.1209, -0.6694]"; json load "rx_m" (stat glmm [X] [y] [g] "poisson" (concat "{\"offset\": " [off] ", \"quadrature\": 7}")); -> "[res]" (concat "rate ratio of the program " (math round (math exp (json select "rx_m" "/fixed_effects/[1]/estimate")) 2) " [" (math round (math exp (json select "rx_m" "/fixed_effects/[1]/lower")) 2) ", " (math round (math exp (json select "rx_m" "/fixed_effects/[1]/upper")) 2) "], p = " (math round (json select "rx_m" "/fixed_effects/[1]/p_value") 4) " | baseline " (math round (math exp (json select "rx_m" "/fixed_effects/[0]/estimate")) 2) " per 1000 catheter-days | sd between hospitals " (math round (json select "rx_m" "/group_sd") 2)); json unload "rx_m"; [res]
rate ratio of the program 0.78 [0.5, 1.23], p = 0.2876 | baseline 1.83 per 1000 catheter-days | sd between hospitals 0.31
- Without the random intercept (stat glm), the interval of the rate ratio would be too narrow: the quarters of one hospital are not independent.
- With 0 / 1 outcomes (infected or not per patient), use the binomial family; with successes out of n, give trials.
Relapse when death competes: cumulative incidence and Fine-Gray
After a treatment, the patients can relapse (cause 1) or die without relapse (cause 2), which prevents the relapse. 1 - Kaplan-Meier treats the deaths as censored and overestimates the risk of relapse; the cumulative incidence gives the real probability of relapse, and the Fine-Gray model compares the two arms.
#1. Months, status (0 = censored, 1 = relapse, 2 = death), arm (0 = standard, 1 = new); -> "[t]" "[0.23, 3.91, 22.87, 5.48, 6.59, 5.9, 5.06, 5.26, 1.65, 10.14, 10.39, 13.48, 0.24, 22.7, 4.22, 2.39, 7.9, 8.66, 8.73, 8.47, 8.19, 1.75, 2.45, 10.72, 5.08, 8.12, 8.66, 1.0, 10.54, 10.65, 0.18, 9.55, 12.05, 8.49, 4.39, 8.56, 3.11, 8.04, 7.56, 12.84, 0.76, 2.29, 8.15, 2.07, 15.87, 10.02, 0.06, 21.05, 10.81, 5.32, 2.47, 3.4, 8.27, 14.6, 2.78, 1.25, 6.58, 4.59, 17.92, 5.21, 1.15, 12.38, 10.53, 12.63, 7.08, 5.68, 0.3, 4.21, 4.79, 12.47, 6.19, 8.32, 11.96, 16.93, 3.0, 16.02, 7.81, 3.87, 2.57, 6.29, 3.29, 3.24, 21.29, 11.19, 16.21, 10.31, 12.36, 10.9, 2.03, 5.81, 1.51, 5.93, 3.61, 6.07, 2.19, 16.4, 18.46, 5.09, 5.03, 11.37, 7.61, 13.91, 0.1, 14.85, 0.84, 2.97, 6.16, 4.36, 1.91, 9.71, 0.99, 3.57, 8.22, 9.88, 15.93, 21.06, 2.57, 4.61, 4.16, 2.3, 2.11, 13.34, 12.49, 22.09, 8.4, 6.66, 4.15, 7.47, 7.53, 21.53, 13.72, 1.97, 9.12, 12.35, 8.32, 6.25, 10.83, 5.76, 4.06, 1.42, 1.59, 1.01, 4.47, 20.55, 1.92, 7.71, 12.38, 14.08, 4.55, 7.17, 5.77, 1.39, 5.88, 12.05, 4.96, 14.5, 4.29, 1.4, 0.4, 11.78]"; -> "[s]" "[2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 1, 0, 1, 1, 1, 0, 2, 1, 2, 1, 2, 1, 0, 0, 1, 1, 1, 0, 2, 2, 1, 1, 0, 2, 2, 2, 2, 0, 1, 2, 2, 2, 2, 2, 1, 0, 1, 2, 2, 2, 2, 0, 2, 2, 0, 2, 0, 2, 2, 1, 1, 2, 1, 2, 1, 1, 0, 2, 1, 0, 2, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 0, 1, 2, 1, 2, 2, 1, 1, 1, 2, 1, 0, 2, 1, 2, 0, 1, 0, 1, 1, 1, 2, 2, 1, 2, 1, 1, 2, 1, 0, 2, 1, 1, 2, 1, 1, 1, 2, 0, 0, 2, 0, 0, 2, 0, 1, 2, 2, 0, 1, 1, 0, 1, 2, 2, 1, 0, 2, 1, 2, 2, 0, 0, 2, 1, 2, 1, 0, 2, 0, 0, 2, 1, 0]"; -> "[x]" "[[0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1]]"; json load "rx_c" (stat cuminc [t] [s] "{\"groups\": [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1], \"times\": [24]}"); json load "rx_f" (stat finegray [x] [t] [s]); -> "[res]" (concat "relapse at 24 months: standard " (math round (* 100 (json select "rx_c" "/groups/[0]/causes/[0]/at/[0]/cif")) 1) " %, new " (math round (* 100 (json select "rx_c" "/groups/[1]/causes/[0]/at/[0]/cif")) 1) " % | subdistribution hazard ratio " (math round (json select "rx_f" "/coefficients/[0]/shr") 2) " [" (math round (json select "rx_f" "/coefficients/[0]/shr_lower") 2) ", " (math round (json select "rx_f" "/coefficients/[0]/shr_upper") 2) "], p = " (math round (json select "rx_f" "/coefficients/[0]/p_value") 4) " | " (json select "rx_f" "/competing_events") " deaths without relapse"); json unload "rx_c"; json unload "rx_f"; [res]
relapse at 24 months: standard 46.4 %, new 34.3 % | subdistribution hazard ratio 0.68 [0.4, 1.14], p = 0.1433 | 72 deaths without relapse
- The cause-specific Cox model (stat cox with the other cause censored) answers another question: the effect on the rate of relapse among the patients still alive.
- Add {"bootstrap": 500} to stat cuminc for the intervals at the given times.
Observational study: effect of a treatment with a propensity score
In a registry, the statins were given more often to the older, diabetic patients with a high LDL: comparing the event rates of the treated and the untreated mixes the effect of the drug with the risk of the patients. The propensity score weights (IPTW) and matching make the two groups comparable on these covariates; the balance must be checked.
#1. Covariates [age, diabetes, LDL], statin (0 / 1), cardiovascular event (0 / 1); -> "[X]" "[[70, 0, 2.45], [53, 1, 3.58], [55, 1, 4.25], [49, 1, 4.14], [57, 0, 3.67], [74, 0, 2.62], [81, 0, 3.92], [73, 0, 1.48], [68, 1, 3.67], [45, 0, 3.31], [80, 0, 3.86], [45, 0, 3.02], [53, 0, 3.32], [42, 1, 2.81], [45, 1, 2.53], [52, 1, 3.66], [61, 1, 2.5], [69, 0, 2.27], [69, 0, 4.34], [49, 0, 2.95], [69, 1, 4.02], [55, 0, 4.32], [73, 0, 2.63], [59, 0, 2.46], [47, 0, 4.27], [61, 0, 2.99], [60, 0, 3.75], [59, 1, 3.64], [57, 0, 2.95], [58, 0, 3.9], [65, 0, 1.78], [71, 0, 3.25], [78, 1, 4.09], [43, 0, 3.93], [56, 0, 3.13], [50, 0, 2.85], [76, 1, 3.81], [62, 0, 3.67], [41, 1, 3.48], [46, 0, 3.96], [67, 1, 3.74], [83, 0, 2.54], [52, 0, 3.36], [41, 1, 2.9], [67, 0, 5.55], [43, 0, 2.44], [70, 0, 3.12], [71, 1, 3.64], [47, 0, 4.11], [62, 1, 3.96], [84, 0, 3.68], [54, 1, 4.74], [85, 0, 3.55], [57, 0, 4.47], [51, 1, 1.97], [84, 0, 3.53], [63, 0, 2.96], [71, 0, 3.11], [48, 0, 3.64], [41, 0, 3.22], [51, 0, 3.09], [57, 0, 2.83], [78, 0, 3.81], [50, 0, 3.21], [56, 1, 2.8], [62, 1, 4.38], [70, 1, 2.84], [53, 0, 3.47], [84, 0, 4.52], [49, 1, 3.29], [70, 1, 3.62], [62, 0, 3.7], [75, 1, 3.75], [53, 0, 2.34], [57, 0, 2.48], [81, 0, 5.12], [60, 1, 2.73], [56, 0, 3.42], [77, 1, 4.24], [76, 0, 2.33], [70, 1, 3.82], [54, 1, 2.29], [40, 0, 2.79], [71, 0, 4.77], [41, 0, 2.31], [65, 0, 2.78], [76, 0, 3.63], [62, 1, 2.87], [56, 0, 3.05], [79, 1, 2.89], [58, 0, 4.46], [45, 0, 3.22], [76, 1, 4.8], [74, 0, 1.92], [69, 0, 4.84], [46, 0, 3.53], [59, 0, 1.65], [49, 0, 3.06], [67, 1, 4.08], [48, 0, 4.16], [61, 1, 2.48], [82, 1, 3.91], [80, 0, 2.98], [48, 0, 2.76], [75, 1, 4.08], [77, 0, 3.23], [65, 0, 4.06], [47, 1, 4.33], [59, 0, 4.95], [50, 0, 4.34], [45, 1, 4.74], [52, 1, 4.39], [46, 1, 3.69], [65, 1, 3.01], [44, 0, 3.26], [70, 0, 3.41], [76, 0, 3.31], [49, 0, 3.15], [60, 0, 2.69], [57, 0, 3.17], [49, 0, 4.26], [59, 0, 3.33], [45, 0, 1.98], [46, 0, 3.39], [61, 0, 3.12], [50, 0, 4.11], [69, 1, 3.19], [49, 0, 4.43], [53, 1, 3.39], [67, 1, 4.36], [60, 1, 3.11], [55, 0, 3.01], [53, 1, 3.99], [82, 1, 3.31], [69, 0, 3.21], [46, 0, 4.16], [64, 0, 2.88], [44, 1, 3.33], [58, 0, 3.99], [68, 1, 6.26], [73, 1, 2.0], [82, 1, 3.88], [69, 0, 2.43], [50, 0, 3.17], [60, 1, 2.73], [53, 0, 0.78], [63, 0, 1.96], [64, 0, 2.92], [51, 0, 3.85], [46, 1, 3.48], [64, 0, 3.97], [44, 0, 4.55], [64, 0, 4.36], [44, 1, 3.49], [46, 0, 3.75], [55, 0, 3.69], [53, 1, 3.72], [61, 1, 4.38], [78, 0, 3.58], [78, 0, 4.21], [78, 1, 2.06], [54, 0, 3.87], [66, 0, 4.68], [69, 0, 2.28], [62, 0, 3.23], [85, 0, 4.06], [54, 0, 3.0], [83, 1, 3.63], [46, 1, 3.82], [79, 1, 3.39], [45, 0, 2.65], [71, 1, 3.88], [76, 0, 2.68], [41, 0, 3.52], [62, 0, 3.22], [47, 0, 4.21], [75, 0, 4.44], [40, 0, 3.66], [72, 1, 3.56], [59, 0, 3.58], [77, 1, 3.61], [50, 1, 4.2], [51, 0, 2.18], [52, 0, 3.16], [54, 0, 4.15], [82, 0, 4.69], [75, 0, 2.75], [65, 0, 3.29], [57, 0, 3.53], [85, 0, 3.26], [81, 0, 4.69], [72, 1, 5.25], [73, 0, 2.49], [56, 0, 3.26], [75, 1, 4.39], [44, 0, 2.16], [82, 0, 3.48], [54, 0, 4.09], [69, 1, 4.75], [44, 1, 3.7], [43, 0, 3.24], [85, 1, 4.97], [76, 1, 3.73], [64, 0, 4.75], [83, 0, 3.17], [61, 0, 3.59], [76, 0, 3.87], [50, 1, 2.75], [76, 0, 3.53], [47, 1, 3.91], [66, 0, 4.3], [78, 0, 3.56], [76, 0, 4.0], [78, 1, 2.66], [83, 0, 2.25], [40, 0, 3.19], [65, 0, 3.97], [72, 1, 3.04], [51, 0, 4.58], [75, 0, 4.07], [51, 0, 2.19], [68, 1, 3.51], [68, 0, 3.6], [59, 0, 4.21], [73, 0, 2.07], [47, 1, 5.26], [82, 0, 3.61], [52, 1, 3.02], [67, 0, 3.47], [54, 1, 3.88], [55, 0, 1.58], [57, 0, 2.47], [54, 0, 3.36], [76, 1, 3.92], [74, 0, 2.61], [65, 0, 1.97], [55, 0, 1.84], [81, 1, 2.93], [64, 0, 2.48], [48, 0, 2.84], [66, 0, 1.99], [61, 0, 4.16], [61, 1, 2.86], [80, 0, 3.72], [58, 0, 2.21], [42, 1, 3.3], [43, 0, 3.7], [42, 0, 2.59], [41, 0, 3.99], [42, 0, 3.77], [60, 0, 4.09], [55, 0, 3.92], [61, 0, 3.7], [46, 0, 3.58], [84, 0, 4.16], [62, 0, 3.87], [67, 0, 2.48], [44, 1, 2.46], [48, 1, 4.57], [69, 1, 4.02], [79, 0, 4.01], [54, 1, 3.63], [73, 1, 3.51], [78, 0, 2.97], [49, 1, 2.83], [49, 0, 3.55], [45, 0, 3.18], [85, 0, 3.28], [46, 0, 2.57], [47, 1, 3.62], [44, 0, 4.35], [83, 1, 3.25], [46, 0, 3.0], [50, 0, 2.77], [50, 1, 4.37], [66, 0, 3.81], [83, 0, 2.57], [72, 0, 2.93], [73, 1, 3.97], [79, 1, 4.46], [66, 1, 2.41], [47, 1, 4.55], [47, 1, 4.08], [70, 1, 3.33], [63, 1, 4.29], [85, 1, 4.02], [52, 0, 5.01], [73, 1, 3.64], [48, 0, 4.14], [49, 1, 3.27], [47, 0, 4.15], [53, 0, 3.18], [58, 0, 2.41], [51, 0, 2.89], [53, 0, 2.48], [63, 0, 2.94], [80, 0, 4.02], [59, 1, 1.4], [79, 1, 2.71], [46, 0, 2.62], [82, 0, 3.77], [57, 1, 3.1], [41, 0, 2.89], [56, 0, 2.71], [50, 1, 2.69], [52, 0, 3.42], [80, 0, 3.63], [45, 0, 2.49], [40, 0, 3.94], [46, 1, 2.66], [66, 0, 4.36], [55, 0, 3.08], [59, 0, 3.9], [53, 0, 4.96], [63, 0, 4.43], [70, 1, 3.33], [62, 1, 3.51], [46, 1, 3.89], [48, 1, 3.6], [70, 1, 5.06], [41, 0, 2.02], [73, 1, 3.63], [84, 1, 3.14], [46, 1, 4.31], [55, 1, 2.49], [82, 1, 3.03], [52, 0, 2.74], [76, 0, 3.88], [47, 0, 2.96], [61, 1, 3.93], [65, 0, 4.49], [76, 0, 3.14], [77, 0, 2.53], [55, 0, 4.12], [51, 0, 3.06], [57, 0, 4.2], [44, 0, 2.89], [45, 0, 2.24], [44, 0, 4.12], [74, 1, 4.72], [45, 0, 3.8], [82, 1, 2.62], [80, 1, 3.46], [53, 0, 4.6], [66, 1, 3.54], [80, 0, 2.56], [71, 0, 4.0], [76, 1, 4.39], [41, 0, 5.26], [79, 0, 3.42], [65, 1, 3.97], [82, 0, 3.2], [68, 1, 2.86], [48, 0, 3.84], [52, 1, 1.86], [61, 0, 5.56], [65, 0, 3.37], [44, 0, 1.77], [59, 0, 4.25], [59, 0, 3.76], [69, 1, 4.52], [59, 1, 4.69], [70, 0, 2.99], [60, 1, 4.56], [79, 0, 4.52], [80, 1, 3.6], [79, 1, 4.99], [49, 0, 2.19], [51, 0, 3.25], [63, 0, 2.95], [64, 0, 2.3], [69, 1, 5.18], [47, 1, 3.7], [56, 0, 2.47], [45, 0, 2.62], [42, 1, 2.85], [58, 0, 3.66], [70, 0, 3.35], [50, 0, 4.39], [70, 1, 2.34], [55, 0, 2.92], [47, 0, 2.58], [72, 0, 2.84], [45, 0, 3.69], [64, 0, 3.35], [82, 1, 4.09], [50, 0, 4.34], [56, 0, 1.68], [70, 1, 3.12], [57, 1, 5.72], [74, 0, 3.02], [57, 1, 3.1], [83, 1, 3.29], [66, 0, 3.14], [51, 0, 3.67], [69, 1, 2.76], [61, 0, 3.31], [45, 0, 2.81], [65, 1, 3.88], [73, 0, 3.41]]"; -> "[t]" "[1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1]"; json load "rx_ps" (stat propensity [X] [t] "{\"outcome\": [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1], \"bootstrap\": 200}"); -> "[res]" (concat "risk difference: naive " (math round (* 100 (json select "rx_ps" "/naive/difference")) 1) " pts, IPTW " (math round (* 100 (json select "rx_ps" "/iptw/effect/difference")) 1) " pts [" (math round (* 100 (json select "rx_ps" "/iptw/bootstrap_lower")) 1) ", " (math round (* 100 (json select "rx_ps" "/iptw/bootstrap_upper")) 1) "], matching " (math round (* 100 (json select "rx_ps" "/matching/effect/difference")) 1) " pts (" (json select "rx_ps" "/matching/pairs") " pairs) | SMD of the age: " (math round (json select "rx_ps" "/balance/[0]/smd_before") 2) " -> " (math round (json select "rx_ps" "/balance/[0]/smd_iptw") 2)); json unload "rx_ps"; [res]
risk difference: naive 8.5 pts, IPTW -9.5 pts [-23.1, 3.2], matching -10.4 pts (115 pairs) | SMD of the age: 0.74 -> -0.05
- The naive comparison makes the drug look harmful because the treated patients were at higher risk: the weighting reverses the conclusion.
- Only the measured confounders are corrected: an unmeasured one (smoking here) can still bias the estimate.
A treatment received during the follow-up: Cox model with a time-varying covariate
Patients on a waiting list may receive a transplant after some months. Comparing the transplanted with the others at the inclusion is biased (immortal time: a patient must survive until the transplant). The counting process form splits each transplanted patient in two rows (before and after the transplant) so that the covariate changes at the right time.
#1. One row per period: [transplanted], start, stop, death at the end of the period; -> "[X]" "[[0], [1], [0], [0], [0], [1], [0], [0], [0], [1], [0], [0], [1], [0], [0], [1], [0], [0], [0], [0], [1], [0], [1], [0], [0], [0], [0], [1], [0], [1], [0], [0], [0], [0], [0], [0], [0], [1], [0], [0], [1], [0], [0], [0], [0], [0], [0], [1], [0], [0], [0], [1], [0], [1], [0], [0], [0], [0], [0], [1], [0], [0], [1], [0], [0], [0], [0], [1], [0], [1], [0], [0], [1], [0], [1], [0], [1], [0], [0], [0], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [1], [0], [0], [0], [0], [1], [0], [0], [0], [0], [1], [0], [1], [0], [1], [0], [0], [0], [1], [0], [1], [0], [0], [1], [0], [1], [0], [1], [0], [1], [0], [0], [0], [1], [0], [1], [0], [0], [0], [0], [0], [1], [0], [0], [0], [0], [0], [0], [1], [0], [1], [0], [1], [0], [0], [1], [0], [0], [1], [0], [0], [1], [0], [0], [1], [0], [1], [0], [0], [0], [0], [0], [0], [1], [0], [0], [1], [0], [0]]"; -> "[stop]" "[9.36, 17.48, 25.37, 0.05, 0.57, 9.4, 22.37, 0.58, 0.98, 6.59, 2.22, 6.42, 13.32, 4.46, 27.73, 32.64, 2.18, 3.74, 8.78, 3.9, 4.81, 10.13, 24.88, 4.18, 3.32, 7.09, 0.87, 34.41, 9.2, 36.96, 27.72, 11.25, 2.95, 24.5, 0.89, 3.12, 18.35, 32.38, 12.43, 1.91, 33.86, 9.25, 6.14, 0.17, 0.4, 27.53, 4.16, 8.94, 0.43, 13.8, 3.14, 9.19, 3.8, 5.37, 33.23, 0.13, 3.21, 6.27, 2.96, 18.85, 1.73, 10.61, 19.3, 1.1, 7.31, 1.9, 13.47, 23.44, 8.28, 24.72, 0.83, 9.74, 30.04, 3.89, 28.95, 5.23, 31.52, 16.43, 30.97, 4.69, 7.63, 38.91, 3.0, 8.24, 0.66, 38.31, 0.92, 8.6, 8.21, 17.83, 2.35, 14.6, 5.42, 10.49, 5.49, 2.54, 3.71, 31.14, 0.67, 4.58, 0.79, 23.62, 1.16, 26.29, 1.66, 10.16, 30.91, 0.3, 18.39, 25.35, 13.51, 28.61, 6.87, 2.83, 5.29, 5.74, 15.69, 0.3, 30.42, 0.88, 18.38, 21.69, 4.01, 6.08, 15.22, 1.41, 34.2, 4.08, 12.7, 8.24, 10.34, 7.61, 32.67, 12.81, 13.49, 20.87, 3.27, 7.5, 4.4, 10.79, 0.91, 25.69, 1.05, 22.73, 23.2, 4.5, 14.78, 0.6, 12.99, 31.93, 0.06, 6.87, 15.47, 24.49, 3.7, 24.33, 7.92, 30.79, 7.83, 26.24, 12.32, 2.63, 5.84, 5.51, 16.54, 7.65, 0.48, 4.86, 10.63, 15.05]"; -> "[ev]" "[0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1]"; json load "rx_c" (stat cox [X] [stop] [ev] "{\"start\": [0, 9.36, 0, 0, 0, 0.57, 0, 0, 0, 0.98, 0, 0, 6.42, 0, 0, 27.73, 0, 0, 0, 0, 3.9, 0, 10.13, 0, 0, 0, 0, 0.87, 0, 9.2, 0, 0, 0, 0, 0, 0, 0, 18.35, 0, 0, 1.91, 0, 0, 0, 0, 0, 0, 4.16, 0, 0, 0, 3.14, 0, 3.8, 0, 0, 0, 0, 0, 2.96, 0, 0, 10.61, 0, 0, 0, 0, 13.47, 0, 8.28, 0, 0, 9.74, 0, 3.89, 0, 5.23, 0, 0, 0, 0, 7.63, 0, 3.0, 0, 0.66, 0, 0.92, 0, 8.21, 0, 2.35, 0, 0, 0, 0, 2.54, 0, 0, 0, 0, 0.79, 0, 1.16, 0, 1.66, 0, 0, 0, 18.39, 0, 13.51, 0, 0, 2.83, 0, 5.74, 0, 0.3, 0, 0.88, 0, 0, 0, 6.08, 0, 1.41, 0, 0, 0, 0, 0, 7.61, 0, 0, 0, 0, 0, 0, 4.4, 0, 0.91, 0, 1.05, 0, 0, 4.5, 0, 0, 12.99, 0, 0, 6.87, 0, 0, 3.7, 0, 7.92, 0, 0, 0, 0, 0, 0, 5.51, 0, 0, 0.48, 0, 0]}"); -> "[res]" (concat "hazard ratio of the transplant " (math round (json select "rx_c" "/coefficients/[0]/hazard_ratio") 2) " [" (math round (json select "rx_c" "/coefficients/[0]/hr_lower") 2) ", " (math round (json select "rx_c" "/coefficients/[0]/hr_upper") 2) "], p = " (math round (json select "rx_c" "/coefficients/[0]/p_value") 4) " | " (json select "rx_c" "/n") " rows, " (json select "rx_c" "/events") " deaths"); json unload "rx_c"; [res]
hazard ratio of the transplant 0.65 [0.38, 1.12], p = 0.1214 | 170 rows, 79 deaths
- The same rows handle a late entry (left truncation): start = the age or the date of inclusion.
- With {"strata": [...]} each center keeps its own baseline hazard; {"predict": [[0], [1]]} gives the survival curves.
Dengue incidence by town: spatial autocorrelation and hot spots
The weekly dengue incidence (cases per 100 000) of 20 towns of Reunion island. Moran's I says whether high incidences cluster in space (a common vector source) rather than being scattered; Getis-Ord Gi* names the towns at the heart of a hot spot, where vector control must be reinforced first.
#1. Town centers and incidence [lat, lon, incidence]; -> "[pts]" "[[-20.88, 55.45, 28.0], [-20.9, 55.55, 8.4], [-20.91, 55.61, 17.0], [-20.96, 55.65, 13.4], [-21.0, 55.68, 9.7], [-21.03, 55.71, 11.6], [-20.94, 55.29, 11.9], [-20.93, 55.34, 14.4], [-21.01, 55.27, 17.3], [-21.17, 55.29, 21.1], [-21.24, 55.33, 41.2], [-21.26, 55.37, 46.5], [-21.29, 55.41, 42.5], [-21.34, 55.48, 40.4], [-21.28, 55.52, 38.4], [-21.35, 55.56, 33.9], [-21.38, 55.62, 30.3], [-21.36, 55.77, 39.2], [-21.13, 55.79, 13.7], [-21.14, 55.47, 16.6]]"; -> "[names]" "[\"Saint-Denis\", \"Sainte-Marie\", \"Sainte-Suzanne\", \"Saint-Andr\u00e9\", \"Bras-Panon\", \"Saint-Beno\u00eet\", \"Le Port\", \"La Possession\", \"Saint-Paul\", \"Saint-Leu\", \"Les Avirons\", \"\u00c9tang-Sal\u00e9\", \"Saint-Louis\", \"Saint-Pierre\", \"Le Tampon\", \"Petite-\u00cele\", \"Saint-Joseph\", \"Saint-Philippe\", \"Sainte-Rose\", \"Cilaos\"]"; json load "rx_m" (geo morans_i [pts] "{\"k\": 4}"); json load "rx_h" (geo hotspots [pts] "{\"k\": 4}"); json load "rx_t" [names]; -> "[hot]" ""; -> "[cold]" ""; for (-> "[i]" 0) (< [i] (json count "rx_t" "/")) (++ "[i]") { -> "[sp]" (json select "rx_h" (concat "/points/[" [i] "]/spot")); if (equal [sp] "hot spot") {-> "[hot]" (concat [hot] (json select "rx_t" (concat "/[" [i] "]")) " ");}; if (equal [sp] "cold spot") {-> "[cold]" (concat [cold] (json select "rx_t" (concat "/[" [i] "]")) " ");}; }; -> "[res]" (concat "Moran I " (math round (json select "rx_m" "/i") 3) " (expected " (math round (json select "rx_m" "/expected") 3) ", p = " (json select "rx_m" "/p_value") "): " (json select "rx_m" "/interpretation") " | hot spots: " [hot] "| cold spots: " [cold]); json unload "rx_m"; json unload "rx_h"; json unload "rx_t"; [res]
Moran I 0.555 (expected -0.053, p = 0.001): positive autocorrelation (similar neighbors) | hot spots: Étang-Salé Saint-Louis Saint-Pierre Le Tampon Petite-Île Saint-Joseph Cilaos | cold spots: Sainte-Marie Sainte-Suzanne Saint-André Bras-Panon Saint-Benoît
- geo lisa gives the local Moran cluster of each town (high-high, low-low, outliers); geo spatial_lag regresses the incidence on rainfall or altitude while modeling the spatial dependence.
- With the case addresses instead of rates, geo dbscan finds the clusters of cases and geo kde the densest places.
Effect of a treatment in observational data: doubly robust estimate and E-value
In a cohort of 120 hypertensive patients, the younger and the diabetic ones received a new treatment more often: the crude difference of systolic pressure is confounded. The AIPW estimator combines a propensity model and an outcome model (right if one of the two is right); the E-value says how strong an unmeasured confounder should be to explain the effect away.
#1. Confounders [age, diabetes], treatment, systolic pressure (mmHg); -> "[X]" "[[67, 0], [59, 0], [49, 0], [58, 0], [74, 0], [77, 0], [72, 1], [76, 0], [75, 0], [76, 1], [60, 1], [61, 0], [64, 0], [73, 0], [84, 1], [82, 0], [51, 0], [75, 0], [70, 0], [52, 0], [46, 1], [52, 1], [80, 1], [58, 0], [70, 0], [76, 1], [57, 0], [60, 0], [71, 0], [71, 0], [83, 0], [82, 1], [71, 1], [67, 0], [57, 1], [73, 0], [49, 1], [78, 1], [70, 1], [71, 0], [84, 1], [75, 1], [56, 0], [74, 1], [77, 0], [71, 0], [50, 0], [52, 1], [83, 0], [63, 0], [47, 0], [57, 0], [65, 0], [70, 0], [76, 0], [56, 0], [71, 0], [77, 1], [57, 1], [51, 0], [83, 1], [85, 0], [76, 0], [49, 0], [75, 0], [80, 1], [66, 0], [53, 0], [77, 1], [54, 0], [55, 1], [63, 0], [84, 1], [53, 0], [57, 0], [62, 1], [51, 1], [47, 0], [75, 1], [67, 1], [46, 1], [58, 0], [65, 0], [56, 0], [65, 0], [56, 0], [59, 0], [69, 0], [46, 0], [77, 1], [54, 0], [59, 0], [57, 0], [60, 0], [73, 0], [56, 0], [65, 1], [82, 0], [64, 1], [51, 0], [76, 1], [81, 0], [55, 0], [76, 0], [80, 1], [79, 0], [58, 1], [46, 0], [53, 0], [66, 1], [73, 0], [67, 0], [81, 1], [73, 1], [46, 0], [67, 1], [83, 0], [66, 0], [55, 0], [58, 1]]"; -> "[t]" "[0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1]"; -> "[y]" "[139.1, 144.6, 111.8, 146.0, 129.0, 138.3, 141.0, 150.5, 137.4, 155.4, 133.8, 140.3, 134.3, 154.2, 145.8, 137.6, 141.0, 128.0, 141.2, 125.7, 131.5, 133.9, 155.2, 134.1, 138.4, 149.8, 124.8, 132.3, 129.6, 139.0, 148.0, 157.1, 138.1, 130.3, 132.5, 145.9, 145.4, 142.5, 135.1, 145.6, 155.3, 140.1, 130.5, 147.1, 144.5, 136.7, 134.1, 126.0, 148.2, 143.2, 122.6, 122.7, 139.1, 128.6, 141.4, 137.9, 144.8, 151.9, 146.7, 121.9, 153.4, 148.4, 141.7, 129.1, 133.5, 141.0, 147.0, 133.3, 140.9, 131.0, 131.6, 135.0, 155.1, 122.2, 126.2, 144.4, 131.4, 135.0, 142.9, 147.8, 142.3, 137.2, 120.5, 137.0, 123.1, 119.6, 134.3, 134.0, 123.4, 150.9, 128.5, 128.4, 125.2, 123.7, 140.3, 151.8, 134.6, 131.3, 141.6, 121.9, 152.1, 147.1, 136.3, 139.7, 147.5, 149.9, 131.7, 129.1, 116.5, 145.8, 153.2, 140.2, 142.6, 137.7, 130.4, 149.5, 141.5, 140.8, 130.3, 137.1]"; json load "rx_a" (stat aipw [X] [t] [y]); #2. The effect as a ratio of means, for the E-value (sd of the outcome: approximate RR = exp(0.91 d)); -> "[d]" (/ (json select "rx_a" "/ate/estimate") (stat stddev [y])); -> "[dup]" (/ (json select "rx_a" "/ate/upper") (stat stddev [y])); json load "rx_e" (stat evalue (math exp (* 0.91 [d])) (concat "{\"upper\": " (math exp (* 0.91 [dup])) "}")); -> "[res]" (concat "AIPW effect " (math round (json select "rx_a" "/ate/estimate") 2) " mmHg [" (math round (json select "rx_a" "/ate/lower") 2) ", " (math round (json select "rx_a" "/ate/upper") 2) "], p = " (math round (json select "rx_a" "/ate/p_value") 6) " | outcome model only " (math round (json select "rx_a" "/outcome_regression_only") 2) " | E-value " (math round (json select "rx_e" "/evalue") 2) " (interval bound " (math round (json select "rx_e" "/evalue_ci") 2) ")"); json unload "rx_a"; json unload "rx_e"; [res]
AIPW effect -9.09 mmHg [-11.17, -7.01], p = 0 | outcome model only -8.84 | E-value 4.14 (interval bound 3.28)
- The conversion of a standardized difference d to a risk ratio, RR = exp(0.91 d), is the one proposed by VanderWeele and Ding for continuous outcomes.
- stat ipw, stat matching and stat did are the other designs of the Causal inference page.
