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_mass(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_raster',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 n_tpts=length(time_ids);
0346 if n_tpts<=1,
0347 error('This function requires a time window of interest that spans more than one time point.');
0348 end
0349
0350
0351
0352 ICs_removed=remove_artifact_ics(p.Results.bsln_wind,VERBLEVEL);
0353
0354
0355 if ~isempty(flt)
0356
0357 if ~isempty(flt),
0358 if max(flt)>(EEG.srate/2),
0359 error('''filt'' parameters need to be less than or equal to (sampling rate)/2 (i.e., %f).',EEG.srate/2);
0360 elseif (flt(2)==(EEG.srate/2)) && (flt(1)==0),
0361 error('If second element of ''filt'' parameter is EEG.srate/2, then the first element must be greater than 0.');
0362 elseif abs(flt(2))<=abs(flt(1)),
0363 error('Second element of ''filt'' parameters must be greater than first in absolute value.');
0364 elseif (flt(1)<0) || (flt(2)<0),
0365 if (flt(1)>=0) || (flt(2)>=0),
0366 error('BOTH parameters of ''filt'' need to be greater than or equal to zero OR need to be negative.');
0367 end
0368 if min(flt)<=(-EEG.srate/2),
0369 error('''filt'' parameters need to be greater than sampling rate/2 (i.e., -%f) when creating a stop band.',EEG.srate/2);
0370 end
0371 end
0372
0373 end
0374 EEG=butterfilt(EEG,flt,[],p.Results.n_pad);
0375 end
0376
0377
0378 if ~isempty(p.Results.bsln_wind) && isempty(ICs_removed),
0379 EEG=baseline_EEG(EEG,p.Results.bsln_wind,VERBLEVEL);
0380 end
0381
0382 n_pos_chan=EEG.nbchan;
0383 chan_ids=zeros(1,n_pos_chan);
0384
0385 if sub==1,
0386
0387 if isempty(p.Results.predictors),
0388
0389 epoch_flds=fieldnames(EEG.epoch);
0390 n_epoch_flds=length(epoch_flds);
0391 poss_pred=cell(1,n_epoch_flds);
0392 poss_pred_fullname=cell(1,n_epoch_flds);
0393
0394 fprintf('\n**** Choose Predictor(s) for Regression Analysis ****\n');
0395 ct=0;
0396 for fld_loop=1:n_epoch_flds,
0397 if ~strcmpi('event',epoch_flds{fld_loop}) && ~strcmpi('eventtype',epoch_flds{fld_loop}),
0398 ct=ct+1;
0399 poss_pred_fullname{ct}=epoch_flds{fld_loop};
0400 if strcmpi('event',epoch_flds{fld_loop}(1:5)),
0401 poss_pred{ct}=epoch_flds{fld_loop}(6:end);
0402 else
0403 poss_pred{ct}=epoch_flds{fld_loop};
0404 end
0405 fprintf('%d) %s\n',ct,poss_pred{ct});
0406 end
0407 end
0408
0409 EEG_flds=fieldnames(EEG);
0410 if ismember('rtmsec',EEG_flds) && ismember('rtbins',EEG_flds),
0411 ct=ct+1;
0412 poss_pred{ct}='Reaction Time';
0413 poss_pred_fullname{ct}='Reaction Time';
0414 fprintf('%d) %s\n',ct,poss_pred{ct});
0415 end
0416
0417 pred_ids=[];
0418 while isempty(pred_ids),
0419 pred_ids=input('Enter the number(s) of desired predictor(s): ','s');
0420 fprintf('\n');
0421 pred_ids=str2num(pred_ids);
0422 if max(pred_ids)>ct,
0423 fprintf('ERROR: There are no predictor indices greater than %d.\n',ct);
0424 pred_ids=[];
0425 elseif min(pred_ids)<1,
0426 fprintf('ERROR: All predictor indices must be greater than 0.\n');
0427 pred_ids=[];
0428 end
0429 end
0430 predictors=poss_pred_fullname(unique(pred_ids));
0431 predictor_names_neat=poss_pred(unique(pred_ids));
0432 if ismember('Reaction Time',predictors),
0433 rt_selected=1;
0434 else
0435 rt_selected=0;
0436 end
0437 else
0438
0439 epoch_flds=fieldnames(EEG.epoch);
0440 n_epoch_flds=length(epoch_flds);
0441 poss_pred=cell(1,n_epoch_flds);
0442 poss_pred_fullname=cell(1,n_epoch_flds);
0443 ct=0;
0444 for fld_loop=1:n_epoch_flds,
0445 if ~strcmpi('event',epoch_flds{fld_loop}) && ~strcmpi('eventtype',epoch_flds{fld_loop}),
0446 ct=ct+1;
0447 poss_pred_fullname{ct}=epoch_flds{fld_loop};
0448 if strcmpi('event',epoch_flds{fld_loop}(1:5)),
0449 poss_pred{ct}=epoch_flds{fld_loop}(6:end);
0450 else
0451 poss_pred{ct}=epoch_flds{fld_loop};
0452 end
0453 end
0454 end
0455
0456 EEG_flds=fieldnames(EEG);
0457 if ismember('rtmsec',EEG_flds) && ismember('rtbins',EEG_flds),
0458 ct=ct+1;
0459 poss_pred{ct}='Reaction Time';
0460 poss_pred_fullname{ct}='Reaction Time';
0461 end
0462
0463
0464
0465 if ischar(p.Results.predictors)
0466 requested_pred{1}=p.Results.predictors;
0467 else
0468 requested_pred=p.Results.predictors;
0469 end
0470 pred_ids=zeros(1,length(requested_pred));
0471 ct=0;
0472 n_poss=length(poss_pred);
0473 for dloop=1:length(requested_pred),
0474 found=0;
0475 for eloop=1:n_poss,
0476 if strcmpi(requested_pred{dloop},poss_pred{eloop}) || ...
0477 strcmpi(requested_pred{dloop},poss_pred_fullname{eloop})
0478 found=1;
0479 ct=ct+1;
0480 pred_ids(ct)=eloop;
0481 break;
0482 end
0483 end
0484 if ~found,
0485 fprintf('*** ERROR!! ***\n');
0486 fprintf('Requested predictor %s was not found in this EEG variable.\n', ...
0487 requested_pred{dloop});
0488 fprintf('Possible predictors are:\n');
0489 for floop=1:n_poss,
0490 if ~isempty(poss_pred{floop}),
0491 fprintf('%d) %s\n',floop,poss_pred{floop});
0492 end
0493 end
0494 error('Requested predictor not found.');
0495 end
0496 end
0497 predictors=poss_pred_fullname(unique(pred_ids));
0498 predictor_names_neat=poss_pred(unique(pred_ids));
0499 if ismember('Reaction Time',predictors),
0500 rt_selected=1;
0501 else
0502 rt_selected=0;
0503 end
0504 end
0505 n_pred=length(predictors);
0506
0507
0508
0509 use_chan_labels=cell(1,n_pos_chan);
0510 ct=0;
0511 if ~isempty(exclude_chans),
0512 for x=1:n_pos_chan,
0513 if ~ismember(EEG.chanlocs(x).labels,exclude_chans),
0514 ct=ct+1;
0515 chan_ids(ct)=x;
0516 use_chan_labels{ct}=EEG.chanlocs(x).labels;
0517 end
0518 end
0519 elseif ~isempty(include_chans),
0520 for x=1:n_pos_chan,
0521 if ismember(EEG.chanlocs(x).labels,include_chans),
0522 ct=ct+1;
0523 chan_ids(ct)=x;
0524 use_chan_labels{ct}=EEG.chanlocs(x).labels;
0525 end
0526 end
0527 else
0528 for x=1:n_pos_chan,
0529 use_chan_labels{x}=EEG.chanlocs(x).labels;
0530 end
0531 chan_ids=1:n_pos_chan;
0532 ct=n_pos_chan;
0533 end
0534 chan_ids=chan_ids(1:ct);
0535 use_chan_labels(ct+1:end)=[];
0536 n_chan=length(chan_ids);
0537 chanlocs=EEG.chanlocs(chan_ids);
0538
0539
0540 sub_coefs=zeros(n_chan,n_tpts,n_pred+1,n_sub);
0541 R_collinear=zeros(n_pred+1,n_sub)*NaN;
0542 else
0543
0544 ct=0;
0545 for a=1:n_chan,
0546 found=0;
0547 for b=1:n_pos_chan,
0548
0549
0550 if strcmpi(EEG.chanlocs(b).labels,use_chan_labels{a}),
0551 found=1;
0552 ct=ct+1;
0553 chan_ids(ct)=b;
0554 break;
0555 end
0556 end
0557 if ~found,
0558 error('Missing channel %s for participant %s.',use_chan_labels{a}, ...
0559 infiles{sub});
0560 end
0561 end
0562 chan_ids=chan_ids(1:ct);
0563 end
0564
0565 poss_prdct=fieldnames(EEG.epoch);
0566 if rt_selected,
0567 EEG_flds=fieldnames(EEG);
0568 if ismember('rtmsec',EEG_flds) && ismember('rtbins',EEG_flds),
0569 poss_prdct{1+length(poss_prdct)}='Reaction Time';
0570 end
0571 end
0572 missing_prdct=setdiff(predictors,poss_prdct);
0573 if ~isempty(missing_prdct),
0574 fprintf('ERROR: File %s is missing the following predictor(s): \n', ...
0575 infiles{sub});
0576 for a=1:length(missing_prdct),
0577 fprintf('%s \n',missing_prdct{a});
0578 end
0579 error('Regression aborted.');
0580 end
0581
0582 n_trial=length(EEG.epoch);
0583 use_trial=zeros(1,n_trial);
0584 pred_val=zeros(n_pred,n_trial);
0585 EEG_val=zeros(n_chan,n_tpts,n_trial);
0586 for trial=1:n_trial,
0587 n_type=length(EEG.epoch(trial).eventtype);
0588
0589 if isempty(p.Results.use_bins),
0590 use_trial(trial)=1;
0591 else
0592
0593 for typ=1:n_type,
0594 for bin=p.Results.use_bins,
0595 if strcmpi(['bin' num2str(bin)],EEG.epoch(trial).eventtype{typ}),
0596 use_trial(trial)=1;
0597 break;
0598 end
0599 end
0600 if use_trial(trial),
0601 break;
0602 end
0603 end
0604 end
0605
0606
0607 if use_trial(trial),
0608 for pred=1:n_pred,
0609 if strcmpi(predictors{pred},'Reaction Time'),
0610 n_rt_bin=length(EEG.rtbins{trial});
0611 bin_rts=zeros(1,n_rt_bin);
0612 bin_ct=0;
0613 for b=1:n_rt_bin,
0614 if ismember(EEG.rtbins{trial}(b),p.Results.use_bins),
0615 bin_ct=bin_ct+1;
0616 bin_rts(bin_ct)=EEG.rtmsec{trial}(b);
0617 end
0618 end
0619 bin_rts=bin_rts(1:bin_ct);
0620 uni_rt=unique(bin_rts);
0621 if length(uni_rt)>1
0622 error('Trial %d has more than one reaction time for the bins you''ve selected.',trial);
0623 elseif isempty(uni_rt),
0624 watchit(sprintf('Epoch %d has an empty value for reaction time. Please use NaN for missing predictor values.\n', ...
0625 trial));
0626 pred_val(pred,trial)=NaN;
0627 else
0628 pred_val(pred,trial)=uni_rt;
0629 end
0630 else
0631 cmnd=['emptyval=isempty(EEG.epoch(trial).' predictors{pred} ');'];
0632 eval(cmnd);
0633 if emptyval,
0634 watchit(sprintf('Epoch %d has an empty value for predictor %s. Please use NaN for missing predictor values.\n', ...
0635 trial,predictor_names_neat{pred}));
0636 cmnd=['pred_val(pred,trial)=NaN;'];
0637 eval(cmnd);
0638 else
0639 cmnd=['temp_pred=EEG.epoch(trial).' predictors{pred} ';'];
0640 eval(cmnd);
0641 if iscell(temp_pred),
0642 if all_same_cell_elements(temp_pred),
0643 pred_val(pred,trial)=temp_pred{1};
0644 else
0645 error('Epoch %d has multiple values for predictor %s.', ...
0646 trial,predictors{pred});
0647 end
0648 else
0649 pred_val(pred,trial)=temp_pred;
0650 end
0651 end
0652 end
0653 end
0654 for c=1:n_chan,
0655 EEG_val(c,:,trial)=EEG.data(chan_ids(c),time_ids,trial);
0656 end
0657 end
0658 end
0659
0660
0661 trial_ids=find(use_trial==1);
0662 if n_chan>1,
0663 EEG_val=EEG_val(:,:,trial_ids);
0664 else
0665 EEG_val=EEG_val(:,trial_ids);
0666 end
0667 if n_pred>1,
0668 pred_val=pred_val(:,trial_ids);
0669 else
0670 pred_val=pred_val(trial_ids);
0671 end
0672
0673
0674 if n_pred>1,
0675 has_NaN=sum(isnan(pred_val));
0676 non_NaN_ids=find(has_NaN==0);
0677 NaN_ids=find(has_NaN);
0678 pred_val=pred_val(:,non_NaN_ids);
0679 else
0680 non_NaN_ids=find(~isnan(pred_val));
0681 NaN_ids=find(isnan(pred_val));
0682 pred_val=pred_val(non_NaN_ids);
0683 end
0684 if n_chan>1,
0685 EEG_val=EEG_val(:,:,non_NaN_ids);
0686 else
0687 EEG_val=EEG_val(:,non_NaN_ids);
0688 end
0689
0690 n_nonNaN_trials=length(non_NaN_ids);
0691 n_selected_trials=length(trial_ids);
0692 if n_selected_trials~=n_nonNaN_trials,
0693 watchit(sprintf('%d of the selected trials have NaN predictor values for at least one predictor variable.', ...
0694 n_selected_trials-n_nonNaN_trials));
0695 NaN_trial_ids=trial_ids(NaN_ids);
0696 if VERBLEVEL>2,
0697 disp(['The EEG.epoch indices of those trials are:' num2str(NaN_trial_ids)]);
0698 end
0699 end
0700 n_regress_trial(sub)=n_nonNaN_trials;
0701 fprintf('Number of usable trials: %d\n',n_nonNaN_trials);
0702
0703 if ~n_nonNaN_trials,
0704 if isempty(p.Results.use_bins),
0705 error('Participant %s has no usable trials. Regression aborted.', ...
0706 infiles{sub});
0707 else
0708 error('Participant %s has no trials for Bin(s) %s. Regression aborted.', ...
0709 infiles{sub},num2str(p.Results.use_bins));
0710 end
0711 elseif n_nonNaN_trials<=n_pred,
0712 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).', ...
0713 infiles{sub},n_nonNaN_trials,n_pred);
0714 end
0715
0716
0717 for pred_loop=1:n_pred,
0718 fprintf('%s (min/max): %f/%f\n',predictor_names_neat{pred_loop}, ...
0719 min(pred_val(pred_loop,:)),max(pred_val(pred_loop,:)));
0720 end
0721
0722
0723 for c=1:n_chan,
0724 for tp=1:n_tpts,
0725 [coeff, S_err, XTXI, R_sq, R_sq_adj, F_val, Coef_stats, y_hat, residuals]= ...
0726 mregress(squeeze(EEG_val(c,tp,:)),pred_val',1);
0727
0728
0729 sub_coefs(c,tp,1:n_pred+1,sub)=coeff;
0730 end
0731 end
0732 if sum(isnan(coeff)),
0733 error('The data for participant %s exhibits perfect collinearity and the regression coefficients cannot be determined.',infiles{sub});
0734 end
0735
0736 if n_pred>1,
0737 for a=1:n_pred,
0738 [coeffCOL, S_errCOL, XTXIcol, R_sqCOL]=mregress(pred_val(a,:)', ...
0739 pred_val(setdiff(1:n_pred,a),:)',1);
0740 R_collinear(a+1,sub)=R_sqCOL;
0741 end
0742 end
0743 fprintf('\n');
0744 end
0745
0746 regress_results.sub_coefs=sub_coefs;
0747 regress_results.mn_coefs=mean(sub_coefs,4);
0748 regress_results.se_coefs=stderr(sub_coefs,4);
0749 t_df=n_sub-1;
0750 crit_t=tinv(.975,t_df);
0751 regress_results.hi_ci=regress_results.mn_coefs+crit_t*regress_results.se_coefs;
0752 regress_results.low_ci=regress_results.mn_coefs-crit_t*regress_results.se_coefs;
0753 regress_results.t_coefs=regress_results.mn_coefs./regress_results.se_coefs;
0754 regress_results.t_df=t_df;
0755 regress_results.d_coefs=regress_results.mn_coefs./std(sub_coefs,0,4);
0756 regress_results.F_coefs=zeros(n_chan,n_pred)*NaN;
0757 regress_results.tail=p.Results.tail;
0758 regress_results.p_coefs_uncor=zeros(n_chan,n_tpts,n_pred)*NaN;
0759 if n_chan==1,
0760 regress_results.crctn_method='Not applicable';
0761 else
0762 regress_results.crctn_method=p.Results.correction;
0763 end
0764 regress_results.p_coefs_cor=regress_results.p_coefs_uncor;
0765 regress_results.h_coefs_cor=regress_results.p_coefs_uncor;
0766 regress_results.predictor_names=cell(1,n_pred+1);
0767 regress_results.predictor_names{1}='Intercept';
0768
0769
0770 regress_results.predictor_names(2:n_pred+1)=predictor_names_neat;
0771 regress_results.R_collinear=R_collinear;
0772 regress_results.family_alpha=p.Results.alpha;
0773 regress_results.chanlocs=chanlocs;
0774 if isempty(p.Results.use_bins),
0775 regress_results.bins='All';
0776 else
0777 regress_results.bins=p.Results.use_bins;
0778 end
0779 regress_results.time_wind=time_wind;
0780 regress_results.participant_filenames=infiles;
0781 regress_results.perm_test_info.n_perm=NaN;
0782 regress_results.perm_test_info.tmx_ptile=NaN;
0783 regress_results.perm_test_info.seed_state=NaN;
0784 regress_results.perm_test_info.est_alpha=NaN;
0785 for pred=1:n_pred+1,
0786 fprintf('Computing significance of mean coefficient(s) for predictor %s.\n', ...
0787 regress_results.predictor_names{pred});
0788 if ~isempty(p.Results.seed_state) && strcmpi(p.Results.correction,'perm');
0789 fprintf('Seeding permutation test with specified seed_state.\n');
0790 end
0791 for c=1:n_chan,
0792 for tp=1:n_tpts,
0793 if ~p.Results.tail
0794
0795 regress_results.p_coefs_uncor(c,tp,pred)=2*(1-tcdf(abs(regress_results.t_coefs(c,tp,pred)),n_sub-1));
0796 elseif p.Results.tail==1,
0797
0798 regress_results.p_coefs_uncor(c,tp,pred)=(1-tcdf(regress_results.t_coefs(c,tp,pred),n_sub-1));
0799 else
0800
0801 regress_results.p_coefs_uncor(c,tp,pred)=tcdf(regress_results.t_coefs(c,tp,pred),n_sub-1);
0802 end
0803 regress_results.F_coefs(c,tp,pred)=finv(1-regress_results.p_coefs_uncor(c,tp,pred),1,regress_results.t_df);
0804 end
0805 end
0806 if strcmpi(p.Results.correction,'Bonf-Holm'),
0807 [regress_results.p_coefs_cor(:,:,pred), regress_results.h_coefs_cor(:,:,pred)]= ...
0808 bonf_holm(squeeze(regress_results.p_coefs_uncor(:,:,pred)),p.Results.alpha);
0809 elseif strcmpi(p.Results.correction,'FDR BH'),
0810 [regress_results.h_coefs_cor(:,:,pred) , dummy, regress_results.p_coefs_cor(:,:,pred)]= ...
0811 fdr_bh(squeeze(regress_results.p_coefs_uncor(:,:,pred)),p.Results.alpha,'pdep','yes');
0812 elseif strcmpi(p.Results.correction,'FDR BY'),
0813 [regress_results.h_coefs_cor(:,:,pred) dummy regress_results.p_coefs_cor(:,:,pred)]= ...
0814 fdr_bh(squeeze(regress_results.p_coefs_uncor(:,:,pred)),p.Results.alpha,'dep','yes');
0815 else
0816 if pred>1 || isempty(p.Results.seed_state),
0817 [pval, t_orig, tmx_ptile, seed_state, est_alpha]=mxt_perm1(squeeze(sub_coefs(:,:,pred,:)), ...
0818 p.Results.n_perm,p.Results.alpha,p.Results.tail,VERBLEVEL);
0819 else
0820
0821
0822 [pval, t_orig, tmx_ptile, seed_state, est_alpha]=mxt_perm1(squeeze(sub_coefs(:,:,pred,:)), ...
0823 p.Results.n_perm,p.Results.alpha,p.Results.tail,VERBLEVEL,p.Results.seed_state);
0824 end
0825 regress_results.p_coefs_cor(:,:,pred)=pval;
0826 regress_results.h_coefs_cor(:,:,pred)=pval<p.Results.alpha;
0827 regress_results.perm_test_info.n_perm=p.Results.n_perm;
0828 regress_results.perm_test_info.tmx_ptile=tmx_ptile;
0829 if pred==1,
0830 regress_results.perm_test_info.seed_state=seed_state;
0831 end
0832 regress_results.perm_test_info.est_alpha=est_alpha;
0833 end
0834 end
0835
0836
0837 fprintf('\n\n');
0838
0839
0840 fprintf('**ANALYSIS PARAMETERS**\n');
0841 fprintf('Number of participants: %d\n',n_sub);
0842 fprintf('Regression analysis t-score degrees of freedom: %d\n',t_df);
0843 if p.Results.tail>0,
0844 fprintf('Tail of tests: Upper\n');
0845 elseif p.Results.tail<0,
0846 fprintf('Tail of tests: Lower\n');
0847 else
0848 fprintf('Tail of tests: Two-Tailed\n');
0849 end
0850 if n_chan==1,
0851 fprintf('Multiple comparison correction method: Not Applicable\n');
0852 else
0853 if strcmpi(p.Results.correction,'perm'),
0854 fprintf('Multiple comparison correction method: Within-subjects permutation test\n');
0855 elseif strcmpi(p.Results.correction,'FDR BH'),
0856 fprintf('Benjamini & Hochberg FDR control procedure\n');
0857 elseif strcmpi(p.Results.correction,'FDR BY'),
0858 fprintf('Benjamini & Yekutieli FDR control procedure\n');
0859 else
0860 fprintf('Multiple comparison correction method: Bonferroni-Holm procedure\n');
0861 end
0862 end
0863 fprintf('Specified Family-Wise Alpha Level: %.3f\n',p.Results.alpha);
0864 if strcmpi(p.Results.correction,'perm'),
0865 fprintf('Estimated Perm Test Family-Wise Alpha Level: %.3f\n', ...
0866 regress_results.perm_test_info.est_alpha);
0867 fprintf('# of permutations used for permutation test: %d\n', ...
0868 regress_results.perm_test_info.n_perm);
0869 end
0870 fprintf('Number of channels analyzed: %d\n',n_chan);
0871 fprintf('Mean EEG amplitude measured from %d to %d ms.\n',time_wind(1), ...
0872 time_wind(2));
0873 fprintf('Mean (SD) # of trials per participant: %.1f (%.1f)\n',mean(n_regress_trial),std(n_regress_trial));
0874 fprintf('Minimum and maximum # of trials per participant: %.1f to %.1f\n',min(n_regress_trial),max(n_regress_trial));
0875 fprintf('**RESULTS (Summarized across all channels)**\n');
0876 if n_pred>1,
0877 fprintf('Predictor\t#_of_Sig_Tests\tMedian(IQR)_Collinearity\tp-values\n');
0878 for a=1:n_pred+1,
0879 mn_p=min(min(regress_results.p_coefs_cor(:,:,a)));
0880 if mn_p>=p.Results.alpha,
0881
0882 if a==1,
0883
0884 fprintf('%s\t\t%d\t\t\tNA\t\t\tp>=%f\n',regress_results.predictor_names{a}, ...
0885 sum(sum(regress_results.h_coefs_cor(:,:,a))),mn_p);
0886 else
0887 fprintf('%s\t\t%d\t\t\t%.2f(%.2f)\t\tp>=%f\n',regress_results.predictor_names{a}, ...
0888 sum(sum(regress_results.h_coefs_cor(:,:,a))),median(R_collinear(a,:)), ...
0889 iqr(R_collinear(a,:)),mn_p);
0890 end
0891 else
0892
0893 p_coefs_cor=regress_results.p_coefs_cor(:,:,a);
0894 sig_p_id=find(p_coefs_cor<p.Results.alpha);
0895 mx_p=max(p_coefs_cor(sig_p_id));
0896 if a==1,
0897
0898 fprintf('%s\t\t%d\t\t\tNA\t\t%f>=sig p>=%f\n',regress_results.predictor_names{a}, ...
0899 sum(sum(regress_results.h_coefs_cor(:,:,a))),mx_p,mn_p);
0900 else
0901 fprintf('%s\t\t%d\t\t\t%.2f(%.2f)\t%f>=sig p>=%f\n',regress_results.predictor_names{a}, ...
0902 sum(sum(regress_results.h_coefs_cor(:,:,a))),median(R_collinear(a,:)), ...
0903 iqr(R_collinear(a,:)),mx_p,mn_p);
0904 end
0905 end
0906 end
0907 else
0908 fprintf('Predictor\t#_of_Sig_Channels\t\tp-values\n');
0909 for a=1:2,
0910 mn_p=min(min(regress_results.p_coefs_cor(:,:,a)));
0911 if mn_p>=p.Results.alpha,
0912
0913 fprintf('%s\t\t%d\t\t\tp>=%f\n',regress_results.predictor_names{a}, ...
0914 sum(sum(regress_results.h_coefs_cor(:,:,a))),mn_p);
0915 else
0916
0917 p_coefs_cor=regress_results.p_coefs_cor(:,:,a);
0918 sig_p_id=find(p_coefs_cor<p.Results.alpha);
0919 mx_p=max(p_coefs_cor(sig_p_id));
0920
0921
0922 fprintf('%s\t\t%d\t\t%f>=sig p>=%f\n',regress_results.predictor_names{a}, ...
0923 sum(sum(regress_results.h_coefs_cor(:,:,a))),mx_p,mn_p);
0924 end
0925 end
0926 end
0927
0928
0929 if ~isempty(p.Results.output_file),
0930 watchit('This function cannot produce an output text file yet.');
0931 if 0
0932 [fid msg]=fopen(p.Results.output_file,'w');
0933 if fid==-1,
0934 error('Cannot create file %s for writing. According to fopen.m: %s.', ...
0935 p.Results.file,msg);
0936 else
0937
0938 fprintf(fid,'**ANALYSIS_PARAMETERS**\n');
0939 fprintf(fid,'Number of participants: %d\n',n_sub);
0940 fprintf(fid,'Regression analysis t-score degrees of freedom: %d\n',n_sub-1);
0941 if p.Results.tail>0,
0942 fprintf(fid,'Tail of tests: Upper\n');
0943 elseif p.Results.tail<0,
0944 fprintf(fid,'Tail of tests: Lower\n');
0945 else
0946 fprintf(fid,'Tail of tests: Two-Tailed\n');
0947 end
0948 if n_chan==1,
0949 fprintf(fid,'Multiple comparison correction method: Not Applicable\n');
0950 else
0951 if strcmpi(p.Results.correction,'perm'),
0952 fprintf(fid,'Multiple comparison correction method: Within-subjects permutation test\n');
0953 elseif strcmpi(p.Results.correction,'FDR BH'),
0954 fprintf(fid,'Benjamini & Hochberg FDR control procedure\n');
0955 elseif strcmpi(p.Results.correction,'FDR BY'),
0956 fprintf(fid,'Benjamini & Yekutieli FDR control procedure\n');
0957 else
0958 fprintf(fid,'Multiple comparison correction method: Bonferroni-Holm procedure\n');
0959 end
0960 end
0961 fprintf(fid,'Family-Wise Alpha Level: %.3f\n',p.Results.alpha);
0962 if strcmpi(p.Results.correction,'perm'),
0963 fprintf(fid,'Estimated Perm Test Family-Wise Alpha Level: %.3f\n', ...
0964 regress_results.perm_test_info.est_alpha);
0965 fprintf(fid,'# of permutations used for permutation test: %d\n', ...
0966 regress_results.perm_test_info.n_perm);
0967 end
0968 fprintf(fid,'Number of channels analyzed: %d\n',n_chan);
0969 fprintf(fid,'Mean EEG amplitude measured from %d to %d ms.\n',time_wind(1), ...
0970 time_wind(2));
0971 fprintf(fid,'Mean (SD) # of trials per participant: %.1f (%.1f)\n',mean(n_regress_trial),std(n_regress_trial));
0972 fprintf(fid,'Minimum and maximum # of trials per participant: %.1f to %.1f\n',min(n_regress_trial),max(n_regress_trial));
0973 fprintf(fid,'*.set_files_used_in_analysis #_of_trials_from_setfile_used\n');
0974 for a=1:n_sub,
0975 fprintf(fid,'%s %d\n',infiles{a},n_regress_trial(a));
0976 end
0977 fprintf(fid,'**REGRESSION_RESULTS_SUMMARY**\n');
0978 if n_pred>1,
0979 fprintf(fid,'Predictor\t#_of_Sig_Channels\tMedian(IQR)_Collinearity\tp-values\n');
0980 for a=1:n_pred+1,
0981 mn_p=min(regress_results.p_coefs_cor(:,a));
0982 if mn_p>=p.Results.alpha,
0983
0984 if a==1,
0985
0986 fprintf(fid,'%s\t\t%d\t\t\tNA\t\t\tp>=%f\n',regress_results.predictor_names{a}, ...
0987 sum(regress_results.h_coefs_cor(:,a)),mn_p);
0988 else
0989 fprintf(fid,'%s\t\t%d\t\t\t%.2f(%.2f)\t\tp>=%f\n',regress_results.predictor_names{a}, ...
0990 sum(regress_results.h_coefs_cor(:,a)),median(R_collinear(a,:)), ...
0991 iqr(R_collinear(a,:)),mn_p);
0992 end
0993 else
0994
0995 sig_p_id=find(regress_results.p_coefs_cor(:,a)<p.Results.alpha);
0996 mx_p=max(regress_results.p_coefs_cor(sig_p_id,a));
0997 if a==1,
0998
0999 fprintf(fid,'%s\t\t%d\t\t\tNA\t\t%f>=sig p>=%f\n',regress_results.predictor_names{a}, ...
1000 sum(regress_results.h_coefs_cor(:,a)),mx_p,mn_p);
1001 else
1002 fprintf(fid,'%s\t\t%d\t\t\t%.2f(%.2f)\t%f>=sig p>=%f\n',regress_results.predictor_names{a}, ...
1003 sum(regress_results.h_coefs_cor(:,a)),median(R_collinear(a,:)), ...
1004 iqr(R_collinear(a,:)),mx_p,mn_p);
1005 end
1006 end
1007 end
1008 else
1009 fprintf(fid,'Predictor\t#_of_Sig_Channels\t\tp-values\n');
1010 for a=1:2,
1011 mn_p=min(regress_results.p_coefs_cor(:,a));
1012 if mn_p>=p.Results.alpha,
1013
1014 fprintf(fid,'%s\t\t%d\t\t\tp>=%f\n',regress_results.predictor_names{a}, ...
1015 sum(regress_results.h_coefs_cor(:,a)),mn_p);
1016 else
1017
1018 sig_p_id=find(regress_results.p_coefs_cor(:,a)<p.Results.alpha);
1019 mx_p=max(regress_results.p_coefs_cor(sig_p_id,a));
1020 fprintf(fid,'%s\t\t%d\t\t%f>=sig p>=%f\n',regress_results.predictor_names{a}, ...
1021 sum(regress_results.h_coefs_cor(:,a)),mx_p,mn_p);
1022 end
1023 end
1024 end
1025
1026 fprintf(fid,'**REGRESSION_RESULTS_PER_CHANNEL**\n');
1027 for c=1:n_chan,
1028 fprintf(fid,'Results_for_Channel: %s \n',regress_results.chanlocs(c).labels);
1029 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', ...
1030 37,37);
1031 for pred=1:n_pred+1,
1032
1033
1034
1035
1036
1037 if pred==1,
1038 pred_name='Intercept';
1039 else
1040 pred_name=predictors{pred-1};
1041 end
1042 fprintf(fid,'%s \t%f \t%f \t%f \t%.3f \t%.3f \t%d \t%.2f\n', ...
1043 pred_name,regress_results.mn_coefs(c,pred), ...
1044 regress_results.low_ci(c,pred),regress_results.hi_ci(c,pred), ...
1045 regress_results.t_coefs(c,pred),regress_results.p_coefs_uncor(c,pred), ...
1046 regress_results.h_coefs_cor(c,pred),regress_results.d_coefs(c,pred));
1047 end
1048 end
1049 fclose(fid);
1050 end
1051 end
1052 end
1053
1054
1055 if p.Results.plot_raster,
1056 for a=1:length(regress_results.predictor_names)
1057 sig_raster_coefs(regress_results,'predictor',a);
1058 end
1059 end
1060
1061
1062
1063 function all_same=all_same_cell_elements(cell_array)
1064
1065
1066 n=length(cell_array);
1067 all_same=1;
1068 for a=2:n,
1069 if ~(isnan(cell_array{1}) && isnan(cell_array{a}))
1070 if ~isequal(cell_array{1},cell_array{a}),
1071 all_same=0;
1072 break;
1073 end
1074 end
1075 end
1076
1077