Function Name: t_test % Inputs (1): - (double) vector of data points from one sa
ID: 3539761 • Letter: F
Question
Function Name: t_test
% Inputs (1): - (double) vector of data points from one sample population
% - (double) vector of data points from another sample population
% Outputs(2): - (double) calculated t-statistic
% - (char) string describing the level of significance
% predicted by the t-statistic
%
% Function Description:
% In statistics, a t-test is a method used to determine if there is a
% significant difference between the means of two groups, or samples,
% which are typically subject to different treatments in a study. Your
% task is to create a function called "t_test" that takes in two vectors,
% each containing a set of data points from a different sample and
% performs a two-tailed student's t-test. Specifically, the function
% should output the calculated t-statistic as well as a string describing
% how significant the difference between the means of the two samples is.
%
% Although there are many types of student's t-tests, for the purposes of
% this problem, you should calculate your t-statistic based on the
% Welch's t-test, whose formula for the t-statistic is as follows:
% t = abs(x1_bar - x2_bar)/SEM
% where x1_bar is the mean of the first sample, x2_bar is the mean of the
% second sample, and SEM is the standard error of the mean calculated as
% follows:
% SEM = sqrt(var1/n1 + var2/n2)
% Here n1 and n2 are the number of data points in the first and second
% samples, respectively. Similarly, var1 and var2 represent the variance
% (or standard deviation squared) of both samples.
%
% To perform the t-test after calculating the t-statistic, one needs to
% consult a t-table, which has been given to you in the file
% 't_table.xls'. To read a t-table, first locate the row corresponding to
% the correct degrees of freedom (equal to n1 - 1) given in the first
% column. The remaining three values within that row are p-values, which
% when compared to the t-statistic represent the probability that the
% difference between the two samples is entirely due to chance.
% Therefore, if the calculated t-statistic is greater than or equal to
% the last and highest of the three values, your output string should be
% 'The difference in the means is very highly significant.' because this
% would mean the probability of the difference arising by chance is less
% than 1%. If the calculated t-statistic is only greater than or equal to
% the second value, your output string should be 'The difference in the
% means is highly significant.'. If the calculated t-statistic is only
% greater than or equal to the first value, your output string should be
% 'The difference in the means is significant.'. Finally, if your
% t-statistic is less than all the p-values, your output string should be
% 'The difference in the means is not significant.'
%
% Notes:
% - The sample sizes of both populations is guaranteed to be the same.
% - The desired degrees of freedom is guaranteed to be given in the
% 't_table.xls' file
% - The same t-table file should be used for comparing all t-statistics.
%
% Test Cases:
% [tstat1 str1] = t_test([310 250 350 360], [20 160 140 170])
% tstat1 => 4.5594
% str1 => The difference in the means is highly significant.
%
% [tstat2 str2] = t_test([360 340 460 330 480 390], [440 480 360 390 470 360])
% tstat2 => 0.6884
% str2 => The difference in the means is not significant.
't_table.xls' file below
d.f.
p = 0.1
p = 0.05
p = 0.01
2
2.92
4.3
9.92
3
2.35
3.18
5.84
4
2.13
2.78
4.6
5
2.02
2.57
4.03
6
1.94
2.45
3.71
7
1.89
2.36
3.5
8
1.86
2.31
3.36
9
1.83
2.26
3.25
10
1.81
2.23
3.17
11
1.8
2.2
3.11
12
1.78
2.18
3.05
13
1.77
2.16
3.01
14
1.76
2.14
2.98
15
1.75
2.13
2.95
16
1.75
2.12
2.92
17
1.74
2.11
2.9
18
1.73
2.1
2.88
19
1.73
2.09
2.86
20
1.72
2.09
2.85
21
1.72
2.08
2.83
22
1.72
2.07
2.82
23
1.71
2.07
2.81
24
1.71
2.06
2.8
25
1.71
2.06
2.79
26
1.71
2.06
2.78
27
1.7
2.05
2.77
28
1.7
2.05
2.76
29
1.7
2.05
2.76
30
1.7
2.04
2.75
35
1.69
2.03
2.72
40
1.68
2.02
2.7
45
1.68
2.01
2.69
50
1.68
2.01
2.68
60
1.67
2
2.66
70
1.67
1.99
2.65
80
1.66
1.99
2.64
90
1.66
1.99
2.63
100
1.66
1.98
2.63
d.f.
p = 0.1
p = 0.05
p = 0.01
2
2.92
4.3
9.92
3
2.35
3.18
5.84
4
2.13
2.78
4.6
5
2.02
2.57
4.03
6
1.94
2.45
3.71
7
1.89
2.36
3.5
8
1.86
2.31
3.36
9
1.83
2.26
3.25
10
1.81
2.23
3.17
11
1.8
2.2
3.11
12
1.78
2.18
3.05
13
1.77
2.16
3.01
14
1.76
2.14
2.98
15
1.75
2.13
2.95
16
1.75
2.12
2.92
17
1.74
2.11
2.9
18
1.73
2.1
2.88
19
1.73
2.09
2.86
20
1.72
2.09
2.85
21
1.72
2.08
2.83
22
1.72
2.07
2.82
23
1.71
2.07
2.81
24
1.71
2.06
2.8
25
1.71
2.06
2.79
26
1.71
2.06
2.78
27
1.7
2.05
2.77
28
1.7
2.05
2.76
29
1.7
2.05
2.76
30
1.7
2.04
2.75
35
1.69
2.03
2.72
40
1.68
2.02
2.7
45
1.68
2.01
2.69
50
1.68
2.01
2.68
60
1.67
2
2.66
70
1.67
1.99
2.65
80
1.66
1.99
2.64
90
1.66
1.99
2.63
100
1.66
1.98
2.63
Explanation / Answer
% make sure t_table.xls exists in the same directory where you save this code in
function [t res]= t_test(x1,x2)
t=(abs(mean(x1)-mean(x2)))/(sqrt((var(x1)/length(x1))+(var(x2)/length(x2))));
df=length(x1)-1;
vals=xlsread('t_table.xls');
[r c]=size(vals);
for k=1:r
if vals(k,1)==df
if t>=vals(k,4)
res= 'The difference in the means is very highly significant.';
elseif t>=vals(k,3)
res='The difference in the means is highly significant.';
elseif t>=vals(k,2)
res='The difference in the means is significant.';
else
res='The difference in the means is not significant.';
end
break;
end
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.