Lecture 24 Power and Size by Simulation
This is the R session for week 8. The lectures this week have been about the language of tests and about the Neyman–Pearson lemma. In this session we will see how tests perform in practice. The R technique we will use here is the indicator average from lecture 12, where the indicator now marks the samples for which the test rejects. The written reference for this is in appendix B.
24.1 The Size of a Test by Simulation
The test we consider today is the one-sided -test from example 22.6, in the numerical setting of the example: we have observations from with known and we want to test against at level , i.e. we reject if and only if . By definition 22.4, the power function is a probability, and by proposition A.14 we can approximate this probability by the proportion of samples where the test rejects. As in lecture 12, we write a function which, for one run of the test, returns TRUE if the test rejects and FALSE otherwise. The command mean can then be used to get the proportion of rejections. Before you run the code, predict the value it prints. Will it be exactly ? How far away from would it have to be before you would suspect a mistake in the code?
The rejection rate under is . This is the estimated size of the test in the sense of definition 22.5. The value is not exactly , and it should not be: as in lecture 12, the estimate has standard deviation . Thus, a value between and is what we expect for a correct test, while a value of would point to an error in the code. Calling the same function with a value of above gives the power at this point; for and the exact values are and , from example 22.6.
24.2 The Power Curve
A single number is a poor summary of a test, so we now consider the power function over a grid of alternatives. Figure 24.1 shows the rejection rates on the grid of values from to in steps of for three level tests: the one-sided -test from above, the two-sided -test from example 22.7 and the one-sided -test applied to the first observations only. The last test is wasteful, but it is a legal level test. The script R/S24-power.R can be used to reproduce the figure. Before you look at the figure, predict how the two-sided test compares with the one-sided test. Is its rejection rate above or below that of the one-sided test for ? Is it above or below for ?

The line in the figure is the exact power function (22.1) of the one-sided test, and the filled circles lie on it. All three curves pass through at , as required for level tests, and the tests differ only in power. The two-sided test has lower power than the one-sided test for and much higher power for . Thus, neither curve lies above the other everywhere, and this illustrates proposition 23.5. From example 23.3 and proposition 23.4 we know that no level test based on these observations can reject at more than of the time. The crosses show the cost of having discarded thirteen of the observations: the power at decreases from to .
24.3 Looking at the Data More Than Once
The recipe of the first section gives the size of every procedure which ends in a decision, and we now apply it to a procedure whose size is not what it seems to be. Assume that the observations arrive one at a time, up to in total, and that we perform the two-sided -test from example 22.7 after every ten observations. We stop and reject as soon as one of the -values is below : this is the case of the researcher who “collects a few more data points and looks again”. The following function simulates one run of this procedure under , using the -value from lecture 22, and returns TRUE if the procedure rejects. Before you run the code, predict the rejection rate. Each of the ten tests has level . Will the rejection rate be ? Could it be as large as ?
The procedure rejects a true null hypothesis in of all runs, four times as often as the level of the single tests suggests. By lemma 22.9, each of the ten -values is uniformly distributed under , and thus each single look rejects with probability . The procedure, however, rejects if at least one of the ten looks rejects. Since the probability of a union is at most the sum of the probabilities, the rejection rate cannot exceed , and ten independent tests would give . Here the looks share observations: a sample which is far from rejection at one look is likely to be far from rejection at the next look as well, and for this reason the rate is smaller than both values. The level is a property of the whole procedure, including the rule which determines when to stop, and for this reason the sample size must be fixed before the data are seen.
24.4 A Task to Take Home
Set the seed of the random number generator to your student ID and consider the setting from section 24.2, but with sample size instead of . Estimate the size of the one-sided -test for samples and state whether the estimated size is consistent with , within simulation error. Next, estimate the power of the test on the grid seq(9, 13, by = 0.1) and create a plot of the estimated power curve, together with the exact power function from lecture 22; the script R/S24-power.R shows how the command sapply can be used to apply the rejection-rate recipe to every point of a grid. From the plot, determine the smallest at which the power is greater than . Finally, modify the function peek from section 24.3 to test after every five observations, estimate the rejection rate under from runs and compare it with .
Before the next session, please type the commands of this session into R, and check that what you get is what the text says you should get.
-
•
The written reference for the commands used in this session is appendix B.
-
•
We can estimate the size and power of a test by simulation, by taking the average of the indicator function for the rejection. An estimated rate for samples has standard deviation of approximately near .
-
•
The power curve of every level test passes through the point at , and the tests differ in power only. The two-sided test is weaker than the one-sided test on one side and stronger on the other, and a test which discards observations is weaker for all .
-
•
A procedure which tests after every ten observations and stops at the first -value below rejects a true null hypothesis with probability . The level is a property of the whole procedure, including the stopping rule.