\documentclass[12pt]{article}

\usepackage{Math203}

\usepackage{amsmath,amssymb}
\usepackage{hyperref}
\usepackage{color}
\usepackage{xcolor}
\usepackage{enumitem}
\usepackage{xcolor}
\usepackage{longtable}
\usepackage{booktabs}


\pagestyle{plain}

\renewcommand{\labelenumi}{\arabic{enumi}.}
\renewcommand{\labelenumii}{\arabic{enumi}.\arabic{enumii}}
\renewcommand{\labelenumiii}{\arabic{enumi}.\arabic{enumii}.\arabic{enumiii}}

\setlength{\topmargin}{-0.75 in} \setlength{\textheight}{9.5 in}

\parindent0in

\newenvironment{packed_enum}{
\begin{enumerate}
  \setlength{\itemsep}{12pt}
  \setlength{\parskip}{0pt}
  \setlength{\parsep}{0pt}
  \setlength{\topsep}{0pt}
}{\end{enumerate}}

\renewcommand{\labelitemii}{$\blacktriangleright$}

\def\ctitle{Math 203: Principles of Statistics 1}
\def\ttitle{Fall 2024}
\def\stitle{Two sample problems: Examples}

\makeatletter
\def\@maketitle{%
  \newpage
  \null
  \begin{center}%
    {\textsclarge{\ctitle} \par}%
    \medskip
    {\textsclarge{\ttitle} \par}%
    \medskip
    {\textsc{\stitle} \par}
  \end{center}%
  \par
  \vskip 1.5em}
\makeatother

\begin{document}

<<funcs,echo=FALSE>>=
myrnd <- function(x,d) {formatC(x, format="f", digits=d)}
library(shape)
library(diagram)
@

\maketitle



\begin{enumerate}[label=\textbf{Example \arabic*},itemsep=0.2in,leftmargin=1in]

\item Two sections of a class in statistics were taught by two different methods. Scores on a standardized test are shown below. 

\begin{table}[ht]
\centering
\begin{tabular}{rr|rr}
\multicolumn{2}{c|}{Class A} & \multicolumn{2}{c}{Class B}\\
\hline
74&76& 78&79\\
97&75& 92&76\\
79&82& 94&93\\
88&86& 78&82\\
78&100& 71&69\\
93&94& 85&84\\
70 & & &
\end{tabular}
\end{table}
Do the results present evidence of a difference in the effectiveness of the two methods? Test using $\alpha=0.01$.

<<Q1>>=
x<-c(74,76,97,75,79,82,88,86,78,100,93,94,70)
m<-length(x);xbar<-mean(x); s1<-sd(x)
y<-c(78,79,92,76,94,93,78,82,71,69,85,84)
n<-length(y); ybar<-mean(y); s2<-sd(y)
sp<-sqrt(((m-1)*s1^2+(n-1)*s2^2)/(m+n-2))
tobs<-(xbar-ybar)/(sp*sqrt(1/m+1/n))
alpha<-0.01
t.al<-qt(1-alpha/2,m+n-2)
@

\medskip
\textbf{Solution:}  Here we have $m=\Sexpr{m}$, $n=\Sexpr{n}$ and so we must assume that the samples are Normally distributed, and that the population variances are equal.  We have
\[
\xbar = \Sexpr{xbar} \quad s_1=\Sexpr{s1} \qquad \ybar = \Sexpr{ybar} \quad s_2 = \Sexpr{s2} 
\]
so that $s_p = \Sexpr{sp}$.  We may test the hypotheses
\begin{align*}
  H_0 & : \mu_1 = \mu_2 \\
  H_a & : \mu_1 \neq \mu_2
