Search code examples
rr-packaget-test

independent sample t-test in R using test statistics (mean, std. dev, count)


Hi I am trying to do an independent sample t-test, but do not have full data, only summary statistics. Is there an R package where I can use the mean, standard deviation, and count for each sample?


Solution

  • The TTestA function from the DescTools package.

    Also: you can implement this yourself! From the Wikipedia article on t tests, section on independent sample t test, unequal sample size

    Example data:

    means <- c(2.5, 3.7)
    stddev <- c(1.5, 1.8)
    n <- c(25,35)
    

    Calculate summary statistics:

    df <- sum(n)-2
    stddev_pooled <- sqrt(sum((n-1)*stddev^2)/df)
    tstat <- (means[1]-means[2])/(stddev_pooled*sqrt(sum(1/n)))
    

    Now calculate the p-value:

    2*pt(abs(tstat),df=df,lower.tail=FALSE)
    

    If you want the unequal variances case you can implement the formula in the next section of the Wikipedia page (Welch's t-test).