% power analysis given cohen's d, n, and a1(one tailed alpha level)
%reference  Keppel, G. & Wickens, T. D.,(2004). Design and analysis: A researcher's handbook. Pearson Prentice-Hall.
%Cohen, J. (1977). Statistical power for the behavioral sciences (revised edition).
%the relations between d (which is defined by t*sqrt(2/n) and is sqrt(2)*dz for within design) and etaSqr for two conditions, 
%F=d^2*(n/2) and
%F=etaSq/(1-etaSq)*dferror

%% given a d
close all;
clear all;

d=0.707;%0.5*sqrt(2);%observedD=observedt*sqrt(2/N), which is sqrt(2)*dz for within design; 
n=(4:1:100)'; % n is the subject number in one condition, so for between design N=2*n


withinOrBetween=1; %withinOrBetween=1 for within, 2 for between
tail=2; % tail=1 for one tail, =2 for two tails

powerAnalysis=collectivePowerAnalysis

[n, df,criticalT, observedT, p2]=powerAnalysis.powerCohensd(d, n,withinOrBetween,tail);
    %search for the closest value raltive to a prior power
%     prior_power=0.99;
%     [c, index] = min(abs(p2-prior_power));
%     Sub_NO(index)

%% given an eta squared, 
%single variable, change df (df for errors) for between or within design 
close all
clear all

etaSqr=0.058823529 % which is like cohen f=0.25 
%etaSqr=0.137931034 % which is like cohen f=0.4 
k=2; % levels

n=(4:1:100)'; %n is the subject number is each condition, so for between design N=k*n
%n=15
withinORbetween=2; %1 for within, 2 for between

powerAnalysis=collectivePowerAnalysis
[n,  dferror,critical_F, F, lambda, p2]= powerAnalysis.eta_singleVariable(etaSqr, k, n, withinORbetween);% compare to G-power in G-power use SPSS in option




%% given an eta sqaured and df for effect, 
%two way between design which means both variables are between 
close all
clear all

%configure 
%first, the range of subject numbers, 
%second, the number of group in each variable, eg 4 and 3 for 4by3 design. the one to be calcualted always is listed as
%variable A, if we are interested in the power for the varialbe with 3 groups then list groupA=3
%last whether the interested one is interaction or main effect (i.e. effect A as specified already)

n=(4:1:100)';
groupA=3
groupB=3
effectOrInteraction=1 %1 for 'main effect A', 2 for interaction
etaSqr=0.058823529 % which is like cohen f=0.25 from a specific effect for groupA

powerAnalysis=collectivePowerAnalysis
[n, dferror,critical_F, F, lambda, p2]= powerAnalysis.eta_twoBetweenVariables(etaSqr, groupA, groupB, n, effectOrInteraction);


%% %% given an eta sqaured and df for effect, 
%within design which means all variables are within if there are multiple variables
close all
clear all

%configure 
%first, the range of subject numbers, 
%second, the number of measurment in each variable, eg 4 and 3 for 4by3 design. the one to be calcualted always is listed as
%variable A, if we are interested in the power for the varialbe with 3 measurement then list measurmentA=3
%last whether the interested one is interaction or main effect (i.e. effect A as specified already)
%Note for all effect, main, interaction, df(error)= df(effect)*(n-1) , same as in one variable within design, so we can either treat it as one variable and make measurement = df(effect)+1 in the one variable within case 
%for example for 4by3 design if we are interested in interaction, then we
%can use measument=(4-1)*(3-1)+1 in the single variable power analysis

n=(4:1:100)'
measurementA=5 
measurementB=3
effectOrInteraction=2 %1 for 'effect', 2 for interaction
%
etaSqr=0.058823529 %given an eta squared=0.11 we can change it to any priori value
%finGpower=sqrt(etaTrue/(1-etaTrue))
powerAnalysis=collectivePowerAnalysis
[n, dferror,critical_F, F, lambda, p2]= powerAnalysis.eta_twoWithinVariables(etaSqr, measurementA, measurementB, n, effectOrInteraction);




%% given an eta sqaured and df for effect, 
close all
clear all

%mixed design one between and one within design  tested
           

%configure 
%first, the range of subject numbers, 
%second, the number of levels in each variable, eg 4 and 3 for 4by3 design. the between one is listed as
%variable A
%last whether the interested one is between main, within main or interaction or main effect (i.e. effect A as specified already)

n=(4:1:100)'
groupA=4
measurementB=1
effectOrInteraction=2 %%1 for 'within effect',2 for between main,  3 for interaction
etaSqr=0.058823529 %from observed partial eta squared for any specifc effect, change effectOrInteraction as well according to the effect

powerAnalysis=collectivePowerAnalysis
[n, dferror,critical_F, F, lambda, p2]= powerAnalysis.eta_twoMixedVariables(etaSqr, groupA, measurementB, n, effectOrInteraction);