\end{align*}
using the test statistic
\[
T = \dfrac{\overline{X} - \overline{Y}}{S_p \sqrt{\dfrac{1}{m} + \dfrac{1}{n}}} \quad \textrm{with observed value} \quad T_{obs} = \dfrac{\xbar - \ybar}{s_p \sqrt{\dfrac{1}{m} + \dfrac{1}{n}}} = \Sexpr{tobs}
\]
Here the Rejection Region for the two-sided alternative is $\{|T| \geq t_{\alpha/2,m+n-2}\}$, where
\[
t_{\alpha/2,m+n-2} = t_{\Sexpr{alpha/2},\Sexpr{m+n-2}} = \Sexpr{myrnd(t.al,4)}
\]
so we do \textbf{not} reject $H_0$ in favour of $H_a$ for $\alpha = \Sexpr{alpha}$.

\pagebreak

\item The table below shows the observed pollution indexes of air samples in two areas of a city. \label{Ex2}
\begin{table}[ht]
\centering
\begin{tabular}{rr|rr}
\multicolumn{2}{c|}{Area A} & \multicolumn{2}{c}{Area B}\\
\hline
2.92&4.69&1.84&3.44\\
1.88&4.86&0.95&3.69\\
5.35&5.81&4.26&4.95\\
3.81&5.55&3.18&4.47
\end{tabular}
\end{table}
Test the hypothesis that the mean pollution indexes are the same for the two areas. Test using $\alpha=0.05$.

<<Q2>>=
x<-c(2.92,4.69,1.88,4.86,5.35,5.81,3.81,5.55)
m<-length(x);xbar<-mean(x); s1<-sd(x)
y<-c(1.84,3.44,0.95,3.69,4.26,4.95,3.18,4.47)
n<-length(y); ybar<-mean(y); s2<-sd(y)
sp<-sqrt(((m-1)*s1^2+(n-1)*s2^2)/(m+n-2))
tobs<-(xbar-ybar)/(sp*sqrt(1/m+1/n))
alpha<-0.05
t.al<-qt(1-alpha/2,m+n-2)
ci<-(xbar-ybar)+c(-1,1)*t.al*sp*sqrt(1/m+1/n)
@

\medskip
\textbf{Solution:}  Here we have $m=\Sexpr{m}$, $n=\Sexpr{n}$ and so we must assume that the samples are Normally distributed, and that the population variances are equal.  We have
\[
\xbar = \Sexpr{xbar} \quad s_1=\Sexpr{s1} \qquad \ybar = \Sexpr{ybar} \quad s_2 = \Sexpr{s2} 
\]
so that $s_p = \Sexpr{sp}$.  We may test the hypotheses
\begin{align*}
  H_0 & : \mu_1 = \mu_2 \\
  H_a & : \mu_1 \neq \mu_2
\end{align*}
using the test statistic
\[
T = \dfrac{\overline{X} - \overline{Y}}{S_p \sqrt{\dfrac{1}{m} + \dfrac{1}{n}}} \quad \textrm{with observed value} \quad T_{obs} = \dfrac{\xbar - \ybar}{s_p \sqrt{\dfrac{1}{m} + \dfrac{1}{n}}} = \Sexpr{tobs}
\]
Here the Rejection Region for the two-sided alternative is $\{|T| \geq t_{\alpha/2,m+n-2}\}$, where
\[
t_{\alpha/2,m+n-2} = t_{\Sexpr{alpha/2},\Sexpr{m+n-2}} = \Sexpr{myrnd(t.al,4)}
\]
so we do \textbf{not} reject $H_0$ in favour of $H_a$ for $\alpha = \Sexpr{alpha}$.  

\medskip 
We may construct a 95\% Confidence Interval for $\mu_1 - \mu_2$ as
\[
(\xbar - \ybar) \pm t_{\Sexpr{alpha/2},\Sexpr{m+n-2}} s_p \sqrt{\dfrac{1}{m} + \dfrac{1}{n}} = (\Sexpr{myrnd(ci,4)}).
\]

\pagebreak

