⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rri_multiblock_boot.m

📁 绝对经典,老外制作的功能强大的matlab实现PLS_TOOBOX
💻 M
📖 第 1 页 / 共 2 页
字号:
%RRI_MULTIBLOCK_BOOT Apply Behavioral or Task PLS test and bootstrap test
%	on RRI scan.
%
%   See also PLS_BOOT_TEST, PLS_DEVIATION_BOOT_TEST, BEHAVPLS_BOOT, TASKPLS_BOOT
%

%   I (ismean) - 1 if using grand mean deviation;
%   I (ishelmert) - 1 if using helmert matrix;
%   I (iscontrast) - 1 if using contrast data;
%   I (isbehav) - 1 if using behavior data;
%   I (newdata_lst) - A group list of datamat files;
%   I (num_cond_lst) - A group list of condition numbers;
%   I (num_subj_lst) - A group list of subject numbers;
%   I (behavdata_lst) - A group list of behav data with selected columns;
%   I (helmertdata_lst) - A group list of helmert matrix with selected columns;
%   I (contrastdata_lst) - A group list of contrast data with selected columns;
%                       contrastdata_lst has been orthchecked in
%                       erp_get_common function
%   I (num_boot) - Number of Permutation;
%   I (Clim) - upper limit of confidence interval estimated.
%
%   O (brainlv) - Left singular value vector. It is LV for the brain dimension.
%   O (s) - Singular values vector.
%   O (behavlv) - Right singular vector. It is LV for the behavior OR contrast
%		dimension for each scan.
%   O (brainscores) - The brain score.
%   O (behavscores) - If use GRAND_MEAN method, behavscores = behavlv, then
%		expand each condition for all the subjects. If not, it is
%		calculated for each set of condition, then stack together.
%   O (lvcorrs) - Correlates brain scores with behavior data for multiple scans,
%		return [] if for Task PLS or if use GRAND_MEAN method.
%   O (boot_result) - A Structure array containing the bootstrap result data.
%
%   Created on 03-OCT-2002 by Jimmy Shen for PLS test
%   Modified on 27-OCT-2002 by Jimmy Shen to add bootstrap test
%   Modifyed on Jan 13,03 for ERP analysis
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [brainlv,s,designlv,behavlv,brainscores,designscores,behavscores, ...
		lvcorrs,boot_result,datamatcorrs_lst,b_scores, ...
		behav_row_idx,behavdata_lst] = ...
                rri_multiblock_boot(ismean, ishelmert, iscontrast, isbehav, ...
                newdata_lst,num_cond_lst,num_subj_lst, ...
                behavdata_lst, helmertdata_lst, contrastdata_lst, ...
                num_boot, Clim, ...
		min_subj_per_group,is_boot_samples,boot_samples, ...
		new_num_boot,bscan)

    % Init
    %
    brainlv = [];
    s = [];
    designlv = [];
    behavlv = [];
    brainscores = [];
    designscores = [];
    behavscores = [];
    lvcorrs = [];
    boot_result = [];
    b_scores = [];
    datamatcorrs_lst = {};

    stacked_behavdata = [];
    stacked_TBdatamatcorrs = [];
    stacked_helmertdata = [];
    stacked_contrastdata = [];
    stacked_datamatcorrs = [];
    stacked_datamat = [];
    stacked_mask = [];

    num_groups = length(newdata_lst);

    % keeps track of number of times a new bootstrap had to be generated
    %
    countnewtotal=0;
    num_LowVariability_behav_boots = [];
    badbeh = [];

    progress_hdl = rri_progress_ui('initialize');

    msg = 'Working on PLS ...';
    rri_progress_ui(progress_hdl, '', msg);

%    boot_progress = rri_progress_ui('initialize');

    if 0
