Another possible solution is to simulate the datasets and then use the standard t test function. It may be less efficient, computationally speaking, but it is very simple.
t.test.from.summary.data <- function(mean1, sd1, n1, mean2, sd2, n2, ...) {
data1 <- scale(1:n1)*sd1 + mean1
data2 <- scale(1:n2)*sd2 + mean2
t.test(data1, data2, ...)
}
Given that the t test depends only on the sample summary statistics but disregards the actual sample distributions, this function will give exactly the same results (except for variable names) as the t test function:
x <- c(1.0, 1.2, 2.3, 4.2, 2.1, 3.0, 1.9, 2.0, 3.2, 1.6)
y <- c(3.5, 4.2, 3.3, 2.0, 1.7, 4.5, 2.7, 2.8, 3.3)
m_x <- mean(x)
m_y <- mean(y)
s_x <- sd(x)
s_y <- sd(y)
t.test.from.summary.data(m_x, s_x, 10, m_y, s_y, 9)
Welch Two Sample t-test
data: data1 and data2
t = -1.9755, df = 16.944, p-value = 0.06474
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-1.78101782 0.05879559
sample estimates:
mean of x mean of y
2.250000 3.111111
t.test(x,y)
Welch Two Sample t-test
data: x and y
t = -1.9755, df = 16.944, p-value = 0.06474
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-1.78101782 0.05879559
sample estimates:
mean of x mean of y
2.250000 3.111111
?pt
) -- see especiallypt()
-- do have all the info you'd need to do this yourself. And you'll learn a lot about stats and R if you do that. $\endgroup$tsum.test
function in package BSDA, which implements a t-test (two sample; Welch or equal-variance and also one sample) from summary data you supply. It basically works like the t-test in vanilla R but on the summary info. $\endgroup$tsum.test()
from theBSDA library
as stated by @Nick Cox. It does exactly the same thing as what @macro wrote in lines of code. If the question asked, what is the understanding of the background calculation for computing the t-test statistic in R then Marco would be more appropriate an answer. Please note, I am not trying to offend anyone, just stating my personal opinion related to my professional background. And @marco that is some neat coding :) $\endgroup$