% DEMO NP

% Many Vietnam veterans have dangerously high levels of the dioxin
% 2,3,7,8-TCDD in blood and fat tissue as a result of their exposure to
% the defoliant Agent Orange. A study published in {\it Chemosphere
% (Vol. 20, 1990)} reported on the TCDD levels of 20 Massachusetts
% Vietnam veterans who were possibly exposed to Agent Orange.
% The amounts of TCDD (measured in parts per trillion) in blood
% plasma and fat tissue drawn from each veteran are measured.
% 
%  Is there sufficient evidence of a difference between the
% distributions of TCDD levels in plasma and fat tissue for
% Vietnam veterans exposed to Agent Orange?
% Use sign test and $\alpha = 0.10.$
% 

 tcddpla = [2.5 3.1 2.1 3.5 3.1 1.8 6.8 3.0 36.0 ...
   4.7 6.9 3.3 4.6 1.6 7.2 1.8 20.0 2.0 2.5 4.1];
 tcddfat = [4.9 5.9 4.4 3.5 7.0 4.2 10.0 5.5 41.0 ...
   4.4 7.0 2.9 4.6 1.4 7.7 1.8 11.0 2.5 2.3 2.5];
% ignore ties
 [pvae, pvaa, n, plusses, ties] =signtst(tcddpla, tcddfat)
%pvae =0.1662
%pvaa =0.1660
%n =17
%plusses =6
%ties =3

% randomize ties
 [pvae, pvaa, n, plusses, ties] =signtst(tcddpla, tcddfat,'R')
% pvae = 2 * 0.2517
% take the conservative, least favorable approach
 [pvae, pvaa, n, plusses, ties] =signtst(tcddpla, tcddfat,'C')
% pvae = 2 * 0.0577
 [pvae, pvaa, n, plusses, ties] =signtst(tcddpla, tcddfat,'I')
% pvae = 2 * 0.1660

%%
%----------------------------------------------------------
[p, h, stats] = signtest(tcddpla, tcddfat)
%p = 0.3323
%h =0
%stats =  sign: 6
[h p] = ttest(tcddpla- tcddfat)
%h = 0
%p =0.7927

%%
%----------------------------------------------------------

%  Identical Twins. Twelve sets of identical
% twins underwent psychological tests to measure the amount of
% aggressiveness in each person's personality. We are interested in
% comparing the twins to each other to see if the first born twin
% tends to be more aggressive than the other. The results are as
% follows, the higher score indicates more aggressiveness.


   fb = [86 71 77 68 91 72 77 91 70 71 88 87];
   sb = [88 77 76 64 96 72 65 90 65 80 81 72];
   [t1, z1, p] = wsirt(fb, sb, 1)

   %t1 =   17           %value of T
   %z1 =   0.7565       %value of Z
   %p =    0.2382       %p-value of the test
   
   [h p]=ttest(fb-sb)
%%
%-----------------
%   WILCOXON-MANN-WHITNEY TEST
%   Input:  data1, data2 - first and second sample
%           alt - code for alternative hypothesis;
%               -1  mu1<m2; 0 mu1 ne m2; and 1 mu1>mu2
%   Output: R - sum of the ranks for the first sample. If
%               there is no ties, the standardization by ER and
%               Var R allows using standard normal quantiles
%               as long as sample sizes are larger than 15-20.
%           T - standardized R but adjusted for the ties
%           p - p-value for testing equality of distributions
%               (equality of locations) against the alternative
%               specified by input "alt"
%   Example of use:
 dat1=[1 3 2 4 3 5 5 4 2 3 4 3 1 7 6 6 5 4 5 8 7 3 3 4];
 dat2=[2 5 4 3 4 3 2 2 1 2 3 2 3 4 3 2 3 4 4 3 5];
 [sumranks1, tstat, pval] = wsurt(dat1, dat2, 1)
%sumranks1 = 639.5000
%tstat = 2.0350
%pval = 0.0206

%%
%----------------------------------
%Kruskal Wallis (ANOVA on ranks)
data  =  [ 1 3 4  3 4 5  4 4 4 6 5 ];
belong = [ 1 1 1  2 2 2  3 3 3 3 3 ];
[H, p, averranks] = kruskalwallistest(data, belong)
kwpairwise(data, belong)
% H =3.8923
% p =0.1428
% averranks = [3.1667   6   7.7]
%%
% The following data are
%from a classic agricultural experiment measuring crop yield in
%four different plots. For simplicity, we identify the treatment
%(plot) using the integers  1,2,3, and 4. The third treatment mean
%measures far above the rest, and the null hypothesis (the
%treatment means are equal) is rejected with a  p -value less than 0.0002


data= [83 91 94 89 89 96 91 92 90 84 91 90 81 83 84 83 ...
  88 91 89 101 100 91 93 96 95 94 81 78  82  81 77 79 81 80];
belong = [1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 ...
              3 3 3 3 3 3 3 3 4 4 4 4 4 4 4];
[H, p, averranks] = kruskalwallistest(data, belong)

%  H  =  20.3371
%  p = 1.4451e-004
% averranks= [21    15.5    26.6875     4.5714]
kwpairwise(data, belong)

 %MATLAB Built In
[p table stats]=kruskalwallis(data, belong)
multcompare(stats)

%%

% In an evaluation of
% vehicle performance, six professional drivers, (labelled
% I,II,III,IV,V,VI) evaluated three cars ($A$, $B$, and $C$) in a
% randomized order. Their grades concern only the performance of the
% vehicles and supposedly are not influenced by the vehicle brand
% name or similar exogenous information. Here are their rankings on
% the scale 1--10:
% 
% 
%  Car &   I &  II & III & IV &  V &  VI
%  -------------------------------------
%  A   &   7 &   6 &   6 &  7 &  7 &   8 
%  B   &   8 &  10 &   8 &  9 & 10 &   8 
%  C   &   9 &   7 &   8 &  8 &  9 &   9
% 
% 
%  
 data = [7  8  9;    6  10  7;   6  8  8; ...
         7  9  8;    7  10  9;   8  8  9];
 [S,F,pS,pF] = friedmantest(data)

% S =
%     8.2727
% F =
%     11.0976
% pS =
%     0.0160
% pF =
%     0.0029   % this p-value is more reliable
  
  %MATLAB Built In
   [P,TABLE,STATS] = friedman(data)
   multcompare(STATS)
  

 
 