Example: Evaluating Fits

Note

This page shows an example of a hypothetical lecture note that combines several features of Quarto. This was inspired by notes written by Magnus Kjærgaard.

Computers are very good at least-squares optimization. They can often find parameters for a model even when the model is not a sensible description of the experiment. The scientific question is therefore not only whether a curve can be drawn through the data, but whether the fit is good enough, simple enough, and consistent with what we know about the system.

In kinetic experiments, a fitted parameter might be interpreted as a rate constant. That interpretation only makes sense if the model is appropriate for the data. A high \(R^2\) value is useful, but it is not a substitute for looking at the data, the residuals, and the model assumptions.

flowchart TD
  data["Data"]
  model["Candidate model"]
  fit["Fit parameters"]
  residuals["Residuals"]
  parsimony["Parsimony"]
  theory["Theory check"]
  decision["Accept or revise"]

  data --> model --> fit --> residuals --> parsimony --> theory --> decision
  residuals -. "pattern?" .-> model
  theory -. "implausible?" .-> model
Figure 1: A practical workflow for evaluating a fitted model. Using a mermaid diagram

Experimental variation

Measurements from biochemical experiments are not expected to lie exactly on a theoretical curve. Technical variation can come from pipetting, temperature fluctuations, instrument precision, and other small differences between repeated measurements. For small data sets, showing the individual measurements is often more informative than only showing error bars.

Figure 2 creates a small synthetic decay experiment with technical replicates. The mean values and standard deviations summarize the replicates, but the individual points are still visible.

Code
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(7)
x = np.linspace(0, 20, 11)
true_y = 1.05 * np.exp(-0.11 * x) + 0.04
replicates = true_y[:, None] + rng.normal(0, 0.045, size=(len(x), 4))
means = replicates.mean(axis=1)
sd = replicates.std(axis=1, ddof=1)

fig, ax = plt.subplots(figsize=(7, 4))
for column in replicates.T:
    ax.scatter(x, column, color="#5b7f95", alpha=0.55, s=28)
ax.errorbar(
    x,
    means,
    yerr=sd,
    fmt="o",
    color="#202020",
    ecolor="#202020",
    capsize=3,
    label="mean ± SD",
)
ax.plot(x, true_y, color="#b84a39", label="underlying curve")
ax.set(xlabel="Time", ylabel="Signal")
ax.grid(alpha=0.25)
ax.legend()
plt.show()
Figure 2: Synthetic kinetic data shown as individual technical replicates and mean ± SD. Using Python code cells
NoteWhat the error bars can and cannot tell us

If deviations between data and model are similar in size to the experimental variation, the model may be adequate within error. If deviations are much larger, or if they follow a systematic pattern, the model needs more scrutiny.

Residual explorer

Residuals are the vertical differences between the observed data and the fitted model. For an appropriate model, residuals should usually scatter around zero without a clear pattern. A poor model can still have a convincing-looking curve and a high \(R^2\), but the residuals often reveal what is wrong.

Use the widget to simulate different kinds of data and fit different models. Try fitting a line to exponential data, then inspect the residual plot. Then compare the single exponential, double exponential, and quadratic polynomial when extrapolation is enabled.

#| '!! shinylive warning !!': |
#|   shinylive does not work in self-contained HTML documents.
#|   Please set `embed-resources: false` in your metadata.
#| standalone: true
#| viewerHeight: 760

from shiny import App, reactive, render, ui
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from scipy.optimize import curve_fit, OptimizeWarning
import warnings


def linear_model(x, a, b):
    return a * x + b


def exponential_model(x, amplitude, rate, baseline):
    return amplitude * np.exp(-rate * x) + baseline


def quadratic_model(x, a, b, c):
    return a * x**2 + b * x + c


def double_exponential_model(x, a1, rate1, a2, rate2, baseline):
    return a1 * np.exp(-rate1 * x) + a2 * np.exp(-rate2 * x) + baseline


