# Finding student t critical value...

110 views (last 30 days)
Sabina Yasmin on 17 Mar 2022
Answered: Star Strider on 17 Mar 2022
Is there any code for student t critical value for two sided test like tinv.how to use it...

Scott MacKenzie on 17 Mar 2022
Edited: Scott MacKenzie on 17 Mar 2022
Here's demo code using tinv to produce the t-critical values for a two-sided test with independent samples (i.e., between-subjects). The example uses a random data set (10x2) with one column biased to occasionally produce a significant difference. If you run this script multiple times, you'll find that t falls within the lower and upper t-critical values when p > .05 and falls outside the lower and upper t-critical values when p < .05.
% design parameters
n = 10; % Note: df = 2*(n-1)
alpha = 0.05;
% test data
M(:,1) = rand(1,n);
M(:,2) = rand(1,n) + 0.3; % add bias to get significance (sometimes)
% perform t-test
[~, p, ~, stats] = ttest2(M(:,1), M(:,2));
% compute lower and upper t-critical values
pLower = alpha/2;
pUpper = 1 - alpha/2;
tCritical = tinv([pLower pUpper], stats.df);
% output t, p, df, and t-critical lower and upper
fprintf('t=%.4f, p=%.4f, df=%d, t-critical: %.4f | %.4f\n', stats.tstat, p, stats.df, tCritical(1), tCritical(2));
t=-2.0592, p=0.0542, df=18, t-critical: -2.1009 | 2.1009

Star Strider on 17 Mar 2022
Your question implies that you do not have the tinv function.
The t distribution and inverse t distribution are relatively easy to code using the betainc and fzero functions.
% % % % T-DISTRIBUTIONS —
% Variables:
% t: t-statistic
% v: degrees of freedom
tdist2T = @(t,v) (1-betainc(v/(v+t^2),v/2,0.5)); % 2-tailed t-distribution
tdist1T = @(t,v) 1-(1-tdist2T(t,v))/2; % 1-tailed t-distribution
% This calculates the inverse t-distribution (parameters given the
% probability ‘alpha’ and degrees of freedom ‘v’:
t_inv = @(alpha,v) fzero(@(tval) (max(alpha,(1-alpha)) - tdist1T(tval,v)), 5); % T-Statistic Given Probability 1alpha’ & Degrees-Of-Freedom ‘v’
% CHECK —
alfa = 0.975;
nu = 6;
CritVal = t_inv(alfa,nu) % This Function
CritVal = 2.4469
CritVal = tinv(alfa, nu) % MATLAB 'tinv'
CritVal = 2.4469
.

R2021b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!