function   [pvae, pvaa, n, pluses, ties] = signtst( data1, data2 , tietreat)
% ------------------------------------------------------------------------------------
% [pvae, pvaa, n, numberpluses, ties] = signtest( data1, data2 , tietreat)
% data1 and data2 are inputs of the same length
% tietreat = 'R' randomizing, tierteat= 'C' conservative (least favorable 
% assignment  for H_0, tietreat = 'I', or omitted, ignore ties.
%-------------------------------------------------------------------------------------
  if nargin < 3 
      tietreat = 'I';
  end
data1 = data1(:)' ;          % convert sample 1 to a row vector
n = length( data1 );       % n  is the size of the first sample, data1
data2 = data2(:)' ;          % convert sample 2 to a row vector
  m = length( data2 );    % m is the size of the second sample, data2
if (n == m)
 pluses = sum( (data1 > data2 ) );
 minuses = sum( (data1 < data2) );
 ties = n - pluses - minuses;
 %----------------------------------------------------------------------
    if (ties>0 && (tietreat == 'R' || tietreat =='r'))
       pluses = pluses + sum(rand(1,ties) > 0.5);
  %randomize
    elseif (ties>0 && (tietreat == 'C' || tietreat == 'c'))
       pluses = pluses +  ties;
       minuses = minuses + ties;
       pluses = min( [pluses, minuses, n-pluses, n-minuses ]');
   %be conservative
    else
        n=n-ties;
    end
 pluses = min([ pluses, n-pluses ]');
 pvae = 2 * binocdf(pluses, n, 0.5);
 pvaa = 2 * normcdf( (pluses - n/2 + 0.5)/sqrt(n/4) );
else
    error('sample sizes should be the same')
end
%-------------------------------------------------------------------------------------
% Copyright (C) 2002 by GaTech
% Ver 0.1   9/28/2002