文件准备
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分析完成!');