Using computational tools to determine whether what is seen in the data can be assumed to apply more broadly
Professor Di Cook
Department of Econometrics and Business Statistics
What this class is about
Revisiting hypothesis testing
(Frequentist) hypothesis testing framework
Suppose \(X\) is the number of heads out of \(n\) independent tosses.
Let \(p\) be the probability of getting a for this coin.
Hypotheses
\(H_0: p = 0.5\) vs. \(H_a: p > 0.5\). Note \(p_0=0.5\). Alternative\(H_a\)is saying we believe that the coin is biased to heads.
NOTE: Alternative needs to be decided before seeing data.
Assumptions Each toss is independent with equal chance of getting a head.
Test statistic
It has been mathematically shown that \(X \sim B(n, p)\). If \(H_0\) is true, we expect the number of heads to be \(np_0\). We observe \(n, x\) and compute the estimate, \(\widehat{p} = \frac{x}{n}\). If \(np_0 \geq 5, n(1-p_0)\geq 5\) it has been shown mathematically that this test statistic is \(\frac{(\widehat{p} - p_0)}{\sqrt{p_0(1-p_0)/n}}\) has a normal distribution.
This can be used to compute the probability of seeing more than\(x\) heads in \(n\) coin flips, the \(p\)-value. If this is small (e.g. \(<\) 0.05), it is unlikely to have happened, if \(H_0\) was true.
Computationally testing coin bias (1/3)
Experiment 1: I flipped the coin 10 times and this is the result:
You need to be able to calculate the probability of something happening, if the null was true.
Evidence by test statistic computer using the observations. Judgement by \(p\)-value (probability that the event, or more extreme events, happened assuming the null is true).
Does the test statistic have to be numerical?
Testing hypotheses using data plots
Motivation
Is this an example of a good residual plot, or does it suggest that there is a problem with the model fit?
Hypothesis testing in a visual inference framework is where:
the test statistic is a plot and
judgement is by human visual perception.
Why is the plot a test statistic? We’ll see why soon.
You, we, me actually do visual inference many times but generally in an informal fashion.
The problem with doing this is we are making an inference on whether the plot has any patterns based on a single data plot.
The single data plot needs to be examined in the context of what might this look like if different samples were shown.
Reading data plots requires calibration.
Visual inference steps
Write your plot description using ggplot. DON’T MAKE THE DATA PLOT!
Based on the description articulate the null and alternate hypotheses, e.g. \(H_A:\) the two variables have association \(\longrightarrow\)\(H_0:\) there is no association between the two variables.
Decide on a method to generate null data, sample that are consistent with the null hypothesis.
Make \(m-1\) null samples.
Make a lineup of the plots of the null samples with the plot of the data plots randomly inserted. All plots use the same ggplot description, just different samples.
Ask \(n\) human viewers to select a plot in the lineup that looks different to others. Don’t give any context, and disguise axes.
The nullabor::lineup() function can do simple lineups.
Using the same type of calculation as done for the coi flipping, compute the probability that \(x\) out of \(n\) people detected the data plot from a lineup, then
the visual inference p-value is given as \[P(X \geq x)\] where \(X \sim B(n, 1/m)\), and
the power of a lineup is estimated as \(x/n\).
Easier, still use the nullabor::pvisual() function to compute it.
Example: residual plots (1/4)
Which plot has a pattern that is most different from other plots?
Residuals from dist~speed using datasets::cars.
Code
lm(dist ~ speed, data = cars)
This is a lineup of the residual plot
Which plot (if any) looks different from the others?
Why do you think it looks different?
> decrypt("clZx bKhK oL 3OHohoOL 0B")
[1] "True data in position 11"
Code
nullabor::pvisual(2, 16, 20)
x simulated binom
[1,] 2 0.2 0.19
Residual plots need context
You are asked to decide IF THERE IS NO PATTERN. This is hard!
Numerical tests (e.g. Breusch-Pagan, Shapiro-Wilk) are either insensitive or overly-sensitive
Visual inspection is effective but subjective and unscalable
Different analysts reading the same plot reach different conclusions
Residual plots are better when viewed in the context of good residual plots, where we know the assumptions of the model are satisfied.
Variables can have distributions, e.g. \(X_1 \sim N(0,1), ~~X_2 \sim \text{Exp}(1) ...\)
Why is a data plot a statistic? (2/2)
A statistic is a function on the values of items in a sample, e.g. for \(n\) iid random variates \(\bar{X}_1=\sum_{i=1}^n X_{i1}\), \(s_1^2=\frac{1}{n-1}\sum_{i=1}^n(X_{i1}-\bar{X}_1)^2\)
We study the behaviour of the statistic over all possible samples of size \(n\).
The grammar of graphics is the mapping of (random) variables to graphical elements, making plots of data into statistics
🤔 Which of these plot definitions would most match to a null hypothesis stating there is no difference in the distribution between the groups?
What is the null hypothesis? (2/2)
A
\(H_o:\) no association between x1 and x2
B
\(H_o:\) no difference in association of between x1 and x2 between levels of cl
C
\(H_o:\) the distribution of x1 is XXX
D
\(H_o:\) no difference in the distribution of x1 between levels of cl
How do you generate null samples
Primary null-generating mechanisms
Null samples can be generated using two basic approaches:
Permutation: randomizing the order of one of the variables breaks association, but keeps marginal distributions the same.
Simulation: from a given distribution, or model. Assumption is that the data comes from that model.
applied to subsets, or conditioning on other variables. Simulation may require computing summary statistics from the data to use as parameter estimates.
Association: cars
Null plots generated by permutingx variable.
Time series: cross-currency rates
Nulls generated by simulating from an ARIMA model.
Beyond \(p\)-value to power
What is power?
A statistic is said to be more powerful than another statistic if it has a higher probability of correctly rejecting the null hypothesis when the alternative hypothesis is true.
The effectiveness of two plots designs for the same data can be compared by computing power from a lineup.
The power of a lineup is calculated as \(x/n\) where \(x\) is the number of people who detected the data plot out of \(n\) people.
Power of plot design
We’re going to play a game to determine, which of these plots is more effective for assessing difference between groups.
Each group will see only one of the next four plots for 3 second. Choose which of the plots, 1, …, 10, is most different, and record it until we are ready to collect the data.
Make a note whether it is plot 1,, 2,, 3,, 4,, 5,, 6,, 7,, 8,, 9,, 10, which is most different.
You can use this framework to find the optimal plot design
Which is the winner?
The process for choosing a plot design
Decide on your best plot designs
Make lineups, using the same data, same position with each
Show independent groups the lineups to evaluate
Calculate the power for each, or use speed as an alternative
Whichever design produces the higher power, is the one where the reader can best see the structure.
Main takeaways
Hypothesis tests can be built computationally, by simulating or permuting data under \(H_0\), instead of relying on textbook reference distributions.
A data plot can itself be a test statistic: the lineup protocol embeds the data plot among null plots generated under \(H_0\), and a viewer “rejects” \(H_0\) if they pick out the data plot.
Choosing how to generate null plots matters: use permutation when the null implies no association, and simulation (e.g. from a fitted model) when the null specifies a particular data-generating process.
Any plot — residual plots, sports analytics, time series, associations, spatial maps — is easier to read and trust when compared against null plots as a reference.
The effectiveness of competing plot designs can be measured empirically and quantitatively as power (\(x/n\), the proportion of viewers who spot the real data), giving an evidence-based way to choose between visualization designs.
Wickham, Hadley, Dianne Cook, Heike Hofmann, and Andreas Buja. 2010. “Graphical Inference for Infovis.” IEEE Transactions on Visualization and Computer Graphics 16 (6): 973–79.