Automated assessment of residual plots with computer vision models

BASF Data Analytics (ML and AI) Community Meeting

Patrick Li

Australian National University

About me

Hi! I’m Patrick Li.

  • PhD in Statistics, Monash University (EBS)

    Developed computer vision methods to automate the assessment of regression diagnostic plots.

  • Postdoctoral Research Fellow, Australian National University (ANU)

    Contributing to the Analytics for the Australian Grains Industry (AAGI) project, applying machine learning and image analytics to plant phenotyping.

  • Current focus: torch-openreml

    A PyTorch-based framework for flexible linear mixed-effects modelling using AI-REML estimation.

️Co-authors

Prof Dianne Cook

Department of Econometrics and Business Statistics, Melbourne, Monash University

Dr Emi Tanaka

Research School of Finance, Actuarial Studies and Statistics, Australian National University

A/Prof Susan VanderPlas

Department of Mathematics and Statistics, Utah State University

A/Prof Klaus Ackermann

Department of Econometrics and Business Statistics, Melbourne, Monash University, Australia

Challenges in interpreting residual plots reliably

Challenges in interpreting residual plots reliably




    Heteroskedasticity ?

      Vertical spread of the points varies with the fitted values.

However, this is an over-interpretation.

The data are simulated from the correctly specified model \boldsymbol{Y} = -\boldsymbol{X} + \boldsymbol{e}, \boldsymbol{e} \sim \mathcal{N} (\boldsymbol{0}, \boldsymbol{I}).

The visual pattern is caused by a skewed distribution of the predictor \ln \boldsymbol{X} \sim \mathcal{N}(\boldsymbol{0}, \boldsymbol{I}).

How do we validate visual discoveries?

Visual discoveries that may appear to violate model assumptions:

  1. Random variation or sampling noise
  2. Features of the predictors
  3. Outliers
  4. Plotting artifacts
  5. Genuine model-assumption violations
  6. …

Traditional statistical testing

Specify the null hypothesis H_0

↓

Construct a test statistic T(X)

↓

Derive the null distribution F_0(t) under H_0

↓

Compare the observed statistic t_{\mathrm{obs}} with the null distribution.


For an upper-tailed test, the p-value under H_0 is P\big(T(X) \geq t_{\mathrm{obs}}\big).

Decision: Reject H_0 if p \leq \alpha.

Visual inference (Buja, et al. 2009)

  1. H_0: The linear regression model satisfies all model assumptions.

  2. T(X): The residual v.s. fitted plot.

  3. F_0(t): Simulate residuals from the fitted regression model (details on the next slide) to generate a collection of null plots.

  4. Randomly place the observed plot, t_{\mathrm{obs}}, among 19 null plots to form a lineup. Ask N observers to identify the most different plot(s).

p = P(K \geq k) = \sum_{j=k}^{N} \binom{N}{j} \left(\frac{1}{20}\right)^j \left(1-\frac{1}{20}\right)^{N-j}.

Null residuals simulation (Buja, et al. 2009)

For a classical normal linear regression model:                                                                                                  