\item A closer examination of the records of the air samples in \ref{Ex2} reveals that each line of the data actually represents readings on the same day: 2.92 and 1.84 are from day 1, and so forth. 
\begin{table}[ht]
\centering
\begin{tabular}{c|c|c}
Area A&Area B& Difference\\
\hline
2.92&1.84&1.08\\
1.88&0.95&0.93\\
5.35&4.26&1.09\\
3.81&3.18&0.63\\
4.69&3.44&1.25\\
4.86&3.69&1.17\\
5.81&4.95&0.86\\
5.55&4.47&1.08\\
\end{tabular}
\end{table}

With this new information, test the hypothesis that the mean pollution indexes are the same for the two areas. Test using $\alpha=0.05$.

<<Q3>>=
d<-x-y
dbar<-mean(d)
sigd<-sd(d)
tobs<-dbar/(sigd*sqrt(1/n))
alpha<-0.05
t.al<-qt(1-alpha/2,n-1)
ci<-dbar+c(-1,1)*t.al*sigd/sqrt(n)
@

\textbf{Solution:}  Here we have a paired sample case, with $n=\Sexpr{n}$ and so we must assume that the differences are Normally distributed.  We have
\[
\overline{d} = \Sexpr{dbar} \quad s_d=\Sexpr{sigd}.
\]
We may test the hypotheses concerning $\mu_d = \mu_1 - \mu_2$
\begin{align*}
  H_0 & : \mu_d = 0 \\
  H_a & : \mu_d \neq 0
\end{align*}
using the test statistic
\[
T=\frac{\overline{D}}{S_{d} / \sqrt{n}} \quad \textrm{with observed value} \quad T_{obs} = \frac{\overline{d}}{s_{d} / \sqrt{n}} = \Sexpr{tobs}.
\]
Here the Rejection Region for the two-sided alternative is $\{|T| \geq t_{\alpha/2,n-1}\}$, where
\[
t_{\alpha/2,n-1} = t_{\Sexpr{alpha/2},\Sexpr{n-1}} = \Sexpr{myrnd(t.al,4)}
\]
so we \textbf{do} reject $H_0$ in favour of $H_a$ for $\alpha = \Sexpr{alpha}$.

\medskip 
We may construct a 95\% Confidence Interval for $\mu_d$ as
\[
\overline{d} \pm t_{\Sexpr{alpha/2},\Sexpr{n-1}}  \dfrac{s_d}{\sqrt{n}} = (\Sexpr{myrnd(ci,4)}).
\]

\pagebreak

\item A random sample of 170 recently sold properties in a city was collected to examine the additional cost of waterfront properties. The summary of the data (in thousands of dollars) is reported in the following table.
    
\begin{table}[ht]
  \centering
  \begin{tabular}{|l|ccc|}
    \hline
     & Sample size & Sample mean & Sample sd. \\
Non-Waterfront & 100 & 219.8966 & 94.62715 \\
Waterfront & 70 & 319.9064 & 153.3038 \\
    \hline
  \end{tabular}
\end{table}
Is there any evidence that waterfront properties do cost more than non-waterfront properties ?  Test at the $\alpha=0.05$ level.


<<Q4>>=
m<-100;xbar<-219.8966; s1<-94.62715
n<-70; ybar<-319.9064; s2<-153.3038
se.val<-sqrt(s1^2/m+s2^2/n)
tobs<-(xbar-ybar)/se.val
alpha<-0.05
z.al<-qnorm(1-alpha)
ci<-(xbar-ybar)+c(-1,1)*qnorm(1-alpha/2)*se.val
@

\medskip

\textbf{Solution:} Here we have $m=\Sexpr{m}$, $n=\Sexpr{n}$, so we are in the large sample case. We may test the hypotheses
\begin{align*}
  H_0 & : \mu_1 = \mu_2 \\
  H_a & : \mu_1 < \mu_2
