Sampling and selection bias
Watch who gets into the data bend one simple average four ways.
Explain it simply
Each bar is one customer. Its length shows how many months they stayed before they left. Filled bars are heavy users, and hollow bars are light users. Heavy users stay 18 months on average, light users 6. The dashed line marks the true mean of all 24 customers: 12 months.
24 bars, one per customer, from 1 to 30 months long. Light users average 6 months and heavy users 18. The true mean is 12 months.
The true mean adds every stay and divides by all 24 customers.
Show the code ▾Hide the code ▴ · Try it yourself · Python 3
light = [14, 11, 9, 7, 2, 4, 8, 5, 2, 3, 1, 6] # stays in months, mean 6
heavy = [13, 27, 19, 16, 11, 21, 9, 15, 30, 6, 26, 23] # stays in months, mean 18
# (kind, join month, stay, k): customer 2k is light, 2k + 1 heavy
customers = []
for k in range(12):
customers += [('light', 2 * k, light[k], k), ('heavy', 2 * k + 1, heavy[k], k)]
def mean(xs):
return sum(xs) / len(xs)
print(mean([c[2] for c in customers])) # 12.0 the true mean
reach = {'light': 1, 'heavy': 3} # in-app survey: quarters of each group it reaches
first = {1: [0], 2: [0, 2], 3: [0, 1, 2]}
asked = [c for c in customers if c[3] % 4 in first[reach[c[0]]]]
print(mean([c[2] for c in asked])) # 15.0 the sample mean
def weighted(rows): # each answer counts 1/p times
w = [4 / reach[c[0]] for c in rows]
return sum(wi * c[2] for wi, c in zip(w, rows)) / sum(w)
print(round(weighted(asked), 2)) # 12.0 the weights undo the sampling bias
answers = [c for c in asked if c[2] > 3] # leavers within 3 months skip the survey
print(round(weighted(answers), 2)) # 17.0 selection bias: the weights cannot fix it
today = 24 # the month the company reads its data
still = [c for c in customers if c[2] > today - c[1]]
print(len(still), round(mean([c[2] for c in still]), 2)) # 8 19.25 survivors only
seen = [min(c[2], today - c[1]) for c in customers] # censored at today
exits = [c[2] <= today - c[1] for c in customers]
print(sum(seen), sum(exits)) # 195 16
print(round(sum(seen) / len(seen), 2)) # 8.12 count as ended
print(round(mean([t for t, e in zip(seen, exits) if e]), 2)) # 8.38 drop them
print(round(sum(seen) / sum(exits), 2)) # 12.19 months per exit
Copy it into a file or a Python shell and run it. Change reach and today to match the controls.
Common mix-ups
MYTHToday's customers show how long a customer stays.
FIXToday's table holds only survivors. The short stays left first, so the 8 survivors average 19.3 months, not 12.
MYTHDropping the unfinished stays removes the censoring problem.
FIXDropping them removes the longest stays, so the mean falls to 8.4 months. Months per exit gives 12.2.
Takeaways
- If you know how a sample was drawn, weights undo sampling bias.
- If the outcome decides who is in the data, weights cannot fix it.
- Count a censored stay's months, but never count it as an exit.