Counts, proportions and skewed responses: generalized linear models#

Least squares assumes that the response varies by the same amount about every fitted value. Several common responses do not:

  • a count of defects varies more where the count is larger;

  • a number of successes out of a number of trials is bounded by 0 and the trials;

  • a time to failure spreads in proportion to its size.

A generalized linear model relates the mean of such a response to the factors through a link (the log of a count’s mean, the log-odds of a proportion), and lets the variance follow the mean as the response’s distribution (its family) says it should. analysis_type="glm" fits one, by maximum likelihood.

The coupon experiment#

Montgomery (2013, section 15.1) describes a test of three features of a coupon mailing: A, B and C, each at two levels. Each of the eight combinations was mailed to 1000 customers, and the response is the number who redeemed the coupon: a number of successes out of 1000 trials, so the family is binomial.

import pandas as pd
from process_improve.experiments import analyze_experiment

runs = pd.DataFrame({
    "A": [-1, 1, -1, 1, -1, 1, -1, 1],
    "B": [-1, -1, 1, 1, -1, -1, 1, 1],
    "C": [-1, -1, -1, -1, 1, 1, 1, 1],
    "redeemed": [200, 250, 265, 347, 210, 286, 271, 326],
})
out = analyze_experiment(
    runs, response_column="redeemed", model="redeemed ~ A + B + C + B:C",
    analysis_type="glm", family="binomial", trials=1000,
)["glm"]
a = out["coefficients"][1]
print(round(a["coefficient"], 4), round(a["exp_coefficient"], 2))   # 0.1687 1.18

Under the logit link, a coefficient is a change in the log-odds of redemption, and exp_coefficient is the odds ratio: moving A by one coded unit multiplies the odds of redemption by 1.18. The result also holds:

  • each coefficient’s Wald z_value, p_value and interval;

  • the log_likelihood, deviance and pearson_chi2;

  • the goodness_of_fit tests, with Hosmer and Lemeshow’s for a binomial response;

  • the fitted mean of each run with its interval.

The interval for a mean is worked on the scale of the link and carried back, so a proportion’s interval stays between 0 and 1, and is not symmetric about the fitted value.