Ir para o conteúdo

Lição 15 · Many Variables

Confounders and Simpson's Paradox

See how a third variable can create, hide or reverse a pattern, work through Simpson's paradox, and use stratification to explore confounding carefully.

A pattern between two variables can change completely when you bring in a third. A gap may shrink, vanish or even point the other way. Learning to ask "What else differs?" is one of the most valuable habits in data work.

What is a confounder?

A confounder is a variable that influences both the supposed cause and the outcome. It can make them look linked, or hide a real link.

flowchart TD
  C["Hot weather"] --> A["Ice cream sales"]
  C --> B["Swimming accidents"]

Weather affects both, so sales and accidents move together without one causing the other.

Simpson's paradox

Simpson's paradox happens when a trend that appears inside every subgroup reverses when the groups are combined. Here is an invented example with two ways of handling support cases. The team assigns hard cases mostly to method B.

Python A reversal, step by step

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

B does better on easy cases and on hard cases. Yet A looks far better overall. The reason is the case mix: A handled mostly easy cases and B mostly hard ones. The combined rate compares different mixes, not only different methods.

Which view is right?

There is no automatic answer. It depends on how the data arose.

  • If difficulty was fixed before the method was chosen and affected the choice, comparing within difficulty levels makes sense.
  • If the third variable is a result of the method, splitting by it can remove part of the very effect you care about.

For example, if a new method causes some cases to be escalated, and you split by escalation, the comparison can become misleading. Think about the order of events. The numbers alone cannot choose for you.

Three roles of a third variable

Confounder Affects both; often adjust
Mediator On the path; think first
Collider Affected by both; adjusting can bias
1 / 2

A collider can fool you in another way. Suppose a school admits students who are strong in sports, in exams or both. Among admitted students only, sports and exam scores may look negatively related, even if they are unrelated among all applicants. Selecting on the result of two variables created the link.

Python Selection creates a fake relationship

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Stratification

Stratification means splitting by a variable and comparing inside each stratum (layer). To get one adjusted figure, you can apply each method's stratum rates to the same standard mix. This is called standardization.

Python Compare with a common case mix

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With the same mix, B comes out ahead. The exact adjusted rates depend on the mix you choose, so state it. This adjustment handles only variables that you measured and included. Other confounders can remain.

Stratify with SQL

SQL Rates by method and difficulty

Press Run (or Ctrl + Enter) to see the result here.

How to look for confounders

  • Ask domain experts how cases are assigned or chosen.
  • Check whether groups differ in size, age, location, season or difficulty.
  • Draw a simple arrow diagram of what you believe affects what.
  • Check key comparisons within the most plausible strata.
  • Remember that very small strata are unstable.

Randomized experiments are powerful because random assignment tends to balance both measured and unmeasured factors across groups. Even then, check that the experiment ran as planned.

Do not keep splitting by new variables until the result flips the way you like. Choose adjustment variables for reasons about how the data was created, and report the comparisons you tried.

Reporting an adjusted comparison

Show readers three things: the overall numbers, the numbers within each stratum and, if you compute one, the adjusted figure with its standard mix. Add the counts. A reader who sees only the adjusted result cannot judge how it was built, and a reader who sees only the overall result may draw the opposite conclusion.

Use careful words: "After comparing cases of the same difficulty, method B had higher solve rates in this sample. Other differences between the cases were not examined." That statement is honest about what was held constant.

Try it yourself

In the first example, move cases so that both methods receive the same mix of easy and hard cases, while keeping each method's rate within each difficulty level. Confirm that the reversal disappears. This shows that the paradox comes from unequal mixes rather than from strange arithmetic.

Questions to ask in practice

Imagine that one hospital ward, sales team or server group looks worse than another. Ask who or what gets sent there. Strong teams are often given the hardest work, and newer systems may receive only simple traffic at first. Ask whether the way cases are assigned changed during the period. A fair-looking ranking can mostly reflect assignment rules.

Write down the possible confounders before you test them, and mark each one as measured, partly measured or not measured. Partly measured matters: splitting cases into just "easy" and "hard" can leave large differences inside each level. Readers should know about the gaps in your adjustment as well as the adjustment itself.

A common mistake

A frequent error is to say that you "controlled for everything" after adding a few columns. You controlled only for those columns, in the form you used them. Name each one in your report. If an important factor was never recorded, such as how motivated a person was, say that it could still explain part or all of the pattern.

Try the explanation on new data

If stratification suggests an explanation, predict what you should see in another period or location where the assignment rule differs. A prediction that holds makes the explanation more credible, although it still is not proof.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What is a confounder?

    Its influence on both can create or hide an association.
  2. 2. In the support example, why did A look better overall?

    The case mix differed strongly between the two methods.
  3. 3. What can happen when you study only cases selected by a combination of two variables?

    This is selection on a collider.
  4. 4. What limits adjustment by stratification?

    Unmeasured factors can still differ between groups.

Summary

  • A third variable can create, hide or reverse a relationship.
  • Simpson's paradox arises when group mixes differ strongly.
  • Whether to adjust depends on whether the variable is a confounder, mediator or collider.
  • Stratification and standardization compare like with like, only for measured variables.
  • Think about how data was created, not only what the numbers say.

Next: Derived Features and Segments

Seu progresso fica salvo neste navegador. Não precisa de conta.