svgrecursive-coef
Recursive coefficient estimates
Does a regression coefficient settle down as data accumulate, or drift — a sign the relationship is changing?
How to read it
- Read the line as the coefficient re-estimated as more rows arrive.
- The band shows how uncertain it is at each size: wide early, narrower later.
- A steady drift away from the dashed line suggests the relationship is changing.
- Callout 1: Early estimates: unstable and uncertain.
- Callout 2: The final estimate on all rows.
- Callout 3: Dashed line: the full-sample value.
Watch out
- Row order matters: this only speaks to time-related change when rows are in time order.
Defaults
No settings: the chart draws one way.
Pinned in the code
- Each point is the OLS estimate from rows 1…r in time order; SE = √(σ̂²_r [(X'X)⁻¹_r]_jj) with σ̂²_r = RSS_r / (r − k).
- Early estimates are noisy by construction; read the shape after the band narrows.
# Recursive coefficient estimates — Codesplash Chart Lab (recipe "recursive-coef")
# controls: defaults
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
try:
import codesplash as cs # optional: the app's chart theme
except ImportError:
cs = None
if cs:
cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else plt.rcParams["axes.prop_cycle"].by_key()["color"]
df = pd.read_csv("recursive-coef-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
_tokens = cs.palette(dark=False) if cs else {}
SEQ = _tokens.get("sequential", ["#eff6fd", "#1a469d"]) # sequential ramp (low → high)
DIV = _tokens.get("diverging", ["#c13e2e", "#f3f5f8", "#0859ac"]) # negative, midpoint, positive
REF = _tokens.get("reference", "#4e5661")
GREY = _tokens.get("dim", "#ced1d6")
def ordered(d, x, cols):
"""Rows with the needed values, sorted by the time column (stable); row order when there is none."""
d = d.dropna(subset=[c for c in cols if c] + ([x] if x else []))
return d.sort_values(x, kind="stable") if x else d
def time_of(d, x):
return d[x] if x else pd.Series(np.arange(1, len(d) + 1), index=d.index)
X = "row"
Y = "slope"
LO = "lower"
HI = "upper"
d = ordered(df, X, [Y])
t = time_of(d, X)
fig, ax = plt.subplots(figsize=(8, 4))
if LO and HI:
ok = d[LO].notna() & d[HI].notna()
ax.fill_between(t[ok], d.loc[ok, LO], d.loc[ok, HI], color=PALETTE[0], alpha=0.16, lw=0, label="± 1.96 SE")
ax.axhline(d[Y].iloc[-1], color=REF, linestyle="--", linewidth=1, label="full-sample estimate")
ax.plot(t, d[Y], color=PALETTE[0], linewidth=1.8, label="estimate on the first r rows")
ax.set_xlabel("row")
ax.set_ylabel(Y)
ax.legend(frameon=False)
plt.show()Needs pandas, numpy, matplotlib · reads recursive-coef-demo.csv · app theme from the optional codesplash helper
# Recursive coefficient estimates — Codesplash Chart Lab (recipe "recursive-coef")
# controls: defaults
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
try:
import codesplash as cs # optional: the app's chart theme
except ImportError:
cs = None
if cs:
cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else px.colors.qualitative.Plotly
df = pd.read_csv("recursive-coef-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
_tokens = cs.palette(dark=False) if cs else {}
SEQ = _tokens.get("sequential", ["#eff6fd", "#1a469d"]) # sequential ramp (low → high)
DIV = _tokens.get("diverging", ["#c13e2e", "#f3f5f8", "#0859ac"]) # negative, midpoint, positive
REF = _tokens.get("reference", "#4e5661")
GREY = _tokens.get("dim", "#ced1d6")
def ordered(d, x, cols):
"""Rows with the needed values, sorted by the time column (stable); row order when there is none."""
d = d.dropna(subset=[c for c in cols if c] + ([x] if x else []))
return d.sort_values(x, kind="stable") if x else d
def time_of(d, x):
return d[x] if x else pd.Series(np.arange(1, len(d) + 1), index=d.index)
X = "row"
Y = "slope"
LO = "lower"
HI = "upper"
d = ordered(df, X, [Y])
t = time_of(d, X)
fig = go.Figure()
if LO and HI:
fig.add_scatter(x=t, y=d[HI], mode="lines", line=dict(width=0), showlegend=False)
fig.add_scatter(x=t, y=d[LO], mode="lines", line=dict(width=0), fill="tonexty", fillcolor="rgba(30, 90, 200, 0.16)", name="± 1.96 SE")
fig.add_scatter(x=t, y=d[Y], mode="lines", line=dict(color=PALETTE[0], width=1.8), name="estimate on the first r rows")
fig.add_hline(y=d[Y].iloc[-1], line=dict(color=REF, dash="dash"))
fig.update_layout(xaxis_title="row", yaxis_title=Y)
fig.show()Needs pandas, numpy, plotly · reads recursive-coef-demo.csv · app theme from the optional codesplash helper
# Recursive coefficient estimates — Codesplash Chart Lab (recipe "recursive-coef")
# controls: defaults
library(ggplot2)
has_cs <- isTRUE(requireNamespace("codesplashr", quietly = TRUE)) # optional: the app's chart theme
theme_cs <- if (has_cs) codesplashr::theme_codesplash(dark = FALSE) else theme_minimal()
cs_cat <- if (has_cs) codesplashr::cs_palette(dark = FALSE)$categorical else grDevices::palette.colors(8, "Okabe-Ito")
if (has_cs) update_geom_defaults("text", list(colour = codesplashr::cs_palette(dark = FALSE)$foreground))
df <- read.csv("recursive-coef-demo.csv", check.names = FALSE, stringsAsFactors = FALSE, na.strings = "") # empty cells are missing; Chart Lab → Code → Download demo CSV
tok <- if (has_cs) codesplashr::cs_palette(dark = FALSE) else list()
seq_ramp <- if (is.null(tok$sequential)) c("#eff6fd", "#1a469d") else tok$sequential # low → high
div_ramp <- if (is.null(tok$diverging)) c("#c13e2e", "#f3f5f8", "#0859ac") else tok$diverging # −, 0, +
ref_col <- if (is.null(tok$reference)) "#4e5661" else tok$reference
grey_col <- if (is.null(tok$dim)) "#ced1d6" else tok$dim
ordered <- function(d, x, cols) {
keep <- stats::complete.cases(d[, c(cols, x), drop = FALSE])
d <- d[keep, , drop = FALSE]
if (!is.null(x)) d[order(d[[x]]), , drop = FALSE] else d
}
time_of <- function(d, x) if (is.null(x)) seq_len(nrow(d)) else d[[x]]
x_col <- "row"
y_col <- "slope"
lo_col <- "lower"
hi_col <- "upper"
d <- ordered(df, x_col, y_col)
d$t <- time_of(d, x_col)
p <- ggplot(d, aes(t, .data[[y_col]])) +
{ if (!is.null(lo_col) && !is.null(hi_col)) geom_ribbon(aes(ymin = .data[[lo_col]], ymax = .data[[hi_col]]), fill = cs_cat[1], alpha = 0.16) } +
geom_hline(yintercept = d[[y_col]][nrow(d)], colour = ref_col, linetype = "dashed") + geom_line(colour = cs_cat[1], linewidth = 0.9) +
labs(x = "row", y = y_col) + theme_cs
print(p)Needs ggplot2 · reads recursive-coef-demo.csv · app theme from the optional codesplashr helper