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.25^2=0.0625,
%or if sum_i alpha^2_i is equal to the overall sigma^2.
k=4; alpha = 0.05; n=60; f2 = 0.0625; lambda = k*n*f2;
pow = 1-ncfcdf( finv(1-alpha, k-1, k*n-k), k-1, k*n-k, lambda) %0.9122
%%
k=4; alpha = 0.05; f2 = 0.0625;
pf = @(n) 1-ncfcdf( finv(1-alpha, k-1,k*n-k),k-1,k*n-k, n*k*f2 ) - 0.90;
ssize = fzero(pf, 100) %57.6731
%Sample size of n=58 (per treatment) ensures the power of 90% for
%the effect size f^2=0.0625.
%%
% 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.
%
a=4; b=3; n=20; alpha = 0.05; f2=0.0625; lambda = a*b*n*f2;
pow = 1-ncfcdf( finv(1-alpha, a-1, a*b*(n-1)),...
a-1, a*b*(n-1), lambda) %0.9121
%Sample size for which the effect f2=0.0625 is
%detected with a power of 90% and alpha = 0.05.
a=4; b=3; alpha = 0.05; f2=0.0625;
pf = @(n) 1-ncfcdf( finv(1-alpha, a-1,a*b*(n-1)),a-1, a*b*(n-1), a*b*n*f2 ) - 0.90;
ssize = fzero(pf, 100) %19.2363
% sample size of 20 (by rounding 19.2363 up) ensures 90% power
% for the preassigned precision
%%
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'