Lab 12

Published

April 14, 2026

Download R code

Objectives

  1. Conduct two-sample t-test in R

    • Student’s
    • Welch’s
  2. Practice

  3. Introduce Practice Assessment1

Case Study

Diarrhea is a major public health problem in underdeveloped countries, especially for babies. Diarrhea leads to dehydration, which results in millions of deaths each year worldwide.

Researchers in Peru conducted a double-blind randomized controlled trial, published in The New England Journal of Medicine, to determine whether Bismuth salicylate would improve the outcomes of infants suffering from diarrhea.

In their study, all infants received the standard therapy for diarrhea: oral re-hydration. In addition to the rehydration, 85 babies received bismuth salicylate, while 84 babies received a placebo. The total stool volumes for all infants over the course of their illness was measured. To adjust for body size, the researchers divided by body weight to obtain their outcome of interest: stool output per kilogram of body weight. The results of their study are available on the course website.

library(ggplot2)
study_data <- read.delim("https://raw.githubusercontent.com/IowaBiostat/data-sets/main/diarrhea/diarrhea.txt")
ggplot(study_data, aes(x=Stool, y = Group)) +
  theme_classic() +
  labs(
    title = "Effect of Bismuth Salicylate", 
    x = "Infant Stool Output (ml/kg)"
  )+
  geom_boxplot(fill = c("grey", "snow2"))

What do you notice about the distributions here?

Testing the Difference

To decide which test would be more appropriate, we should look at the standard deviations of each group. There are multiple ways to do this, but here we will introduce subsetting.

Warningsubset function

Sometimes we are interested in only portions of our data that match some condition. In our case here, we are interested in the summary statistics from each group (mean, sd).

treat <- subset(study_data, Group == "Treatment")[,-1] # [,-1] removes Group column
control <- subset(study_data, Group == "Control")[,-1] # which is unnecessary after subset

Take a look at each of these new datasets using the View() function, or by clicking on its name in the environment.

After subsetting, we can look at both standard deviations. How similar/different are they? How does this influence our decision for our test?

c(sd(treat), sd(control))
[1] 197.3636 253.6135

Student’s

For Student’s two-sample t-test, we move forward assuming the standard deviations from each group are about the same. If you would like to do this by hand the pooled standard deviation is approximately 227.07. 2

Conduct a test of the difference between the means from the control versus the treatment group. What is the null hypothesis that we are testing?

Null Hypothesis

There is no difference in infant stool output between the treatment, bismuth salicylate, and the control group.

OR

The effect of bismuth salicylate is the same as the control on infant stool output.

t.test(treat, control, paired = FALSE, var.equal = TRUE)

    Two Sample t-test

data:  treat and control
t = -2.245, df = 167, p-value = 0.02608
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -147.396700   -9.457362
sample estimates:
mean of x mean of y 
 181.8706  260.2976 
What would you conclude from these results?
Conclusion There is moderately strong evidence that the effect of Bismuth Salicylate lowers infant stool output, improving health by decreasing risk of severe dehydration.

Handwritten solution

Welch’s

For Welch’s two-sample t-test, we do not make the assumption that the standard deviations are equal. We would have concluded this by noting that the standard deviations for treatment and control were 197.36 and 253.61, respectively.

The null hypothesis is the same as before, all that changes about the test is how the degrees of freedom is calculated and the pooled standard error substituted for something else. For our class, we don’t need to worry about the formulas for these–we just need to remember that they are different from Student’s t-test.

t.test(treat, control, paired = FALSE, var.equal = FALSE)

    Welch Two Sample t-test

data:  treat and control
t = -2.2417, df = 156.64, p-value = 0.02638
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -147.530979   -9.323083
sample estimates:
mean of x mean of y 
 181.8706  260.2976 

What would you conclude from this output? How does it differ from the previous output?

Conclusion

We would conclude the same as before: There is moderately strong evidence that the effect of Bismuth Salicylate lowers infant stool output, improving health by decreasing risk of severe dehydration.

This may be a little surprising because of how different the standard deviations were. The sample sizes for each group were about the same and both sizes were fairly large, leading to a similar answer as the Student’s test.

Practice Problems

Some infants are born with congenital heart defects and require surgery very early in life. One approach to performing this surgery is known as “circulatory arrest.” A downside of this procedure, however, is that it cuts off the flow of blood to the brain, possibly resulting in brain damage. An alternative procedure, “low-flow bypass” maintains circulation to the brain, but does so with an external pump that potentially causes other sorts of injuries to the brain.

