Functions & Formulae

Each applied formula has its own function page, with a signature, implementations, and tests.

Deviance residuals for finding poorly fitted patients in cost regressions and survival models

r_D = sign(y - mu) * sqrt(d); D = sum of d over patients

Turns each patient's contribution to a fitted model's deviance into a signed residual, so the patients a cost regression or a survival curve describes worst can be found before its estimates feed a cost-effectiveness model. The generalised linear model version takes the square root of the unit deviance with the sign of the raw residual; the survival version applies the same idea to the martingale residual, treating the event indicator as a Poisson count with mean equal to the fitted cumulative hazard. The Cox-Snell residual itself, the fitted cumulative hazard, is HE-FM-CUMH-001 on the Cumulative Hazard page. Notation follows the Deviance Residual article.

  • Gamma unit deviance and deviance residual of one patient's cost in a generalised linear model

    d = 2 * ((y - mu) / mu - log(y / mu)); r_D = (y - mu) / abs(y - mu) * sqrt(d)

    For a gamma GLM of positive costs, the unit deviance compares the observed cost with its fitted mean through the proportional gap minus the log of their ratio, so it depends only on y / mu. The deviance residual takes its square root with the sign of y minus mu, and the squared residuals add up to the model deviance. In the Math line the sign is written (y minus mu) / |y minus mu|; at y = mu the residual is 0 by convention, a case the calculator cannot show because it divides by zero there.

  • Martingale residual and survival deviance residual of one patient from a fitted cumulative hazard

    M = delta - H; r_D = M / abs(M) * sqrt(-2 * (M + delta * log(delta - M)))

    The martingale residual is the event indicator minus the fitted cumulative hazard at the patient's observed time, the Cox-Snell residual. It cannot exceed 1 and has no lower bound, so it is skewed; the deviance residual rescales it to be roughly symmetric about zero. Since delta minus M equals the fitted cumulative hazard, the log term is always defined, and for a censored patient, with delta = 0, the residual reduces to minus the square root of twice the cumulative hazard. Treating delta as a Poisson count with mean H, the quantity under the square root is the Poisson unit deviance given in Stata's glm Methods and formulas, which links the survival residual to the GLM residual of HE-FM-DEVR-001.