Wu course 20200131: MATLAB t-test

load examgrades;
%only take column 1
grade1 = grades(:,1);
%look at the data
figure;
hist(grade1)
mean(grade1)
std(grade1)

%one sample t test
%if the national mean for the exam is 70
%Question: is grade1 different from the national mean?
%Hypothesis: grade1 is different from 70.
%equal variance assumed
%step by step
proposed_mean = 70;
sx_top = std(grade1);
n = length(grade1);
sx_bottom = sqrt(n);
sx = sx_top/sx_bottom;
t_top = mean(grade1)-proposed_mean;
t_bottom = sx;
t = t_top/t_bottom
%one step
[h,p,ci,stats] = ttest(grade1,proposed_mean)
%h = 1, means hypothesis is true. Grade1 is different from 70.


%independent samples t tests
grade2 = grades(:,2);
%if grade1 and grade2 belong to two classrooms.
%Question: is grade1 different from grade2? (Do the two classrooms have
%same or different exam score?)
%Hypothesis: grade1 is different from grade2.
figure;
hist(grade2)
%Test the null hypothesis that the two data samples are from populations with equal means.
%step by step
n1 = length(grade1);
n2 = length(grade2);
sp_top = (n1-1)*std(grade1)^2+(n2-1)*std(grade2)^2;
sp_bottom = n1+n2-2;
sp = sqrt(sp_top/sp_bottom);
t_top = mean(grade1)-mean(grade2);
t_bottom = sp*sqrt(1/n1+1/n2);
t = t_top/t_bottom
%one step
[h,p,ci,stats] = ttest2(grade1,grade2)
%h = 0, hypothesis false. grade1 is not significantly different from
%grade2.


%Repeated samples t test
%if grade1 and grade2 are two exams (eg, exam1 and exam2) in one classroom
%and each row of grade1 and grade2 corresponds to the same student
%Question: is grade1 different from grade2? (Was the performance better or
%worse in exam2 than exam1)
%step by step
%compare the difference of to zero
d = grade1 - grade2;
proposed_mean = 0;
n = length(d);
sx_top = std(d);
sx_bottom = sqrt(n);
sx = sx_top/sx_bottom;
t_top = mean(d)-proposed_mean;
t_bottom = sx;
t = t_top/t_bottom
%one step
[h,p,ci,stats] = ttest(grade1,grade2)
%n = 0, hypothesis false. grade1 is not different from grade2

%homework learn how to use function 'readtable' to read your own data
%then do some t tests on your own data.
%hint, need to convert data to a csv. 
help readtable
%d bloggers like this: