clear all
close all
%

 disp('Power Calculation for ANOVA')
 lw = 3; 
 set(0, 'DefaultAxesFontSize', 16);
 fs = 16;
 msize = 10;
%

%Suppose $k=4$ treatment
%means are to be compared at a significance level of $\alpha = 0.05.$
%The experimenter is to decide how many replicates $n$ to run at each
%level, so that the null  hypothesis is rejected with a
%probability of at least 0.9 if $f^2 = 0.0625,$ 
%or if $\sum_i \alpha^2_i$ is equal to the overall $\sigma^2.$

1-ncfcdf( finv(1-0.05, 4-1, 4*60-4), 4-1, 4*60-4, 15)  %0.9122
 
%%
% Assume two factor fixed effect ANOVA. Test for factor A
% at a=4 levels is to be evaluated for its power. 
% Factor B  has b=3 levels.
% Assume balanced design with 4 x 3 cells, with n=20 subjects in
% each cell. Total number of subjects is N = 3 x 4 x 20 = 240.
% 
% For alpha = 0.05, medium effect size f^2 = 0.0625, and
% lambda = N f^2 = 20*4*3*0.0625 = 15, the power is 0.9075.
% 
1-ncfcdf( finv(1-0.05, 4-1,  4*3*(20-1)),...
    4-1, 4*3*(20-1), 15)  %0.9075
%%
  close all
  k=4;  %number of treatments}
  alpha = 0.05;  % significance level} 
  y=[];  %set values of power for n 
  for n=2:100
   y =[y 1-ncfcdf(finv(1-alpha, k-1, k*(n-1)), ...
                              k-1, k*(n-1), n/4)];
   end
  plot(2:100, y,'b-','linewidth',3)
  hold on
  xlabel('Group sample size n'); ylabel('Power')
  print -depsc 'C:\BESTAT\ANOVA\ANOVAeps\powermat.eps'

 
 
 