The Power of the one sided z test for the mean
The one sided z-test for the mean $(\mu)$ is based on a random sample where $X_1 \ldots X_n \sim n(\mu, \sigma^2)$ are independent and $\sigma^2$ is known.\\
The power of the test for an arbitrary $\mu $ can be computed as:
$$
\beta(\mu) = 1 - \Phi \left ( \frac{\mu_0 - \mu }{\frac{\sigma} {\sqrt{n}}} + z_{1- \alpha} \right )
$$
Details
The one sided z-test for the mean $(\mu)$ is based on a random sample where $X_1 \ldots X_n \sim n(\mu, \sigma^2)$ are independent and $\sigma^2$ is known.\\
If the hypotheses are:
$ H_0 : \mu = \mu_0$ vs
$ H_a : \mu > \mu_0$\\
Then we know that, if $H_0$ is true
$$Z = \frac{\bar {X} - \mu_0}{\frac{\sigma} {\sqrt{n}}} \sim n (0,1)$$
Given data $x_1, \ldots x_n$, the z-value is
$$z = \frac{\bar {x} - \mu_0}{\frac{\sigma} {\sqrt{n}}}$$
We reject $H_0$ if $z > z_{1-\alpha}$
The level of this test is
$$
P_{\mu_0} [Reject H_0]= P_{\mu_0}[\frac{\bar {X} - \mu_0}{\frac{\sigma} {\sqrt{n}}} > z_{1- \alpha}]
$$
$$= P[z > z_{1- \alpha}] = {\alpha} $$
since $ Z \sim n (0,1)$ when $\mu_0$ is the true value.\\\\
The power of the test for an arbitrary $\mu $ can be computed as follows.
$$\beta(\mu) = P_{\mu} [reject H_0]$$
$$= P_{\mu}[\frac{\bar {X} - \mu_0}{\frac{\sigma} {\sqrt{n}}} > z_{1- \alpha}]$$
$$= P_{\mu} [\bar {X}> \mu_0 + z_{1- \alpha}{\frac{\sigma} {\sqrt{n}}}]$$
$$= P_{\mu} [\frac{\bar {X} - \mu}{\frac{\sigma} {\sqrt{n}}}> \frac{\mu_0 - \mu }{\frac{\sigma} {\sqrt{n}}}+ z_{1- \alpha}]$$
$$ = P[Z > \frac{\mu_0 - \mu}{\frac{\sigma} {\sqrt{n}}} + z_{1- \alpha}]$$
We obtain
$$
\beta(\mu) = 1 - \Phi \left ( \frac{\mu_0 - \mu }{\frac{\sigma} {\sqrt{n}}} + z_{1- \alpha} \right )
$$
Examples
\begin{xmpl}
Suppose we know $\sigma=2$ and we will take a sample
from $n\left ( \mu, \sigma^2\right)$
intending to test the hypothesis $\mu=3$ at level $\alpha=0.05$. We want to know the power
against a one-tailed alternative when the true mean is actually $\mu=4$ when the sample size is $n=25$. \\\\
We can set this up in R with:
\begin{lstlisting}
alpha<-0.05
n<-25
sigma<-2
mu0<-3
mu<-4
zcrit<-qnorm(1-alpha)
\end{lstlisting}
Sticking the formula into R gives
\begin{lstlisting}
1-pnorm((mu0-mu)/(sigma/sqrt(n))+zcrit)
[1] 0.803765
\end{lstlisting}
On the other hand, one can also use a simple simulation approach. First, decide how many samples are to be simulated (Nsim). Then, generate all of these samples, arrange them in a matrix and compute the mean of each sample. The z-value of each of these Nsim tests are then computed and a check is made whether it exceeds the critical point (1) or not (0).\\
\begin{lstlisting}
Nsim<-10000
m<-matrix(rnorm(Nsim*n,mu,sigma),ncol=n)
mn<-apply(m,1,mean)
z<-(mn-mu0)/(sigma/sqrt(n))
i<-ifelse(z>zcrit,1,0)
sum(i/Nsim)
[1] 0.8081
\end{lstlisting}
\end {xmpl}