ISLR2::Bikeshare: 8,645 hours of one year in Washington, D.C.
bikers, the number of bikes rented in the hourhr: hour of the day, 0 to 23workingday: working day or notmnth: monthtemp: temperature, rescaled to \([0, 1]\)weathersit: weather\[P(Y = y) = \frac{\mu^{y} e^{-\mu}}{y!}, \qquad y = 0, 1, 2, \ldots\]
\(\mu > 0\) — mean
\[E[Y] = Var[Y] = \mu\]
{
const pmf = transpose(poisson_pmf).filter(d => Math.abs(d.mu - mu) < 1e-9);
const sd = Math.sqrt(mu);
const xmax = Math.max(10, Math.ceil(mu + 4 * sd));
const ymax = 1.15 * Math.max(...pmf.map(d => d.prob));
const plot = Plot.plot({
width: 900, height: 340, style: {fontSize: "16px"},
x: {domain: [-0.5, xmax + 0.5], label: "Count y"},
y: {domain: [0, ymax], label: "P(Y = y)"},
marks: [
Plot.rect([{x1: mu - sd, x2: mu + sd}],
{x1: "x1", x2: "x2", y1: 0, y2: ymax,
fill: "#0072B2", fillOpacity: 0.12}),
Plot.ruleX(pmf.filter(d => d.y <= xmax),
{x: "y", y1: 0, y2: "prob", strokeWidth: 2}),
Plot.dot(pmf.filter(d => d.y <= xmax),
{x: "y", y: "prob", r: 2.5, fill: "black"}),
Plot.ruleX([mu], {stroke: "#0072B2", strokeDasharray: "6,4",
strokeWidth: 2})
]
});
return html`<p style="text-align: center"><b>mean = variance = ${mu.toFixed(1)}</b>,
standard deviation = ${sd.toFixed(2)}, P(Y = 0) = ${Math.exp(-mu).toFixed(3)}</p>
<div style="display: flex; justify-content: center">${plot}</div>`;
}
\[\log Y_i \mid x_i \stackrel{ind}{\sim} N\left(x_i\gamma, \sigma^2\right)\]

\[Y_i \mid x_i \stackrel{ind}{\sim} \text{a distribution with mean } \mu_i, \qquad g(\mu_i) = \eta_i, \qquad \eta_i = x_i\beta\]
| Model | Response | Distribution of \(Y_i\) | Link \(g(\mu_i)\) | Mean \(\mu_i\) | Variance | glm() family |
|---|---|---|---|---|---|---|
| Linear regression | Quantitative | \(N(\mu_i, \sigma^2)\) | identity, \(\mu_i\) | \(\eta_i\) | \(\sigma^2\) | gaussian |
| Logistic regression | Binary (\(0/1\)) | \(\text{Bernoulli}(\mu_i)\) | logit, \(\log\left[\mu_i/(1 - \mu_i)\right]\) | \(e^{\eta_i}/\left(1 + e^{\eta_i}\right)\) | \(\mu_i(1 - \mu_i)\) | binomial |
| Poisson regression | Count | \(\text{Poisson}(\mu_i)\) | log, \(\log \mu_i\) | \(e^{\eta_i}\) | \(\mu_i\) | poisson |
\[Var[Y_i \mid x_i] = \phi\, V(\mu_i)\]
| Distribution | \(V(\mu)\) | \(\phi\) |
|---|---|---|
| Normal | \(1\) | \(\sigma^2\) |
| Bernoulli | \(\mu(1 - \mu)\) | \(1\) |
| Poisson | \(\mu\) | \(1\) |
\[\text{log-count linear regression: } E[\log Y_i \mid x_i] = x_i\gamma, \qquad \text{Poisson regression: } \log E[Y_i \mid x_i] = x_i\beta\]
A GLM transforms the mean, not the response
A log link never takes \(\log y_i\): a count of 0 is fine
Rain scales the profile: 0.54 times clear at 3 a.m., 0.63 times at 5 p.m.

