美文网首页
matlab配对T检验fmri

matlab配对T检验fmri

作者: 养猪场小老板 | 来源:发表于2025-03-04 19:21 被阅读0次
文件准备
mask准备
协变量
clc; clear;
spm('Defaults', 'fMRI');
spm_jobman('initcfg');

% 定义基础路径和协变量路径
base_dir = 'E:\ICA2025\ica2025-1\recovery_vs_acute';
cov_dir = fullfile(base_dir, 'covariates');

% 加载协变量数据(假设已调整为每个扫描的协变量,共126个)
age = load(fullfile(cov_dir, 'Age.txt'));    % 需确保是126x1向量,每个被试重复两次
sex = load(fullfile(cov_dir, 'Sex.txt'));    % 同上
edu = load(fullfile(cov_dir, 'Edu.txt'));    % 同上

% 定义要分析的IC列表
ic_numbers = [2,3,4,5,6,10,11,12,16,17,18,21,22,24,27,30,31]; 

for ic_num = ic_numbers
    % ========== 动态生成路径 ==========
    result_dir = fullfile(base_dir, sprintf('result3_ica_ic%d_paired', ic_num));
    if ~exist(result_dir, 'dir')
        mkdir(result_dir);
    end
    
    mask_file = fullfile(base_dir, 'compute_mask\result3_ica_ICs_mask',...
        sprintf('IC%d_mask.nii,1', ic_num));
    
    % ========== 生成文件列表(配対结构)==========
    data_dir = fullfile(base_dir, 'result3_189');
    
    % 生成Group1和Group2文件列表(假设sub001-063为条件1,sub127-189为对应的条件2)
    scans_condition1 = cell(63,1);
    scans_condition2 = cell(63,1);
    for i = 1:63
        sub_cond1 = sprintf('sub%03d', i);
        sub_cond2 = sprintf('sub%03d', i+126); % 假设sub127对应sub001的配对
        scans_condition1{i} = fullfile(data_dir,...
            sprintf('ica3_%s_component_ica_s1_.nii,%d', sub_cond1, ic_num));
        scans_condition2{i} = fullfile(data_dir,...
            sprintf('ica3_%s_component_ica_s1_.nii,%d', sub_cond2, ic_num));
    end
    
    % ========== 配置分析流程 ==========
    matlabbatch = {};
    
    % 步骤1: 指定配对设计模型
    matlabbatch{1}.spm.stats.factorial_design.dir = {result_dir};
    matlabbatch{1}.spm.stats.factorial_design.des.pt.pair = struct('scans', {}, 'levels', {});
    
    % 为每个被试添加扫描对
    for subj = 1:63
        matlabbatch{1}.spm.stats.factorial_design.des.pt.pair(subj).scans = {
            scans_condition1{subj}
            scans_condition2{subj}
        };
    end
    
    % 协变量设置(需确保长度与总扫描数一致,此处为126)
    matlabbatch{1}.spm.stats.factorial_design.cov(1).c = age;
    matlabbatch{1}.spm.stats.factorial_design.cov(1).cname = 'age';
    matlabbatch{1}.spm.stats.factorial_design.cov(1).iCFI = 1;
    matlabbatch{1}.spm.stats.factorial_design.cov(1).iCC = 1;
    
    matlabbatch{1}.spm.stats.factorial_design.cov(2).c = sex;
    matlabbatch{1}.spm.stats.factorial_design.cov(2).cname = 'sex';
    matlabbatch{1}.spm.stats.factorial_design.cov(2).iCFI = 1;
    matlabbatch{1}.spm.stats.factorial_design.cov(2).iCC = 1;
    
    matlabbatch{1}.spm.stats.factorial_design.cov(3).c = edu;
    matlabbatch{1}.spm.stats.factorial_design.cov(3).cname = 'edu';
    matlabbatch{1}.spm.stats.factorial_design.cov(3).iCFI = 1;
    matlabbatch{1}.spm.stats.factorial_design.cov(3).iCC = 1;
    
    % 掩膜及其他设置
    matlabbatch{1}.spm.stats.factorial_design.masking.tm.tm_none = 1;
    matlabbatch{1}.spm.stats.factorial_design.masking.im = 1;
    matlabbatch{1}.spm.stats.factorial_design.masking.em = {mask_file};
    matlabbatch{1}.spm.stats.factorial_design.globalc.g_omit = 1;
    matlabbatch{1}.spm.stats.factorial_design.globalm.gmsca.gmsca_no = 1;
    matlabbatch{1}.spm.stats.factorial_design.globalm.glonorm = 1;
    
    % 步骤2: 参数估计
    matlabbatch{2}.spm.stats.fmri_est.spmmat = {fullfile(result_dir, 'SPM.mat')};
    matlabbatch{2}.spm.stats.fmri_est.write_residuals = 0;
    matlabbatch{2}.spm.stats.fmri_est.method.Classical = 1;
    
    % 步骤3: 对比定义(配对T检验对比)
    matlabbatch{3}.spm.stats.con.spmmat = {fullfile(result_dir, 'SPM.mat')};
    matlabbatch{3}.spm.stats.con.consess{1}.tcon.name = 'Condition1 > Condition2';
    matlabbatch{3}.spm.stats.con.consess{1}.tcon.weights = [1 -1];
    matlabbatch{3}.spm.stats.con.consess{1}.tcon.sessrep = 'none';
    matlabbatch{3}.spm.stats.con.delete = 0;
    
    % 步骤4: 结果报告
    matlabbatch{4}.spm.stats.results.spmmat = {fullfile(result_dir, 'SPM.mat')};
    matlabbatch{4}.spm.stats.results.conspec.contrasts = 1;
    matlabbatch{4}.spm.stats.results.conspec.threshdesc = 'none';
    matlabbatch{4}.spm.stats.results.conspec.thresh = 0.001;
    matlabbatch{4}.spm.stats.results.conspec.extent = 0;
    matlabbatch{4}.spm.stats.results.conspec.mask = {mask_file};
    matlabbatch{4}.spm.stats.results.units = 1;
    matlabbatch{4}.spm.stats.results.print = 'ps';
    
    % ========== 运行分析 ==========
    try
        spm_jobman('run', matlabbatch);
        fprintf('IC%d 分析成功完成\n', ic_num);
    catch ME
        fprintf('IC%d 分析失败: %s\n', ic_num, ME.message);
    end
