R - two-sample_t_test Unchanged When Switching Between Pooled Variance and non-Pooled Variance
I run the R code as follows:
library(oibiostat)
data("swim")
## independent two-sample pooled t test
t.test(swim$wet.suit.velocity, swim$swim.suit.velocity,
alternative = "two.sided", paired = FALSE, var.equal = TRUE)
#unequal variance two-sample t test
t.test(swim$wet.suit.velocity, swim$swim.suit.velocity,
alternative = "two.sided", paired = FALSE, var.equal = FALSE)
Which results in the same output:
Two Sample t-test
data: swim$wet.suit.velocity and swim$swim.suit.velocity
t = 1.3688, df = 22, p-value = 0.1849
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.03992124 0.19492124
sample estimates:
mean of x mean of y
1.506667 1.429167
and
Welch Two Sample t-test
data: swim$wet.suit开发者_运维技巧.velocity and swim$swim.suit.velocity
t = 1.3688, df = 21.974, p-value = 0.1849
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.03992937 0.19492937
sample estimates:
mean of x mean of y
1.506667 1.429167
The pooled two-sample t.test should be different from un-pooled one in terms of formulas.
But if I run the code as follows:
set.seed(5)
x1 = rnorm(15, 95, 20)
x2 = rnorm(50, 110, 5)
t.test(x1, x2) # Welch
t.test(x1, x2, var.eq=T) # pooled
The outputs from both t.test are clearly different. So, I just got a coincidence of data set?
I calculate by hand and find that the output from Welch Two Sample t-test
is right. I am very confused why the output of pooled t.test is wrong.
Edit
Like I say in comment, package oibiostat
is not on CRAN, it's on GitHub. If not installed yet, run
devtools::install_github("OI-Biostat/oi_biostat_data")
And there's no need to load a package to access one of its data sets, the following will load it.
data(swim, package = "oibiostat")
精彩评论