svgspec-curve
Specification curve
Does the conclusion survive every reasonable way of specifying the analysis?
How to read it
- Read the top panel as estimates across every specification, ranked by size.
- Read the dots below for which choices each specification uses.
- Look for choices that line up at one end: they drive the result.
- Callout 1: Estimates sorted by size.
- Callout 2: The choices behind each specification.
- Callout 3: The median and the share with the same sign.
Watch out
- The specifications aren't independent, and the set you list shapes the picture.
Defaults
- Null value
- 0
- The value that means no effect.
- Range -1,000,000,000 to 1,000,000,000, step 0.1
Pinned in the code
- Draws the run's numbers as given (computed by the Python/R engine); the chart computes nothing statistical.
- Input has one row per specification × choice dimension; specifications are drawn in the order of `spec` (the engine ranks them by estimate).
- The summary counts significant specifications and those sharing the median's sign; Simonsohn et al.'s permutation inference is reported by the engine when run.
# Specification curve — Codesplash Chart Lab (recipe "spec-curve")
# controls: null=0
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("spec-curve-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["dimension"] = pd.Categorical(df["dimension"].astype("string"), categories=["covariates", "outliers", "outcome", "sample"])
SPEC = "specification"
E = "effect (SD)"
LO = "ci_low"
HI = "ci_high"
SIG = "significant"
DIM = "dimension"
OPT = "option"
NULL = 0
specs = df.drop_duplicates(SPEC).sort_values(SPEC)
rows = df[[DIM, OPT]].astype(str).drop_duplicates().values.tolist()
fig, (ax, bx) = plt.subplots(2, 1, figsize=(8, 6), sharex=True, gridspec_kw=dict(height_ratios=[2, 1.3]))
col = np.where(specs[SIG].astype(bool), PALETTE[0], "grey")
if LO and HI: ax.vlines(specs[SPEC], specs[LO], specs[HI], colors=col, alpha=0.35)
ax.scatter(specs[SPEC], specs[E], c=col, s=12)
ax.axhline(NULL, color="grey", linewidth=0.8)
ax.axhline(specs[E].median(), color=PALETTE[1], linestyle="--", linewidth=1)
ax.set_ylabel(E)
for k, (dim, opt) in enumerate(rows):
used = df[(df[DIM].astype(str) == dim) & (df[OPT].astype(str) == opt)][SPEC]
bx.scatter(used, [k] * len(used), color="black", s=6)
bx.set_yticks(range(len(rows)), [f"{d}: {o}" for d, o in rows], fontsize=7)
bx.invert_yaxis()
bx.set_xlabel("specifications, ranked by estimate")
plt.show()Needs pandas, numpy, matplotlib · reads spec-curve-demo.csv · app theme from the optional codesplash helper
# Specification curve — Codesplash Chart Lab (recipe "spec-curve")
# controls: null=0
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("spec-curve-demo.csv", keep_default_na=False, na_values=[""]) # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV
df["dimension"] = pd.Categorical(df["dimension"].astype("string"), categories=["covariates", "outliers", "outcome", "sample"])
SPEC = "specification"
E = "effect (SD)"
LO = "ci_low"
HI = "ci_high"
SIG = "significant"
DIM = "dimension"
OPT = "option"
from plotly.subplots import make_subplots
specs = df.drop_duplicates(SPEC).sort_values(SPEC)
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, row_heights=[0.6, 0.4])
kw = dict(error_y=dict(type="data", symmetric=False, array=specs[HI] - specs[E], arrayminus=specs[E] - specs[LO], thickness=1)) if LO and HI else {}
fig.add_scatter(x=specs[SPEC], y=specs[E], mode="markers", marker=dict(color=np.where(specs[SIG].astype(bool), PALETTE[0], "grey"), size=6), showlegend=False, row=1, col=1, **kw)
lab = df[DIM].astype(str) + ": " + df[OPT].astype(str)
fig.add_scatter(x=df[SPEC], y=lab, mode="markers", marker=dict(color="black", size=4), showlegend=False, row=2, col=1)
fig.update_yaxes(title_text=E, row=1, col=1)
fig.show()Needs pandas, numpy, plotly · reads spec-curve-demo.csv · app theme from the optional codesplash helper
# Specification curve — Codesplash Chart Lab (recipe "spec-curve")
# controls: null=0
library(ggplot2)
library(patchwork)
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("spec-curve-demo.csv", check.names = FALSE, stringsAsFactors = FALSE, na.strings = "") # empty cells are missing; Chart Lab → Code → Download demo CSV
df[["significant"]] <- as.logical(df[["significant"]])
df[["dimension"]] <- factor(df[["dimension"]], levels = c("covariates", "outliers", "outcome", "sample"))
df[["option"]] <- factor(df[["option"]])
spec_col <- "specification"
e_col <- "effect (SD)"
lo_col <- "ci_low"
hi_col <- "ci_high"
sig_col <- "significant"
dim_col <- "dimension"
opt_col <- "option"
null_value <- 0
d <- data.frame(spec = df[[spec_col]], est = df[[e_col]], sig = as.logical(df[[sig_col]]), dim = as.character(df[[dim_col]]), opt = as.character(df[[opt_col]]),
lo = if (is.null(lo_col)) NA_real_ else df[[lo_col]], hi = if (is.null(hi_col)) NA_real_ else df[[hi_col]])
specs <- d[!duplicated(d$spec), ]
top <- ggplot(specs, aes(spec, est, colour = sig)) + geom_hline(yintercept = null_value, colour = "grey50") +
geom_linerange(aes(ymin = lo, ymax = hi), alpha = 0.35, na.rm = TRUE) + geom_point(size = 1.2) +
geom_hline(yintercept = stats::median(specs$est), colour = cs_cat[2], linetype = "dashed") +
scale_colour_manual(values = c("FALSE" = "grey55", "TRUE" = cs_cat[1]), guide = "none") + labs(x = NULL, y = e_col) + theme_cs
d$row <- factor(paste0(d$dim, ": ", d$opt), levels = rev(unique(paste0(d$dim, ": ", d$opt))))
bottom <- ggplot(d, aes(spec, row)) + geom_point(size = 0.8) + labs(x = "specifications, ranked by estimate", y = NULL) + theme_cs
p <- patchwork::wrap_plots(top, bottom, ncol = 1, heights = c(2, 1.3))
print(p)Needs ggplot2, patchwork · reads spec-curve-demo.csv · app theme from the optional codesplashr helper