% LMcs140303_4th.m
% Larsen & Marx (2006) Introduction to Mathematical Statistics, 4th edition
% page 821. A case study solved by the sign and Wilcoxon signed rank
% test
% Written by Eugene.Gallagher@umb.edu 11/16/10 Revised 3/1/11
% 
D=[4.67 4.36;3.5 3.64;3.5 4;3.88 3.26;3.94 4.06;4.88 4.58;4 3.52
    4.4 3.66;4.41 4.43;4.11 4.28;3.45 4.25;4.29 4;4.25 5;4.18 3.85
    4.65 4.18];
expected=0;
[P,H,STATS] = signrank(D(:,1),D(:,2),'alpha',0.05,'method','exact');
fprintf(...
'\nThe signed rank test statistic is %4.1f with exact 2-tailed p=%6.4g\n',...
 STATS.signedrank,P);
[P,H,STATS] = signrank(D(:,1),D(:,2),'alpha',0.05,'method','approximate');
fprintf('The signed rank test approximate 2-tailed p=%6.4g for z=%6.4f\n',P,STATS.zval);
[H,P,CI,STATS] = ttest(D(:,1),D(:,2),0.05,'both');
fprintf('The paired t test 2-tailed p=%6.4g\n',P);
fprintf(...
'The mean difference in ratings (In-Class - Online) = %4.2f with 95%% CI: [%4.2f %4.2f]\n',...
    mean(D(:,1)-D(:,2)), CI(1),CI(2));
[p,h,stats] = signtest(D(:,1),D(:,2),0.05,'method','exact');
fprintf('The sign test statistic is %4.1f with exact 2-tailed p=%6.4g\n',stats.sign,p);
[p,h,stats] = signtest(D(:,1),D(:,2),'method','approximate');
fprintf('The sign test z=%5.3f with approximate 2-tailed p=%6.4g\n',stats.zval,p);

% Plot histogram and check for symmetry
binsize=.2;  % Needed in order to properly scale the normal pdf
edges=-.8:binsize:.8;
% hist(D);
[N,BIN] = histc(D(:,1)-D(:,2),edges);
bar(edges,N,'histc')
axis([-.82 .82 0 max(N)+0.2])
set(get(gca,'Children'),'FaceColor',[.8 .8 1]);
xlabel('Difference in evaluations, In-Class - Online','FontSize',20);
ylabel('Number of Cases','FontSize',20);
ax1=gca;
set(ax1,'Ytick',[0:max(N)],'Xtick',edges)
figure(gcf);pause
% Superimpose the normal probability pdf on a histogram of differences.
% The normal probability equation is provided on p. 293
% This is for mean 0, and unit standard
% deviation. The more general equation (Legendre & Legendre, 1998 p. 147) is:
% f(y_j)=1/(sqrt(2*pi)*sigma_j)*exp(-1/2*((y_j-mu_j)/sigma_j)^2);
n=length(D(:,1)-D(:,2));
mu_j=mean(D(:,1)-D(:,2));
sigma_j=std(D(:,1)-D(:,2)); % sigmaj is the standard deviation; = 1 after Z transform
y_j=-.82:0.01:.82;
fy_j=1/(sqrt(2*pi)*sigma_j)*exp(-1/2*((y_j-mu_j)./sigma_j).^2);
fy_j=n*binsize*fy_j;
cutoff=expected;
fy_cutoff=n*binsize/(sqrt(2*pi)*sigma_j)*exp(-1/2*((cutoff-mu_j)./sigma_j).^2);
% will properly scale the height of the pdf
% fyj=1/(sqrt(2*pi)*sigmaj)*exp(-1/2*((y-muj)/sigmaj).^2); 
% Plot using ax1 handle, saved above,to save this graph
% on top of the previous graph.
v=axis;
h1=line(y_j,fy_j ,'Color','r','Parent',ax1);
set(h1,'linestyle','--','color','r','linewidth',2)
h1=line([cutoff cutoff]',[0 fy_cutoff]','Color','b','Parent',ax1);
set(h1,'linestyle','-.','color','b','linewidth',3)
h2=line([cutoff cutoff]',[fy_cutoff v(4)]','Color','b','Parent',ax1);
set(h2,'linestyle','-.','color','b','linewidth',3)
s=sprintf('Case Study 14.3.3, %2.0f samples untransformed',n);
title(s,'FontSize',22)
figure(gcf);pause