To investigate the treatments, surgeons at Harvard Medical School conducted a randomized controlled trial. In the trial, 70 infants received low-flow bypass surgery and 73 received the circulatory arrest approach. The researchers looked at two outcomes: the Psychomotor Development Index (PDI), which measures physiological development, and the Mental Development Index (MDI), which measures mental development. For both indices, higher scores indicate greater levels of development. The results of their study are on the course website.

Read in the infant-heart dataset from the course website, name it heart, and complete the following.

1

Calculate the standard deviations of PDI and MDI for each group. Based on these values, should we do a Student’s test or a Welch’s test?

Answer

Using subset

arrest <- subset(heart, Treatment == "Circulatory arrest")[,-1] 
bypass <- subset(heart, Treatment == "Low-flow bypass")[,-1]

pdi_arrest <- sd(arrest$PDI) |> round(2)
mdi_arrest <- sd(arrest$MDI) |> round(2)
pdi_bypass <- sd(bypass$PDI) |> round(2)
mdi_bypass <- sd(bypass$MDI) |> round(2)


data.frame(
  pdi_arrest = (pdi_arrest),
  pdi_bypass = (pdi_bypass),
  mdi_arrest = (mdi_arrest),
  mdi_bypass = (mdi_bypass)
)
  pdi_arrest pdi_bypass mdi_arrest mdi_bypass
1      16.49      14.69      16.56      14.57

Alternatively, you can use by and with, 3

with(heart, by(PDI, Treatment, sd))
Treatment: Circulatory arrest
[1] 16.488
------------------------------------------------------------ 
Treatment: Low-flow bypass
[1] 14.68527
with(heart, by(MDI, Treatment, sd))
Treatment: Circulatory arrest
[1] 16.56448
------------------------------------------------------------ 
Treatment: Low-flow bypass
[1] 14.57256

2

Conduct a t-test (whichever one you decided was most appropriate) to determine whether the difference in physiological development between the infants in the circulatory arrest group and the low-flow bypass group is statistically significant.

Answer
t.test(heart$PDI ~ heart$Treatment, var.equal = T)

    Two Sample t-test

data:  heart$PDI by heart$Treatment
t = -2.2385, df = 141, p-value = 0.02676
alternative hypothesis: true difference in means between group Circulatory arrest and group Low-flow bypass is not equal to 0
95 percent confidence interval:
 -11.0232390  -0.6840017
sample estimates:
mean in group Circulatory arrest    mean in group Low-flow bypass 
                        91.91781                         97.77143 
There is significant evidence that psychomotor development is better in the low-flow bypass group.

3

Conduct a t-test to determine whether the difference in mental development between the infants in the circulatory arrest group and the low-flow bypass group is statistically significant.

Answer
t.test(heart$MDI ~ heart$Treatment, var.equal = T)

    Two Sample t-test

data:  heart$MDI by heart$Treatment
t = -1.2696, df = 141, p-value = 0.2063
alternative hypothesis: true difference in means between group Circulatory arrest and group Low-flow bypass is not equal to 0
95 percent confidence interval:
 -8.484009  1.848393
sample estimates:
mean in group Circulatory arrest    mean in group Low-flow bypass 
                        103.0822                         106.4000 
There is insufficient evidence that mental development is different between the two groups.

4

Report and interpret the confidence intervals from each test.
Answer

When testing which treatment group had better psychomotor development, the 95% confidence interval indicated that infants who receive the low-flow bypass could have between 0.68 and 11.02 point increase in their PDI score on average.

When testing which treatment group had better mental development, the 95% confidence interval indicated that infants who receive low-flow bypass could have a 1.85 point reduction or up to a 8.48 increase in MDI score on average.

Conclusion

For the interested reader, this is the summary taken from the journal that we can compare with our results.4

Of the 171 patients enrolled in the study, 155 were evaluated. After adjustment for the presence or absence of a ventricular septal defect, the infants assigned to circulatory arrest, as compared with those assigned to low-flow bypass, had a lower mean score on the Psychomotor Development Index. The method of support was not associated with the prevalence of abnormalities on MRI scans of the brain, or scores on the Mental Development Index. Heart surgery performed with circulatory arrest as the predominant support strategy is associated with a higher risk of delayed motor development and neurologic abnormalities at the age of one year than is surgery with low-flow bypass as the predominant support strategy.

Footnotes

  1. Practice assessment found here↩︎

  2. The St.Devs are clearly not the same here, but for demonstration purposes we will move forward↩︎

  3. In this situation, using the combination of with and by is much simpler to code. Syntax for these can be found in the Functions document.↩︎

  4. https://pubmed.ncbi.nlm.nih.gov/7838188/↩︎