Kickstarting R - Tests of proportion

Many simple hypotheses are concerned with whether the proportion of one group (e.g. females) with a certain characteristic (e.g. tobacco smoking) is different from that of another group (e.g. males). In the package ctest, which is now loaded automatically when R starts up, is the function prop.test(). This function tests whether two or more samples divided on a dichotomous variable have the same proportions of each value. Here's an example:

> sexsmoke<-matrix(c(70,120,65,140),ncol=2,byrow=T)
> rownames(sexsmoke)<-c("male","female")
> colnames(sexsmoke)<-c("smoke","nosmoke")
> prop.test(sexsmoke)

In this case, we passed a matrix of "successes" (i.e. smokers) and "failures" (i.e. non-smokers). prop.test() will also accept separate vectors of "successes" and "totals", like this:

> prop.test(c(70,65),c(190,205))

You can also specify the hypothetical proportions, if you want to test the samples against a particular set of values, whether your hypothesis is directional, and the confidence interval in the case of a two sample test.

> prop.test(c(70,65),c(190,205),conf.level=0.99)
> prop.test(c(70,65),c(190,205),c(0.33,0.33))

An alternative function is fisher.test(), also in the package ctest. This function performs Fisher's exact test on contingency tables. For a function that will perform multiple comparisons of proportions, see the group.prop.test function.


For more information, see the R help index - package ctest.

Back to Table of Contents