end
disp('所有IC分析完成!');

#接下来可以用xjview卡阈值voxel20/FDR0.05

双样本T检验

clc; clear;
spm('Defaults', 'fMRI');
spm_jobman('initcfg');

% 定义基础路径和协变量路径
base_dir = 'E:\ICA2025\ica2025-1\recovery_vs_acute';
cov_dir = fullfile(base_dir, 'covariates');

% 加载协变量数据
age = load(fullfile(cov_dir, 'Age.txt'));
sex = load(fullfile(cov_dir, 'Sex.txt'));
edu = load(fullfile(cov_dir, 'Edu.txt'));

% 定义要分析的IC列表
%ic_numbers = 2;  % 根据需要修改IC编号范围
ic_numbers = [2,3,4,5,6,10,11,12,16,17,18,21,22,24,27,30,31]; 
for ic_num = ic_numbers
    % ========== 动态生成路径 ==========
    % 结果目录
    result_dir = fullfile(base_dir, sprintf('result3_ica_ic%d_t2', ic_num));
    if ~exist(result_dir, 'dir')
        mkdir(result_dir);
    end
    
    % 掩膜文件路径
    mask_file = fullfile(base_dir, 'compute_mask\result3_ica_ICs_mask',...
        sprintf('IC%d_mask.nii,1', ic_num));
    
    % ========== 生成文件列表 ==========
    data_dir = fullfile(base_dir, 'result3_189');
    
    % 生成Group1文件列表(001-063)
    scans1 = cell(63,1);
    for i = 1:63
        sub_str = sprintf('sub%03d', i);
        scans1{i} = fullfile(data_dir,...
            sprintf('ica3_%s_component_ica_s1_.nii,%d', sub_str, ic_num)); % 根据实际文件名调整
    end
    
    % 生成Group2文件列表(064-126)
    scans2 = cell(63,1);
    for i = 127:189
        sub_str = sprintf('sub%03d', i);
        scans2{i-126} = fullfile(data_dir,...
            sprintf('ica3_%s_component_ica_s1_.nii,%d', sub_str, ic_num)); % 根据实际文件名调整
    end
    
    % ========== 配置分析流程 ==========
    matlabbatch = {};
    
    % 步骤1: 指定模型
    matlabbatch{1}.spm.stats.factorial_design.dir = {result_dir};
    matlabbatch{1}.spm.stats.factorial_design.des.t2.scans1 = scans1;
    matlabbatch{1}.spm.stats.factorial_design.des.t2.scans2 = scans2;
    matlabbatch{1}.spm.stats.factorial_design.des.t2.dept = 0;
    matlabbatch{1}.spm.stats.factorial_design.des.t2.variance = 1;
    matlabbatch{1}.spm.stats.factorial_design.des.t2.gmsca = 0;
    matlabbatch{1}.spm.stats.factorial_design.des.t2.ancova = 0;
    
    % 协变量设置
    matlabbatch{1}.spm.stats.factorial_design.cov(1).c = age;
    matlabbatch{1}.spm.stats.factorial_design.cov(1).cname = 'age';
    matlabbatch{1}.spm.stats.factorial_design.cov(1).iCFI = 1;
    matlabbatch{1}.spm.stats.factorial_design.cov(1).iCC = 1;
    
    matlabbatch{1}.spm.stats.factorial_design.cov(2).c = sex;
    matlabbatch{1}.spm.stats.factorial_design.cov(2).cname = 'sex';
    matlabbatch{1}.spm.stats.factorial_design.cov(2).iCFI = 1;
    matlabbatch{1}.spm.stats.factorial_design.cov(2).iCC = 1;
    
    matlabbatch{1}.spm.stats.factorial_design.cov(3).c = edu;
    matlabbatch{1}.spm.stats.factorial_design.cov(3).cname = 'edu';
    matlabbatch{1}.spm.stats.factorial_design.cov(3).iCFI = 1;
    matlabbatch{1}.spm.stats.factorial_design.cov(3).iCC = 1;
    
    % 掩膜设置
    matlabbatch{1}.spm.stats.factorial_design.masking.tm.tm_none = 1;
    matlabbatch{1}.spm.stats.factorial_design.masking.im = 1;
    matlabbatch{1}.spm.stats.factorial_design.masking.em = {mask_file};
    matlabbatch{1}.spm.stats.factorial_design.globalc.g_omit = 1;
    matlabbatch{1}.spm.stats.factorial_design.globalm.gmsca.gmsca_no = 1;
    matlabbatch{1}.spm.stats.factorial_design.globalm.glonorm = 1;
    
    % 步骤2: 参数估计
    matlabbatch{2}.spm.stats.fmri_est.spmmat = {fullfile(result_dir, 'SPM.mat')};
    matlabbatch{2}.spm.stats.fmri_est.write_residuals = 0;
    matlabbatch{2}.spm.stats.fmri_est.method.Classical = 1;
    
    % 步骤3: 对比定义
    matlabbatch{3}.spm.stats.con.spmmat = {fullfile(result_dir, 'SPM.mat')};
    matlabbatch{3}.spm.stats.con.consess{1}.tcon.name = 'Group1 > Group2';
    matlabbatch{3}.spm.stats.con.consess{1}.tcon.weights = [1 -1];
    matlabbatch{3}.spm.stats.con.consess{1}.tcon.sessrep = 'none';
    matlabbatch{3}.spm.stats.con.delete = 0;
    
    % 步骤4: 结果报告
    matlabbatch{4}.spm.stats.results.spmmat = {fullfile(result_dir, 'SPM.mat')};
    matlabbatch{4}.spm.stats.results.conspec.contrasts = 1;
    matlabbatch{4}.spm.stats.results.conspec.threshdesc = 'none';
    matlabbatch{4}.spm.stats.results.conspec.thresh = 0.001;
    matlabbatch{4}.spm.stats.results.conspec.extent = 0;
    matlabbatch{4}.spm.stats.results.conspec.mask = {mask_file};
    matlabbatch{4}.spm.stats.results.units = 1;
    matlabbatch{4}.spm.stats.results.print = 'ps';
    
    % ========== 运行分析 ==========
    try
        for step = 1:4
            spm_jobman('run', matlabbatch(step));
        end
        fprintf('IC%d 分析成功完成\n', ic_num);
    catch ME
        fprintf('IC%d 分析失败: %s\n', ic_num, ME.message);
    end
