Each of the 10 subjects received both type A and type B aspirin (one week apart, with the order randomized). The two measurements therefore come from the same person and are dependent, which calls for a paired analysis.
person aspirinA aspirinB difference
1 1 15 13 2
2 2 26 20 6
3 3 13 10 3
4 4 28 21 7
5 5 17 17 0
6 6 20 22 -2
7 7 7 5 2
8 8 36 30 6
9 9 12 7 5
10 10 18 11 7
Variable Mean SD n
1 Aspirin A 19.2 8.625543 10
2 Aspirin B 15.6 7.777460 10
3 Difference (A - B) 3.6 3.098387 10
The mean within-person difference is 3.6 mg% with a standard deviation of 3.098 mg%.
Eight of the ten subjects show a decrease from A to B, one shows no change, and one shows an increase — the paired structure is clearly informative.
Let \(d_i = (\text{Aspirin A concentration})_i - (\text{Aspirin B concentration})_i\) for subject \(i\), and let \(\mu_d\) denote the true mean difference.
\[H_0: \mu_d = 0 \qquad \text{(the mean concentrations of the two drugs are the same)}\] \[H_1: \mu_d \neq 0 \qquad \text{(the mean concentrations of the two drugs differ)}\]
Because the question asks whether the means are the same, this is a two-sided test. The test statistic is
\[t = \frac{\bar{d} - 0}{s_d/\sqrt{n}} \sim t_{n-1} = t_{9} \quad \text{under } H_0.\]
Paired t-test
data: aspirinA and aspirinB
t = 3.6742, df = 9, p-value = 0.005121
alternative hypothesis: true mean difference is not equal to 0
95 percent confidence interval:
1.383548 5.816452
sample estimates:
mean difference
3.6
One Sample t-test
data: aspirin$difference
t = 3.6742, df = 9, p-value = 0.005121
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
1.383548 5.816452
sample estimates:
mean of x
3.6
Results.
Conclusion. Since the p-value (0.00512) is less than \(\alpha = 0.05\), we reject \(H_0\). There is statistically significant evidence that the mean 1-hour urine concentrations of the two aspirin types are not the same. On average, type A produces a concentration about 3.6 mg% higher than type B, and the 95% confidence interval excludes zero (it lies entirely above 0), which is consistent with rejecting the null hypothesis.
Shapiro-Wilk normality test
data: aspirin$difference
W = 0.91574, p-value = 0.3228
The Shapiro-Wilk test does not reject normality of the differences, so the paired t-test is appropriate.
Two Sample t-test
data: aspirinA and aspirinB
t = 0.9802, df = 18, p-value = 0.34
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-4.116103 11.316103
sample estimates:
mean of x mean of y
19.2 15.6
p-value \(= 0.34\) with \(t = 0.9802\) and \(df = 18\).
(For completeness, Welch’s unequal-variance version gives essentially the same answer:)
[1] 0.3401157
Comparison and interpretation.
| Analysis | t | df | p-value | Decision at \(\alpha = 0.05\) |
|---|---|---|---|---|
| Paired t-test | 3.674 | 9 | 0.005121 | Reject \(H_0\) |
| Two-sample t-test | 0.98 | 18 | 0.34 | Fail to reject \(H_0\) |
The two analyses lead to opposite conclusions. The estimated mean difference is identical in both cases (19.2 − 15.6 = 3.6 mg%), but the standard errors are very different. The two-sample test treats the 20 measurements as independent, so its standard error is driven by the large between-person variability (subject concentrations range from 5 to 36 mg%). The paired test removes that person-to-person variability by working with the within-person differences, whose standard deviation is only 3.1 mg% compared with roughly 8.6 and 7.8 mg% for the individual groups.
Ignoring the pairing therefore throws away the design’s main advantage and loses a great deal of power; it is also statistically invalid here because the independence assumption of the two-sample t-test is violated. The paired t-test is the correct analysis.
Twelve one-week-old male infants were randomly allocated to one of two groups, so the two samples are independent.
time group
1 9.50 Active
2 10.00 Active
3 9.75 Active
4 9.75 Active
5 9.00 Active
6 13.00 Active
7 11.50 None
8 12.00 None
9 13.25 None
10 11.50 None
11 13.00 None
12 9.00 None
Group n Mean Median SD Min Max
1 Active Exercise 6 10.16667 9.75 1.428869 9 13.00
2 No Exercise 6 11.70833 11.75 1.520005 9 13.25
The question is directional (is the mean time to walk less for the Active Exercise group?), so a one-sided test is used. Because we will analyze the data with a rank-based method, the hypotheses are most properly stated in terms of the distributions / locations of walking times.
Stated in terms of means, \(H_0: \mu_{\text{Active}} = \mu_{\text{None}}\) versus \(H_1: \mu_{\text{Active}} < \mu_{\text{None}}\), with \(\alpha = 0.05\).
Shapiro-Wilk normality test
data: active
W = 0.72061, p-value = 0.01012
Shapiro-Wilk normality test
data: none
W = 0.88521, p-value = 0.2939
Wilcoxon rank sum test with continuity correction
data: time by group
W = 9, p-value = 0.08523
alternative hypothesis: true location shift is less than 0
group rank
1 Active 30
2 None 48
Because there are tied observations (9.00, 9.75 and 13.00 each appear twice), R cannot compute an exact p-value and uses the normal approximation with a continuity correction. For comparison, the exact test obtained by ignoring the ties is:
[1] 0.08523293
Results.
Conclusion. The p-value (0.0852) is greater than \(\alpha = 0.05\), so we fail to reject \(H_0\). At the 5% level there is not sufficient evidence to conclude that the mean (median) time to walk independently is less for infants who receive active exercise than for those who do not. The observed medians do differ in the hypothesized direction (9.75 vs. 11.75 months), and the result would be significant at \(\alpha = 0.10\), but with only six infants per group the study has very limited power. A larger trial would be needed before drawing a conclusion about the effect of active exercise.
Two Sample t-test
data: time by group
t = -1.8102, df = 10, p-value = 0.05019
alternative hypothesis: true difference in means between group Active and group None is less than 0
95 percent confidence interval:
-Inf 0.001957879
sample estimates:
mean in group Active mean in group None
10.16667 11.70833
Note that the parametric t-test lands almost exactly on the boundary (\(p \approx 0.050\)) while the rank-based test gives \(p \approx 0.086\); the difference is driven almost entirely by the single 13.00-month observation in the Active group, which illustrates precisely why the non-parametric approach is the safer choice here.
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = TRUE,
fig.align = "center", fig.width = 6, fig.height = 4)
person <- 1:10
aspirinA <- c(15, 26, 13, 28, 17, 20, 7, 36, 12, 18)
aspirinB <- c(13, 20, 10, 21, 17, 22, 5, 30, 7, 11)
aspirin <- data.frame(person, aspirinA, aspirinB,
difference = aspirinA - aspirinB)
aspirin
summary_stats <- data.frame(
Variable = c("Aspirin A", "Aspirin B", "Difference (A - B)"),
Mean = c(mean(aspirinA), mean(aspirinB), mean(aspirin$difference)),
SD = c(sd(aspirinA), sd(aspirinB), sd(aspirin$difference)),
n = c(10, 10, 10)
)
summary_stats
plot(rep(1:2, each = 10), c(aspirinA, aspirinB),
xaxt = "n", xlim = c(0.8, 2.2),
xlab = "", ylab = "1-hour urine concentration (mg%)",
main = "Paired observations by subject", pch = 19, col = "steelblue")
axis(1, at = 1:2, labels = c("Aspirin A", "Aspirin B"))
segments(1, aspirinA, 2, aspirinB, col = "grey60")
paired_test <- t.test(aspirinA, aspirinB, paired = TRUE,
alternative = "two.sided", conf.level = 0.95)
paired_test
t.test(aspirin$difference, mu = 0)
shapiro.test(aspirin$difference)
qqnorm(aspirin$difference, main = "Normal Q-Q plot of the differences")
qqline(aspirin$difference, col = "red")
two_sample_test <- t.test(aspirinA, aspirinB, paired = FALSE,
var.equal = TRUE, alternative = "two.sided")
two_sample_test
t.test(aspirinA, aspirinB, paired = FALSE, var.equal = FALSE)$p.value
active <- c( 9.50, 10.00, 9.75, 9.75, 9.00, 13.00) # Active Exercise
none <- c(11.50, 12.00, 13.25, 11.50, 13.00, 9.00) # No Exercise
walking <- data.frame(
time = c(active, none),
group = factor(rep(c("Active", "None"), each = 6),
levels = c("Active", "None")) # Active first => "less" is correct
)
walking
data.frame(
Group = c("Active Exercise", "No Exercise"),
n = c(length(active), length(none)),
Mean = c(mean(active), mean(none)),
Median = c(median(active), median(none)),
SD = c(sd(active), sd(none)),
Min = c(min(active), min(none)),
Max = c(max(active), max(none))
)
boxplot(time ~ group, data = walking,
ylab = "Age at independent walking (months)", xlab = "Group",
main = "Walking age by treatment group",
col = c("lightblue", "lightpink"))
stripchart(time ~ group, data = walking, vertical = TRUE, method = "jitter",
pch = 19, col = "grey30", add = TRUE)
shapiro.test(active)
shapiro.test(none)
mwu <- wilcox.test(time ~ group, data = walking,
alternative = "less", # Active < None
exact = FALSE, # ties present => normal approximation
correct = TRUE)
mwu
walking$rank <- rank(walking$time)
aggregate(rank ~ group, data = walking, FUN = function(x) c(sum = sum(x)))
suppressWarnings(
wilcox.test(active, none, alternative = "less", exact = TRUE)$p.value
)
t.test(time ~ group, data = walking, alternative = "less", var.equal = TRUE)