DEV Community

Nylah Reynard
Nylah Reynard

Posted on

Why Splitting Your Data Into Two Subgroups and Comparing p-Values Is a Statistical Bug

A pull request in our experimentation pipeline landed on my desk last week with a celebratory note: "Feature rollout is a massive success on mobile, but ineffective on desktop."

The author had sliced our A/B test logs into two cohorts. In Subgroup A (mobile), conversion lift showed p = 0.0183. In Subgroup B (desktop), lift showed p = 0.2143. Based on that contrast, the team proposed shipping exclusively to mobile while routing desktop resources toward a redesign.

When I checked the underlying estimates, the data told a very different story. Subgroup A had an effect estimate of 0.4800 with SE = 0.2000. Subgroup B had an effect estimate of 0.2500 with SE = 0.2000. Running a formal interaction test yielded z = 0.8132 and p = 0.4161.

There was zero evidence of any difference between platforms. The team had fallen for one of the most common statistical bugs in data science: comparing isolated within-group p-values.


Andrew Gelman and Hal Stern dissected this fallacy in their 2006 paper in The American Statistician: "The Difference Between 'Significant' and 'Not Significant' is not Itself Statistically Significant."

When teams evaluate features, they often partition data along arbitrary cuts—mobile versus desktop, or new versus returning users—and run separate tests on each slice. If one subgroup achieves p < 0.05 and the other lands at p >= 0.05, they declare that the feature acts differently across segments.

That inference is mathematically invalid.

In our concrete example, Subgroup A is significant (p = 0.0183) with an estimate 2.40 standard errors from zero. Subgroup B is not significant (p = 0.2143) with an estimate 1.25 standard errors from zero. But is the difference between 0.4800 and 0.2500 distinguishable from noise?

To test whether the subgroups differ, we evaluate the contrast: delta = 0.4800 - 0.2500 = 0.2300. Because both cohorts are independent, the variance of their difference is the sum of their variances: 0.2000^2 + 0.2000^2 = 0.0800, giving SE = sqrt(0.0800) = 0.2828.

Dividing the difference by its standard error gives z = 0.2300 / 0.2828 = 0.8132, yielding a two-tailed p-value of 0.4161. A difference this large occurs by chance over 41% of the time under identical dynamics. Claiming heterogeneous behavior asserts a distinction unsupported by the data.


The problem gets worse when you examine statistical power. In 2007, Rui Wang and colleagues published a landmark methodological guide in the New England Journal of Medicine ("Reporting of Subgroup Analyses in Clinical Trials", NEJM 357:2189-2194). They demonstrated that subgroup analyses routinely suffer from severe power loss and inflated false positives when evaluated through isolated within-group tests.

Suppose you size an overall experiment for 80% power with true Cohen's d = 0.40 and total N = 200 (100 control, 100 treatment).

Splitting that sample in half into Subgroup A and Subgroup B (50 per arm) drops sample size per test by half. Because standard error scales with 1 / sqrt(n), halving n cuts the test statistic and collapses individual subgroup power from 80% to roughly 50%.

When both subgroups have 50% power under the exact same true effect, the probability that Subgroup A achieves p < 0.05 is 0.50, and the probability that Subgroup B achieves p < 0.05 is 0.50. Under independent samples, the probability that exactly one subgroup is significant is:

0.50 * (1 - 0.50) + (1 - 0.50) * 0.50 = 0.50 (50%).

By naively checking whether one subgroup is significant and the other is not, you will falsely claim subgroup heterogeneity half the time—even when the underlying effect is completely identical.


Here is the verified Python reproduction and Monte Carlo simulation testing both the analytical example and a 20,000-trial experiment under identical true effects:

import numpy as np
from scipy import stats

# Part 1: Concrete two-subgroup dataset where Subgroup A has p < 0.05 and Subgroup B has p > 0.20,
# yet the formal subgroup interaction test shows zero difference between subgroups!
rng_ex = np.random.default_rng(42)
# Subgroup A (n=50 per arm): estimate ~ 0.48, SE ~ 0.20 => z ~ 2.40, p ~ 0.016
# Subgroup B (n=50 per arm): estimate ~ 0.25, SE ~ 0.20 => z ~ 1.25, p ~ 0.211
est_a, se_a, df_a = 0.4800, 0.2000, 98
est_b, se_b, df_b = 0.2500, 0.2000, 98

p_a = 2.0 * (1.0 - stats.t.cdf(abs(est_a / se_a), df=df_a))
p_b = 2.0 * (1.0 - stats.t.cdf(abs(est_b / se_b), df=df_b))
diff_ab = est_a - est_b
se_diff = np.sqrt(se_a**2 + se_b**2)
z_int = diff_ab / se_diff
p_int = 2.0 * (1.0 - stats.norm.cdf(abs(z_int)))

print(f"Subgroup A isolated test: diff = {est_a:.4f}, SE = {se_a:.4f}, p = {p_a:.4f}")
print(f"Subgroup B isolated test: diff = {est_b:.4f}, SE = {se_b:.4f}, p = {p_b:.4f}")
print(f"Formal interaction test:  delta = {diff_ab:.4f}, SE = {se_diff:.4f}, z = {z_int:.4f}, p = {p_int:.4f}")

