0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264 function regress_results=rm_regress(gui_infiles_or_tmplt,time_wind,varargin)
0265
0266
0267
0268
0269 p=inputParser;
0270 p.addRequired('gui_infiles_or_tmplt',@(x) ischar(x) || iscell(x));
0271 p.addRequired('time_wind',@(x) isnumeric(x) && (length(x)==2) && isvector(x));
0272 p.addParamValue('predictors',[],@(x) ischar(x) || iscell(x));
0273 p.addParamValue('alpha',.05,@(x) isnumeric(x) && (length(x)==1) && isvector(x) && (x>0) && (x<1));
0274 p.addParamValue('sub_ids',[],@isnumeric);
0275 p.addParamValue('use_bins',[],@(x) isnumeric(x) && isvector(x));
0276 p.addParamValue('bsln_wind',[],@(x) isnumeric(x) && length(x)==2);
0277 p.addParamValue('freq_filt',[],@(x) isnumeric(x) && length(x)==2);
0278 p.addParamValue('n_pad',[],@(x) isnumeric(x) && length(x)==1);
0279 p.addParamValue('plot_topos',1,@isnumeric);
0280 p.addParamValue('exclude_chans',[],@(x) ischar(x) || iscell(x));
0281 p.addParamValue('include_chans',[],@(x) ischar(x) || iscell(x));
0282 p.addParamValue('output_file',[],@ischar);
0283 p.addParamValue('n_perm',5000,@(x) isscalar(x) && (x>0));
0284 p.addParamValue('tail',0,@(x) isscalar(x) && ((x==0) || (x==-1) || (x==1)));
0285 p.addParamValue('correction','perm',@(x) ischar(x) && (strcmpi(x,'Bonf-Holm') ...
0286 || strcmpi(x,'perm') || strcmpi(x,'FDR BH') || strcmpi(x,'FDR BY')));
0287 p.addParamValue('seed_state',[],@isnumeric);
0288 p.addParamValue('use_bins_and_evcode','no',@(x) ischar(x) && (strcmpi(x,'yes')) || (strcmpi(x,'no')));
0289
0290 p.parse(gui_infiles_or_tmplt,time_wind,varargin{:});
0291
0292
0293
0294 if ~isempty(p.Results.include_chans) && ~isempty(p.Results.exclude_chans)
0295 error('You cannot use BOTH ''include_chans'' and ''exclude_chans'' options.');
0296 end
0297 if ischar(p.Results.exclude_chans),
0298 exclude_chans{1}=p.Results.exclude_chans;
0299 elseif isempty(p.Results.exclude_chans)
0300 exclude_chans=[];
0301 else
0302 exclude_chans=p.Results.exclude_chans;
0303 end
0304 if ischar(p.Results.include_chans),
0305 include_chans{1}=p.Results.include_chans;
0306 elseif isempty(p.Results.include_chans)
0307 include_chans=[];
0308 else
0309 include_chans=p.Results.include_chans;
0310 end
0311 flt=p.Results.freq_filt;
0312
0313 VERBLEVEL=2;
0314 global EEG;
0315
0316
0317 infiles=get_set_infiles(gui_infiles_or_tmplt,p.Results.sub_ids);
0318
0319
0320 n_sub=length(infiles);
0321 if n_sub==1,
0322 error('You need more than one participant for repeated measures regression.');
0323 end
0324
0325 n_regress_trial=zeros(1,n_sub);
0326 for sub=1:n_sub,
0327 fprintf('Loading data for Participant %s\n',infiles{sub});
0328 EEG=pop_loadset(infiles{sub});
0329
0330
0331
0332 if time_wind(1)<EEG.times(1),
0333 error('Analysis time window (%d to %d ms) begins before earliest time point in file %s (%d ms).', ...
0334 time_wind(1),time_wind(2),infiles{sub},EEG.times(1));
0335 else
0336 start_tpt=find_tpt(time_wind(1),EEG.times);
0337 end
0338 if time_wind(2)>EEG.times(end),
0339 error('Analysis time window (%d to %d ms) extends beyond latest time point in file %s (%d ms).', ...
0340 time_wind(1),time_wind(2),infiles{sub},EEG.times(end));
0341 else
0342 end_tpt=find_tpt(time_wind(2),EEG.times);
0343 end
0344 time_ids=start_tpt:end_tpt;
0345
0346
0347
0348 ICs_removed=remove_artifact_ics(p.Results.bsln_wind,VERBLEVEL);
0349
0350
0351 if ~isempty(flt)
0352
0353 if ~isempty(flt),
0354 if max(flt)>(EEG.srate/2),
0355 error('''filt'' parameters need to be less than or equal to (sampling rate)/2 (i.e., %f).',EEG.srate/2);
0356 elseif (flt(2)==(EEG.srate/2)) && (flt(1)==0),
0357 error('If second element of ''filt'' parameter is EEG.srate/2, then the first element must be greater than 0.');
0358 elseif abs(flt(2))<=abs(flt(1)),
0359 error('Second element of ''filt'' parameters must be greater than first in absolute value.');
0360 elseif (flt(1)<0) || (flt(2)<0),
0361 if (flt(1)>=0) || (flt(2)>=0),
0362 error('BOTH parameters of ''filt'' need to be greater than or equal to zero OR need to be negative.');
0363 end
0364 if min(flt)<=(-EEG.srate/2),
0365 error('''filt'' parameters need to be greater than sampling rate/2 (i.e., -%f) when creating a stop band.',EEG.srate/2);
0366 end
0367 end
0368
0369 end
0370 EEG=butterfilt(EEG,flt,[],p.Results.n_pad);
0371 end
0372
0373
0374 if ~isempty(p.Results.bsln_wind) && isempty(ICs_removed),
0375 EEG=baseline_EEG(EEG,p.Results.bsln_wind,VERBLEVEL);
0376 end
0377
0378 n_pos_chan=EEG.nbchan;
0379 chan_ids=zeros(1,n_pos_chan);
0380
0381 if sub==1,
0382
0383 if isempty(p.Results.predictors),
0384
0385 epoch_flds=fieldnames(EEG.epoch);
0386 n_epoch_flds=length(epoch_flds);
0387 poss_pred=cell(1,n_epoch_flds);
0388 poss_pred_fullname=cell(1,n_epoch_flds);
0389
0390 fprintf('\n**** Choose Predictor(s) for Regression Analysis ****\n');
0391 ct=0;
0392 for fld_loop=1:n_epoch_flds,
0393 if ~strcmpi('event',epoch_flds{fld_loop}) && ~strcmpi('eventtype',epoch_flds{fld_loop}),
0394 ct=ct+1;
0395 poss_pred_fullname{ct}=epoch_flds{fld_loop};
0396 if strcmpi('event',epoch_flds{fld_loop}(1:5)),
0397 poss_pred{ct}=epoch_flds{fld_loop}(6:end);
0398 else
0399 poss_pred{ct}=epoch_flds{fld_loop};
0400 end
0401 fprintf('%d) %s\n',ct,poss_pred{ct});
0402 end
0403 end
0404
0405 EEG_flds=fieldnames(EEG);
0406 if ismember('rtmsec',EEG_flds) && ismember('rtbins',EEG_flds),
0407 ct=ct+1;
0408 poss_pred{ct}='Reaction Time';
0409 poss_pred_fullname{ct}='Reaction Time';
0410 fprintf('%d) %s\n',ct,poss_pred{ct});
0411 end
0412
0413 pred_ids=[];
0414 while isempty(pred_ids),
0415 pred_ids=input('Enter the number(s) of desired predictor(s): ','s');
0416 fprintf('\n');
0417 pred_ids=str2num(pred_ids);
0418 if max(pred_ids)>ct,
0419 fprintf('ERROR: There are no predictor indices greater than %d.\n',ct);
0420 pred_ids=[];
0421 elseif min(pred_ids)<1,
0422 fprintf('ERROR: All predictor indices must be greater than 0.\n');
0423 pred_ids=[];
0424 end
0425 end
0426 predictors=poss_pred_fullname(unique(pred_ids));
0427 predictor_names_neat=poss_pred(unique(pred_ids));
0428 if ismember('Reaction Time',predictors),
0429 rt_selected=1;
0430 else
0431 rt_selected=0;
0432 end
0433 else
0434
0435 epoch_flds=fieldnames(EEG.epoch);
0436 n_epoch_flds=length(epoch_flds);
0437 poss_pred=cell(1,n_epoch_flds);
0438 poss_pred_fullname=cell(1,n_epoch_flds);
0439 ct=0;
0440 for fld_loop=1:n_epoch_flds,
0441 if ~strcmpi('event',epoch_flds{fld_loop}) && ~strcmpi('eventtype',epoch_flds{fld_loop}),
0442 ct=ct+1;
0443 poss_pred_fullname{ct}=epoch_flds{fld_loop};
0444 if strcmpi('event',epoch_flds{fld_loop}(1:5)),
0445 poss_pred{ct}=epoch_flds{fld_loop}(6:end);
0446 else
0447 poss_pred{ct}=epoch_flds{fld_loop};
0448 end
0449 end
0450 end
0451
0452 EEG_flds=fieldnames(EEG);
0453 if ismember('rtmsec',EEG_flds) && ismember('rtbins',EEG_flds),
0454 ct=ct+1;
0455 poss_pred{ct}='Reaction Time';
0456 poss_pred_fullname{ct}='Reaction Time';
0457 end
0458
0459
0460
0461 if ischar(p.Results.predictors)
0462 requested_pred{1}=p.Results.predictors;
0463 else
0464 requested_pred=p.Results.predictors;
0465 end
0466 pred_ids=zeros(1,length(requested_pred));
0467 ct=0;
0468 n_poss=length(poss_pred);
0469 for dloop=1:length(requested_pred),
0470 found=0;
0471 for eloop=1:n_poss,
0472 if strcmpi(requested_pred{dloop},poss_pred{eloop}) || ...
0473 strcmpi(requested_pred{dloop},poss_pred_fullname{eloop})
0474 found=1;
0475 ct=ct+1;
0476 pred_ids(ct)=eloop;
0477 break;
0478 end
0479 end
0480 if ~found,
0481 fprintf('*** ERROR!! ***\n');
0482 fprintf('Requested predictor %s was not found in this EEG variable.\n', ...
0483 requested_pred{dloop});
0484 fprintf('Possible predictors are:\n');
0485 for floop=1:n_poss,
0486 if ~isempty(poss_pred{floop}),
0487 fprintf('%d) %s\n',floop,poss_pred{floop});
0488 end
0489 end
0490 error('Requested predictor not found.');
0491 end
0492 end
0493 predictors=poss_pred_fullname(unique(pred_ids));
0494 predictor_names_neat=poss_pred(unique(pred_ids));
0495 if ismember('Reaction Time',predictors),
0496 rt_selected=1;
0497 else
0498 rt_selected=0;
0499 end
0500 end
0501 n_pred=length(predictors);
0502
0503
0504
0505 use_chan_labels=cell(1,n_pos_chan);
0506 ct=0;
0507 if ~isempty(exclude_chans),
0508 for x=1:n_pos_chan,
0509 if ~ismember(EEG.chanlocs(x).labels,exclude_chans),
0510 ct=ct+1;
0511 chan_ids(ct)=x;
0512 use_chan_labels{ct}=EEG.chanlocs(x).labels;
0513 end
0514 end
0515 elseif ~isempty(include_chans),
0516 for x=1:n_pos_chan,
0517 if ismember(EEG.chanlocs(x).labels,include_chans),
0518 ct=ct+1;
0519 chan_ids(ct)=x;
0520 use_chan_labels{ct}=EEG.chanlocs(x).labels;
0521 end
0522 end
0523 else
0524 for x=1:n_pos_chan,
0525 use_chan_labels{x}=EEG.chanlocs(x).labels;
0526 end
0527 chan_ids=1:n_pos_chan;
0528 ct=n_pos_chan;
0529 end
0530 chan_ids=chan_ids(1:ct);
0531 use_chan_labels(ct+1:end)=[];
0532 n_chan=length(chan_ids);
0533 chanlocs=EEG.chanlocs(chan_ids);
0534
0535
0536 sub_coefs=zeros(n_chan,n_pred+1,n_sub);
0537 R_collinear=zeros(n_pred+1,n_sub)*NaN;
0538 else
0539
0540 ct=0;
0541 for a=1:n_chan,
0542 found=0;
0543 for b=1:n_pos_chan,
0544
0545
0546 if strcmpi(EEG.chanlocs(b).labels,use_chan_labels{a}),
0547 found=1;
0548 ct=ct+1;
0549 chan_ids(ct)=b;
0550 break;
0551 end
0552 end
0553 if ~found,
0554 error('Missing channel %s for participant %s.',use_chan_labels{a}, ...
0555 infiles{sub});
0556 end
0557 end
0558 chan_ids=chan_ids(1:ct);
0559 end
0560
0561 poss_prdct=fieldnames(EEG.epoch);
0562 if rt_selected,
0563 EEG_flds=fieldnames(EEG);
0564 if ismember('rtmsec',EEG_flds) && ismember('rtbins',EEG_flds),
0565 poss_prdct{1+length(poss_prdct)}='Reaction Time';
0566 end
0567 end
0568 missing_prdct=setdiff(predictors,poss_prdct);
0569 if ~isempty(missing_prdct),
0570 fprintf('ERROR: File %s is missing the following predictor(s): \n', ...
0571 infiles{sub});
0572 for a=1:length(missing_prdct),
0573 fprintf('%s \n',missing_prdct{a});
0574 end
0575 error('Regression aborted.');
0576 end
0577
0578 n_trial=length(EEG.epoch);
0579 use_trial=zeros(1,n_trial);
0580 pred_val=zeros(n_pred,n_trial);
0581 EEG_val=zeros(n_chan,n_trial);
0582 for trial=1:n_trial,
0583 n_type=length(EEG.epoch(trial).eventtype);
0584
0585 if isempty(p.Results.use_bins),
0586 use_trial(trial)=1;
0587 else
0588
0589 for typ=1:n_type,
0590 for bin=p.Results.use_bins,
0591 if strcmpi(['bin' num2str(bin)],EEG.epoch(trial).eventtype{typ}),
0592 use_trial(trial)=1;
0593 break;
0594 end
0595 end
0596 if use_trial(trial),
0597 break;
0598 end
0599 end
0600 end
0601
0602
0603 if use_trial(trial),
0604 for pred=1:n_pred,
0605 if strcmpi(predictors{pred},'Reaction Time'),
0606 n_rt_bin=length(EEG.rtbins{trial});
0607 bin_rts=zeros(1,n_rt_bin);
0608 bin_ct=0;
0609 for b=1:n_rt_bin,
0610 if ismember(EEG.rtbins{trial}(b),p.Results.use_bins),
0611 bin_ct=bin_ct+1;
0612 bin_rts(bin_ct)=EEG.rtmsec{trial}(b);
0613 end
0614 end
0615 bin_rts=bin_rts(1:bin_ct);
0616 uni_rt=unique(bin_rts);
0617 if length(uni_rt)>1
0618 error('Trial %d has more than one reaction time for the bins you''ve selected.',trial);
0619 elseif isempty(uni_rt),
0620 watchit(sprintf('Epoch %d has an empty value for reaction time. Please use NaN for missing predictor values.\n', ...
0621 trial));
0622 pred_val(pred,trial)=NaN;
0623 else
0624 pred_val(pred,trial)=uni_rt;
0625 end
0626 else
0627 cmnd=['emptyval=isempty(EEG.epoch(trial).' predictors{pred} ');'];
0628 eval(cmnd);
0629 if emptyval,
0630 watchit(sprintf('Epoch %d has an empty value for predictor %s. Please use NaN for missing predictor values.\n', ...
0631 trial,predictor_names_neat{pred}));
0632 cmnd=['pred_val(pred,trial)=NaN;'];
0633 eval(cmnd);
0634 else
0635 cmnd=['temp_pred=EEG.epoch(trial).' predictors{pred} ';'];
0636 eval(cmnd);
0637 if iscell(temp_pred),
0638 if all_same_cell_elements(temp_pred),
0639 pred_val(pred,trial)=temp_pred{1};
0640 else
0641 error('Epoch %d has multiple values for predictor %s.', ...
0642 trial,predictors{pred});
0643 end
0644 else
0645 pred_val(pred,trial)=temp_pred;
0646 end
0647 end
0648 end
0649 end
0650 for c=1:n_chan,
0651 EEG_val(c,trial)=squeeze(mean(EEG.data(chan_ids(c),time_ids,trial),2));
0652 end
0653 end
0654 end
0655
0656
0657 trial_ids=find(use_trial==1);
0658 if n_chan>1,
0659 EEG_val=EEG_val(:,trial_ids);
0660 else
0661 EEG_val=EEG_val(trial_ids);
0662 end
0663 if n_pred>1,
0664 pred_val=pred_val(:,trial_ids);
0665 else
0666 pred_val=pred_val(trial_ids);
0667 end
0668
0669
0670 if n_pred>1,
0671 has_NaN=sum(isnan(pred_val));
0672 non_NaN_ids=find(has_NaN==0);
0673 NaN_ids=find(has_NaN);
0674 pred_val=pred_val(:,non_NaN_ids);
0675 else
0676 non_NaN_ids=find(~isnan(pred_val));
0677 NaN_ids=find(isnan(pred_val));
0678 pred_val=pred_val(non_NaN_ids);
0679 end
0680 if n_chan>1,
0681 EEG_val=EEG_val(:,non_NaN_ids);
0682 else
0683 EEG_val=EEG_val(non_NaN_ids);
0684 end
0685
0686 n_nonNaN_trials=length(non_NaN_ids);
0687 n_selected_trials=length(trial_ids);
0688 if n_selected_trials~=n_nonNaN_trials,
0689 watchit(sprintf('%d of the selected trials have NaN predictor values for at least one predictor variable.', ...
0690 n_selected_trials-n_nonNaN_trials));
0691 NaN_trial_ids=trial_ids(NaN_ids);
0692 if VERBLEVEL>2,
0693 disp(['The EEG.epoch indices of those trials are:' num2str(NaN_trial_ids)]);
0694 end
0695 end
0696 n_regress_trial(sub)=n_nonNaN_trials;
0697 fprintf('Number of usable trials: %d\n',n_nonNaN_trials);
0698
0699 if ~n_nonNaN_trials,
0700 if isempty(p.Results.use_bins),
0701 error('Participant %s has no usable trials. Regression aborted.', ...
0702 infiles{sub});
0703 else
0704 error('Participant %s has no trials for Bin(s) %s. Regression aborted.', ...
0705 infiles{sub},num2str(p.Results.use_bins));
0706 end
0707 elseif n_nonNaN_trials<=n_pred,
0708 error('Participant %s has insufficient usable trials (i.e., %d) for the number of predictors in this analysis (i.e., %d plus an intercept term).', ...
0709 infiles{sub},n_nonNaN_trials,n_pred);
0710 end
0711
0712
0713 for pred_loop=1:n_pred,
0714 fprintf('%s (min/max): %f/%f\n',predictor_names_neat{pred_loop}, ...
0715 min(pred_val(pred_loop,:)),max(pred_val(pred_loop,:)));
0716 end
0717
0718
0719 for c=1:n_chan,
0720 [coeff, S_err, XTXI, R_sq, R_sq_adj, F_val, Coef_stats, y_hat, residuals]= ...
0721 mregress(EEG_val(c,:)',pred_val',1);
0722
0723
0724 sub_coefs(c,1:n_pred+1,sub)=coeff;
0725 end
0726 if sum(isnan(coeff)),
0727 error('The data for participant %s exhibits perfect collinearity and the regression coefficients cannot be determined.',infiles{sub});
0728 end
0729
0730 if n_pred>1,
0731 for a=1:n_pred,
0732 [coeffCOL, S_errCOL, XTXIcol, R_sqCOL]=mregress(pred_val(a,:)', ...
0733 pred_val(setdiff(1:n_pred,a),:)',1);
0734 R_collinear(a+1,sub)=R_sqCOL;
0735 end
0736 end
0737 fprintf('\n');
0738 end
0739
0740 regress_results.sub_coefs=sub_coefs;
0741 regress_results.mn_coefs=mean(sub_coefs,3);
0742 regress_results.se_coefs=stderr(sub_coefs,3);
0743 t_df=n_sub-1;
0744 crit_t=tinv(.975,t_df);
0745 regress_results.hi_ci=regress_results.mn_coefs+crit_t*regress_results.se_coefs;
0746 regress_results.low_ci=regress_results.mn_coefs-crit_t*regress_results.se_coefs;
0747 regress_results.t_coefs=regress_results.mn_coefs./regress_results.se_coefs;
0748 regress_results.t_df=t_df;
0749 regress_results.d_coefs=regress_results.mn_coefs./std(sub_coefs,0,3);
0750 regress_results.F_coefs=zeros(n_chan,n_pred)*NaN;
0751 regress_results.tail=p.Results.tail;
0752 regress_results.p_coefs_uncor=zeros(n_chan,n_pred)*NaN;
0753 if n_chan==1,
0754 regress_results.crctn_method='Not applicable';
0755 else
0756 regress_results.crctn_method=p.Results.correction;
0757 end
0758 regress_results.p_coefs_cor=regress_results.p_coefs_uncor;
0759 regress_results.h_coefs_cor=regress_results.p_coefs_uncor;
0760 regress_results.predictor_names=cell(1,n_pred+1);
0761 regress_results.predictor_names{1}='Intercept';
0762
0763
0764 regress_results.predictor_names(2:n_pred+1)=predictor_names_neat;
0765 regress_results.R_collinear=R_collinear;
0766 regress_results.family_alpha=p.Results.alpha;
0767 regress_results.chanlocs=chanlocs;
0768 if isempty(p.Results.use_bins),
0769 regress_results.bins='All';
0770 else
0771 regress_results.bins=p.Results.use_bins;
0772 end
0773 regress_results.time_wind=time_wind;
0774 regress_results.participant_filenames=infiles;
0775 regress_results.perm_test_info.n_perm=NaN;
0776 regress_results.perm_test_info.tmx_ptile=NaN;
0777 regress_results.perm_test_info.seed_state=NaN;
0778 regress_results.perm_test_info.est_alpha=NaN;
0779 for pred=1:n_pred+1,
0780 fprintf('Computing significance of mean coefficient(s) for predictor %s.\n', ...
0781 regress_results.predictor_names{pred});
0782 if ~isempty(p.Results.seed_state) && strcmpi(p.Results.correction,'perm');
0783 fprintf('Seeding permutation test with specified seed_state.\n');
0784 end
0785 for c=1:n_chan,
0786 if ~p.Results.tail
0787
0788 regress_results.p_coefs_uncor(c,pred)=2*(1-tcdf(abs(regress_results.t_coefs(c,pred)),n_sub-1));
0789 elseif p.Results.tail==1,
0790
0791 regress_results.p_coefs_uncor(c,pred)=(1-tcdf(regress_results.t_coefs(c,pred),n_sub-1));
0792 else
0793
0794 regress_results.p_coefs_uncor(c,pred)=tcdf(regress_results.t_coefs(c,pred),n_sub-1);
0795 end
0796 regress_results.F_coefs(c,pred)=finv(1-regress_results.p_coefs_uncor(c,pred),1,regress_results.t_df);
0797 end
0798 if n_chan==1,
0799 regress_results.p_coefs_cor(:,pred)=regress_results.p_coefs_uncor(:,pred);
0800 regress_results.h_coefs_cor(:,pred)=(regress_results.p_coefs_cor(:,pred)<p.Results.alpha);
0801 else
0802 if strcmpi(p.Results.correction,'Bonf-Holm'),
0803 [regress_results.p_coefs_cor(:,pred), regress_results.h_coefs_cor(:,pred)]= ...
0804 bonf_holm(regress_results.p_coefs_uncor(:,pred),p.Results.alpha);
0805 elseif strcmpi(p.Results.correction,'FDR BH'),
0806 [regress_results.h_coefs_cor(:,pred) dummy regress_results.p_coefs_cor(:,pred)]= ...
0807 fdr_bh(regress_results.p_coefs_uncor(:,pred),p.Results.alpha,'pdep','yes');
0808 elseif strcmpi(p.Results.correction,'FDR BY'),
0809 [regress_results.h_coefs_cor(:,pred) dummy regress_results.p_coefs_cor(:,pred)]= ...
0810 fdr_bh(regress_results.p_coefs_uncor(:,pred),p.Results.alpha,'dep','yes');
0811 else
0812 if pred>1 || isempty(p.Results.seed_state),
0813 [pval, t_orig, tmx_ptile, seed_state, est_alpha]=mxt_perm1(sub_coefs(:,pred,:), ...
0814 p.Results.n_perm,p.Results.alpha,p.Results.tail,VERBLEVEL);
0815 else
0816
0817
0818 [pval, t_orig, tmx_ptile, seed_state, est_alpha]=mxt_perm1(sub_coefs(:,pred,:), ...
0819 p.Results.n_perm,p.Results.alpha,p.Results.tail,VERBLEVEL,p.Results.seed_state);
0820 end
0821 regress_results.p_coefs_cor(:,pred)=pval;
0822 regress_results.h_coefs_cor(:,pred)=pval<p.Results.alpha;
0823 regress_results.perm_test_info.n_perm=p.Results.n_perm;
0824 regress_results.perm_test_info.tmx_ptile=tmx_ptile;
0825 if pred==1,
0826 regress_results.perm_test_info.seed_state=seed_state;
0827 end
0828 regress_results.perm_test_info.est_alpha=est_alpha;
0829 end
0830 end
0831 end
0832
0833
0834 fprintf('\n\n');
0835
0836
0837 fprintf('**ANALYSIS PARAMETERS**\n');
0838 fprintf('Number of participants: %d\n',n_sub);
0839 fprintf('Regression analysis t-score degrees of freedom: %d\n',t_df);
0840 if p.Results.tail>0,
0841 fprintf('Tail of tests: Upper\n');
0842 elseif p.Results.tail<0,
0843 fprintf('Tail of tests: Lower\n');
0844 else
0845 fprintf('Tail of tests: Two-Tailed\n');
0846 end
0847 if n_chan==1,
0848 fprintf('Multiple comparison correction method: Not Applicable\n');
0849 else
0850 if strcmpi(p.Results.correction,'perm'),
0851 fprintf('Multiple comparison correction method: Within-subjects permutation test\n');
0852 elseif strcmpi(p.Results.correction,'FDR BH'),
0853 fprintf('Benjamini & Hochberg FDR control procedure\n');
0854 elseif strcmpi(p.Results.correction,'FDR BY'),
0855 fprintf('Benjamini & Yekutieli FDR control procedure\n');
0856 else
0857 fprintf('Multiple comparison correction method: Bonferroni-Holm procedure\n');
0858 end
0859 end
0860 fprintf('Specified Family-Wise Alpha Level: %.3f\n',p.Results.alpha);
0861 if strcmpi(p.Results.correction,'perm'),
0862 fprintf('Estimated Perm Test Family-Wise Alpha Level: %.3f\n', ...
0863 regress_results.perm_test_info.est_alpha);
0864 fprintf('# of permutations used for permutation test: %d\n', ...
0865 regress_results.perm_test_info.n_perm);
0866 end
0867 fprintf('Number of channels analyzed: %d\n',n_chan);
0868 fprintf('Mean EEG amplitude measured from %d to %d ms.\n',time_wind(1), ...
0869 time_wind(2));
0870 fprintf('Mean (SD) # of trials per participant: %.1f (%.1f)\n',mean(n_regress_trial),std(n_regress_trial));
0871 fprintf('Minimum and maximum # of trials per participant: %.1f to %.1f\n',min(n_regress_trial),max(n_regress_trial));
0872 fprintf('**RESULTS (Summarized across all channels)**\n');
0873 if n_pred>1,
0874 fprintf('Predictor\t#_of_Sig_Channels\tMedian(IQR)_Collinearity\tp-values\n');
0875 for a=1:n_pred+1,
0876 mn_p=min(regress_results.p_coefs_cor(:,a));
0877 if mn_p>=p.Results.alpha,
0878
0879 if a==1,
0880
0881 fprintf('%s\t\t%d\t\t\tNA\t\t\tp>=%f\n',regress_results.predictor_names{a}, ...
0882 sum(regress_results.h_coefs_cor(:,a)),mn_p);
0883 else
0884 fprintf('%s\t\t%d\t\t\t%.2f(%.2f)\t\tp>=%f\n',regress_results.predictor_names{a}, ...
0885 sum(regress_results.h_coefs_cor(:,a)),median(R_collinear(a,:)), ...
0886 iqr(R_collinear(a,:)),mn_p);
0887 end
0888 else
0889
0890 sig_p_id=find(regress_results.p_coefs_cor(:,a)<p.Results.alpha);
0891 mx_p=max(regress_results.p_coefs_cor(sig_p_id,a));
0892 if a==1,
0893
0894 fprintf('%s\t\t%d\t\t\tNA\t\t%f>=sig p>=%f\n',regress_results.predictor_names{a}, ...
0895 sum(regress_results.h_coefs_cor(:,a)),mx_p,mn_p);
0896 else
0897 fprintf('%s\t\t%d\t\t\t%.2f(%.2f)\t%f>=sig p>=%f\n',regress_results.predictor_names{a}, ...
0898 sum(regress_results.h_coefs_cor(:,a)),median(R_collinear(a,:)), ...
0899 iqr(R_collinear(a,:)),mx_p,mn_p);
0900 end
0901 end
0902 end
0903 else
0904 fprintf('Predictor\t#_of_Sig_Channels\t\tp-values\n');
0905 for a=1:2,
0906 mn_p=min(regress_results.p_coefs_cor(:,a));
0907 if mn_p>=p.Results.alpha,
0908
0909 fprintf('%s\t\t%d\t\t\tp>=%f\n',regress_results.predictor_names{a}, ...
0910 sum(regress_results.h_coefs_cor(:,a)),mn_p);
0911 else
0912
0913 sig_p_id=find(regress_results.p_coefs_cor(:,a)<p.Results.alpha);
0914 mx_p=max(regress_results.p_coefs_cor(sig_p_id,a));
0915 fprintf('%s\t\t%d\t\t%f>=sig p>=%f\n',regress_results.predictor_names{a}, ...
0916 sum(regress_results.h_coefs_cor(:,a)),mx_p,mn_p);
0917 end
0918 end
0919 end
0920
0921
0922 if ~isempty(p.Results.output_file),
0923 [fid msg]=fopen(p.Results.output_file,'w');
0924 if fid==-1,
0925 error('Cannot create file %s for writing. According to fopen.m: %s.', ...
0926 p.Results.file,msg);
0927 else
0928
0929 fprintf(fid,'**ANALYSIS_PARAMETERS**\n');
0930 fprintf(fid,'Number of participants: %d\n',n_sub);
0931 fprintf(fid,'Regression analysis t-score degrees of freedom: %d\n',n_sub-1);
0932 if p.Results.tail>0,
0933 fprintf(fid,'Tail of tests: Upper\n');
0934 elseif p.Results.tail<0,
0935 fprintf(fid,'Tail of tests: Lower\n');
0936 else
0937 fprintf(fid,'Tail of tests: Two-Tailed\n');
0938 end
0939 if n_chan==1,
0940 fprintf(fid,'Multiple comparison correction method: Not Applicable\n');
0941 else
0942 if strcmpi(p.Results.correction,'perm'),
0943 fprintf(fid,'Multiple comparison correction method: Within-subjects permutation test\n');
0944 elseif strcmpi(p.Results.correction,'FDR BH'),
0945 fprintf(fid,'Benjamini & Hochberg FDR control procedure\n');
0946 elseif strcmpi(p.Results.correction,'FDR BY'),
0947 fprintf(fid,'Benjamini & Yekutieli FDR control procedure\n');
0948 else
0949 fprintf(fid,'Multiple comparison correction method: Bonferroni-Holm procedure\n');
0950 end
0951 end
0952 fprintf(fid,'Family-Wise Alpha Level: %.3f\n',p.Results.alpha);
0953 if strcmpi(p.Results.correction,'perm'),
0954 fprintf(fid,'Estimated Perm Test Family-Wise Alpha Level: %.3f\n', ...
0955 regress_results.perm_test_info.est_alpha);
0956 fprintf(fid,'# of permutations used for permutation test: %d\n', ...
0957 regress_results.perm_test_info.n_perm);
0958 end
0959 fprintf(fid,'Number of channels analyzed: %d\n',n_chan);
0960 fprintf(fid,'Mean EEG amplitude measured from %d to %d ms.\n',time_wind(1), ...
0961 time_wind(2));
0962 fprintf(fid,'Mean (SD) # of trials per participant: %.1f (%.1f)\n',mean(n_regress_trial),std(n_regress_trial));
0963 fprintf(fid,'Minimum and maximum # of trials per participant: %.1f to %.1f\n',min(n_regress_trial),max(n_regress_trial));
0964 fprintf(fid,'*.set_files_used_in_analysis #_of_trials_from_setfile_used\n');
0965 for a=1:n_sub,
0966 fprintf(fid,'%s %d\n',infiles{a},n_regress_trial(a));
0967 end
0968 fprintf(fid,'**REGRESSION_RESULTS_SUMMARY**\n');
0969 if n_pred>1,
0970 fprintf(fid,'Predictor\t#_of_Sig_Channels\tMedian(IQR)_Collinearity\tp-values\n');
0971 for a=1:n_pred+1,
0972 mn_p=min(regress_results.p_coefs_cor(:,a));
0973 if mn_p>=p.Results.alpha,
0974
0975 if a==1,
0976
0977 fprintf(fid,'%s\t\t%d\t\t\tNA\t\t\tp>=%f\n',regress_results.predictor_names{a}, ...
0978 sum(regress_results.h_coefs_cor(:,a)),mn_p);
0979 else
0980 fprintf(fid,'%s\t\t%d\t\t\t%.2f(%.2f)\t\tp>=%f\n',regress_results.predictor_names{a}, ...
0981 sum(regress_results.h_coefs_cor(:,a)),median(R_collinear(a,:)), ...
0982 iqr(R_collinear(a,:)),mn_p);
0983 end
0984 else
0985
0986 sig_p_id=find(regress_results.p_coefs_cor(:,a)<p.Results.alpha);
0987 mx_p=max(regress_results.p_coefs_cor(sig_p_id,a));
0988 if a==1,
0989
0990 fprintf(fid,'%s\t\t%d\t\t\tNA\t\t%f>=sig p>=%f\n',regress_results.predictor_names{a}, ...
0991 sum(regress_results.h_coefs_cor(:,a)),mx_p,mn_p);
0992 else
0993 fprintf(fid,'%s\t\t%d\t\t\t%.2f(%.2f)\t%f>=sig p>=%f\n',regress_results.predictor_names{a}, ...
0994 sum(regress_results.h_coefs_cor(:,a)),median(R_collinear(a,:)), ...
0995 iqr(R_collinear(a,:)),mx_p,mn_p);
0996 end
0997 end
0998 end
0999 else
1000 fprintf(fid,'Predictor\t#_of_Sig_Channels\t\tp-values\n');
1001 for a=1:2,
1002 mn_p=min(regress_results.p_coefs_cor(:,a));
1003 if mn_p>=p.Results.alpha,
1004
1005 fprintf(fid,'%s\t\t%d\t\t\tp>=%f\n',regress_results.predictor_names{a}, ...
1006 sum(regress_results.h_coefs_cor(:,a)),mn_p);
1007 else
1008
1009 sig_p_id=find(regress_results.p_coefs_cor(:,a)<p.Results.alpha);
1010 mx_p=max(regress_results.p_coefs_cor(sig_p_id,a));
1011 fprintf(fid,'%s\t\t%d\t\t%f>=sig p>=%f\n',regress_results.predictor_names{a}, ...
1012 sum(regress_results.h_coefs_cor(:,a)),mx_p,mn_p);
1013 end
1014 end
1015 end
1016
1017 fprintf(fid,'**REGRESSION_RESULTS_PER_CHANNEL**\n');
1018 for c=1:n_chan,
1019 fprintf(fid,'Results_for_Channel: %s \n',regress_results.chanlocs(c).labels);
1020 fprintf(fid,'Predictor\tMean_Coefficient\t95%c_CI_lower_bound\t95%c_CI_upper_bound\tt-score\tp-value(Uncorrected)\tSignificant(Corrected)\tCohen''s_d\n', ...
1021 37,37);
1022 for pred=1:n_pred+1,
1023
1024
1025
1026
1027
1028 if pred==1,
1029 pred_name='Intercept';
1030 else
1031 pred_name=predictors{pred-1};
1032 end
1033 fprintf(fid,'%s \t%f \t%f \t%f \t%.3f \t%.3f \t%d \t%.2f\n', ...
1034 pred_name,regress_results.mn_coefs(c,pred), ...
1035 regress_results.low_ci(c,pred),regress_results.hi_ci(c,pred), ...
1036 regress_results.t_coefs(c,pred),regress_results.p_coefs_uncor(c,pred), ...
1037 regress_results.h_coefs_cor(c,pred),regress_results.d_coefs(c,pred));
1038 end
1039 end
1040 fclose(fid);
1041 end
1042 end
1043
1044
1045 if p.Results.plot_topos && n_chan>1,
1046 sig_topo_coefs(regress_results);
1047 sig_topo_coefs(regress_results,'statistic','t');
1048 end
1049
1050
1051
1052
1053 function all_same=all_same_cell_elements(cell_array)
1054
1055
1056 n=length(cell_array);
1057 all_same=1;
1058 for a=2:n,
1059 if ~(isnan(cell_array{1}) && isnan(cell_array{a}))
1060 if ~isequal(cell_array{1},cell_array{a}),
1061 all_same=0;
1062 break;
1063 end
1064 end
1065 end
1066
1067