I am trying to do statistical testing using anova1 function but I am receiving an error.
1 view (last 30 days)
Show older comments
This is my matlab code, it takes data from an excel file that has in the first column patient id, second column is stage either B1 or B2, third column is tissue location either Tumor or Stroma, 5 to 11 columns have markers CD4, CD4 PD1, CD8, CD8 PD1, CK7, PDL1 other, CK7 PDL1, column 13 is sample area. In the statistical testing part, I want to calculate the difference of markers between B2 − B1 and Identify which of the marker differences are statistically significant (hypothesis testing) using anova1 function and display the p-values:
This is the error I am receiving:
Conversion to double from cell is not possible.
Error in Breast_Cancer_Project
[~, p_values(i)] = anova1(marker_diff(:, i), [], 'off');
cancer=readtable('C:\Users\anas9\Documents\MATLAB\Multivariate Project\biopsies.xlsx');
%% Preprocessing
% Density of each marker samples
cancer.Stage = categorical(cancer.Stage);
c=cancer{:,5:11}./cancer{:,13};
cancer{:,5:11}=c;
u=unique(cancer.Patient_ID);
mar={'Patient_ID','CD4','CD4PD1','CD8','CD8PD1','CK7','PDL1','CK7PDL1'};
data_avg=array2table(zeros(size(u,1),8),'VariableNames',mar);
for i = 1:size(u)
x1=find(cancer.Patient_ID==u(i));
avg=mean(cancer{x1,5:11});
data_avg{i,1}=u(i);
data_avg{i,2:8}=avg;
end
% 3D matrices
S=cancer(cancer.TissueLocation=="Stroma",:);
T=cancer(cancer.TissueLocation=="Tumor",:);
location=["Stroma" ;"Tumor"];
S_avg=size(data_avg);
T_avg=size(data_avg);
for i = 1:size(u)
x1=find(S.Patient_ID==u(i));
S_avg(i,1)=u(i);
S_avg(i,2:8)=mean(S{x1,5:11});
x2=find (T.Patient_ID==u(i));
T_avg(i,1)=u(i);
T_avg(i,2:8)=mean(T{x2,5:11});
end
d=cat(3,S_avg,T_avg);
% Split into B1 and B2 data
B1=cancer(cancer.Stage=='B1',:);
B2=cancer(cancer.Stage=='B2',:);
u1 = unique(B1.Patient_ID);
u2 = unique(B2.Patient_ID);
mar={'Patient_ID','CD4','CD4PD1','CD8','CD8PD1','CK7','PDL1','CK7PDL1'};
B1_avg=array2table(zeros(size(u1,1),8),'VariableNames',mar);
B2_avg=array2table(zeros(size(u2,1),8),'VariableNames',mar);
ind=1;
for i = 1:size(u1)
x1=find (B1.Patient_ID==u1(i));
avg=mean(B1{x1,5:11});
B1_avg{ind,1}=u1(i);
B1_avg{ind,2:8}=avg;
ind=ind+1;
end
for i=1:size(u2)
x1=find (B2.Patient_ID==u2(i));
avg=mean(B2{x1,5:11});
B2_avg{ind,1}=u2(i);
B2_avg{ind,2:8}=avg;
ind=ind+1;
end
[~,TF] = rmoutliers(B1_avg(:,2:8),'mean');
index=TF;
B1_avg(index,:)=[];
[~,TF] = rmoutliers(B2_avg(:,2:8),'mean');
index=find(TF);
B2_avg(index,:)=[];
u1 = unique(B1_avg.Patient_ID);
u2 = unique(B2_avg.Patient_ID);
pos1=setdiff(u1,u2);
for i=1:size(pos1)
B1_avg(B1_avg.Patient_ID==pos1(i),:)=[];
end
u3 = unique(B1_avg.Patient_ID);
u4 = unique(B2_avg.Patient_ID);
pos2=setdiff(u4,u3);
for i=1:size(pos2)
B2_avg(B2_avg.Patient_ID==pos2(i),:)=[];
end
%% Statistical testing
% Difference of markers between B2 and B1
% Calculate the difference for each marker
marker_diff = B2_avg{:, 2:8} - B1_avg{:, 2:8};
% Perform hypothesis testing using anova1
p_values = zeros(1, 7);
for i = 1:7
[~, p_values(i)] = anova1(marker_diff(:, i), [], 'off');
end
% Display the p-values
disp('P-values for each marker:');
disp(array2table(p_values, 'VariableNames', {'CD4', 'CD4PD1', 'CD8', 'CD8PD1', 'CK7', 'PDL1', 'CK7PDL1'}));
%% Functions
function [test, train] = kfolds(data, k)
n = size(data, 1);
indices = crossvalind('Kfold', n, k);
test = cell(k, 1);
train = cell(k, 1);
for i = 1:k
test_indices = (indices == i);
train_indices = ~test_indices;
test{i} = data(test_indices, :);
train{i} = data(train_indices, :);
end
end
2 Comments
Torsten
on 5 May 2023
I can only guess because the Excel file is missing and I cannot run your code.
Maybe
marker_diff = cell2mat(B2_avg{:, 2:8} - B1_avg{:, 2:8});
instead of
marker_diff = B2_avg{:, 2:8} - B1_avg{:, 2:8};
helps.
Answers (1)
Aditya
on 25 Aug 2023
Hi Anas,
Upon reviewing above code, I noticed that there is minor issue with the syntax used for the “anova1” function as mentioned in the documentation that the first parameter should be the pvalue.
The correct syntax should be as follows:
[p_values(i), ~] = anova1(marker_diff(:, i), [], 'off');
To gain a more comprehensive understanding of the “anova1” function, you may find it helpful to refer to the documentation available at the following link: anova1 documentation.
I hope this helps!
0 Comments
See Also
Categories
Find more on Analysis of Variance and Covariance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!