\end{align*}
using the test statistic
\[
T = \dfrac{\overline{X} - \overline{Y}}{\sqrt{\dfrac{S_1^2}{m} + \dfrac{S_2^2}{n}}} \quad \textrm{with observed value} \quad T_{obs} = \dfrac{\xbar - \ybar}{\sqrt{\dfrac{s_1^2}{m} + \dfrac{s_2^2}{n}}} = \Sexpr{tobs}
\]
Here the Rejection Region for the one-sided alternative is $\{ T \leq -z_{\alpha}\}$, where
\[
z_{\alpha} = z_{\Sexpr{alpha}} = \Sexpr{myrnd(z.al,4)}
\]
so we \textbf{do} reject $H_0$ in favour of $H_a$ for $\alpha = \Sexpr{alpha}$.  

\medskip 
We may construct a 95\% Confidence Interval for $\mu_1 - \mu_2$ as
\[
(\xbar - \ybar) \pm z_{0.025} \sqrt{\dfrac{s_1^2}{m} + \dfrac{s_2^2}{n}} = (\Sexpr{myrnd(ci,4)})
\]
where $z_{0.025} = \Sexpr{myrnd(qnorm(0.975),4)}$.

\pagebreak

\item Eight samples of effluent from a pulp mill were each divided into ten batches. From each quantity, five randomly selected batches were subjected to a treatment process intended to remove toxic substances. Five fish of the same species were placed in each batch, and the mean number surviving in the five treated and untreated portions of each effluent quantity after five days were recorded and are given below. \begin{table}[ht]
  \centering
  \begin{tabular}{|l|r|r|r|r|r|r|r|r|}
     \hline
Sample          & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \\
          \hline
Untreated & 5.0 & 1.0 & 1.8 & 1.0 & 3.6 & 5.0 & 2.6 & 1.0 \\
Treated   & 5.0 & 5.0 & 1.2 & 4.8 & 5.0 & 5.0 & 4.4 & 2.0 \\
\hline
Difference & 0.0 & -4.0 & 0.6 & -3.8 &-1.4  & 0.0 & -1.8 & -1.0 \\
     \hline
   \end{tabular}
\end{table}

Test to see if the treatment increased the mean number of surviving fish. Test using $\alpha=0.01$.

<<Q5>>=
x<-c(5.0,1.0,1.8,1.0,3.6,5.0,2.6,1.0)
y<-c(5.0,5.0,1.2,4.8,5.0,5.0,4.4,2.0)
d<-x-y
n<-length(d)
dbar<-mean(d)
sigd<-sd(d)
tobs<-dbar/(sigd*sqrt(1/n))
alpha<-0.01
t.al<-qt(1-alpha,n-1)
ci<-dbar+c(-1,1)*qt(1-alpha/2,n-1)*sigd/sqrt(n)
@

\textbf{Solution:}  Here we have a paired sample case, with $n=\Sexpr{n}$ and so we must assume that the differences are Normally distributed.  We have
\[
\overline{d} = \Sexpr{dbar} \quad s_d=\Sexpr{sigd}.
\]
We may test the hypotheses concerning $\mu_d = \mu_1 - \mu_2$
\begin{align*}
  H_0 & : \mu_d = 0 \\
  H_a & : \mu_d < 0
\end{align*}
using the test statistic
\[
T=\frac{\overline{D}}{S_{d} / \sqrt{n}} \quad \textrm{with observed value} \quad T_{obs} = \frac{\overline{d}}{s_{d} / \sqrt{n}} = \Sexpr{tobs}.
\]
Here the Rejection Region for the two-sided alternative is $\{T \leq -t_{\alpha,n-1}\}$, where
\[
t_{\alpha,n-1} = t_{\Sexpr{alpha},\Sexpr{n-1}} = \Sexpr{myrnd(t.al,4)}
\]
so we do \textbf{not} reject $H_0$ in favour of $H_a$ for $\alpha = \Sexpr{alpha}$.