%       [reorder, new_num_boot] = rri_boot_order(num_subj_lst, num_cond_lst(1), num_boot, 1, boot_progress, ...
       [reorder, new_num_boot] = rri_boot_order(num_subj_lst, num_cond_lst(1), num_boot, 1, ...
		min_subj_per_group,is_boot_samples,boot_samples,new_num_boot);
    else
%       [reorder, new_num_boot] = rri_boot_order(num_subj_lst, num_cond_lst(1), num_boot, 0, boot_progress, ...
       [reorder, new_num_boot] = rri_boot_order(num_subj_lst, num_cond_lst(1), num_boot, 0, ...
		min_subj_per_group,is_boot_samples,boot_samples,new_num_boot);
    end

    if isempty(reorder)
       return;
    end;

        k = num_cond_lst(1);
        kk = length(bscan);

    % loop accross the groups, and
    % calculate datamatcorrs for each group
    %
    for i = 1:num_groups

        n = num_subj_lst(i);

        datamat = newdata_lst{i};

        rri_progress_ui(progress_hdl,'',2/10+5/10*(i-1)/(num_groups)+1/(10*num_groups));

       % compute task mean
       %
       Tdatamatcorrs = rri_task_mean(datamat,n)-ones(k,1)*mean(datamat);

        % compute correlation or covariance
        %
            Bdatamatcorrs = rri_corr_maps_notall(behavdata_lst{i}, datamat, n, bscan);
            datamatcorrs_lst = [datamatcorrs_lst, {Bdatamatcorrs}];

        rri_progress_ui(progress_hdl,'',2/10+5/10*(i-1)/(num_groups)+3/(10*num_groups));

       %  stack task and behavior - keep un-normalize data that will be
       %  used to recover the normalized one
       %
       TBdatamatcorrs = [Tdatamatcorrs; Bdatamatcorrs];

       %  stack task and behavior - normalize to unit length to reduce
       %  scaling difference
       %
       datamatcorrs = [normalize(Tdatamatcorrs,2); normalize(Bdatamatcorrs,2)];

       stacked_TBdatamatcorrs = [stacked_TBdatamatcorrs; TBdatamatcorrs];

            stacked_behavdata = [stacked_behavdata; behavdata_lst{i}];

        stacked_datamat = [stacked_datamat; datamat];
        stacked_datamatcorrs = [stacked_datamatcorrs; datamatcorrs];

        rri_progress_ui(progress_hdl,'',2/10+5/10*(i-1)/(num_groups)+5/(10*num_groups));

    end		% for

    % actually, all the groups must have the same condition number
    %
    num_cond = num_cond_lst(1);

    % Singular Value Decomposition
    %
    [r c] = size(stacked_datamatcorrs);
    if r <= c
        % transpose datamatcorrs to ensure SVD operation will be
        % on smallest of RxC dimension
        %
        [brainlv,s,v] = svd(stacked_datamatcorrs',0);
    else
        [v,s,brainlv] = svd(stacked_datamatcorrs,0);
    end

    original_sal = brainlv * s;
    orig_behavlv = v * s;
    original_v = orig_behavlv;

    s = diag(s);

    rri_progress_ui(progress_hdl,'',9/10);

      %  Separate v into 2 parts: designlv and behavlv
      %
      designlv = [];
      behavlv = [];

      for g = 1:num_groups
         t = size(stacked_behavdata, 2);

         designlv = [designlv; v((g-1)*k+(g-1)*kk*t+1 : (g-1)*k+(g-1)*kk*t+k,:)];
         behavlv = [behavlv; v((g-1)*k+(g-1)*kk*t+k+1 : (g-1)*k+(g-1)*kk*t+k+kk*t,:)];
      end

      num_col = size(designlv, 2);

      % expand the num_subj for each row (cond)
      % did the samething as testvec
      %
      designscores = [];
      row_idx = [];
      last = 0;

      for g = 1:num_groups
         n = num_subj_lst(g);

         tmp = reshape(designlv((g-1)*k+1:(g-1)*k+k,:),[1, num_col*k]);
         tmp = repmat(tmp, [n, 1]);		% expand to num_subj
         tmp = reshape(tmp, [n*k, num_col]);

         designscores = [designscores; tmp];		% stack by groups

         %  take this advantage (having g & n) to get row_idx
         %
         tmp = 1:n*k;
         tmp = reshape(tmp, [n k]);
         tmp = tmp(:, bscan);
         behavdata_lst{g} = behavdata_lst{g}(tmp(:),:);

         row_idx = [row_idx ; tmp(:) + last];
         last = last + n*k;
      end

    behav_row_idx = row_idx;

    % calculate behav scores
    %
        b_scores = stacked_datamat * brainlv;
        behav = stacked_behavdata;

        [brainscores, behavscores, lvcorrs] = ...
		rri_get_behavscores(stacked_datamat(row_idx,:), ...
		stacked_behavdata(row_idx,:), ...
		brainlv, behavlv, kk, num_subj_lst);

%      lvcorrs = original_v;

        orig_corr = lvcorrs;
        [r1 c1] = size(orig_corr);
        distrib = zeros(r1, c1, num_boot+1);
        distrib(1:r1, 1:c1, 1) = orig_corr;


    rri_progress_ui(progress_hdl,'',1);

    %  Begin bootstrap loop
    %
    %  reorder = rri_mkboot_order(num_cond_lst(1), num_subj_lst, num_boot);

    %  move the following code up, so we don't need to run obserbation
    %  test if bootstrap can not run
    %
    % [reorder, new_num_boot] = ...
	% rri_boot_order(num_subj_lst, num_cond_lst(1), num_boot, 1);

    if new_num_boot ~= num_boot
        num_boot = new_num_boot;
        h0 = findobj(0,'tag','PermutationOptionsFigure');
        h = findobj(h0,'tag','NumBootstrapEdit');
        set(h,'string',num2str(num_boot));

            distrib = zeros(r1, c1, num_boot+1);
            distrib(1:r1, 1:c1, 1) = orig_corr;
    end

    max_subj_per_group = 8;
    if (sum(num_subj_lst <= max_subj_per_group) == num_groups)
        is_boot_samples = ones(1,num_groups);
    else
        is_boot_samples = zeros(1,num_groups);
    end

    if isempty(reorder)
       return;
    end

        sal_sq = original_sal.^2;
        sal_sum = original_sal;
        dsal_sq = orig_behavlv.^2;
        dsal_sum = orig_behavlv;


    %  Check min% unique values for all behavior variables
    %
    num_LowVariability_behav_boots = zeros(1, size(stacked_behavdata, 2));

    for bw = 1:size(stacked_behavdata, 2)
       for p = 1:num_boot
          vv = stacked_behavdata(reorder(:,p),bw);

%          if unique(vv) <= size(stacked_behavdata, 1)*0.5
          if rri_islowvariability(vv, stacked_behavdata(:,bw))
             num_LowVariability_behav_boots(bw) = num_LowVariability_behav_boots(bw) + 1;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -