%% Model Statistic Analysis Function (Last Update September, 2016)
    % This function computes the statistical difference between two
    % conditions/participants while accounting for within-subjects variability.
    
    % This function was scripted according to the following book chapter: 
        % Bates, James, & Dufek (2004). Single Subject Analysis. In: N. Stergiou, Innovative analyses of human movement. Champaign, Ill. Human Kinetics.

%% Author of Function: John R. Harry, MS,CSCS
            % University of Nevada, Las Vegas, Las Vegas, NV
            % Email: harry@unlv.nevada.edu

% Function Inputs:
    % A: mean value from condition/participant 1 trials
    % B: stdev associated with A
    % a: mean value from condition/participant 2 trials
    % b: stdev associated with a
    % n = # of trials used to compute the means (to determine the critical value)
            % Note: If different # of trials were recorded for "A" versus
            % "a", "n" should reflect the fewest # of trials between "A"
            % and "a"

% Function Output:
    % 1 = The difference is significant
    % 0 = The difference is not significant
    
function h = modelstat(A,B,a,b,n)

% Note critical values for trials 1-2 are 0 (No fewer than 3 trials are
% accepted for the Model Statistic

% Note critical values are in the following order: 
    % 3-20 (in order),25,30,35,40,45,50.
crit_values = [0.0 0.0 1.6533 1.5058 1.3662 1.2408 1.1306 1.0351 0.9536 0.8857 0.8307 0.7867 0.7516 0.7234 0.7001 0.6798 0.6618 0.6458 0.6311 0.6175 0.5572 0.5097 0.4729 0.4442 0.4207 0.4000]';

df = crit_values(n); % select the critical value

% Compute Mean Standard Deviation:
MSD = sqrt(((B*B)+(b*b))/2);

% Determine the Observed Difference
OD = abs(A-a);

% Determine the Critical Difference
CD = df*MSD;

% Decision for Significance

if OD > CD % if the observed differences exceeds the critical difference
    h = 1; % Significant Difference
    elseif OD <= CD % if the observed difference does not exceed the critical difference
      h = 0; % Not a Significant Difference
end
if h == 0
    disp('The Conditions are NOT Significantly Different (p > 0.05)')
elseif h == 1
    disp('The Conditions are Significantly Different (p < 0.05)')
end
end