\medskip 
We may construct a 99\% Confidence Interval for $\mu_d$ as
\[
\overline{d} \pm t_{\Sexpr{alpha/2},\Sexpr{n-1}}  \dfrac{s_d}{\sqrt{n}} = (\Sexpr{myrnd(ci,4)})
\]
where $t_{\Sexpr{alpha/2},\Sexpr{n-1}} = \Sexpr{myrnd(qt(1-alpha/2,n-1),4)}$.

\pagebreak

\item Researchers wish to compare the average weights of blue crabs in two river basins.  Based on the health of the two rivers, it is believed that the crabs in the Basin 2 will be larger, on average, than those in Basin 1. 100 blue crabs are sampled in each basin.  The mean weight (in grams) of the Basin 1 crabs is 703.2g with a standard deviation of 301.5g.  The mean weight of the Basin 2 crabs is 810.2g with a standard deviation of 397.3g.
    
    \medskip
    Is there any evidence to support the research hypothesis ?  Test with $\alpha=0.05$.

<<Q6>>=
m<-100;xbar<-703.2; s1<-301.5
n<-100; ybar<-810.2; s2<-397.3
se.val<-sqrt(s1^2/m+s2^2/n)
tobs<-(xbar-ybar)/se.val
alpha<-0.05
z.al<-qnorm(1-alpha)
ci<-(xbar-ybar)+c(-1,1)*qnorm(1-alpha/2)*se.val
@

\medskip

\textbf{Solution:} Here we have $m=\Sexpr{m}$, $n=\Sexpr{n}$, so we are in the large sample case. We may test the hypotheses
\begin{align*}
  H_0 & : \mu_1 = \mu_2 \\
  H_a & : \mu_1 < \mu_2
\end{align*}
using the test statistic
\[
T = \dfrac{\overline{X} - \overline{Y}}{\sqrt{\dfrac{S_1^2}{m} + \dfrac{S_2^2}{n}}} \quad \textrm{with observed value} \quad T_{obs} = \dfrac{\xbar - \ybar}{\sqrt{\dfrac{s_1^2}{m} + \dfrac{s_2^2}{n}}} = \Sexpr{tobs}
\]
Here the Rejection Region for the one-sided alternative is $\{ T \leq -z_{\alpha}\}$, where
\[
z_{\alpha} = z_{\Sexpr{alpha}} = \Sexpr{myrnd(z.al,4)}
\]
so we \textbf{do} reject $H_0$ in favour of $H_a$ for $\alpha = \Sexpr{alpha}$.  

\medskip 
We may construct a 95\% Confidence Interval for $\mu_1 - \mu_2$ as
\[
(\xbar - \ybar) \pm z_{0.025} \sqrt{\dfrac{s_1^2}{m} + \dfrac{s_2^2}{n}} = (\Sexpr{myrnd(ci,4)})
\]
where $z_{0.025} = \Sexpr{myrnd(qnorm(0.975),4)}$.


\pagebreak

\item  In a test of the effectiveness of a device that is supposed to increase gasoline mileage in a particular make of US automobiles, 12 cars were run, in random order, over a prescribed course both with and without the device in random order. The mileages per gallon are given below.  Test using $\alpha=0.01$.
    
\begin{table}[ht]
  \centering
  \begin{tabular}{|c|c|c|r|}
  \hline
Car&Without Device&With Device& \multicolumn{1}{c|}{Difference}\\
\hline
1&21.0&20.6&–0.4\\
2&30.0&29.9&–0.1\\
3&29.8&30.7&0.9\\
4&27.3&26.5&–0.8\\
5&27.7&26.7&–1.0\\
6&33.1&32.8&–0.3\\
7&18.8&21.7&2.9\\
8&26.2&28.2&2.0\\
9&28.0&28.9&0.9\\
10&18.9&19.9&1.0\\
11&29.3&32.4&3.1\\
12&21.0&22.0&1.0\\
     \hline
   \end{tabular}
\end{table}

