More Hypothesis Testing

The purpose of this short TP is to get a little practice on some more tests using R. You will carry out testing using the Wilcoxon, chi-square and Fisher exact tests. As usual, make sure that you read the help for any new functions that you use.


Nonparametric Testing

In TP 2a, you carried out one-sample, two-sample and matched pairs t-tests. On the same data, use wilcox.test to carry out the Wilcoxon test in R. Don't forget to load the library ISwR.

Testing in Contingency Tables

Here you can practice entering table data into R and carrying out a chi-square or Fisher exact test.

Caffeine consumption data

In this example, we wish to study the association between caffeine consumption and marital status among women giving birth. In R, a two-way table needs to be a matrix object, so read the help for the matrix function. Input the table and look at it:

caff.marital <- matrix(c(652,1537,598,242,36,46,38,21,218,327,106,67),
  nrow=3, byrow=TRUE)
colnames(caff.marital) <- c("0","1-150","151-300",">300")
rownames(caff.marital) <- c("Married","Prev. Married","Single")
caff.marital

Does there appear to be any association? If so, how would you describe the association?

After you have thought about association in the data, you can get a p-value by carrying out a chi-square test:

chisq.test(caff.marital)

Does the p-value agree with your thinking?

When you find a significant result, usually you would like to have an idea of the nature of the deviations. chisq.test returns some additional components that can help you to explore this. For example, you can get the observed and expected values, and contributions to the chi-square statistic as follows:

Obs <- chisq.test(caff.marital)$observed
Ex <- chisq.test(caff.marital)$expected
((Obs - Ex)^2)/Ex

Which categories are making large contributions to the chi-square statistic? Is there a simple way to describe the association in this table?

Quality of sleep

Use a chi-square test to analyze association in the Quality of Sleep table from lecture 4b.

Is there any reason you should not use a chi-square test here? Now use fisher.test to carry out the test. Do you make the same conclusion? Which test is more appropriate here?