\[Y_i \mid x_i \stackrel{ind}{\sim} \text{Poisson}(\mu_i), \qquad \log \mu_i = x_i\beta = \beta_0 + \beta_1 x_{i1} + \cdots + \beta_p x_{ip}\]
Poisson random component, log link
\[\mu_i = e^{x_i\beta} = e^{\beta_0} \times e^{\beta_1 x_{i1}} \times \cdots \times e^{\beta_p x_{ip}}\]
Features add on the log scale, multiply on the scale of the mean
\[\ell(\beta) = \sum_{i=1}^n \left[y_i \log \mu_i - \mu_i - \log(y_i!)\right] = \sum_{i=1}^n \left[y_i\, x_i\beta - e^{x_i\beta} - \log(y_i!)\right]\]
\[\sum_{i=1}^n x_{ij}\left(y_i - \mu_i\right) = 0, \qquad j = 0, 1, \ldots, p\]
Solved by iteratively reweighted least squares
Estimate Std. Error z value Pr(>|z|)
(Intercept) 3.36291632 0.01208439 278.286 < 2.2e-16 ***
workingYes -0.91915334 0.01608340 -57.149 < 2.2e-16 ***
temp_c 0.01605829 0.00024332 65.998 < 2.2e-16 ***
weathercloudy/misty -0.06709450 0.00218683 -30.681 < 2.2e-16 ***
weatherrain/snow -0.58063152 0.00406292 -142.910 < 2.2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

\(e^{\beta_0}\) — mean count when every feature is zero
\[\log \mu(x_j + 1) - \log \mu(x_j) = \beta_j, \qquad \frac{\mu(x_j + 1)}{\mu(x_j)} = e^{\beta_j}\]
\(e^{\beta_j}\) — rate ratio: factor multiplying the mean count per one-unit increase in feature \(j\), other features fixed
| Coefficient | Estimate | \(e^{\widehat\beta_j}\) |
|---|---|---|
| Intercept | 3.3629 | 28.8733 |
| Working day (at hour 0) | -0.9192 | 0.3989 |
| Temperature (per degree C) | 0.0161 | 1.0162 |
| Cloudy/misty vs. clear | -0.0671 | 0.9351 |
| Rain/snow vs. clear | -0.5806 | 0.5595 |
Intercept: 28.9 bikers at hour 0, non-working day, January, clear, 0 °C (linear regression: −10.9)
Rain/snow: ×0.560, a 44% reduction at every hour
5 °C: ×1.084, an increase of 8.4%
\[z = \frac{\widehat\beta_j - \beta_j^0}{\text{SE}\left(\widehat\beta_j\right)}\]
\(\approx N(0, 1)\) under \(H_0: \beta_j = \beta_j^0\)
\[\widehat\beta_j \pm z_{1-\alpha/2}\,\text{SE}\left(\widehat\beta_j\right)\]
Exponentiate the endpoints: interval for the rate ratio \(e^{\beta_j}\)
\[D = 2\left[\ell_{\text{sat}} - \ell\left(\widehat\beta\right)\right]\]
\[G^2 = D_r - D_f = -2\left[\ell\left(\widehat\beta_r\right) - \ell\left(\widehat\beta_f\right)\right]\]
Under \(H_0\): the \(k\) dropped coefficients are zero, \(G^2 \approx \chi^2_k\)
anova(reduced, full, test = "Chisq")
Analysis of Deviance Table
Model 1: bikers ~ hr + working + mnth + temp_c + weather
Model 2: bikers ~ hr * working + mnth + temp_c + weather
Resid. Df Resid. Dev Df Deviance Pr(>Chi)
1 8606 228046
2 8583 99377 23 128669 < 2.2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\(G^2 = 128{,}669\) on \(k = 23\) degrees of freedom, against a \(\chi^2_{23}\) mean of 23
Overdispersion: \(Var[Y_i \mid x_i] > \mu_i\)
\[\frac{y_i - \widehat\mu_i}{\sqrt{\widehat\mu_i}}\]
Pearson residual: standardized by the Poisson standard deviation
\[\widehat\phi = \frac{1}{n - (p+1)}\sum_{i=1}^n \frac{\left(y_i - \widehat\mu_i\right)^2}{\widehat\mu_i}\]

\[G^2 / \widehat\phi = 11{,}380\]
495 times its 23 degrees of freedom: the interaction test’s conclusion survives
DS 3030