I've got thea problem with Z$z$-test and confidence intervals. ZThe $z$-test rejects the null hypothesis (proportions are equal), but the confidence intervals are intersect.
Let us consider 2 companies. The proportion of smokers in company A$A$ is p(A)=0.22$p(A)=0.22$. And the proportion of smokers in company B$B$ is p(B)=0.303$p(B)=0.303$. The amount of employees in company A$A$ is 403$403$. And the amount of employees in company B$B$ is 404$404$.
So, we have the folowingfollowing data:
p(A)=0.22
n(A)=403
p(B)=0.303
n(B)=404\begin{align}
p(A)&=0.22 &n(A)&=403 \\
p(B)&=0.303 &n(B)&=404
\end{align}
Now let's apply the Z$z$-test by the following forumulaformula for proportions (I'veI took the formula from https://onlinecourses.science.psu.edu/stat414/node/268here):
$$Z= \frac{p(B)-p(A)}{\sqrt{p(1-p)(\frac{1}{n(A)}+\frac{1}{n(b)})}}$$$$z= \frac{p(B)-p(A)}{\sqrt{p(1-p)(\frac{1}{n(A)}+\frac{1}{n(b)})}}$$
Where p$p$ is $$p=\frac{p(A)n(A)+p(B)n(B)}{n(A)+n(B)}$$
I'veI made the following computation: $$p=\frac{0.22*403+0.303*404}{403+404}=0.2614$$
$$z= \frac{0.303-0.22}{\sqrt{0.2614(1-0.2614)(\frac{1}{403}+\frac{1}{404})}}=2.6828$$
So, z$z$ more than 1.96$1.96$, it means that we reject the null hipothesishypothesis. The proportions p(A)$p(A)$ and p(B)$p(B)$ are not equal.
But then I've computed the 95% confidence intervals by the following formulas (I'veI took them formfrom https://onlinecourses.science.psu.edu/stat200/node/48here):
$$low=P-1.96*\sqrt{\frac{P*(1-P)}{N}}$$ $$high=P+1.96*\sqrt{\frac{P*(1-P)}{N}}$$\begin{align} \newcommand{\low}{{\rm low}} \newcommand{\high}{{\rm high}} \low &= P-1.96 \sqrt{\frac{P(1-P)}{N}} \\ \high &= P+1.96 \sqrt{\frac{P(1-P)}{N}} \end{align} Where P$P$ is proportion of smokers in some company and N$N$ is amount of employees in this company.
I've computed the interval for company A$A$: $$low(A)=p(A)-1.96*\sqrt{\frac{p(A)(1-p(A))}{n(A)}}=0.22-1.96*\sqrt{\frac{0.22*0.78}{403}}=0.1796$$ $$high(A)=p(A)+1.96*\sqrt{\frac{p(A)(1-p(A))}{n(A)}}=0.22+1.96*\sqrt{\frac{0.22*0.78}{403}}=0.2604$$\begin{align} \low(A) &= p(A)-1.96 \sqrt{\frac{p(A)(1-p(A))}{n(A)}} = 0.22-1.96 \sqrt{\frac{0.22*0.78}{403}} = 0.1796 \\ \high(A) &= p(A)+1.96 \sqrt{\frac{p(A)(1-p(A))}{n(A)}} = 0.22+1.96 \sqrt{\frac{0.22*0.78}{403}} = 0.2604 \end{align} So, for p(A)$p(A)$ the CI is (0.1796;0.2696)$(0.1796;0.2696)$.
And finally I've computed the interval for the company B$B$: $$low(B)=p(B)-1.96 \sqrt{{p(B)(1-p(B))}{n(B)}} =0.303-1.96 \sqrt{\frac{0.303 * 0.697}{404}}=0.2581$$ $$high(B)=p(B)+1.96*\sqrt{{p(B)(1-p(B))}{n(B)}} =0.303+1.96 \sqrt{\frac{0.303 * 0.697}{404}}=0.3477$$\begin{align} \low(B) &= p(B)-1.96 \sqrt{{p(B)(1-p(B))}{n(B)}} = 0.303-1.96 \sqrt{\frac{0.303 * 0.697}{404}} = 0.2581 \\ \high(B) &= p(B)+1.96 \sqrt{{p(B)(1-p(B))}{n(B)}} = 0.303+1.96 \sqrt{\frac{0.303 * 0.697}{404}} = 0.3477 \end{align} For p(B)$p(B)$ the confidence interval is (0.2581;0.3477)$(0.2581; 0.3477)$.
And now we see that confidence intervals are intersect, but it contradicts the computations for z$z$-test.
Finally I've used a web service for zcomputing the $z$-test and the CI computing. The service draweddrew the intersection of the CI's and rejected the null hipothesishypothesis of zthe $z$-test, too.
Please, could you explain me whatWhat is the cause of the contradiction?