# ttest2 with a group describing independent values (statistics)

19 views (last 30 days)
Pedro Gomis on 5 Jan 2021
Edited: Jeff Miller on 9 Jan 2021
Hi,
I use Matlab ttest2 function to assess if observations from vectors x and y come from populations with different means. The function works fine if vectors are passed as arguments:
[h, p] = ttest2(x, y)
My data includes biomarker (as blood pressure) in x and y during day and night, respectively. Therefore, we assess paired groups into a matrix [x y], where:
x = [120, 110.5, 123, 140, 142, 151, 121, 119, 110, 115, 130]' % there are more observations
y = [118, 110, 120, 135, 134, 140, 120, 116, 110, 113 ,127]' % there are more observations
m = [x y];
The point is that we also want to compare unpaired or independent groups (males vs. females), at day and night. For this, a third column is included defining the group (male=0, female=1) in matrix [x y g], where
g = [0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1]' % there are more observations
m = [x y g];
Function anova1() has the functionality of including a group as second argument for each observation in x or y.
I wonder if it is possible to use ttest2 function in anova1() fashion, something like
[h, p] = ttest2(x, g) % where x contains the observation and g contains categorical values
To compare the biomarker, e.g. during night, between male and female, without rearrenge vectors
Thanks

Jeff Miller on 5 Jan 2021
Edited: Jeff Miller on 9 Jan 2021
No, it is not possible to use ttest2 as you suggest.
Your design has 2 factors: gender and time (day/night) with repeated measures on the time factor, and you can conveniently analyze it with 'fitlm' and 'ranova', something like this (note the categorical conversions added on edit):
x = [120, 110.5, 123, 140, 142, 151, 121, 119, 110, 115, 130]'; % there are more observations
y = [118, 110, 120, 135, 134, 140, 120, 116, 110, 113 ,127]'; % there are more observations
g = [0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1]'; % there are more observations
m = [x y g];
t = array2table(m,'VariableNames',{'x','y','gender'});
t.gender = categorical(t.gender);
timeDesign = table([1 2]','VariableNames',{'time'});
timeDesign.time = categorical(timeDesign.time);
rm = fitrm(t,'x-y~gender','WithinDesign',timeDesign);
ranova(rm,'WithinModel','time')
Pedro Gomis on 5 Jan 2021
I will try fitrm and repeated measures anova.