%%%this class has output= HotelingtTest(self,M, withinOrBetween), in output
%%% output=[tsquared, df1_tsquared, df2_tsquared, F, df1,df2, p, d]; note
%%% that d is dz for within and d for between
%This class also has [n, df2, critical_F, F, lambdaF, p2]=Hotelingtpowerd(self,d,k,n,alpha,withinOrBetween)
% note that d is define as t/sqrt(2/n), so if we use within and dz, use
% d=dz*sqrt(2)
%this class has lr01= HotelingtlrfromDs(self,dob,dtie,withinOrBetween,k,n)
%it is based on an point alternative, dob and dtie are both defined by t/sqrt(2/n)
%BF01=HotelingBF01BasedonD_cauchy(self,dob, k, n, withinOrBetween,scale)
%it is based on a cauchy alternative, dob is defined by t/sqrt(2/n)
% note that all these functions can be used for any dimension data, k=1 is
% univariate
classdef collectiveHotelingTsquared
methods
function [output,raw]= HotelingtTest(self,M, withinOrBetween)
%input parameters: M should be nX(2k) matrix, the second half collumns would be the same values across rows if it is used for one sample test, withinOrBetween=1 for within (for paired or one sample), 2 for between (indepdent sample)
%output tsquared is hoteling tsquared, F is, p is p value,
% d is effect size, tsquared=d^2*n/2 when withinOrBetween=2 (between), tsquared=d^2*n when withinOrBetween=1 (within, like dz),
k=length(M(1,:))/2; %k is the dimension of multivariate. k=1 is univariate
n=length(M(:,1));
if withinOrBetween==1
Md=[]
for i=1:k
Md=[Md,M(:,i)-M(:,i+k)];
end
%Md=[M(:,1)-M(:,3),M(:,2)-M(:,4)];
COVMd=cov(Md);%unbiased cov
MeanMd=mean(Md);
dsquared=MeanMd*inv(COVMd)*MeanMd';
d=sqrt(dsquared); %d is similar to cohen's dz
tsquared=d^2*n;
rawd=MeanMd;
rawcov=COVMd;
elseif withinOrBetween==2
%M1=[M(:,1),M(:,2)];
%M2=[M(:,3),M(:,4)];
M1=M(:,1:k);
M2=M(:,k+1:end);
MeanM1=mean(M1);
MeanM2=mean(M2);
COVM1=cov(M1);
COVM2=cov(M2);
COVpool=(COVM1+COVM2)/2;
%n=length(M(:,1));
dsquared=(MeanM1-MeanM2)*inv(COVpool)*(MeanM1-MeanM2)';
d=sqrt(dsquared); %d is similar to cohen's d
tsquared=d^2*n/2;
rawd=MeanM1-MeanM2;
rawcov=COVpool;
else
print('withinOrBetween should be 1 or 2')
end
df1_tsquared=k;
df2_tsquared=withinOrBetween*(n-1);
F=tsquared*(df2_tsquared-df1_tsquared+1)/(df1_tsquared*df2_tsquared); % F(2,n-2)
df1=df1_tsquared;
df2=df2_tsquared-df1_tsquared+1;
p=1-ncfcdf(F,df1,df2,0);
% output.Hotelingtsquare=tsquared;
% output.d=d;
% output.F=F;
% output.p=p;
output=[tsquared, df1_tsquared, df2_tsquared, F, df1,df2, p, d];
raw.d=rawd,
raw.cov=rawcov;
end
function [n, df2, critical_F, F, lambdaF, p2]=Hotelingtpowerd(self,d,k,n,alpha,withinOrBetween)
%d=t*sqrt(2/N), which is sqrt(2)*dz for within design; withinOrBetween=1 for within, 2 for between
%k is the diension of data
% here d is defined as following
% Hoteling tsquared
% dsquared=d^2
% tsquared = dsquared*n/2
% so dz=dsquared/sqrt(2) if we use dz in within situation and d in
% between case
tsquared=d^2*n/2;
df1_tsquared=k;
df2_tsqaured=withinOrBetween*(n-1);
F=tsquared.*(df2_tsqaured-df1_tsquared+1)./(df1_tsquared.*df2_tsqaured); % F(2,n-2)
df1=df1_tsquared;
df2=df2_tsqaured-df1_tsquared+1;
lambdaF=tsquared; %lambdaF is sqaure of predicted t or predicted hoteling Tsquared
%lambdaF1=F*df1;
%critical_F=finv(0.95,df1,df2);
critical_F=finv(1-alpha,df1,df2);
p2=1-ncfcdf(critical_F,df1,df2,lambdaF);
%[n, df1,df2, critical_F, lambdaF, p2]
showFigure=0;
if showFigure==1
figure
plot(n,p2,'-')
end
end
function lr01= HotelingtlrfromDs(self,dob,dtie,withinOrBetween,k,n)
% alternative is a point
% lr=lrfromDs(0.5,0.6585, 2, 20)
%<