Is there evidence that the device is effective? Test using $\alpha=0.01$.

<<Q7>>=
d<-c(-0.4,-0.1,0.9,-0.8,-1.0,-0.3,2.9,2.0,0.9,1.0,3.1,1.0)
n<-length(d)
dbar<-mean(d)
sigd<-sd(d)
tobs<-dbar/(sigd*sqrt(1/n))
alpha<-0.01
t.al<-qt(1-alpha,n-1)
ci<-dbar+c(-1,1)*qt(1-alpha/2,n-1)*sigd/sqrt(n)
@

\textbf{Solution:}  Here we have a paired sample case, with $n=\Sexpr{n}$ and so we must assume that the differences are Normally distributed.  We have
\[
\overline{d} = \Sexpr{dbar} \quad s_d=\Sexpr{sigd}.
\]
We may test the hypotheses concerning $\mu_d = \mu_1 - \mu_2$
\begin{align*}
  H_0 & : \mu_d = 0 \\
  H_a & : \mu_d > 0
\end{align*}
using the test statistic
\[
T=\frac{\overline{D}}{S_{d} / \sqrt{n}} \quad \textrm{with observed value} \quad T_{obs} = \frac{\overline{d}}{s_{d} / \sqrt{n}} = \Sexpr{tobs}.
\]
Here the Rejection Region for the one-sided alternative is $\{T \geq t_{\alpha,n-1}\}$, where
\[
t_{\alpha,n-1} = t_{\Sexpr{alpha},\Sexpr{n-1}} = \Sexpr{myrnd(t.al,4)}
\]
so we do \textbf{not} reject $H_0$ in favour of $H_a$ for $\alpha = \Sexpr{alpha}$.

\medskip 
We may construct a 99\% Confidence Interval for $\mu_d$ as
\[
\overline{d} \pm t_{\Sexpr{alpha/2},\Sexpr{n-1}}  \dfrac{s_d}{\sqrt{n}} = (\Sexpr{myrnd(ci,4)})
\]
where $t_{\Sexpr{alpha/2},\Sexpr{n-1}} = \Sexpr{myrnd(qt(1-alpha/2,n-1),4)}$.

\pagebreak

\item A feeding test is conducted on a herd of 25 dairy cows to compare two diets, A and B. A sample of 13 cows randomly selected from the herd are fed diet A and the remaining cows are fed with diet B. From observations made over a three-week period, the average daily milk production (in litres) is recorded for each cow:

\begin{table}[ht]
  \centering
  \begin{tabular}{ccccccccccccccc}
Diet A: &44&44&56&46&47&38&58&53&49&35&46&30&41\\
Diet B: &35&47&55&29&40&39&32&41&42&57&51&39\\
   \end{tabular}
\end{table}

Is there any evidence that Diet A has a higher milk production than Diet B ? Test with $\alpha = 0.05$.

<<Q8>>=
x<-c(44,44,56,46,47,38,58,53,49,35,46,30,41)
m<-length(x);xbar<-mean(x); s1<-sd(x)
y<-c(35,47,55,29,40,39,32,41,42,57,51,39)
n<-length(y); ybar<-mean(y); s2<-sd(y)
sp<-sqrt(((m-1)*s1^2+(n-1)*s2^2)/(m+n-2))
tobs<-(xbar-ybar)/(sp*sqrt(1/m+1/n))
alpha<-0.05
t.al<-qt(1-alpha,m+n-2)
ci<-(xbar-ybar)+c(-1,1)*qt(1-alpha/2,m+n-2)*sp*sqrt(1/m+1/n)
@

\medskip
\textbf{Solution:}  Here we have $m=\Sexpr{m}$, $n=\Sexpr{n}$ and so we must assume that the samples are Normally distributed, and that the population variances are equal.  We have
\[
\xbar = \Sexpr{xbar} \quad s_1=\Sexpr{s1} \qquad \ybar = \Sexpr{ybar} \quad s_2 = \Sexpr{s2} 
\]
so that $s_p = \Sexpr{sp}$.  We may test the hypotheses
\begin{align*}
  H_0 & : \mu_1 = \mu_2 \\
  H_a & : \mu_1 > \mu_2
