Difference between revisions of "MEG:ANOVA dans Fieldtrip"

From WikiMEG
Jump to: navigation, search
Line 3: Line 3:
  
 
''Scripts présentés lors du [[MEG:Formations_et_Animations#Mardi 26 Janvier 2016 14h|Club MEG du Mardi 26 Janvier 2016]].''<br />
 
''Scripts présentés lors du [[MEG:Formations_et_Animations#Mardi 26 Janvier 2016 14h|Club MEG du Mardi 26 Janvier 2016]].''<br />
 +
== Le script principal : ins_temporalClusterPermuTest_WithinBetween.m ==
 +
<syntaxhighlight lang="matlab">
 +
% ins_temporalClusterPermuTest_WithinBetween.m
 +
function result = ins_temporalClusterPermuTest_WithinBetween(data, def, cfg, structTemplate)
 +
% http://meg.univ-amu.fr/wiki/Main_Page %%%%%%%%%%%%%
 +
%
 +
% Nonparametric MEG data analysis with two factors (groups and conditions)
 +
% on two levels each. Permutation test based on temporal clusters on the
 +
% data grouped into regions of interest.
 +
% [Analyse non paramétrique de données MEG avec deux facteurs (groupes et
 +
% conditions) à deux niveaux chacun. Test par permutation basé sur des
 +
% clusters temporels sur les données regroupées en régions d'intérêt.]
 +
%
 +
% DEPENDANCES:
 +
% This function uses the FieldTrip MATLAB software toolbox
 +
% (http://www.fieldtriptoolbox.org/)
 +
%
 +
% USAGE:
 +
% result = ins_temporalClusterPermuTest_WithinBetween(
 +
%                              data,
 +
%                              def,
 +
%                              cfg,
 +
%                              structTemplate)
 +
%
 +
% INPUTS:
 +
% data              = array <1xN struct>
 +
%                  where N = person x roi x cond x other criterium (side)
 +
%                  example of struct : person = 'sujet01'
 +
%                                      group = 'control'
 +
%                                      condition = 'condition_A'
 +
%                                      roi = 'roi_3'
 +
%                                      side = 'left'
 +
%                                      time = <1x193 double>
 +
%                                      avgabs = <1x193 double>
 +
% def              = structure arrays with correspondance between waited
 +
%                  field names and data field names, and list of levels
 +
%                  for each categorical field. Waited field names are :
 +
%                  subject, group, cond, roi, other (other selection criterium),
 +
%                  time, value.
 +
% cfg              = configuration parameters (cf. example).
 +
% structTemplate = template to reconstruct the data structure waited by
 +
%                  the FieldTrip function ft_timelockgrandaverage.
 +
%                  (cf. http://www.fieldtriptoolbox.org/reference/ft_timelockgrandaverage)
 +
%
 +
% OUTPUTS:
 +
% result = %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 +
%
 +
% REFERENCES:
 +
% * Cluster-based permutation tests on event related fields.
 +
%  http://www.fieldtriptoolbox.org/tutorial/cluster_permutation_timelock
 +
% ________________________________
 +
% Bernard Giusiano & Sophie Chen
 +
% INSERM UMR 1106 Institut de Neurosciences des Systèmes
 +
% Oct/2015 (first version)
 +
% Oct/2015 (this version)
 +
% http://ins.univ-amu.fr
 +
 +
%% global parameters
 +
cfg.method = 'montecarlo' ;      % use the Monte Carlo Method to calculate the significance probability
 +
cfg.correctm = 'cluster';        % Apply multiple-comparison correction
 +
 +
% Interaction between groups and conditions
 +
if def.stats{1}
 +
    disp('*** 1 - Interaction between groups and conditions ***');
 +
    resultat = ins_temporalClusterPermuTest_interaction(data, def, cfg, structTemplate);
 +
    disp(['=== ' resultat ' ===']);
 +
end
 +
 +
% Main effect of conditions (within)
 +
if def.stats{2}
 +
    disp('*** 2 - Main effect of conditions (within) ***');
 +
    resultat = ins_temporalClusterPermuTest_within(data, def, cfg, structTemplate);
 +
    disp(['=== ' resultat ' ===']);
 +
end
 +
 +
% Main effect of groups (between)
 +
if def.stats{3}
 +
    disp('*** 3 - Main effect of groups (between) ***');
 +
    resultat = ins_temporalClusterPermuTest_between(data, def, cfg, structTemplate);
 +
    disp(['=== ' resultat ' ===']);
 +
end
 +
 +
% Simple effect of conditions by group
 +
if def.stats{4}
 +
    disp('*** 4 - Simple effect of conditions by group ***');
 +
    resultat = ins_temporalClusterPermuTest_within_simple(data, def, cfg, structTemplate);
 +
    disp(['=== ' resultat ' ===']);
 +
end
 +
 +
% Simple effect of groups by condition
 +
if def.stats{5}
 +
    disp('*** 5 - Simple effect of groups by condition ***');
 +
    resultat = ins_temporalClusterPermuTest_between_simple(data, def, cfg, structTemplate);
 +
    disp(['=== ' resultat ' ===']);
 +
end
 +
 +
result = 'END of script ins_temporalClusterPermuTest_WithinBetween.m';
 +
 +
</syntaxhighlight>
 +
== Exemple d'appel du script ci-dessus : RunningExample_TCPTWB.m ==
 +
<syntaxhighlight lang="matlab">
 +
% RunningExample_TCPTWB.m
 +
% Example of utilization of the function
 +
%    ins_temporalClusterPermuTest_WithinBetween.m
 +
% Bernard Giusiano & Sophie Chen - oct 2015
 +
 +
%% directory of scripts and example data :
 +
cd('D:\_Recherche\DEV_temporalClusterPermuTest_WithinBetween\scripts ins_TCPTWB');
 +
% cd('C:\Users\dr00245\Dropbox\_RECHERCHE\INS_MEG_MCI3\clustersTemporels');
 +
load('structurePorteuseDataMEG.mat');
 +
% -> variable 'structurePorteuseDataMEG' (-> paramètre structTemplate)
 +
% data loading
 +
load('mydata.mat');
 +
% makink directory for output
 +
currentFolder = pwd;
 +
outputFolder = sprintf('%s\\output', currentFolder); %%% maj dans les fonctions %%%%
 +
if ~exist(outputFolder, 'dir')
 +
  mkdir(outputFolder);
 +
end
 +
 +
%% definition of the experiment
 +
mydef.subject = 'person';
 +
mydef.list.subject = {'01','02','03','04','05','06','07','11',...
 +
    '12','13','14','15','16','17','18','19'};
 +
mydef.group = 'group';
 +
mydef.list.group = {'subject','subject','subject','subject','subject','subject','subject','subject',...
 +
    'control','control','control','control','control','control','control','control'};
 +
mydef.cond = 'condition';
 +
mydef.list.cond =  {'condition_A','condition_B'};
 +
mydef.roi = 'roi';
 +
% mydef.list.roi = {'roi_1','roi_2','roi_3','roi_4','roi_5','roi_6','roi_7'};
 +
mydef.list.roi = {'roi_1','roi_5','roi_6','roi_7'}; % select 4 roi only
 +
mydef.other = 'side';
 +
% mydef.list.other = {'L','R'};
 +
mydef.list.other = {'L'};                          % select left side only
 +
mydef.time = 'time';
 +
mydef.value = 'avgabs';
 +
 +
mydef.stats = {...  % select statistical calculations to be made (true) or not (false):
 +
    true,...    % Interaction between groups and conditions
 +
    false,...    % Main effect of conditions (within)
 +
    true,...    % Main effect of groups (between)
 +
    true,...    % Simple effect of conditions by group
 +
    false,...    % Simple effect of groups by condition
 +
    };
 +
 +
%% global parameters
 +
% for other parameters values cf. http://www.fieldtriptoolbox.org/tutorial/cluster_permutation_timelock
 +
cfg = [];
 +
cfg.latency = [-0.3 0.9];        % time interval over which the experimental
 +
                                  % conditions must be compared (in seconds) or 'all' (default = 'all')
 +
cfg.numrandomization = 10000;    % Number of draws from the permutation distribution
 +
cfg.clusteralpha = 0.05;          % alpha level of the sample-specific statistic that will be used for thresholding
 +
                                  % the values used to construct clusters
 +
cfg.tail = 0;                    % -1, 1 or 0 (default = 0); one-sided or two-sided test
 +
cfg.clusterstatistic = 'wcm';  % How to combine the single samples that belong to a cluster
 +
                                  % -> test statistic that will be evaluated under the permutation distribution
 +
%  cfg.clusterstatistic = 'maxsum', 'maxsize', 'wcm' (default = 'maxsum')
 +
%                          option 'wcm' refers to 'weighted cluster mass',
 +
%                          a statistic that combines cluster size and
 +
%                          intensity; see Hayasaka & Nichols (2004) NeuroImage
 +
%                          for details
 +
% When cfg.clusterstatistic = 'wcm':
 +
      cfg.wcm_weight = 0.5;      % cf. Hayasaka & Nichols (2004) NeuroImage (page 62)
 +
cfg.clustertail = 0;              % -1, 1 or 0 (default = 0 : two-tail test)
 +
cfg.correcttail = 'alpha';        % correct p-values or alpha-values when doing a two-sided test
 +
                                  % cf. http://www.fieldtriptoolbox.org/faq/why_should_i_use_the_cfg.correcttail_option_when_using_statistics_montecarlo
 +
cfg.alpha = 0.05;  % 0.05/10;    % alpha level of the permutation test / 10 tests
 +
% ATTENTION:
 +
% alpha SHOULD BE CORRECTED based on the number of tests
 +
% example of correction for 7 roi, 2 groups, 2 conditions, 2 sides:
 +
%  - interactions: 7 roi * 2 sides = 14 tests
 +
%  - main effects: 2 factors (groups and conditions) * 7 roi * 2 sides = 28 tests
 +
%  - simple effects: 2 groups * 2 cond * 7 roi * 2 sides = 56 tests
 +
% => 14 + 28 + 56 = 98 tests => Bonferroni's correction: 0.05/98 = 0.0005
 +
% => cfg.alpha = 0.0005;  for a global alpha threshold really equal to 0.05
 +
 +
% Other parameters:
 +
% cfg.clusterthreshold = 'nonparametric_common'; % method for single-sample threshold
 +
 +
cfg.avgoverchan = 'yes';  % average over channels => neighbours no needed
 +
cfg.neighbours  = {};      % cf. http://mailman.science.ru.nl/pipermail/fieldtrip/2013-July/006853.html
 +
 +
%% trace %%%%%%%%%%%%%%%%
 +
% traces are saved during execution of script ins_temporalClusterPermuTest_graph.m
 +
log_filename = ['./output/logProbaMin_' datestr(now, 'yyyymmdd_HHMM') '.txt'];
 +
cfg.fid = fopen(log_filename, 'wt');
 +
%%%%%%%%%%%%%%%%%%%%%%%%%
 +
%%
 +
resultat = ins_temporalClusterPermuTest_WithinBetween(mydata, mydef, cfg, structurePorteuseDataMEG);
 +
disp(['### ' resultat ' ###']);
 +
 +
disp('*** END of script RunningExample_temporalClusterPermuTest_WithinBetween.m ***');
 +
 +
%% trace %%%%%%%%%%%%%%%%
 +
fclose(cfg.fid);
 +
%%%%%%%%%%%%%%%%%%%%%%%%%
 +
</syntaxhighlight>

Revision as of 18:11, 18 April 2016

Analyse non paramétrique de données MEG à deux facteurs (groupes et conditions)

Analyse non paramétrique de données MEG avec deux facteurs (groupes et % conditions) à deux niveaux chacun. Test par permutation basé sur des clusters temporels sur les données regroupées en régions d'intérêt.

Scripts présentés lors du Club MEG du Mardi 26 Janvier 2016.

Le script principal : ins_temporalClusterPermuTest_WithinBetween.m

% ins_temporalClusterPermuTest_WithinBetween.m
function result = ins_temporalClusterPermuTest_WithinBetween(data, def, cfg, structTemplate)
% http://meg.univ-amu.fr/wiki/Main_Page %%%%%%%%%%%%%
% 
% Nonparametric MEG data analysis with two factors (groups and conditions)
% on two levels each. Permutation test based on temporal clusters on the
% data grouped into regions of interest.
% [Analyse non paramétrique de données MEG avec deux facteurs (groupes et
% conditions) à deux niveaux chacun. Test par permutation basé sur des
% clusters temporels sur les données regroupées en régions d'intérêt.]
%
% DEPENDANCES:
% This function uses the FieldTrip MATLAB software toolbox
% (http://www.fieldtriptoolbox.org/)
%
% USAGE:
% result = ins_temporalClusterPermuTest_WithinBetween(
%                               data,
%                               def,
%                               cfg,
%                               structTemplate)
%
% INPUTS:
% data              = array <1xN struct>
%                   where N = person x roi x cond x other criterium (side)
%                   example of struct : person = 'sujet01'
%                                       group = 'control'
%                                       condition = 'condition_A'
%                                       roi = 'roi_3'
%                                       side = 'left'
%                                       time = <1x193 double>
%                                       avgabs = <1x193 double>
% def               = structure arrays with correspondance between waited
%                   field names and data field names, and list of levels
%                   for each categorical field. Waited field names are :
%                   subject, group, cond, roi, other (other selection criterium),
%                   time, value.
% cfg               = configuration parameters (cf. example).
% structTemplate	= template to reconstruct the data structure waited by
%                   the FieldTrip function ft_timelockgrandaverage.
%                   (cf. http://www.fieldtriptoolbox.org/reference/ft_timelockgrandaverage)
%
% OUTPUTS:
% result = %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% REFERENCES:
% * Cluster-based permutation tests on event related fields.
%   http://www.fieldtriptoolbox.org/tutorial/cluster_permutation_timelock
% ________________________________
% Bernard Giusiano & Sophie Chen
% INSERM UMR 1106 Institut de Neurosciences des Systèmes
% Oct/2015 (first version)
% Oct/2015 (this version)
% http://ins.univ-amu.fr
 
%% global parameters
cfg.method = 'montecarlo' ;       % use the Monte Carlo Method to calculate the significance probability
cfg.correctm = 'cluster';         % Apply multiple-comparison correction
 
% Interaction between groups and conditions
if def.stats{1}
    disp('*** 1 - Interaction between groups and conditions ***');
    resultat = ins_temporalClusterPermuTest_interaction(data, def, cfg, structTemplate);
    disp(['=== ' resultat ' ===']);
end
 
% Main effect of conditions (within)
if def.stats{2}
    disp('*** 2 - Main effect of conditions (within) ***');
    resultat = ins_temporalClusterPermuTest_within(data, def, cfg, structTemplate);
    disp(['=== ' resultat ' ===']);
end
 
% Main effect of groups (between)
if def.stats{3}
    disp('*** 3 - Main effect of groups (between) ***');
    resultat = ins_temporalClusterPermuTest_between(data, def, cfg, structTemplate);
    disp(['=== ' resultat ' ===']);
end
 
% Simple effect of conditions by group
if def.stats{4}
    disp('*** 4 - Simple effect of conditions by group ***');
    resultat = ins_temporalClusterPermuTest_within_simple(data, def, cfg, structTemplate);
    disp(['=== ' resultat ' ===']);
end
 
% Simple effect of groups by condition
if def.stats{5}
    disp('*** 5 - Simple effect of groups by condition ***');
    resultat = ins_temporalClusterPermuTest_between_simple(data, def, cfg, structTemplate);
    disp(['=== ' resultat ' ===']);
end
 
result = 'END of script ins_temporalClusterPermuTest_WithinBetween.m';

Exemple d'appel du script ci-dessus : RunningExample_TCPTWB.m

% RunningExample_TCPTWB.m
% Example of utilization of the function
%    ins_temporalClusterPermuTest_WithinBetween.m
% Bernard Giusiano & Sophie Chen - oct 2015
 
%% directory of scripts and example data :
cd('D:\_Recherche\DEV_temporalClusterPermuTest_WithinBetween\scripts ins_TCPTWB');
% cd('C:\Users\dr00245\Dropbox\_RECHERCHE\INS_MEG_MCI3\clustersTemporels');
load('structurePorteuseDataMEG.mat');
% -> variable 'structurePorteuseDataMEG' (-> paramètre structTemplate)
% data loading
load('mydata.mat');
% makink directory for output
currentFolder = pwd;
outputFolder = sprintf('%s\\output', currentFolder); %%% maj dans les fonctions %%%%
if ~exist(outputFolder, 'dir')
  mkdir(outputFolder);
end
 
%% definition of the experiment
mydef.subject = 'person';
mydef.list.subject = {'01','02','03','04','05','06','07','11',...
    '12','13','14','15','16','17','18','19'};
mydef.group = 'group';
mydef.list.group = {'subject','subject','subject','subject','subject','subject','subject','subject',...
    'control','control','control','control','control','control','control','control'};
mydef.cond = 'condition';
mydef.list.cond =  {'condition_A','condition_B'};
mydef.roi = 'roi';
% mydef.list.roi = {'roi_1','roi_2','roi_3','roi_4','roi_5','roi_6','roi_7'};
mydef.list.roi = {'roi_1','roi_5','roi_6','roi_7'}; % select 4 roi only
mydef.other = 'side';
% mydef.list.other = {'L','R'};
mydef.list.other = {'L'};                           % select left side only
mydef.time = 'time';
mydef.value = 'avgabs';
 
mydef.stats = {...  % select statistical calculations to be made (true) or not (false):
    true,...    % Interaction between groups and conditions
    false,...    % Main effect of conditions (within)
    true,...    % Main effect of groups (between)
    true,...    % Simple effect of conditions by group
    false,...    % Simple effect of groups by condition
    };
 
%% global parameters
% for other parameters values cf. http://www.fieldtriptoolbox.org/tutorial/cluster_permutation_timelock
cfg = [];
cfg.latency = [-0.3 0.9];         % time interval over which the experimental 
                                  % conditions must be compared (in seconds) or 'all' (default = 'all')
cfg.numrandomization = 10000;     % Number of draws from the permutation distribution
cfg.clusteralpha = 0.05;          % alpha level of the sample-specific statistic that will be used for thresholding
                                  % the values used to construct clusters
cfg.tail = 0;                     % -1, 1 or 0 (default = 0); one-sided or two-sided test
cfg.clusterstatistic = 'wcm';  % How to combine the single samples that belong to a cluster
                                  % -> test statistic that will be evaluated under the permutation distribution
%   cfg.clusterstatistic = 'maxsum', 'maxsize', 'wcm' (default = 'maxsum')
%                          option 'wcm' refers to 'weighted cluster mass',
%                          a statistic that combines cluster size and
%                          intensity; see Hayasaka & Nichols (2004) NeuroImage
%                          for details
% When cfg.clusterstatistic = 'wcm':
      cfg.wcm_weight = 0.5;      % cf. Hayasaka & Nichols (2004) NeuroImage (page 62)
cfg.clustertail = 0;              % -1, 1 or 0 (default = 0 : two-tail test)
cfg.correcttail = 'alpha';        % correct p-values or alpha-values when doing a two-sided test
                                  % cf. http://www.fieldtriptoolbox.org/faq/why_should_i_use_the_cfg.correcttail_option_when_using_statistics_montecarlo
cfg.alpha = 0.05;  % 0.05/10;     % alpha level of the permutation test / 10 tests
% ATTENTION:
% alpha SHOULD BE CORRECTED based on the number of tests
% example of correction for 7 roi, 2 groups, 2 conditions, 2 sides:
%   - interactions: 7 roi * 2 sides = 14 tests
%   - main effects: 2 factors (groups and conditions) * 7 roi * 2 sides = 28 tests
%   - simple effects: 2 groups * 2 cond * 7 roi * 2 sides = 56 tests
% => 14 + 28 + 56 = 98 tests => Bonferroni's correction: 0.05/98 = 0.0005
% => cfg.alpha = 0.0005;  for a global alpha threshold really equal to 0.05
 
% Other parameters:
% cfg.clusterthreshold = 'nonparametric_common'; % method for single-sample threshold
 
cfg.avgoverchan = 'yes';   % average over channels => neighbours no needed
cfg.neighbours  = {};      % cf. http://mailman.science.ru.nl/pipermail/fieldtrip/2013-July/006853.html
 
%% trace %%%%%%%%%%%%%%%%
% traces are saved during execution of script ins_temporalClusterPermuTest_graph.m
log_filename = ['./output/logProbaMin_' datestr(now, 'yyyymmdd_HHMM') '.txt'];
cfg.fid = fopen(log_filename, 'wt');
%%%%%%%%%%%%%%%%%%%%%%%%%
%%
resultat = ins_temporalClusterPermuTest_WithinBetween(mydata, mydef, cfg, structurePorteuseDataMEG);
disp(['### ' resultat ' ###']);
 
disp('*** END of script RunningExample_temporalClusterPermuTest_WithinBetween.m ***');
 
%% trace %%%%%%%%%%%%%%%%
fclose(cfg.fid);
%%%%%%%%%%%%%%%%%%%%%%%%%