attachment:example_djm_unbalanced.m of COGNESTIC2023 - Methods
location: attachment:example_djm_unbalanced.m of COGNESTIC2023

Attachment 'example_djm_unbalanced.m'

Download

   1 function example_djm_unbalanced()
   2 %{
   3 %%%%%%%%%%%%%%%%%%%%%
   4 djm: This shows a simple group analysis for the vt mask of the AK6 dataset.
   5 It illustrates the issue with unbalanced samples, and three possible solutions.
   6 (Note this code could be made more efficient be rearranging the loops, but
   7 instead I've tried to keep it reasonably simple.)
   8 %%%%%%%%%%%%%%%%%%%%%
   9 %}
  10 
  11 %% Define data
  12 config=cosmo_config();
  13 subs={'s01','s02','s03','s04','s05','s06','s07','s08'};
  14 outputs={'accuracy','balanced_accuracy','acc_rebal'};
  15 % The first two of these are possible arguments to cosmo_crossvalidation_measure
  16 % The last one will use "accuracy" after calling "cosmo_balance_partitions"
  17 
  18 figure(10); clf(10);
  19 
  20 rng(0); % reset random number generator for reproducability
  21 
  22 for balanced=[true false]
  23   for signal=[true false]
  24     situation=sprintf('Signal=%g; balanced=%g',signal,balanced);
  25     fprintf('\n%s',situation) % display current situation to command line
  26 
  27     result=nan( numel(subs), numel(outputs) ); % djm, preallocate array
  28     for s=1:numel(subs)
  29       fprintf('.')
  30       data_path=fullfile(config.tutorial_data_path,'ak6',subs{s});
  31       data_fn=fullfile(data_path,'glm_T_stats_perrun.nii');
  32       mask_fn=fullfile(data_path,'vt_mask.nii');
  33       ds=cosmo_fmri_dataset(data_fn,'mask',mask_fn,...
  34       'targets',repmat(1:6,1,10),... % 6 types of animal
  35       'chunks',floor(((1:60)-1)/6)+1); % 10 runs
  36 
  37       % remove constant features (due to liberal masking)
  38       ds=cosmo_remove_useless_data(ds);
  39 
  40       % Assign a function handle to the cosmo_crossvalidation_measure
  41       measure=@cosmo_crossvalidation_measure;
  42 
  43       % Make a struct containing the arguments for the measure:
  44       args=struct();
  45       args.classifier=@cosmo_classify_naive_bayes; % use naive_bayes in this example because lda insists on balanced samples
  46       args.partitions=cosmo_nfold_partitioner(ds);
  47 
  48       % if signal==false, replace real data with random data
  49       if ~signal, ds.samples=randn(size(ds.samples)); end
  50 
  51       % reassign targets into two classes, and assume chance is 0.5:
  52       if balanced,
  53         ds.sa.targets=  (ds.sa.targets<4)+1; % larger versus smaller animals
  54       else
  55         ds.sa.targets=  (ds.sa.targets<3)+1; % primates vs others
  56         args.check_partitions=false; % otherwise CoSMoMVPA should spot this and return a helpful error message
  57       endif
  58 
  59       for o=1:numel(outputs)
  60 
  61         if strcmp(outputs{o},'acc_rebal')
  62           args.output='accuracy';
  63           args.partitions=cosmo_balance_partitions(args.partitions,ds);
  64         else
  65           args.output=outputs{o};
  66         endif
  67 
  68         % Apply the measure to ds, with args as second argument.
  69         ds_accuracy=measure(ds,args);
  70 
  71         % store the result as accuracy minus chance (so positve values indicate information present)
  72         result(s,o)=ds_accuracy.samples-0.5;
  73 
  74       end % next output score
  75     end % next subject
  76 
  77     % Run ttest versus chance (now zero)
  78     [h, p, ci, stat]=ttest(result);
  79 
  80     % plot results with 95% confidence intervals
  81     figure(10); subplot(2,2,signal*2+balanced+1)
  82     errorbar(mean(result),range(ci)./2,'o')
  83     set(gca,'xtick',1:numel(outputs),'xticklabel',outputs)
  84     ylabel('accuracy minus chance')
  85     title(situation)
  86     ylim([-0.2 0.5]);
  87     hold on
  88     plot(xlim,[0 0],'--')
  89     drawnow
  90 
  91     % pause to discuss
  92     %keyboard
  93 
  94   end % signal present?
  95 end %balanced samples?
  96 
  97 return

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2023-09-26 08:48:28, 451748.0 KB) [[attachment:02_lecture1_MVPA_intro.mp4]]
  • [get | view] (2023-09-26 08:51:53, 575765.6 KB) [[attachment:03_lecture2_Classification.mp4]]
  • [get | view] (2023-09-26 08:54:43, 414220.2 KB) [[attachment:08_lecture6_RSA.mp4]]
  • [get | view] (2023-09-29 06:24:49, 12159.4 KB) [[attachment:AH_slides.pptx]]
  • [get | view] (2023-09-04 12:15:23, 7769.3 KB) [[attachment:BrainStim_slides.pptx]]
  • [get | view] (2023-09-14 09:27:20, 1684.5 KB) [[attachment:COGNESTIC-23_hands-on_materials.pdf]]
  • [get | view] (2023-09-28 13:07:14, 4645.7 KB) [[attachment:COGNESTIC23-presentation_Sebenius.pdf]]
  • [get | view] (2023-09-27 08:34:11, 11017.1 KB) [[attachment:COGNESTIC23_MVPA_djm_part1.pptx]]
  • [get | view] (2023-09-27 08:34:43, 11433.3 KB) [[attachment:COGNESTIC23_MVPA_djm_part2.pptx]]
  • [get | view] (2023-09-17 12:28:39, 7416.4 KB) [[attachment:COGNESTIC_OpenCogNeuro.pdf]]
  • [get | view] (2023-09-20 20:09:47, 15.6 KB) [[attachment:COGNESTIC_exercises_2023.docx]]
  • [get | view] (2023-09-07 11:34:09, 37.8 KB) [[attachment:CamCAN Data User Agreement_COGNESTIC.docx]]
  • [get | view] (2023-09-20 16:23:31, 26025.7 KB) [[attachment:EEGMEG1-preprocessing.zip]]
  • [get | view] (2023-09-20 16:23:44, 5903.9 KB) [[attachment:EEGMEG2-sourceestimation.zip]]
  • [get | view] (2023-09-20 16:23:53, 4194.2 KB) [[attachment:EEGMEG3-timefrequency.zip]]
  • [get | view] (2023-09-20 16:24:00, 98.5 KB) [[attachment:EEGMEG4-advanced.zip]]
  • [get | view] (2023-09-20 16:24:09, 595.9 KB) [[attachment:EEGMEG5-decoding.zip]]
  • [get | view] (2023-09-25 16:12:16, 2963.2 KB) [[attachment:EMEG1_1_Measurement.pdf]]
  • [get | view] (2023-09-25 16:12:29, 2273.0 KB) [[attachment:EMEG1_2_Preprocessing.pdf]]
  • [get | view] (2023-09-25 16:12:49, 1643.3 KB) [[attachment:EMEG1_3_Averaging.pdf]]
  • [get | view] (2023-09-25 16:13:08, 2483.0 KB) [[attachment:EMEG2_1_ForwardModelling.pdf]]
  • [get | view] (2023-09-25 16:13:24, 2290.3 KB) [[attachment:EMEG2_2_MNE.pdf]]
  • [get | view] (2023-09-25 16:13:34, 2337.0 KB) [[attachment:EMEG2_3_SpatialResolution.pdf]]
  • [get | view] (2023-09-25 16:18:32, 2031.8 KB) [[attachment:EMEG2_4_Decoding.pdf]]
  • [get | view] (2023-09-25 16:32:19, 2227.7 KB) [[attachment:EMEG3_1_TimeFrequency.pdf]]
  • [get | view] (2023-09-25 16:13:58, 6451.2 KB) [[attachment:EMEG3_1_TimeFrequency.pptx]]
  • [get | view] (2023-09-25 16:15:17, 1798.5 KB) [[attachment:EMEG3_2_FunctionalConnectivity.pdf]]
  • [get | view] (2023-09-25 16:14:16, 2695.9 KB) [[attachment:EMEG3_3_AdvancedFunctionalConnectivity.pdf]]
  • [get | view] (2023-09-25 16:18:07, 467.0 KB) [[attachment:EMEG4_1_Stats.pdf]]
  • [get | view] (2023-09-25 16:18:20, 2175.9 KB) [[attachment:EMEG4_2_Multimodal.pdf]]
  • [get | view] (2023-09-28 13:00:51, 2143.2 KB) [[attachment:EMEG5_Decoding.pdf]]
  • [get | view] (2023-09-27 13:53:49, 15308.0 KB) [[attachment:EMEG5_Decoding.pptx]]
  • [get | view] (2023-09-22 09:00:49, 112.6 KB) [[attachment:Exercises_EEGMEG.pdf]]
  • [get | view] (2023-09-20 21:18:39, 1.6 KB) [[attachment:FDT_DTI_TBSS.sh]]
  • [get | view] (2023-09-20 21:18:56, 1.3 KB) [[attachment:FDT_DTI_group_QC.sh]]
  • [get | view] (2023-09-20 21:18:28, 4.1 KB) [[attachment:FDT_DTI_pipeline_LiveDemo.sh]]
  • [get | view] (2023-09-20 09:29:08, 0.9 KB) [[attachment:FSLVBM_cognestic_all.sh]]
  • [get | view] (2023-09-20 09:28:46, 2570.9 KB) [[attachment:FSLVBM_tutorials_2023.docx]]
  • [get | view] (2023-09-20 21:22:42, 1322.5 KB) [[attachment:FSL_DTI&TBSS.pdf]]
  • [get | view] (2023-09-20 21:15:13, 4129.4 KB) [[attachment:FSL_FDT_DTI_tutorials_2023.docx]]
  • [get | view] (2023-09-20 09:28:03, 1341.7 KB) [[attachment:FSL_VBM.pdf]]
  • [get | view] (2023-09-20 20:15:14, 1338.8 KB) [[attachment:FS_CorticalThickness.pdf]]
  • [get | view] (2023-09-20 20:14:28, 1.2 KB) [[attachment:FS_ROI_analysis.sh]]
  • [get | view] (2023-09-20 20:14:13, 0.1 KB) [[attachment:FS_check_location.sh]]
  • [get | view] (2023-09-20 20:14:21, 2.1 KB) [[attachment:FS_group_analysis.sh]]
  • [get | view] (2023-09-20 20:14:35, 0.9 KB) [[attachment:FS_visualising_output.sh]]
  • [get | view] (2023-09-20 20:44:04, 2211.7 KB) [[attachment:FreeSurfer_tutorials_2023.docx]]
  • [get | view] (2023-08-29 09:36:06, 108.5 KB) [[attachment:General EEGMEG Literature.pdf]]
  • [get | view] (2023-09-20 10:46:09, 585.4 KB) [[attachment:Intro_Commmand_Line_2023.docx]]
  • [get | view] (2023-09-07 11:38:14, 1243.5 KB) [[attachment:IntroductionToDiffusionMRI_I.pdf]]
  • [get | view] (2023-09-07 11:39:05, 2336.6 KB) [[attachment:IntroductionToDiffusionMRI_II.pdf]]
  • [get | view] (2023-09-07 11:36:05, 807.9 KB) [[attachment:IntroductionToGLM.pdf]]
  • [get | view] (2023-09-07 11:37:10, 2565.7 KB) [[attachment:IntroductionToMRIPhysics.pdf]]
  • [get | view] (2023-09-22 15:02:29, 1.7 KB) [[attachment:MNE-Python_datasets.ipynb]]
  • [get | view] (2023-09-20 08:40:09, 182.1 KB) [[attachment:MNE_Installation_Instructions.pdf]]
  • [get | view] (2023-09-21 21:13:14, 1046.2 KB) [[attachment:MRTrix_tractography.pdf]]
  • [get | view] (2023-09-21 21:15:15, 3.5 KB) [[attachment:MRtrix_dMRI_CSD_tractography_LiveDemo.sh]]
  • [get | view] (2023-09-21 21:15:06, 0.9 KB) [[attachment:MRtrix_dMRI_connectome_LiveDemo.sh]]
  • [get | view] (2023-09-21 21:15:26, 2.5 KB) [[attachment:MRtrix_dMRI_preprocessing_LiveDemo.sh]]
  • [get | view] (2023-09-21 21:13:32, 6635.8 KB) [[attachment:MRtrix_dMRI_tutorials_2023.docx]]
  • [get | view] (2023-09-29 15:16:34, 4133.1 KB) [[attachment:MacIntyre_COGNESTIC.pdf]]
  • [get | view] (2023-09-07 16:20:09, 1065.6 KB) [[attachment:Multimodal_DCM_cognestic_tutorial_fMRI.pdf]]
  • [get | view] (2023-09-26 13:21:22, 25.3 KB) [[attachment:Notebooks_mne_bids_pipeline.zip]]
  • [get | view] (2023-09-27 08:33:01, 3.2 KB) [[attachment:example_djm_partitions.m]]
  • [get | view] (2023-09-27 08:33:38, 3.3 KB) [[attachment:example_djm_unbalanced.m]]
  • [get | view] (2023-09-26 13:31:29, 924.8 KB) [[attachment:mne-bids-pipeline_cognestic.pdf]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.