Hypothesis testing - explained simply
In the first two modules you learned to describe variables and judge samples. In module 3 you put a question to the data: is what I see in my sample real, or could it be chance? That is hypothesis testing. You get a fixed recipe, a handful of tests and a way to measure questionnaires (Likert scales with ). At the exam you do not write R code, but you do read R output: t.test, chisq.test and aov.
A hypothesis is a testable, provisional statement, for example “the population mean is greater than 0.3”. You always work with a pair:
Tap a part for the explanation from the summary.
H1 is true, but you do not reject H0: an effect missed. The probability is called beta.
H1 is true and you reject H0: an existing effect found. Power is 1 - beta.
H0 is true and you do not reject it. The probability is 1 - alpha.
H0 is true, but you reject it: a false alarm. The probability is alpha.
Alpha is the probability of a Type I error. Power is 1 minus the probability of a Type II error.
- H1 (the alternative hypothesis) is what you expect and want to show.
- H0 (the null hypothesis) is the opposite: nothing is going on.
You test H0, not H1, because it is easier to refute a statement than to prove it. If you manage to knock H0 down, that counts as support for H1. When writing them down, always start with H1; H0 then follows. A test can be (H1 has a direction: greater or smaller) or (H1 only says: not equal).
- Formulate H0 and H1.
- Choose a significance level alpha, usually 0.05. This is the false-alarm probability you accept.
- Choose the test and compute the test statistic from your sample.
- Compare with the (the boundary of the ) or look at the .
- Conclude: reject H0 or maintain H0.
The p-value is the probability of a result as extreme as or more extreme than yours, assuming H0 is true. If p <= alpha, you reject H0. If p > alpha, you maintain H0. Note: a large p-value does not prove that H0 is true; you just have insufficient evidence against it. And the p-value is not the probability that H0 is true.
| H0 is true | H0 is false | |
|---|---|---|
| You reject H0 | (false alarm, probability alpha) | correct (probability = ) |
| You maintain H0 | correct | (miss, probability beta) |
The power is 1 minus beta: the probability that you also find a real effect. If you lower alpha, Type I becomes smaller but Type II becomes larger (a trade-off). Only a larger sample reduces both probabilities. A handy picture: a trial. H0 is “innocent”; convicting an innocent person is Type I, acquitting a guilty person is Type II.
- t-test: compares means when the population standard deviation is unknown (usually the case). Three variants: one-sample (mean against a fixed value), two-sample (two independent groups) and paired (before and after measurements on the same people). In R:
t.test(). The argumentvar.equaldecides whether you assume equal variances. - : means of three or more groups. You use it instead of several t-tests because more tests make the probability of a grow. A significant ANOVA only says that at least one group differs. In R:
aov()andsummary(). - : with a known population standard deviation, or with a hypothesis about a proportion.
- test: for categorical variables. It tests whether two variables are related (independence) or whether observed counts fit an expected distribution. In R:
chisq.test().
A quick rule of thumb: first look at whether you compare means or counts, then at how many groups you have.
With t.test() you look for four things: the t value, the df (degrees of freedom), the and the 95 percent . For a test: if the value under H0 lies outside the interval, you reject H0 at alpha = 0.05. chisq.test() shows X-squared, df and the p-value. In summary() of aov() you find the F value and the p-value (Pr(>F)); the stars show the significance level.
You do not measure abstract concepts such as risk attitude with one question but with several items. Such a has three elements: the items, the rating scale and the instruction. Some items are phrased negatively (reverse coded); you flip them (on a scale from 1 to 5 the score becomes 6 minus the score) before you take an average scale score. Keep an eye on answer quality: attention checks and flatliners (people who always tick the same box).
measures internal consistency: do the items point in the same direction? Above 0.7 is acceptable. If alpha is negative, there is usually a coding error: a reverse coded item was not flipped. The output “alpha if item dropped” shows whether an item weakens the scale.
- “p = 0.03 means H0 has a 3% chance”: wrong, it is about the data given H0.
- “p large, so H0 is proven”: wrong, you maintain H0.
- Significant is not the same as important: also look at the size of the effect.
- A on before and after measurements: use the .
There are about 6 multiple-choice questions on this material. The formula sheet contains the formulas for the t and z statistics, the , and , but not the F formula and not the calculation of a . So practise reading output: which test, which hypothesis, what is the conclusion and which error is possible.
Hypothesis testing - exam material
A hypothesis is a testable statement about a population. H1 (alternative hypothesis) is the researcher’s expectation; H0 (null hypothesis) is the opposite and is taken as true as the starting point. You reject H0 only given strong evidence; that protects against confirmation bias. Always formulate H1 first and derive H0 from it. Hypotheses are about population parameters (mu, p), never about sample means.
Tap a part for the explanation from the summary.
H1 is true, but you do not reject H0: an effect missed. The probability is called beta.
H1 is true and you reject H0: an existing effect found. Power is 1 - beta.
H0 is true and you do not reject it. The probability is 1 - alpha.
H0 is true, but you reject it: a false alarm. The probability is alpha.
Alpha is the probability of a Type I error. Power is 1 minus the probability of a Type II error.
The direction of H1 determines the test: right-tail (H1: greater than), left-tail (H1: smaller than) or (H1: not equal, in both tails). In a two-sided test you split alpha over two tails (alpha/2 per tail), so the lies further from the centre than in a test.
The seven-step plan (Neyman-Pearson): (1) state H0 and H1, (2) choose the test statistic, (3) derive the distribution under H0, (4) set the intuitive rejection region, (5) choose alpha, (6) find the critical value(s), (7) perform the test and conclude in the language of the problem (see slides 11 and 31). The idea: compare the sample outcome with the value under H0, divided by the standard error.
- : reject H0 if the test statistic falls in the (: |t| larger than the critical value).
- : the probability of an outcome as extreme as or more extreme than the observed one, assuming H0 is true. Reject H0 if p <= alpha. The lower p, the stronger the evidence against H0.
- Confidence interval: in a two-sided test with alpha = 0.05 you reject H0 if the value under H0 lies outside the 95% interval.
Conclusion sentences: “reject H0” or “maintain H0” (never “H0 is proven” or “H1 is proven”). A p-value is not the probability that H0 is true, and not the probability that you make an error.
| H0 true | H0 false | |
|---|---|---|
| Reject H0 | Type I error (probability alpha) | correct (power = 1 - beta) |
| Maintain H0 | correct | Type II error (probability beta) |
A Type I error is a false positive (seeing an effect that is not there), a Type II error a false negative (missing an existing effect). The trade-off: a lower alpha reduces Type I but increases Type II. Both probabilities fall only with a larger sample. A common requirement for power is 80%. Running several tests in a row makes the total probability of at least one Type I error grow; that is a reason to use instead of several t-tests.
t-test: tests means with unknown population standard deviation; the test statistic is (mean minus value under H0) divided by the standard error, with a t distribution and df = n - 1 (one-sample). Variants:
- one-sample: mean against a fixed value;
- two-sample: two independent groups;
var.equal = TRUEassumes equal variances, otherwise R uses the Welch test (fractional df, for example 628.47); - paired: linked observations (before/after, same day, same person); you test the mean of the differences.
: H0 is that all group means are equal; H1 that at least one group differs. The F statistic compares variation between the groups with variation within the groups. A significant F does not say which groups differ; that needs post-hoc tests (such as Tukey, which gives an adjusted per pair). In the output of summary(aov(...)) you read Df, Sum Sq, Mean Sq, F value and Pr(>F).
: known population standard deviation, or a hypothesis about a proportion. The test statistic is z = (p-hat - p0) / SE, with SE = sqrt(p0 (1 - p0) / n) under H0.
test: for two categorical variables. H0: the variables are independent. The expected frequency per cell is (row total x column total) / total; the statistic is the sum of (observed - expected)^2 / expected, with df = (rows - 1) x (columns - 1). Also usable as a goodness-of-fit test against an expected distribution. A low X-squared (and high p) means: no evidence of association.
| Situation | Test | R |
|---|---|---|
| one mean against a value | one-sample t | t.test(x, mu = ...) |
| two groups | two-sample t | t.test(y ~ g) |
| linked pairs | paired t | t.test(a, b, paired = TRUE) |
| three or more groups | ANOVA | aov(y ~ g) |
| two categorical variables | chi-square | chisq.test(table) |
Look in the output for: the test statistic (t, F, X-squared), the df, the , the interval and the estimate (sample estimates). Note the alternative hypothesis that R prints (“true mean is not equal to …”): that is H1. First check whether the test fits the question, then whether p <= alpha, then the direction of the effect in the means. For the test: check whether the association is practically relevant, not only whether it is significant.
A measures an abstract construct with several items, a rating scale (for example 1 to 5) and an instruction. A reverse coded item is phrased negatively; you flip it (new score = max + min - score, so 6 - score for 1 to 5) before you compute the scale score. Such items keep respondents alert. Other quality tools: attention checks and removing flatliners and respondents who are too fast.
Cronbach’s alpha measures internal consistency, usually between 0 and 1; 0.7 is the usual lower bound. Alpha rises with more items and with higher inter-item correlation. A negative alpha points to a forgotten reversal. Example from the course data: alpha was negative until the three reversed items were flipped, afterwards about 0.80 for eight items. The column “alpha if item dropped” shows which item weakens the scale.
- Reading the as the probability that H0 is true.
- Reading “not significant” as “no effect”; it may be a lack of .
- Using a on linked data, or several t-tests for three groups.
- Forgetting that in a test the p-value is half of the two-sided one (if the effect lies in the right direction).
- Confusing significance with practical importance in large samples.
About 6 multiple-choice questions on module 3 (the questions come from the practice exams). The formula sheet gives the formulas for the , the t and z statistics, and ; the F formula and the calculation are not on it. You must be able to draw a conclusion from R output of t.test, chisq.test or aov, but you do not write code.
Module 3 as Harwin says it
Introduction and exam tips
Module 3 as Harwin says it
Harwin de Vries covers module 3 in four videos: 3.1 on hypothesis testing, p-values and errors, 3.2 on the four tests, 3.3 on Likert scales, and an R tutorial. The quotes below are verbatim from the lecture (Whisper transcript, in Dutch with English technical terms) and are therefore kept in Dutch. After each quote there is an explanation in my own words. The common thread: you test a null hypothesis, you weigh evidence, and you know which errors you can make.
Gezegd door Harwin de Vries in Module 3.1 - High Mastery (video), minuut 03:08:
Wat veel gemakkelijker eigenlijk is, is om het omgekeerde van onze alternatieve hypothese, dat noemen wij de nulhypothese, om die soort van te verwerpen, om die onderuit te schoppen.
Roughly: it is much easier to knock down the opposite of our alternative hypothesis, the null hypothesis. You can rarely prove your own hypothesis because you do not see the whole population. You can refute the opposite (H0); that counts as support for H1. At the exam this means the conclusion is always “reject H0” or “maintain H0”.
Gezegd door Harwin de Vries in Module 3.1 - High Mastery (video), minuut 04:04:
De default position, de basispositie waaruit wij verder gaan.
H0 holds until the opposite is convincingly shown. Harwin compares this with a trial: the suspect is innocent until the evidence is strong enough. It also prevents confirmation bias, falling in love too quickly with your own hypothesis.
Gezegd door Harwin de Vries in Module 3.1 - High Mastery (video), minuut 26:09:
is eigenlijk de kans op het verkrijgen van onze testresultaat of nog meer extreem onder de aanname dat de nulhypothese correct is.
Roughly: the probability of obtaining our test result or something more extreme, assuming that the null hypothesis is correct. Note the words “assuming H0 is correct”: the p-value is a probability of data given H0, not a probability of H0 given the data. The lower p, the stronger the evidence against H0. The example in the video is t = 3.77 for the leverage data.
Gezegd door Harwin de Vries in Module 3.1 - High Mastery (video), minuut 32:22:
Tap a part for the explanation from the summary.
H1 is true, but you do not reject H0: an effect missed. The probability is called beta.
H1 is true and you reject H0: an existing effect found. Power is 1 - beta.
H0 is true and you do not reject it. The probability is 1 - alpha.
H0 is true, but you reject it: a false alarm. The probability is alpha.
Alpha is the probability of a Type I error. Power is 1 minus the probability of a Type II error.
We missen een effect dat er wel echt bestaat, maar we concluderen simpelweg dat onze nulhypothese, dat we die nog niet kunnen verwerpen
Roughly: we miss an effect that does exist, because we conclude that we cannot reject our null hypothesis yet. This is the false negative. Its probability is beta; power is 1 minus beta, in the video 80%. Compare with Type I (rejecting while H0 is true, probability alpha).
Gezegd door Harwin de Vries in Module 3.1 - High Mastery (video), minuut 33:23:
Als wij een lagere alpha kiezen, dan reduceert dat de kans op een type 1 fout, maar dan zorgt dat ervoor dat de kans op een type 2 fout gaat stijgen.
Roughly: a lower alpha reduces the probability of a but increases the probability of a . A stricter alpha looks safer, but you then miss real effects more often. The only way to reduce both errors is to collect more data. This is a favourite exam point.
Gezegd door Harwin de Vries in Module 3.2 - High Mastery (video), minuut 08:59:
die t-test die pas je toe als jij maximaal twee groepen hebt.
Roughly: you apply the t-test when you have at most two groups. There are three variants: one-sample (against a value such as 0.3), two-sample (two independent groups, with the argument var.equal) and paired (before and after measurements, pairs). In the lecture the examples are test scores of two student groups and the leverage data.
Gezegd door Harwin de Vries in Module 3.2 - High Mastery (video), minuut 09:39:
Dus waar hij echt het verschil in maakt is de toepassing in drie of meerdere groepen.
Roughly: where it really makes the difference is the application to three or more groups. H0 is that all group means are equal, H1 that this is not so. ANOVA compares variation between the groups with variation within the groups. The other two of the “four most used tests” are the and the test; the latter is for categorical variables.
Gezegd door Harwin de Vries in Module 3.3 - Likert Scales (video), minuut 08:20:
Sommige items zijn negatief gecodeerd en die moeten we dan eventjes gaan omklappen.
Roughly: some items are coded negatively and we then have to flip them. Before you compute a scale score or you flip reverse coded items (on a scale of 1 to 5: 6 minus the score). Harwin names a second function: such items force respondents to really read the statement.
Gezegd door Harwin de Vries in Module 3.3 - Likert Scales (video), minuut 14:50:
Als die Cronbach’s alpha meer is dan 0,7, dan concludeer je dat de maat van interne consistentie acceptabel is.
Roughly: if Cronbach’s alpha is above 0.7 you conclude that the measure of internal consistency is acceptable. Alpha usually lies between 0 and 1. According to Harwin you need not know the formula by heart (it is on the formula sheet); you must be able to read the result. In R you use the function alpha from the psych package.
Gezegd door Harwin de Vries in Module 3.3 - Likert Scales (video), minuut 15:25:
Het is soms mogelijk dat je alpha negatief is, maar dan is er echt iets geks aan de hand.
Roughly: it is sometimes possible that alpha is negative, but then something really odd is going on. Usually a reverse coded item was not flipped. After flipping, alpha becomes positive. Use “alpha if item dropped” to find weak items.
Harwin’s emphases return in the multiple-choice questions: the direction of H0 and H1, the correct reading of the , the trade-off between Type I and II, the choice between t-test (at most two groups) and (three or more), flipping reverse coded items and the 0.7 threshold for . You write no code, but you do read R output of t.test, chisq.test and aov.
A researcher compares the rating of the train (rateTrain) between men and women in FlightTaxation.csv. Real output from m3-ttest-twee-groepen.
- Means: 26.20 for men, 29.63 for women.
- Welch : t = -1.7013, df = 628.47, p = 0.08939. The 95% interval for the difference runs from -7.38 to 0.53 and contains 0.
- Conclusion: at alpha = 0.05 H0 (no difference in means) is not rejected. You have two groups, so a t-test; with three or more groups you use .
- A p of 0.089 is not proof that the groups are equal, only too little evidence for a difference.
Case: the train rating above, based on assignment 1 of module 3.
- Hypotheses (2 p): H0: the mean rating is the same for men and women; H1: the means differ.
- Decision (3 p): p = 0.08939 is larger than 0.05, so H0 is not rejected. The interval -7.38 to 0.53 contains 0, which fits.
- Meaning (2 p): there is no evidence of a difference between men and women. That does not prove they are equal; the risk of a (missing an existing difference) remains.