Jamie Johnston johnston at KIN.UCALGARY.CA
Wed May 6 20:51:03 CEST 2009

I have fixed the issues in my posting below and seem to be getting closer to having my code run.  However, I still run into a dimension mismatch in clusterstat...I'm trying to understand the following code, where I think this mismatch occurs.  
% determine whether the input represents N-D volumetric data or channel-freq-time data and provide the appropriate details
% TODO this detection should be more robust
if isfield(cfg, 'neighbours') && ~isempty(cfg.neighbours)
channeighbstructmat = makechanneighbstructmat(cfg);
issource = 0;
issource = 1;
% cfg contains dim and inside that are needed for reshaping the data to a volume, and inside should behave as a index vector
cfg = fixinside(cfg, 'index');
The dimension of the data (statobs) that is passed to clusterstat is 520 X 1 (10 channels of coherence differences x 52 frequencies concatenated; note: this use to be 5 channels of powspctrm) which makes sense.  I am trying to cluster by frequency (not channel)...the output of "fixinside" function returns cfg.index of dimension (1 X 260), causing a dimension mismatch at line 188 in clusterstat.  The reason this happens, I think, is in line 37 of fixinside where
elseif isfield(source, 'dim')
source.inside = 1:prod(source.dim);
source.outside = [];
for some reason source.dim still has the dimension 5 X 52 X 1, ie, the dimensions prior to computing the coherence differences.  I'm not sure whether/where I should change the cfg.dim, which I assume should reflect the dimensions of the newly computed data matrix.


From: FieldTrip discussion list on behalf of Jamie Johnston
Sent: Sun 5/3/2009 2:12 PM
Subject: Re: [FIELDTRIP] independsamplesZcoh

Thanks for your response.
Let me clarify....I have 8 subjects (with 5 channels of data, 2 with 4 channels which I'm not using at this point)...I used only 2 subjects to test the code (for faster debugging).   I do want to do a within subject statistical test between my conditions (of which I have 3, but I am only inputting 2 into freqstatistics as I was unclear whether I could run comparisons across 3 conditions).
As you correctly point out, I did compute coherence across my 5 channels first, resulting in 10 spectra...this is what I inputted into freqstatistics, which is clearly incorrect.  This is what I thought, however I didn't know exactly what the data input should look like.
So, just to clarify, I'm running
freqanalysis - inputting raw data, returning "powandcsd'
freqdescriptives - inputting data from freqanalysis, returning 'coh'
freqgrandaverage (keep individual 'yes') - inputting data from freqdescriptives, returning a grandaverage
freqstatistics - inputting two grandaverages (for 2 conditions), using the following parameters:
%Compute Permutation Test
cfg = [];
cfg.parameter = 'cohspctrm';
cfg.method = 'montecarlo';
cfg.frequency = [0 50];
cfg.statistic = 'indepsamplesT';
cfg.correctm = 'cluster';
cfg.clusteralpha = 0.05;
cfg.clusterstatistic = 'maxsum';
cfg.minnbchan = 2;
cfg.tail = 0;
cfg.clustertail = 0;
cfg.alpha = 0.05;
cfg.numrandomization = 100;
cfg.computestat = 'yes';
subj = length(Subjects);
for i = 1:subj
  design(1,i) = i;
for i = 1:subj
  design(1,subj+i) = i;
design(2,1:subj)        = 1;
design(2,subj+1:2*subj) = 2;   = design;
cfg.uvar  = 1;
cfg.ivar  = 2;
[stat] = freqstatistics(cfg, grandavg{1}, grandavg{2});
Are you saying, I should use the output from freqanalysis (skipping freqdescriptives) as input to freqgrandaverage?  Also, should my "cfg.parameter" for the freqstatistics function still be 'cohspctrm' or 'powspctrm'?
Thanks again,
From: FieldTrip discussion list [mailto:FIELDTRIP at NIC.SURFNET.NL] On Behalf Of jan-mathijs schoffelen
Sent: May 3, 2009 1:34 PM
Subject: Re: [FIELDTRIP] independsamplesZcoh
Dear Jamie,
For me it is not exactly clear what you want to do. In an earlier message you referred to our paper using a permutation test to test for a significant difference in coherence between conditions (Maris et al 2007). Essentially, as described, this is a test which is performed within each subject allowing you to do statistical inference about the potential difference between two conditions. As such this is the type of test indepsamplesZcoh can perform, if properly used (see below). Yet, you describe using two subjects as an input. This to me sounds rather that you would like to do statistics across subjects (although 2 subjects of course is a rather low number). This is NOT something indepsamplesZcoh can do  for you. In general it is possible to test for a significant difference between two conditions across a population of subjects, but this would require a different approach. Rather than shuffling single observations across conditions PRIOR TO COMPUTING THE COHERENCE DIFFERENCE (sorry about the capitals but this is essential for what is to follow below), one would swap the sign of the Z-transformed coherence difference for a random subset of subjects prior to averaging.
It seems as if you computed coherence spectra between all unique pairs of channels (4x5)/2 and used this as an input to freqstatistics. Yet, for single subject statistics this does not make sense, because indepsamplesZcoh expects fourier-spectra in the input. Within the function Z-transformed coherence difference will be computed between all pairs of input channels (in your case this would be (9x10)/2 (even though the function does not know you provide it with the wrong input), and multiplying this number with 52 would give your 'magical' 2340).
So, indeed you should either format your data in a different way using single subject fourier spectra in two conditions as an input to freqstatistics, or using a different statistical test to test for differences across subjects.
On May 3, 2009, at 7:00 PM, Jamie Johnston wrote:

Hi all -
I am trying to run a monte carlo - indepsamplesZcoh - cluster on my data using freqstatistics.  I input two data files into freqstatistics with the cohspctrm having dimensions 2 (subjects) x 10 (channels, # of coherence spectra) x 52 (frequencies).  I run into a dimension mismatch in the function clusterstat.  If I run any other statistic (i.e., indepsamplesT) it runs fine.  When running the indepsamplesT, clusterstat receives two inputs: statobs (520x1) and statrnd (520x100). These dimensions make sense to me.  However, when running indepsamplesZcohthese two variables have dimensions 2340x1 and 2340x100, respectively.  This happens around lines 104-108 in indepsamplesZcoh.m with computation of the variables "chancmbsel" and "nnewsamples."  I'm not understanding what these variables do, but my guess is that I need to setup my data files differently in the beginning.  My understanding of what this analysis protocol does is compute the z-statistic on each channel of coherence data (10 for each data file) and then find the difference between the z-statistic for each channel across the data files. Once this is complete it runs the permutations to determine the significance.
Please verify that what I have done is correct and any help with the error I am getting would be greatly appreciated.
Jamie Johnston, Ph.D.
Assistant Professor
Faculty of Kinesiology
University of Calgary
2500 University Dr. NW
Calgary, AB
T2N 1N4
Phone: +1 (403) 220-3649
Fax: +1 (403) 284-3553
email: johnston at
The aim of this list is to facilitate the discussion between users of the FieldTrip toolbox, to share experiences and to discuss new ideas for MEG and EEG analysis.
The aim of this list is to facilitate the discussion between users of the FieldTrip toolbox, to share experiences and to discuss new ideas for MEG and EEG analysis.
The aim of this list is to facilitate the discussion between users of the FieldTrip toolbox, to share experiences and to discuss new ideas for MEG and EEG analysis.

The aim of this list is to facilitate the discussion between users of the FieldTrip  toolbox, to share experiences and to discuss new ideas for MEG and EEG analysis. See also and

More information about the fieldtrip mailing list