\end{align*}
using the test statistic
\[
T = \dfrac{\overline{X} - \overline{Y}}{S_p \sqrt{\dfrac{1}{m} + \dfrac{1}{n}}} \quad \textrm{with observed value} \quad T_{obs} = \dfrac{\xbar - \ybar}{s_p \sqrt{\dfrac{1}{m} + \dfrac{1}{n}}} = \Sexpr{tobs}
\]
Here the Rejection Region for the one-sided alternative is $\{T \geq t_{\alpha,m+n-2}\}$, where
\[
t_{\alpha,m+n-2} = t_{\Sexpr{alpha},\Sexpr{m+n-2}} = \Sexpr{myrnd(t.al,4)}
\]
so we do \textbf{not} reject $H_0$ in favour of $H_a$ for $\alpha = \Sexpr{alpha}$.  

\medskip 
We may construct a 95\% Confidence Interval for $\mu_1 - \mu_2$ as
\[
(\xbar - \ybar) \pm t_{\Sexpr{alpha/2},\Sexpr{m+n-2}} s_p \sqrt{\dfrac{1}{m} + \dfrac{1}{n}} = (\Sexpr{myrnd(ci,4)})
\]
where $t_{\Sexpr{alpha/2},\Sexpr{m+n-2}} = \Sexpr{myrnd(qt(1-alpha/2,m+n-2),4)}$.


\pagebreak

\item A university wants to determine whether a new instructor is successful in their in-class teaching. On one test, 44 out of 60 of Instructor A’s students passed on the first try. In comparison, 57 out of 72 of more experienced Instructor B’s students passed the test on the first try. Is Instructor A’s pass rate worse than Instructor B’s?  Test using $\alpha=0.01$.
    
<<Q9>>=
x<-44;m<-60
y<-57;n<-72
p1.hat<-x/m
p2.hat<-y/n
p.hat<-(x+y)/(m+n)
se.p<-sqrt(p.hat*(1-p.hat)*(1/m+1/n))
tobs<-(p1.hat-p2.hat)/se.p
alpha<-0.01
z.al<-qnorm(1-alpha)
ci<-(p1.hat-p2.hat)+c(-1,1)*
    qnorm(1-alpha/2)*sqrt(p1.hat*(1-p1.hat)/m+p2.hat*(1-p2.hat)/n)
@

\medskip
\textbf{Solution:}  Here we have Bernoulli data with $x,n-x,y,n-y \geq 15$, which is large enough to apply the Central Limit Theorem.  We have
\[
\widehat{p}_1 = \Sexpr{p1.hat} \qquad \widehat{p}_2 = \Sexpr{p2.hat}
\]
and the pooled estimate
\[
\widehat{p} = \frac{x+y}{m+n} = \frac{\Sexpr{x+y}}{\Sexpr{m+n}} = \Sexpr{p.hat}.
\]
We may test the hypotheses
\begin{align*}
  H_0 & : p_1 = p_2 \\
  H_a & : p_1 < p_2
\end{align*}
using the test statistic
\[
T = \dfrac{\widehat{p}_{1}-\widehat{p}_{2}}{\sqrt{\widehat{p}(1-\widehat{p})\left(\dfrac{1}{m}+\dfrac{1}{n}\right)}} 
\]
with observed value
\[
T_{obs} = \dfrac{\widehat{p}_{1}-\widehat{p}_{2}}{\sqrt{\widehat{p}(1-\widehat{p})\left(\dfrac{1}{m}+\dfrac{1}{n}\right)}}  = \Sexpr{tobs}
\]
Here the Rejection Region for the one-sided alternative is $\{T \leq -z_{\alpha}\}$, where
\[
z_{\alpha} = z_{\Sexpr{alpha}} = \Sexpr{myrnd(z.al,4)}
\]
so we do \textbf{not} reject $H_0$ in favour of $H_a$ for $\alpha = \Sexpr{alpha}$.  

