machine1 <- c(30.02,29.99,30.11,29.97,30.01,29.99)
machine2 <- c(29.89,29.93,29.97,29.98,30.02,29.98)
t.test(machine1,machine2, var.equal = TRUE, paried = FALSE)
When I run this code,
Two Sample t-test
data: machine1 and machine2
t = 1.9417, df = 10, p-value = 0.08085
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.007867295 0.114533962
sample estimates:
mean of x mean of y
30.01500 29.96167
I got this result.
But when I tried to calculate t-statistic manually, it's different.
What I got is
s^2 = 0.0023
s = 0.048
t = (30.015-29.962) / 0.048 * sqrt(1/10 1/10) = 0.053/0.021 = 2.4
calculated by R = 1.9417
calculated by myself = 2.4
Which part did I do incorrectly?
Thanks for the advices in advance
CodePudding user response:
Here is the code to compute the pooled standard error from t.test
, slightly simplified:
nx <- length(machine1); ny <- length(machine2)
vx <- var(machine1); vy <- var(machine2)
df <- nx ny - 2
v <- (nx - 1) * vx (ny - 1) * vy
v <- v/df
stderr <- sqrt(v * (1/nx 1/ny))
This comes out to 0.0274 rather than 0.021.
(mean(machine1)-mean(machine2))/0.0274
is 1.9464.
Not quite sure what's going on here except that there may be some significant effects of roundoff error?