t-test in Matlab - no data!
Show older comments
Hi,
Is there a way in Matlab to perform t-test (where we check for variance first and then perform the test and find the p-value) using means and std (no data)?
Thanks
Answers (1)
Aashray
on 7 Feb 2025
Hi Seldeeno,
Yes, you need to perform a "t-test" in MATLAB using summary statistics, using means, standard deviations, and sample sizes of both groups.
The following steps can be followed for performing the “t-test”:
- Define the means, standard deviations, and sample sizes for both groups.
- Conduct an "F-test" to determine whether the variances of the two groups are similar. This will help decide whether to use the standard "t-test" assuming equal variances or opt for Welch's t-test if the variances differ.
- Calculate the t-statistic and determine degrees of freedom.
- Compute the p-value to evaluate the statistical significance of the difference between the groups.
The following piece of code demonstrates a part of the problem:
F = (std1^2) / (std2^2);
pValueF = 2 * min(fcdf(F, df1, df2), 1 - fcdf(F, df1, df2)); % df1 and df2 are degrees of freedom respectively
equalVar = pValueF > alpha; % alpha is significance level
% n1 and n2 are group sizes respectively
if equalVar
sp = sqrt(((n1 - 1) * std1^2 + (n2 - 1) * std2^2) / (n1 + n2 - 2));
tStat = (mean1 - mean2) / (sp * sqrt(1/n1 + 1/n2));
df = n1 + n2 - 2;
else
% Welch's t-test for unequal variances
tStat = (mean1 - mean2) / sqrt((std1^2/n1) + (std2^2/n2));
df = ((std1^2/n1 + std2^2/n2)^2) / (((std1^2/n1)^2 / (n1 - 1)) + ((std2^2/n2)^2 / (n2 - 1)));
end
The following functions are helpful while performing the statistical calculations:
- “fcdf()”: Computes the cumulative distribution function for the F-distribution
- "sqrt()": Calculates the square root
- "tcdf()": Computes the cumulative distribution function for the t-distribution
Categories
Find more on Hypothesis Tests in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!