4.2 Z-test for proportions

To carry out a z test to compare two proportions, we can use the prop.test() command, equivalent to prtest in Stata. It is however, slightly more finicky to use. prop.test() requires two inputs: a vector of ‘successes’ (numerator) and a vector of ‘counts’ (denominator). A ‘vector’ in R is a sequence of objects that are of the same type (i.e. numerical or character…) - it is similar to a ‘list’, but a list may contain objects of any data type.

You will thus need to manually extract these vectors from the output of the 2x2 table. For example, the vector of ‘successes’ (hypertension = yes) for these data is c(27, 62). The c() function creates a vector, the c standing for concatenate. The test is below, invoking the correct = F option to suppress the default behaviour of R of adding a continuity correction.

Note that R does not provide the z score, but it can be derived by noting that the z score is the square root of the chi square – the cc() command is much less work than running the actual test - thus demonstrating the utility of user-built packages! In the Environment pane, we can click on the ztest object and look at the other objects contained within it - one of which is ‘statistic’. We use the $ symbol to show that we want the ‘statistic’ object from the ‘ztest’ object, called indexing. You have seen this before to extract variables from data frames. If you click on the ‘ztest’ object in the environment pane, a list of (nine) objects contained within it can be seen. Try and extract the p value from the test using this indexing strategy.

#--- Run a z test
ztest <- prop.test(c(27, 53), n = c(89, 552), correct = F)
ztest
## 
##  2-sample test for equality of proportions without continuity
##  correction
## 
## data:  c(27, 53) out of c(89, 552)
## X-squared = 30, df = 1, p-value = 0.00000004
## alternative hypothesis: two.sided
## 95 percent confidence interval:
##  0.109 0.306
## sample estimates:
## prop 1 prop 2 
##  0.303  0.096
#--- Extract the z statistic
sqrt(ztest$statistic)
## X-squared 
##      5.49

Exercise 10.4: What is your interpretation of this result?

Exercise 10.5: Check the relationship between the z and the chi-squared values obtained.