%%
% The project assigned to students in BME1300 class several semesters ago was
% to test the precision of a pedometer.
% 
% One group organized the experiment as follows. The subjects would walk on a treadmill
% for some time under different conditions and the pedometer count was contrasted to the
% human count obtained by several designated student-counters who simultaneously 
% counted the steps.
%  The human count was considered exact.
% 
% There were 4 treatments defined by two treadmill speeds and two types of gait.
% 
% Speeds: Normal at 2.7 mi/h and  Slow at 2 mi/h.
% Under both speeds the subject will walk with unconstrained gait and
% constrained gait (Steps constrained by stepping marks on the treadmill)
% 
%           1 = NU (Normal -- Unconstrained) 
%           2 = NC (Normal  -- Constrained) 
%           3 = SU (Slow -- Unconstarined) 
%           4 = SC (Slow -- Constrained) 
%           
% The student-researchers wanted to test that the differences between the
% pedometer count and the human count were equal under all 4 regimes
% of walking thus statistically confirming the constant precision of the pedomemer
% under this experimental design.
% 
% To account for differences among the subjects, each subject went through all
% 4 treatments, making this design repeated measures design.
% In total  n=24 subjects,  N=4  * 24 = 96  differences (pedometer count - human count)
% were measured.
% 
clear all
close all
  disp('Pedometer')
  set(0, 'DefaultAxesFontSize', 16);
  lw =2.5;  fs = 16;  msize = 10;




load 'C:/BESTAT/ANOVA/ANOVAdat/pmr1300.mat'
% 
% reading     count   diffs   treat     subject
%    223        224     -1       1         1
%    215        223     -8       1         2
%    150        153     -3       1         3
%  ...
%  total 96 rows = 4 treatments x 24 subjects

reading = pmr(:,1);
count = pmr(:,2);
diffs = reading - count;
n=length(diffs);
%
treat = pmr(:,4);
subject = pmr(:,5);
%
hist(diffs,15)

%%
% Nonsolution 1
%If 96 subjects involved and 
%for each we had pair of measures 
%without respect to treatments:
t=mean(diffs)/(std(diffs)/sqrt(n))
pval = 2 * tcdf(-abs(t), n-1)

% Solution
varnames ={'Treatment','Subject'};
[p, table, sts] = anovan( diffs, {treat, subject}, 1,3, varnames)
multcompare(sts, 'dimension',1)

%%