MODEL_CONFIG = {
    "Linear": {
        "function": linear_model,
        "guess": lambda x, y: [0.0, float(np.mean(y))],
        "bounds": (-np.inf, np.inf),
        "parameters": ["slope", "intercept"],
    },
    "Single exponential": {
        "function": exponential_model,
        "guess": lambda x, y: [
            max(float(y[0] - y[-1]), 0.1),
            0.1,
            max(float(y[-1]), 0.0),
        ],
        "bounds": ([0, 0, -np.inf], [np.inf, np.inf, np.inf]),
        "parameters": ["amplitude", "rate", "baseline"],
    },
    "Quadratic polynomial": {
        "function": quadratic_model,
        "guess": lambda x, y: list(np.polyfit(x, y, 2)),
        "bounds": (-np.inf, np.inf),
        "parameters": ["x²", "x", "constant"],
    },
    "Double exponential": {
        "function": double_exponential_model,
        "guess": lambda x, y: [
            max(float((y[0] - y[-1]) * 0.7), 0.1),
            0.15,
            max(float((y[0] - y[-1]) * 0.3), 0.1),
            0.03,
            max(float(y[-1]), 0.0),
        ],
        "bounds": ([0, 0, 0, 0, -np.inf], [np.inf, np.inf, np.inf, np.inf, np.inf]),
        "parameters": ["amplitude 1", "rate 1", "amplitude 2", "rate 2", "baseline"],
    },
}


def true_signal(x, pattern):
    if pattern == "Exponential decay":
        return 1.05 * np.exp(-0.12 * x) + 0.05
    if pattern == "Linear trend":
        return 0.95 - 0.035 * x
    return 1.0 - 0.035 * x + 0.0018 * (x - 10) ** 2


def simulate_data(pattern, n_points, noise, seed, resimulate):
    rng = np.random.default_rng(seed + 1009 * resimulate)
    x = np.linspace(0, 20, n_points)
    clean = true_signal(x, pattern)
    y = clean + rng.normal(0, noise, size=n_points)
    return x, y, clean


def fit_model(x, y, model_name):
    config = MODEL_CONFIG[model_name]
    function = config["function"]
    guess = config["guess"](x, y)
    bounds = config["bounds"]
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", OptimizeWarning)
        popt, _ = curve_fit(
            function,
            x,
            y,
            p0=guess,
            bounds=bounds,
            maxfev=20000,
        )
    predicted = function(x, *popt)
    residuals = y - predicted
    rss = float(np.sum(residuals**2))
    tss = float(np.sum((y - np.mean(y)) ** 2))
    r_squared = 1 - rss / tss if tss > 0 else np.nan
    return popt, predicted, residuals, rss, r_squared


app_ui = ui.page_fluid(
    ui.layout_sidebar(
        ui.sidebar(
            ui.input_select(
                "pattern",
                "True data pattern",
                ["Exponential decay", "Linear trend", "Curved trend"],
            ),
            ui.input_select(
                "model",
                "Fitted model",
                ["Linear", "Single exponential", "Quadratic polynomial", "Double exponential"],
                selected="Linear",
            ),
            ui.input_slider("noise", "Noise level", 0.0, 0.20, 0.04, step=0.01),
            ui.input_slider("n_points", "Number of points", 6, 24, 11, step=1),
            ui.input_slider("seed", "Random seed", 1, 100, 7, step=1),
            ui.input_checkbox("extrapolate", "Show extrapolation", False),
            ui.input_action_button("resimulate", "Resimulate"),
        ),
        ui.output_plot("fit_plot"),
        ui.output_table("metrics"),
    )
)


def server(input, output, session):
    @reactive.calc
    def data():
        return simulate_data(
            input.pattern(),
            input.n_points(),
            input.noise(),
            input.seed(),
            input.resimulate(),
        )

    @reactive.calc
    def fit():
        x, y, _ = data()
        return fit_model(x, y, input.model())

    @output
    @render.plot(alt="Fitted model and residual plot")
    def fit_plot():
        x, y, clean = data()
        popt, predicted, residuals, rss, r_squared = fit()
        config = MODEL_CONFIG[input.model()]

        x_max = (36 if input.extrapolate() else 20) + 0.5
        x_min = 0 - 0.5
        x_grid = np.linspace(x_min, x_max, 240)
        fitted_grid = config["function"](x_grid, *popt)
        true_grid = true_signal(x_grid, input.pattern())

        fig, axes = plt.subplots(
            2,
            1,
            figsize=(8, 7),
            gridspec_kw={"height_ratios": [2.2, 1]},
            sharex=False,
        )

        axes[0].scatter(x, y, color="#244c5a", s=42, label="observed data")
        axes[0].plot(x_grid, fitted_grid, color="#b84a39", linewidth=2.2, label="fitted model")
        axes[0].plot(
            x_grid,
            true_grid,
            color="#5e7f42",
            linewidth=1.7,
            linestyle="--",
            label="true pattern",
        )
        axes[0].axvspan(20, x_max, color="#eeeeee", alpha=0.8 if x_max > 20 else 0)
        axes[0].set(ylabel="Signal", xlim=(0, x_max))
        axes[0].grid(alpha=0.25)
        axes[0].legend(loc="best")
        axes[0].set_xlim(x_min, x_max)

        axes[1].axhline(0, color="#202020", linewidth=1)
        axes[1].scatter(x, residuals, color="#6f5a9b", s=38)
        axes[1].vlines(x, 0, residuals, color="#6f5a9b", alpha=0.45)
        axes[1].set(xlabel="Time", ylabel="Residual", xlim=(0, 20))
        axes[1].grid(alpha=0.25)
        axes[1].set_xlim(x_min, x_max)


        fig.tight_layout()
        return fig

    @output
    @render.table
    def metrics():
        popt, predicted, residuals, rss, r_squared = fit()
        config = MODEL_CONFIG[input.model()]
        parameter_text = ", ".join(
            f"{name}={value:.3g}"
            for name, value in zip(config["parameters"], popt)
        )
        return pd.DataFrame(
            {
                "Metric": [
                    "R²",
                    "Residual sum of squares",
                    "Model parameters",
                    "Fitted values",
                ],
                "Value": [
                    f"{r_squared:.4f}",
                    f"{rss:.4f}",
                    str(len(popt)),
                    parameter_text,
                ],
            }
        )

app = App(app_ui, server)
Figure 3: Interactive residual explorer using Shiny Apps. The widget shows the fitted curve, residuals, and metrics, but it deliberately does not decide whether the model is acceptable. That judgment should come from comparing the visual pattern, the parameter count, and the scientific context.
WarningDo not stop at R²

The linear model can achieve a respectable \(R^2\) on exponential data, especially when the noise level is high or the measured range is short. The residual plot is often more diagnostic because it shows whether deviations are random or systematic.

Parsimony and theoretical soundness

More parameters usually make it easier to reduce the residual sum of squares. That does not automatically make the model better. A double exponential has more freedom than a single exponential, and a polynomial can mimic many shapes inside the measured range. Parsimony asks us to prefer the simplest model that explains the data adequately.

Theoretical soundness is a separate check. For a kinetic decay experiment, an exponential model may be justified by the reaction mechanism. A polynomial might fit the measured points just as well, but it can predict an unphysical increase when extrapolated beyond the data. Wrong models often give wrong predictions outside the region where they were fitted.

Authoring note

This page uses ShinyLive because the learner interaction changes the data and refits the selected model in Python. A Plotly or Observable JS widget would be a good fit for exploring prepared results, but this example needs live recalculation with scipy.optimize.curve_fit.

The code is folded rather than hidden. That keeps the page readable as course material while still allowing handbook readers to inspect how the synthetic data, fits, residuals, and embedded app are built.