[FieldTrip] Calculating effect size (cohens d) for an individual cluster in Fieldtrip

Schoffelen, J.M. (Jan Mathijs) janmathijs.schoffelen at donders.ru.nl
Fri Sep 26 17:49:01 CEST 2025


Hi Annie,

I am not sure whether I could completely follow your code, but what I see as problematic in your ‘attempt 2’ is that you take the mean across the first dimension, and then supply that result to the formula for the effect size.
In other words, you squeeze out the relevant dimension for the effect size calculation, shouldn’t you take the mean across the values in the cluster:

 "groupA_means = mean(groupA_data.powspctrm_cluster_only, 2);" ?

(I did not process your attempt 1 in detail).

Best wishes,
Jan-Mathijs



On 25 Sep 2025, at 23:39, Annie Menart via fieldtrip <fieldtrip at science.ru.nl> wrote:

Dear Fieldtrippers,
We've been trying to calculate the effect size for the average over the significant cluster (we only have one from our cluster-based permutation analysis) that we've identified. We've looked at the section "Computing the effect size for the average over the cluster"  (https://www.fieldtriptoolbox.org/example/stats/effectsize/#:~:text=The%20effect%20size%20is%201.47,from%20350%20to%20550%20milliseconds.<https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.fieldtriptoolbox.org%2Fexample%2Fstats%2Feffectsize%2F%23%3A~%3Atext%3DThe%2520effect%2520size%2520is%25201.47%2Cfrom%2520350%2520to%2520550%2520milliseconds.&data=05%7C02%7Cfieldtrip%40science.ru.nl%7Cd6191f13761248dc648c08ddfd1436b9%7C084578d9400d4a5aa7c7e76ca47af400%7C1%7C0%7C638944985447414452%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=3hXAnAttEVnDCIU%2BYYQflJc%2BfUmcsjNV7wmTTWN%2BZcw%3D&reserved=0>) and have attempted to implement it in the section of the code "ATTEMPT 2" shown below and got a cohens d of 4.6582, which was a lot higher than expected, so we don't think it's correct.
To solve this, we tried an alternative method to calculate cohens d using ft_freqstatistics, as shown in the section of code "ATTEMPT 1." The outputted cohens d values (shown in the commented sections) seem much more reasonable, but we're not sure that the code makes sense.
Can anyone provide input on calculating effect size over cluster and if either of these attempts at coding it seem accurate?

Here is our code:
%% CALCULATE COHENS D
% get cluster
cluster = squeeze(stat.posclusterslabelmat)==1;

% get the indices for the cluster
cluster_inds = find(cluster);
cluster_inds_size = size(cluster_inds, 1);

% GROUP A
% get number of participants
num_parts_groupA = size(groupA_data.powspctrm,1);

% create empty matrix
groupA_powspctrm_cluster_only = zeros(num_parts_groupA, cluster_inds_size);

% iterate through each participant
for ind=1:num_parts_groupA
% extract their powspctrm matrix
part_mat = squeeze(groupA_data.powspctrm(ind, :, :, :));
% extract the values in the cluster
part_vals_cluster = part_mat(cluster_inds);
% add participant to list of values for all participants
groupA_powspctrm_cluster_only(ind, :) = part_vals_cluster;
End

% GROUP B
% get number of participants
num_parts_groupB = size(groupB_data.powspctrm,1);

% create empty matrix
groupB_powspctrm_cluster_only = zeros(num_parts_groupB, cluster_inds_size);

% iterate through each participant
for ind=1:num_parts_groupB
% extract their powspctrm matrix
part_mat = squeeze(groupB_data.powspctrm(ind, :, :, :));

% extract the values in the cluster
part_vals_cluster = part_mat(cluster_inds);

% add participant to list of values for all participants
groupB_powspctrm_cluster_only(ind, :) = part_vals_cluster;
End

% add back to group_data
groupA_data.powspctrm_cluster_only = groupA_powspctrm_cluster_only;
groupB_data.powspctrm_cluster_only = groupB_powspctrm_cluster_only;
groupA_data.powspctrm_cluster_onlydimord = 'subj_comp';
groupB_data.powspctrm_cluster_onlydimord = 'subj_comp';

% ATTEMPT 1: Calculate effect size (Cohen's d) for the cluster
cfg = [];
cfg.method = 'analytic';
cfg.statistic = 'cohensd';
cfg.parameter = 'powspctrm_cluster_only';
N_groupA = size(groupA_data.powspctrm_cluster_only, 1);
N_groupB = size(groupB_data.powspctrm_cluster_only, 1);
Nsubj = N_groupA + N_groupB;
design = zeros(1, Nsubj);
design(1,1:N_groupA) = 1;
design(1,N_groupA+1:Nsubj) = 2;
cfg.design = design;
cfg.ivar = 1;

cohensD_output = ft_freqstatistics(cfg, groupA_data, groupB_data);
cohensD_avg = mean(cohensD_output.cohensd); % 0.3958
cohensD_max = max(cohensD_output.cohensd); % 0.5312

%% ATTEMPT 2: https://www.fieldtriptoolbox.org/example/stats/effectsize/<https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.fieldtriptoolbox.org%2Fexample%2Fstats%2Feffectsize%2F&data=05%7C02%7Cfieldtrip%40science.ru.nl%7Cd6191f13761248dc648c08ddfd1436b9%7C084578d9400d4a5aa7c7e76ca47af400%7C1%7C0%7C638944985447435751%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=8gcH4iAsKj%2Bv7QRjUpyhqqQYAgDnvDyOZC42pQPAF2k%3D&reserved=0>

groupA_means = mean(groupA_data.powspctrm_cluster_only, 1);
groupB_means = mean(groupB_data.powspctrm_cluster_only, 1);

x1= groupA_means;
x2 = groupB_means;
cohensd = mean(x1-x2) ./ std(x1-x2); % we get value 4.6582
Thanks!

Annie


_______________________________________________
fieldtrip mailing list
https://mailman.science.ru.nl/mailman/listinfo/fieldtrip<https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmailman.science.ru.nl%2Fmailman%2Flistinfo%2Ffieldtrip&data=05%7C02%7Cfieldtrip%40science.ru.nl%7Cd6191f13761248dc648c08ddfd1436b9%7C084578d9400d4a5aa7c7e76ca47af400%7C1%7C0%7C638944985447445626%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=ZkCixyvwA%2FIxVDsaD3uug6M3GFzltMuAp8efW4Ph4xY%3D&reserved=0>
https://doi.org/10.1371/journal.pcbi.1002202<https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fdoi.org%2F10.1371%2Fjournal.pcbi.1002202&data=05%7C02%7Cfieldtrip%40science.ru.nl%7Cd6191f13761248dc648c08ddfd1436b9%7C084578d9400d4a5aa7c7e76ca47af400%7C1%7C0%7C638944985447455079%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=rcK2aENC0BR9eJp6CjLDI1rH4KBdEK281D3vuA%2BYi0I%3D&reserved=0>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.science.ru.nl/pipermail/fieldtrip/attachments/20250926/ce3ce655/attachment.htm>


More information about the fieldtrip mailing list