Lab 11

Published

April 7, 2026

Download R code

Objectives

  1. Practice conducting a \(\large{\chi^2}\) test of independence

    • in R
    • by hand
  2. Fisher’s Exact Test

  3. Quiz 3 review

\(\large{\chi^2}\) test

Using R

Does D-penicillamine help with liver disease?

In a study conducted at the Mayo Clinic between 1974 and 1984, patients experiencing a type of liver failure called primary biliary cirrhosis, or PBC, participated in the randomized placebo controlled trial of the drug D-penicillamine. Conduct a chi-squared test to evaluate whether D-penicillamine decreases mortality rates among patients experiencing PBC.

The data is found in the cirrhosis data set. When you load it in, name your data cirr.

TipHint

Once your data is loaded into the variable cirr, use the following code to make a slight adjustment to the category levels of the status variable. Since we are only interested in whether the patient died or not, we will simplify this variable into a dichotomous indicator. \(1\) means the patient died and \(0\) means they were alive by the end of the study.

cirr$dead <- ifelse(cirr$Status == 2, 1, 0)

Then use xtabs to create a 2 \(\times\) 2 table of the data.

Answer
cirr <- read.delim('https://raw.githubusercontent.com/IowaBiostat/data-sets/main/cirrhosis/cirrhosis.txt')

cirr$dead <- ifelse(cirr$Status == 2, 1, 0)

tbl <- with(cirr, xtabs(~ Group + dead))
chisq.test(tbl, correct = FALSE)

    Pearson's Chi-squared test

data:  tbl
X-squared = 0.15409, df = 1, p-value = 0.6947

Interpretation

This data provides no evidence that D-penicillamine decreases mortality rates among patients experiencing PBC.

You will also need to know how to construct a table in R if you are given the raw counts (i.e. no dataset provided). This is how it can be done. Simply copy this code and change the numbers when applying this method to homework or assessment questions. This is also found in the Functions document.

tbl2 <- rbind(
  c(93, 65),
  c(94, 60)
)

chisq.test(tbl2, correct = FALSE)

    Pearson's Chi-squared test

data:  tbl2
X-squared = 0.15409, df = 1, p-value = 0.6947

By Hand

Now verify our work in R by practicing by hand.

  1. Use the raw data to construct a table of expected counts
  2. Calculate \(\chi^2\) test statistic by setting up and evaluating the sum \(\sum_i \frac{(O_i - E_i)^2}{E_i}\)
  3. Find approximate p-value using the table
               dead
Group            0  1
  Penicillamine 93 65
  Placebo       94 60
Expected Counts
               dead
Group                  0        1
  Penicillamine 94.69872 63.30128
  Placebo       92.30128 61.69872
Do you notice how close the expected counts are to the actual counts? This should suggest that there is not much of a difference between the drug and placebo. We will see this reflected in the p-value later.
Terms of Sum
[1] 0.0305 0.0313 0.0456 0.0468
Test Statistic
[1] 0.1540909

Does your answer match what we calculated above? If so, great! If not, what went wrong?

Fisher’s Exact Test

We have learned that the \(\chi^2\) test is an approximate method of statistical inference. Using Fisher’s exact test on a \(2 \times 2\) table, we can calculate exact p-values and confidence intervals.

Using R

Using the cirrhosis data, conduct a Fisher’s exact test. Consider taking turns with your neighbor interpreting the p-value and confidence interval. How does this p-value compare to that of the chi-squared test?

Answer

Simply run fisher.test() as follows

fisher.test(tbl)

    Fisher's Exact Test for Count Data

data:  tbl
p-value = 0.7296
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
 0.5658494 1.4734044
sample estimates:
odds ratio 
 0.9135233 

Interpretation

We will conclude as we did before. This data provides no evidence that D-penicillamine decreases mortality rates among patients experiencing PBC. The p-value is even larger here than what we saw in the chi-squared test.

By Hand