\hat{\boldsymbol{\beta}} = (\boldsymbol{X}^\top\boldsymbol{X})^{-1}\boldsymbol{X}^\top\boldsymbol{y},\quad \hat{\sigma}^2 = \frac{(\boldsymbol{y} - \boldsymbol{X}\boldsymbol{\beta})'(\boldsymbol{y} - \boldsymbol{X}\boldsymbol{\beta})}{n-p}.

  1. Simulate \tilde{\boldsymbol{e}} \sim N(\boldsymbol{0}, \hat{\sigma}^2\boldsymbol{I})

  2. Obtain \tilde{\boldsymbol{y}} = \boldsymbol{X}\hat{\boldsymbol{\beta}} + \tilde{\boldsymbol{e}}

  3. Estimate \tilde{\boldsymbol{\beta}} = (\boldsymbol{X}^\top\boldsymbol{X})^{-1}\boldsymbol{X}^\top\tilde{\boldsymbol{y}}

  4. Obtain \boldsymbol{e}_{null} = \tilde{\boldsymbol{y}} - \boldsymbol{X}\tilde{\boldsymbol{\beta}}, \quad\hat{\boldsymbol{y}}_{null} = \boldsymbol{X}\tilde{\boldsymbol{\beta}}

Characteristics of the lineup protocol

✅

  • All-in-one test: can detect many visually recognisable violations

  • Validated visual findings: helps identify which visual patterns are truly meaningful and may guide model refinement.

❌

  • Human constraints:

    • hard to evaluate many lineups
    • hard to process lineups with many plots
  • Resource-intensive: high labour cost and time-consuming


Can we automate it?

Computer vision model

Modern computer vision models are well-suited for addressing this challenge.

Source: https://en.wikipedia.org/wiki/Convolutional_neural_network

To compare observed plot and null plots


How do we define a numerical measure of “difference” or “distance” between residual plots?


  • pixel-wise sum of square differences?
  • Structural Similarity Index Measure (SSIM)?
  • scagnostics?
  • …



-


=


?

Reference and actual residual distribution

Under H_0, the residuals are given by

\hat{\boldsymbol{e}} = \left[ \boldsymbol{I} - \boldsymbol{X}(\boldsymbol{X}^\top\boldsymbol{X})^{-1} \boldsymbol{X}^\top \right]\boldsymbol{e}, \quad \boldsymbol{e} \sim \mathcal{N}(\boldsymbol{0}, \sigma^2\boldsymbol{I}).

Let P denote the reference residual distribution of \hat{\boldsymbol{e}}.

If H_0 does not hold but the data-generating process for \boldsymbol{Y} is known, we can similarly derive the actual residual distribution of

\hat{\boldsymbol{e}} = \left[ \boldsymbol{I} - \boldsymbol{X}(\boldsymbol{X}^\top\boldsymbol{X})^{-1} \boldsymbol{X}^\top \right]\boldsymbol{Y},

denoted by Q.

Distance from “theoretically” good residual plots

We defined a distance measure based on Kullback-Leibler divergence to quantify the extent of model violations:

D = \log\left(1 + \int_{\mathbb{R}^{n}}\log\frac{p(\hat{\boldsymbol{e}})}{q(\hat{\boldsymbol{e}})}p(\hat{\boldsymbol{e}})d\hat{\boldsymbol{e}}\right).

Q

\overset{D}{\longrightarrow}

P

However, Q is unknown in practice \Rightarrow D can not be computed.

Simulation of training data

In a simulation, we can control the data-generating process and therefore know the actual residual distribution Q.

Non-linearity + Heteroskedasticity

Non-normality + Heteroskedasticity

Distribution of predictor

Estimation of D

We train a computer vision model to estimate D with 64,000 simulated residual plots:

\widehat{D} = f_{CV}(V_{h \times w}(\hat{\boldsymbol{e}}, \boldsymbol{\hat{y}})),

where V_{h \times w}(.) generates an h \times w image, and f_{CV}(.) predicts a non-negative distance.

Statistical testing

The p-value is the proportion of null plots having \widehat{D} greater than or equal to the observed one.

autovi Package

Li, W., Cook, D., Tanaka, E., VanderPlas, S., & Ackermann, K. (2025). Automated Residual Plot Assessment With the R Package autovi and the Shiny Application autovi. web. Australian & New Zealand Journal of Statistics.


Core Methods

  • Null residuals simulation: rotate_resid()
  • Visual signal strength (\widehat{D} prediction): vss()
  • Comprehensive checks: check() and summary_plot()

Example: Boston housing

library(autovi)
fitted_model <- lm(MEDV ~ RM + LSTAT + PTRATIO, data = housing)
checker <- residual_checker(fitted_model)
checker$plot_resid()

rotate_resid()

Null residuals are simulated from the fitted model assuming it is correctly specified.

checker$rotate_resid()
# A tibble: 489 × 2
   .fitted   .resid
     <dbl>    <dbl>
 1 632372.   82404.
 2 525177.   24363.
 3 646753.  -16642.
 4 624848.    7895.
 5 611817.  -25387.
 6 551051. -128980.
 7 504757.  -37748.
 8 445700.   33616.
 9 281912.  -17081.
10 453398. -103580.
# ℹ 479 more rows
checker$rotate_resid() |>
  checker$plot_resid()

vss()

Visual signal strength \widehat{D} of the actual residual plot

checker$vss()
✔ Predict visual signal strength for 1 image.
# A tibble: 1 × 1
    vss
  <dbl>
1  6.60

Visual signal strength \widehat{D} of a null plot

checker$rotate_resid() |>
  checker$vss()
✔ Predict visual signal strength for 1 image.
# A tibble: 1 × 1
    vss
  <dbl>
1  1.27

check() and summary_plot()

checker$check()
── <AUTO_VI object>
Status:
 - Fitted model: lm
 - Keras model: (None, 32, 32, 3) + (None, 5) -> (None, 1)
    - Output node index: 1
 - Result:
    - Observed visual signal strength: 6.484 (p-value = 0)
    - Null visual signal strength: [100 draws]
       - Mean: 1.169
       - Quantiles: 
          ╔══════════════════════════════════════════╗
          ║  25%   50%   75%   80%   90%   95%   99% ║
          ║1.037 1.120 1.231 1.247 1.421 1.528 1.993 ║
          ╚══════════════════════════════════════════╝
    - Bootstrapped visual signal strength: [100 draws]
       - Mean: 6.28 (p-value = 0)
       - Quantiles: 
          ╔══════════════════════════════════════════╗
          ║  25%   50%   75%   80%   90%   95%   99% ║
          ║5.960 6.267 6.614 6.693 6.891 7.112 7.217 ║
          ╚══════════════════════════════════════════╝
    - Likelihood ratio: 0.7064 (boot) / 0 (null) = Extremely large 
checker$summary_plot()

Example: Left-triangle

Breusch–Pagan test p-value = 0.0457

💡Example: Dinosaur

Ramsey Regression Equation Specification Error test p-value = 0.742

Breusch–Pagan test p-value = 0.36

Shapiro-Wilk test p-value = 9.21e-05

🌐Shiny Application

Don’t want to install TensorFlow?

Try our shiny web application: http://autovi.patrickli.org/

Takeaway

  • Use lineup-based tests to validate visual discoveries.

With autovi, you can:

  • Evaluate lineups of residual plots from linear regression models.
  • Quantify the strength of model violations using visual signal strength.
  • Automatically detect model misspecification using a visual test.

Extensions to GLM and LMM frameworks are ongoing research.

Thanks! Any questions?

References

Li, W., Cook, D., Tanaka, E., VanderPlas, S., & Ackermann, K. (2026). Automated residual plot assessment with the R package autovi and the Shiny application autovi. web. Australian & New Zealand Journal of Statistics, 68(1), e70027.

Li, W., Cook, D., Tanaka, E., VanderPlas, S., & Ackermann, K. (2024). Automated Assessment of Residual Plots with Computer Vision Models. arXiv preprint arXiv:2411.01001.

Li, W., Cook, D., Tanaka, E., & VanderPlas, S. (2024). A plot is worth a thousand tests: Assessing residual diagnostics with the lineup protocol. Journal of Computational and Graphical Statistics, 33(4), 1497-1511.

Buja, A., Cook, D., Hofmann, H., Lawrence, M., Lee, E. K., Swayne, D. F., & Wickham, H. (2009). Statistical inference for exploratory data analysis and model diagnostics. Philosophical Transactions of the Royal Society A: Mathematical, Physical and Engineering Sciences, 367(1906), 4361-4383.

tengmcing

[email protected]

📦 https://github.com/TengMCing/autovi

📜 https://github.com/TengMCing/basf2026-autovi