\medskip 
We may construct a 95\% Confidence Interval for $p_1 - p_2$ as
\[
(\widehat{p}_{1} -\widehat{p}_{2}) \pm z_{0.025} \sqrt{\frac{\widehat{p}_{1}\left(1-\widehat{p}_{1}\right)}{m}+\frac{\widehat{p}_{2}\left(1-\widehat{p}_{2}\right)}{n}} = (\Sexpr{myrnd(ci,4)})
\]
where $z_{0.025} = \Sexpr{myrnd(qnorm(0.975),4)}$.

\pagebreak
    
\item In a test of the reliability of products produced by two machines, machine A produced 25 defective parts in a run of 208, while machine B produced 18 defective parts in a run of 200. 
    
    \medskip
    
    Do these results imply a difference in the reliability of these two machines?  Test using $\alpha=0.1$ 
    
<<Q10>>=
x<-25;m<-208
y<-18;n<-200
p1.hat<-x/m
p2.hat<-y/n
p.hat<-(x+y)/(m+n)
se.p<-sqrt(p.hat*(1-p.hat)*(1/m+1/n))
tobs<-(p1.hat-p2.hat)/se.p
alpha<-0.1
z.al<-qnorm(1-alpha/2)
ci<-(p1.hat-p2.hat)+c(-1,1)*
    qnorm(1-alpha/2)*sqrt(p1.hat*(1-p1.hat)/m+p2.hat*(1-p2.hat)/n)
@

\medskip
\textbf{Solution:}  Here we have Bernoulli data with $x,n-x,y,n-y \geq 15$, which is large enough to apply the Central Limit Theorem.  We have
\[
\widehat{p}_1 = \Sexpr{p1.hat} \qquad \widehat{p}_2 = \Sexpr{p2.hat}
\]
and the pooled estimate
\[
\widehat{p} = \frac{x+y}{m+n} = \frac{\Sexpr{x+y}}{\Sexpr{m+n}} = \Sexpr{p.hat}.
\]
We may test the hypotheses
\begin{align*}
  H_0 & : p_1 = p_2 \\
  H_a & : p_1 \neq p_2
\end{align*}
using the test statistic
\[
T = \dfrac{\widehat{p}_{1}-\widehat{p}_{2}}{\sqrt{\widehat{p}(1-\widehat{p})\left(\dfrac{1}{m}+\dfrac{1}{n}\right)}} 
\]
with observed value
\[
T_{obs} = \dfrac{\widehat{p}_{1}-\widehat{p}_{2}}{\sqrt{\widehat{p}(1-\widehat{p})\left(\dfrac{1}{m}+\dfrac{1}{n}\right)}}  = \Sexpr{tobs}
\]
Here the Rejection Region for the two-sided alternative is $\{|T| \geq z_{\alpha/2}\}$, where
\[
z_{\alpha} = z_{\Sexpr{alpha/2}} = \Sexpr{myrnd(z.al,4)}
\]
so we do \textbf{not} reject $H_0$ in favour of $H_a$ for $\alpha = \Sexpr{alpha}$.  

\medskip 
We may construct a 90\% Confidence Interval for $p_1 - p_2$ as
\[
(\widehat{p}_{1} -\widehat{p}_{2}) \pm z_{0.05} \sqrt{\frac{\widehat{p}_{1}\left(1-\widehat{p}_{1}\right)}{m}+\frac{\widehat{p}_{2}\left(1-\widehat{p}_{2}\right)}{n}} = (\Sexpr{myrnd(ci,4)})
\]
where $z_{0.05} = \Sexpr{myrnd(qnorm(0.95),4)}$.

\end{enumerate}

\end{document} 