end
disp('所有IC分析完成!');

相关文章

  • 如何选择T检验?

    内容目录 t 检验分类配对 t 检验(非独立t检验)非配对 t 检验(独立t检验)单/双侧 t 检验选择 小结: ...

  • 统计-T检验和ANOVA+代码实战

    细说T检验: A 配对T检验:针对一个样本,当你有实验前和实验后的两组数据时选择配对T检验 不配对T检验推荐选择d...

  • Mcnemar检验,Kappa检验

    1. 配对四格表的卡方检验 熟悉经典比较的都知道有配对t检验,在列联表中也有配对的列联表。与配对t检验类似,配对列...

  • 医学临床试验文献统计方法解读(t检验)

    五、非配对t检验 (一)文献中应用 文献中如此描述t检验的应用:”非配对t检验用来比较RBH、ESBG、MBL在试...

  • 统计学学习笔记——day4

    配对t检验 之前学到的是独立两样本t检验以及秩和检验,本次学习配对(关联)样本t检验。先将样本配成对子再将对子中的...

  • python数据分析之t检验

    t检验应用: 1、单样本检验: 2、样本检验 3、对t检验 4、独立样本t检验 5、“配对”或者“重复测量”检验 ...

  • t检验-SPSS软件应用

    ①单样本t检验 spss操作步骤(正态性检验) spss操作步骤(单样本t检验) ②配对样本t检验 spss操作步...

  • 《白话统计》读书笔记-t and anovar

    t检验t检验的使用条件t检验的实现独立样本t检验配对样本t检验Wilcoxon秩和的实现 方差分析方差分析中变异分...

  • 手把手教你快速完成配对T检验

    一、配对样本T检验 配对t检验,用于配对定量数据之间的差异对比关系。例如在两种背景情况下(有广告和无广告);样本的...

  • t检验-配对样本t检验

    上一篇我们学习了单样本t检验,今天学习配对样本t检验。在学习之前,我们一定要明白“配对"的含义,因为在后续整个统计...

网友评论

      本文标题:matlab配对T检验fmri

      本文链接:https://www.haomeiwen.com/subject/oaohmjtx.html