8 Data 102 Discussion 08: Goodness-of-Fit & Posterior Predictive Checks
8.0.1 Contact Information
| Name | Wesley Zheng |
| Pronouns | He/him/his |
| wzheng0302@berkeley.edu | |
| Discussion | Wednesdays, 11–12 PM @ Wheeler 224 |
| Office Hours | Tuesdays, 9–10 AM & 11-12 PM @ Gateway B1040 |
Contact me by email at ease — I typically respond within a day or so!
8.1 Goodness-of-Fit for Logistic Regression
In a logistic regression summary, the coefficient table describes estimated effects on the model’s link scale. Standard errors and \(p\)-values summarize uncertainty about individual coefficients. The summary also reports quantities for the model as a whole, such as its log-likelihood and residual degrees of freedom.
For independent binary outcomes with predicted probabilities \(p_i\), the likelihood of observed labels \(y_i\in\{0,1\}\) is \[ L = \prod_{i=1}^n p_i^{y_i}(1-p_i)^{1-y_i}. \]
In Discussion 7, we explored logistic regression and learned to interpret its coefficients using the logit link function: \[ \log\left(\frac{p_i}{1-p_i}\right) = x_i^T\beta, \] where \(p_i\) is the probability that the \(i\)-th observation belongs to class 1. A coefficient \(\beta_j\) is the change in log-odds associated with a one-unit increase in the corresponding predictor, holding all other predictors constant.
In this problem, we fit a logistic regression model to a subset of the famous iris dataset. The dataset contains 100 iris flowers, with measurements of sepal length, sepal width, petal length, and petal width. The response indicates whether a flower belongs to the Virginica species (1) or the Versicolor species (0).
8.1.1 (a)
Goodness-of-Fit Metrics for Frequentist GLMs
We compare two logistic regression models fit to the same 100 iris flowers. The first uses sepal width and sepal length; the second also includes petal width. Their summary outputs are shown below.


Which model has a better fit to the training data? How can you tell? Does this necessarily mean the model will perform better on unseen data?
Answer
The larger model has the better fit to the training data, as indicated by its higher log-likelihood.
For the smaller model, the total log-likelihood over 100 observations was \(-55.163\), giving an average log-likelihood of \[ \frac{-55.163}{100} \approx -0.55. \]
For the larger model, the total log-likelihood was \(-12.951\), giving an average log-likelihood of \[ \frac{-12.951}{100} \approx -0.13. \]
Since a higher log-likelihood indicates a better fit to the observed data, the larger model fits the training data better.
However, this does not necessarily mean that the larger model will generalize better to unseen data. Adding predictors can improve training fit without improving predictive performance, potentially leading to overfitting. To assess generalization, we should evaluate the models on held-out test data or use cross-validation.
The larger model also has a smaller Pearson-\(\chi^2\) statistic, but a lower value alone does not tell you which model will generalize better. For individual Bernoulli observations, the usual chi-squared approximation can also be unreliable.
Keep training fit separate from predictive performance: use held-out data or cross-validation to compare how well the models predict unseen observations.
8.1.2 (b)
Understanding the Data Generating Process
Looking at your findings from Part (a), your friend argues that the petal_width feature has a strong predictive effect that makes the second model better, so it must be the only important factor differentiating the Virginica and Versicolor species.
Explain why this argument is flawed.
Answer
Predictive accuracy and goodness-of-fit metrics do not tell the whole story.
Although adding petal width substantially improves the model’s fit to the observed data, this does not establish that petal width is the only important feature distinguishing the two species. Other features, such as sepal width and sepal length, may also contain useful information. Furthermore, predictors may be correlated, meaning that the apparent importance of a feature can depend on which other features are included in the model.
There may also be important characteristics that were not measured in the dataset, such as flower color. Thus, better model fit does not establish that a particular feature is uniquely important or that the model correctly represents the underlying data-generating process.
Strong predictive performance does not necessarily reveal the true data-generating process. A feature can help predict the outcome without being the only important factor or causing the outcome.
8.2 Bayesian Linear Regression and Posterior Predictive Checks (Review)
Bayes’ rule updates a parameter distribution after observing data: \[ p(\theta\mid y) \propto p(y\mid\theta)\,p(\theta), \] where \(p(\theta)\) is the prior, \(p(y\mid\theta)\) is the likelihood, and \(p(\theta\mid y)\) is the posterior.
For a new response \(Y_{\mathrm{new}}\) at input \(x\), the posterior predictive distribution averages the sampling model over posterior uncertainty: \[ p(y_{\mathrm{new}}\mid x,y) = \int p(y_{\mathrm{new}}\mid x,\theta)\,p(\theta\mid y)\,d\theta. \]
Earlier in Data 102, we learned about Bayesian inference and how to use prior distributions to represent our beliefs about unknown parameters. After observing data, we update these beliefs to obtain a posterior distribution. We can then use the posterior to make predictions about new observations through the posterior predictive distribution.
In this problem, we fit a Bayesian linear regression model to a dataset of \(n=100\) observations, \((x_1,y_1),\ldots,(x_n,y_n)\). We assume that \[ Y \mid X=x,a,b \sim \operatorname{Normal}(ax+b,1), \] where \(a\) and \(b\) are unknown parameters and the observation noise has standard deviation 1. For our prior distributions, we assume that \(a\) and \(b\) are independent, with \[ a \sim \operatorname{Normal}(0,10^2), \qquad b \sim \operatorname{Normal}(0,10^2). \]
8.2.1 (a)
Setting Up the Posterior Predictive Check
Suppose that you have access to a sampler that can sample from the posterior distribution. Provide, in pseudocode, a two-step procedure for sampling from the posterior predictive distribution at any given \(x\).
Answer
For each desired predictive draw:
- Sample \((a,b)\) from the posterior distribution given the observed data.
- Sample \(Y\) from \(\operatorname{Normal}(ax+b,1)\) using the sampled values of \(a\) and \(b\).
8.2.2 (b)
Identifying Bayesian Distributions
The four plots below show, in some order:
- The observed data.
- Samples from the prior distribution over \((a,b)\).
- Samples from the posterior distribution over \((a,b)\).
- The posterior predictive distribution at \(x=4\).
Match each plot to its corresponding description.




Answer
From left to right, top to bottom:
- Posterior distribution over \((a,b)\).
- Observed data.
- Posterior predictive distribution at \(x=4\).
- Prior distribution over \((a,b)\).
Your prior represents beliefs about the parameters before seeing the data; the posterior updates those beliefs after seeing the data. The posterior predictive distribution gives the distribution of a new observation while accounting for uncertainty in the parameters.
8.2.3 (c)
Posterior Predictive Checks
Based on the plots above, could the Bayesian linear regression model pass the posterior predictive check? Explain your reasoning.
Answer
No. The model does not appear to pass the posterior predictive check.
Comparing the posterior predictive samples to the observed data, we see that the predictions at \(x=4\) tend to overshoot the observed value of \(y\). Additionally, the posterior predictive distribution suggests substantially less variability than what we observe in the data. This indicates that the model does not adequately capture the observed data, suggesting possible model misspecification.
Use a posterior predictive check to ask whether data generated from your fitted model resemble the observed data. A model may capture some features while missing others, such as the data’s variability or overall pattern.
If a model passes a check, that does not prove the model is correct; it means only that this check did not reveal a clear discrepancy.