I need some help in doing a t-test. I know how to do it for a single data set but I need help doing a 2 sample t-test. I have one dataset called data1:
Asked
Active
Viewed 3,387 times
-2
-
3Please make your situation reproducible, i.e. provide us with the data and the code needed to mimic your situation. See http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for more tips on how to do this. – Paul Hiemstra Mar 20 '13 at 11:51
-
@PaulHiemstra do you have that comment on copy/paste?! I lose count of the number of times I link to that question! – Simon O'Hanlon Mar 20 '13 at 11:52
-
2I do have it as a note in my MacOS dashboard. But there is supposed to be a Stack app that allows you to save a number of standard comments. That is probably even better. – Paul Hiemstra Mar 20 '13 at 11:58
2 Answers
7
The data:
data1 <- data.frame(n = 15, mean = 14, sd = 8)
data2 <- data.frame(n = c(17, 5, 8), mean = c(19, 17, 11), sd = c(7, 6, 9))
A t-test function (based on numbers of observations, mean values, and standard deviations):
T.test <- function(n, mean, sd) {
s <- sum((n - 1) * sd^2) / (sum(n) - 2) # weighted variance
t <- sqrt(prod(n) / sum(n)) * (diff(mean) / sqrt(s)) # t statistic
df <- sum(n) - 2 # degrees of freedom
p <- (1 - pt(abs(t), df)) * 2 # p value
c(t = t, p = p)
}
Apply the function to all rows of data2
:
apply(data2, 1, function(x) T.test(c(x[1], data1$n),
c(x[2], data1$mean),
c(x[3], data1$sd)))
The output shows t- and p-values for all rows in data2
:
[,1] [,2] [,3]
t -1.98618371 -0.8215838 0.8730255
p 0.05621594 0.4220631 0.3925227

Sven Hohenstein
- 80,497
- 17
- 145
- 168
1
There is also a built-in function in R
?t.test
Description Performs one and two sample t-tests on vectors of data.
#just an easy example
a = c(12.9, 13.5, 12.8, 15.6, 17.2, 19.2, 12.6, 15.3, 14.4, 11.3)
b = c(12.7, 13.6, 12.0, 15.2, 16.8, 20.0, 12.0, 15.9, 16.0, 11.1)
t.test(a,b, paired=TRUE)
Paired t-test
data: a and b
t = -0.2133, df = 9, p-value = 0.8358
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-0.5802549 0.4802549
sample estimates:
mean of the differences
-0.05

Gianni Spear
- 7,033
- 22
- 82
- 131
-
The built-in `t.test` function needs raw data, but the OP has mean, SD, and n values. – Sven Hohenstein Mar 20 '13 at 14:23
-
Sadly, this doesnt work for me as I'm comparing values with different n values – Laurence Cais Mar 21 '13 at 23:14