How to plot the power of a test using R

1.3k Views Asked by At

How do you plot the power of a test using R software for a binomial distribution? Just need help with the coding

1

There are 1 best solutions below

0
On

Power Curve in R for test of coin fair: against right-sided alternative.

Suppose you have $n = 50$ tosses of a coin of questionable fairness. Your null hypothesis is that the coin is fair, $H_0: \theta = 1/2.$ Also, suppose you are testing a right-sided alternative at level $\alpha = .05,$ (or as near to that as possible without exceeding .05).

 qbinom(.95, 50, .5)
 ## 31
 1 - pbinom(31, 50, .5)
 ## 0.03245432           # best available < .05
 1 - pbinom(30, 50, .5)
 ## 0.05946023           # too big

So we can test at level $\alpha = .032,$ if we reject for $X \ge 32.$

Now we want to consider various alternative values $\theta_a > 1/2.$ A power curve plots $P(X \ge 32|\theta_a)$ against alternatives $\theta_a.$

There are various styles of plotting, but code for one effective style is shown below:

 theta.a = seq(.5, .9, by=.01);  pwr = 1 - pbinom(31, 50, theta.a)
 plot(theta.a, pwr, type="l", lwd=2)
 abline(h=0:1, col="darkgreen");  abline(v=1/2, col="blue")