# R Notes for Topic 21: Comparing Two Proportions

## 21-3 e

Here are the computations

```pT = 163/9726
pR = 167/9745
pc = (163+165)/(9726+9745)
```

Think about how many decimal places are appropriate. For example, for pT, should you use 0.017, 0.0168, 0.01676, or even more decimal places? Will it make a difference?

## 21-3 f

You should try using the Test of Significance Calculator applet first. Afterwards, compute the test statistic using R. We'll explore different approximations to see if we always get the same value.

Using three places after the decimal.

```(.017-.017)/sqrt(.017*(1-.017)*(1/9726+1/9745))
```

Using four places after the decimal.

```(.0168-.0171)/sqrt(.0169*(1-.0169)*(1/9726+1/9745))
```

Using five places after the deimal.

```(.01676-.01714)/sqrt(.01695*(1-.01695)*(1/9726+1/9745))
```

Using the original calculations, unmodified.

```(pT-pR)/sqrt(pc*(1-pc)*(1/9726+1/9745))
```

What do these differences suggest to you?

## 21-4 d

Let's do the first row first. You can certainly use the applet, although, as you've just seen, it does approximate a bit too much. So, let's also explore how to do the computation in R.

```pw = .71
pm = .81
nw = 100
nm = 100
pc = (pw*nw+pm*nm)/(nw+nm)
z = (pw-pm)/sqrt(pc*(1-pc)*(1/nw+1/nm))
z
pnorm(z)
2*pnorm(z)
```

note that you'll get somewhat different answers than the answer key for two reasons: First, we are using more digits of accuracy (whether or not that's a good idea). Second, we are using Pr(Z < z) while the answer key seems to be using 2*Pr(Z < |z|).

For the second line, we don't need to write quite as much. Basically, we chance `nw` and `nm` and then recalculate.

```nw = 200
nm = 200
pc = (pw*nw+pm*nm)/(nw+nm)
z = (pw-pm)/sqrt(pc*(1-pc)*(1/nw+1/nm))
z
pnorm(z)
2*pnorm(z)
```

You should be able to figure out what to change for the last line of the table.

## 21-5 a

Once again, we can use the applet or R to compute the test statistic.

```pw = 559/1835
pm = 1195/2681
nw = 1835
nm = 2681
pc = (559+1195)/(1835+2681)
z = (pw-pm)/sqrt(pc*(1-pc)*(1/nw+1/nm))
```

Given your hypothesis and that test statistic, you should be able to figure out the p-value.

## 21-6

At this point, you should be able to figure out the computations on your own. Grab one of us if you need help.

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright (c) 2007-8 Samuel A. Rebelsky.

This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. To view a copy of this license, visit `http://creativecommons.org/licenses/by-nc/2.5/` or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.