Note that calculating by hand the CI for the odds ratio uses an approximation, so your answer may not exactly align with what we get from the Fisher’s exact test, but it should be close.

Calculating the odds ratio and associated confidence interval by hand can be summarized in the following steps:

Let our table of counts have the form

diseaseYes

diseaseNo

a

c

b

d

  1. Find the odds ratio: \(\frac{ad}{bc}\)

  2. Find the log odds ratio (natural log): \(\log{(OR)}\)

  3. Find the standard error: \(SE_{logOR}=\sqrt{\frac{1}{a} + \frac{1}{b} + \frac{1}{c} + \frac{1}{d}}\)

  4. Calculate the CI: \(CI_{logOR}=\log{(OR)} \pm z_{\alpha/2} * SE_{logOR}\)

  5. Exponentiate the CI to the original scale: \(CI = \exp{(CI_{logOR})}\)

Handwritten solutions

Quiz Review

Problem 1

We are interested in testing whether a certain at-risk population for diabetes has a daily sugar intake that is equal to the general population, which is equal to 77 grams/day. A sample of size 37 was taken from this at-risk population, and we obtained a sample mean of 80 and sample standard deviation of 11 grams. Perform a hypothesis test to test whether this population has a significantly different mean sugar intake from 77 grams.

Problem 2

The distribution of LDL cholesterol levels in a certain population is approximately normal with mean 90 mg/dl and standard deviation 8 mg/dl.

  1. What is the probability an individual will have a LDL cholesterol level above 95 mg/dl?

  2. Suppose we have a sample of 10 people from this population. What is the probability of exactly 3 of them being above 95 mg/dl?

  3. Take the sample of size 10, as in part b. What is the probability that the sample mean will be above 95 mg/dl?

  4. Suppose we take 5 samples of size 10 from the population. What is the probability that at least one of the sample means will be greater than 95 mg/dl?

Problem 3

In a survey conducted in a large city, 235 pet owners were asked about their cats’ behavior: whether their cat prefers staying indoors or going outside. Out of those surveyed, 155 people said their cat prefers staying indoors, while the remaining 80 said their cat prefers going outside. Use a normal approximation to test the hypothesis that the proportion of cats that prefer staying indoors is equal to the proportion that prefer going outside. Then, construct a 95% confidence interval for the true proportion of cats that prefer staying indoors, still using the normal approximation.

Problem 4

A wildlife research group is studying whether a new dietary supplement affects the resting heart rate of foxes. They track 9 foxes and measure their heart rate before and after one month of adding the supplement to their diet. The mean heart rate for the foxes before the supplement was 125 beats per minute. The mean heart rate one month after the supplement was 120.8 beats per minute. The standard deviation of the differences (after − before) was 3.4 beats per minute. This information is summarized in the table below.

Before supplement After supplement Difference
Mean 125 120.8 -4.2
Sd 3.9 3.1 3.4
  1. Test the hypothesis that the drug has no effect on lowering systolic blood pressure and state your conclusion.

  2. Calculate the 95% confidence interval for the mean difference in blood pressure before and after taking the drug.

Problem 5

An article in the New England Journal of Medicine reported that among adults living in the United States, the levels of albumin in cerebrospinal fluid is normally distributed with a mean of 29.5 mg/dl, and a standard deviation of 9.25 mg/dl. We are going to select a sample of size 20

  1. How does the variability of our sample mean compare with the variability of albumin levels in the population?

  2. What is the probability that our sample mean will lie between 29 and 31 mg/dl?

  3. What two values will contain the middle 50% of our sample means?

  4. Now assume we don’t know the standard deviation or mean of the population and our sample of 20 has a mean albumin level of 30.1 mg/dl and a standard deviation of 8.95 mg/dl. Construct a 95% confidence interval for the population mean and interpret it.

  5. Why is the confidence interval constructed in (d) wider than the confidence interval that would be constructed if we used the normal distribution? What components affect the width of the confidence interval?

Handwritten solutions