# Part 2: 20,000-trial Monte Carlo simulation where BOTH subgroups have the EXACT SAME true effect!
# Overall study has 80% power (true d = 0.40, total N = 200 split 50/50 into Subgroup A and Subgroup B, so n=50/arm in each subgroup)
rng = np.random.default_rng(20261124)
n_trials = 20000
n_sub = 50
true_d = 0.40

a_ctrl = rng.normal(0.0, 1.0, size=(n_trials, n_sub))
a_trt = rng.normal(true_d, 1.0, size=(n_trials, n_sub))
b_ctrl = rng.normal(0.0, 1.0, size=(n_trials, n_sub))
b_trt = rng.normal(true_d, 1.0, size=(n_trials, n_sub))

p_sub_a = stats.ttest_ind(a_trt, a_ctrl, axis=1, equal_var=False).pvalue
p_sub_b = stats.ttest_ind(b_trt, b_ctrl, axis=1, equal_var=False).pvalue

# Naive bug: claiming subgroup heterogeneity whenever one subgroup has p < 0.05 and the other has p >= 0.05
naive_split_claim = (p_sub_a < 0.05) ^ (p_sub_b < 0.05)

# Formal difference-in-differences interaction test across the two subgroups
eff_a = a_trt.mean(axis=1) - a_ctrl.mean(axis=1)
eff_b = b_trt.mean(axis=1) - b_ctrl.mean(axis=1)
var_a = a_trt.var(axis=1, ddof=1) / n_sub + a_ctrl.var(axis=1, ddof=1) / n_sub
var_b = b_trt.var(axis=1, ddof=1) / n_sub + b_ctrl.var(axis=1, ddof=1) / n_sub
z_interaction = (eff_a - eff_b) / np.sqrt(var_a + var_b)
p_interaction = 2.0 * (1.0 - stats.norm.cdf(np.abs(z_interaction)))

print(f"Identical true effect (d=0.40 in both subgroups, n=50/arm per subgroup):")
print(f"  Single-subgroup power in A:              {np.mean(p_sub_a < 0.05):.4f} ({np.mean(p_sub_a < 0.05)*100:.2f}%)")
print(f"  Naive 'one sig, one non-sig' false rate: {np.mean(naive_split_claim):.4f} ({np.mean(naive_split_claim)*100:.2f}%)")
print(f"  Formal interaction test false rate:      {np.mean(p_interaction < 0.05):.4f} ({np.mean(p_interaction < 0.05)*100:.2f}%)")
Enter fullscreen mode Exit fullscreen mode

Running this script outputs:

Subgroup A isolated test: diff = 0.4800, SE = 0.2000, p = 0.0183
Subgroup B isolated test: diff = 0.2500, SE = 0.2000, p = 0.2143
Formal interaction test:  delta = 0.2300, SE = 0.2828, z = 0.8132, p = 0.4161
Identical true effect (d=0.40 in both subgroups, n=50/arm per subgroup):
  Single-subgroup power in A:              0.5099 (50.98%)
  Naive 'one sig, one non-sig' false rate: 0.5035 (50.35%)
  Formal interaction test false rate:      0.0508 (5.08%)
Enter fullscreen mode Exit fullscreen mode

The simulation results confirm the mathematical theory:

First, single-subgroup statistical power in A drops to 50.98%. Halving sample size turns an 80%-powered experiment into a coin flip for each individual subgroup.

Second, the naive "one sig, one non-sig" heuristic triggers a false heterogeneity claim 50.35% of the time. In over half of neutral experiments where both subgroups share the exact same true effect, the informal check invents differences that do not exist.

Third, the formal difference-in-differences interaction test holds Type I error at 5.08%, perfectly aligning with the nominal 5% alpha threshold.


To prevent this statistical bug in production pipelines:

  1. Never compare within-group p-values. Stop checking whether p_a < 0.05 while p_b >= 0.05.

  2. Use a pooled regression model with an explicit interaction term (treatment \* subgroup), or compute the formal difference contrast: z = (eff_a - eff_b) / sqrt(se_a**2 + se_b**2). Only claim heterogeneity if the interaction term reaches statistical significance.

  3. Plot forest plots with confidence intervals. When you plot both subgroup intervals side by side, overlapping estimates make non-significance immediately obvious.

The difference between significant and not significant is not itself statistically significant. When you split data and compare p-values, you are not finding real behavioral patterns—you are just measuring sampling noise.

Top comments (1)

Collapse
 
ahmetozel profile image
Ahmet Özel •

This is one of the most common experiment-review bugs in product analytics: seeing p = 0.0183 on mobile and p = 0.2143 on desktop and concluding the feature only works on mobile when the formal interaction test sits at p = 0.4161. Halving an 80% powered experiment into two subgroups drops each slice to roughly 50% power, which guarantees a false split conclusion half the time even when both platforms share the exact same lift.

Requiring an explicit interaction contrast with combined standard error in PR templates before anyone proposes platform-specific rollouts prevents teams from burning engineering cycles on noise.