How do I fix my variance? Can't figure out what wrong with my code because I keep getting NaN for my variance and y. If there anyway I could fix my variance or an easier way.

2 ビュー (過去 30 日間)
Roxanna Ruiz
Roxanna Ruiz 2021 年 10 月 17 日
回答済み: Walter Roberson 2021 年 10 月 17 日
% load info
Demos = load('Basic_Demos_data-2021-09-16T20_15_30.480Z.mat');
Diagnosis = load('Diagnosis_ClinicianConsensus_data-2021-09-16T20_18_13.004Z.mat');
NIH = load('NIHScoresdata.mat');
%%
% Load demographic information
T_Demos = readtable('Basic_Demos_data-2021-09-16T20_15_30.480Z.csv');
% Load diagnosis information
T_Diagnosis = readtable('Diagnosis_ClinicianConsensus_data-2021-09-16T20_18_13.004Z.csv');
T_NIH = readtable('NIH_Scores_data-2021-09-16T20_21_01.301Z.csv');
T_Data = readtable('NIH_data-2021-09-16T20_22_07.111Z.csv');
%%
% Identify healthy control participants
diagnosis = strcmp('No Diagnosis Given',T_Diagnosis.Diagnosis_ClinicianConsensus_DX_01);
diagnosis_asd = strcmp('Autism Spectrum Disorder',T_Diagnosis.Diagnosis_ClinicianConsensus_DX_01);
EID_included = T_Diagnosis.Diagnosis_ClinicianConsensus_EID(diagnosis);
fprintf('%d\n', length(EID_included));
EID_included_asd = T_Diagnosis.Diagnosis_ClinicianConsensus_EID(diagnosis_asd);
Basic_Demos_Age = T_Demos(:,3);
% Identify demographic information
for subji = 1:length(EID_included)
si = find(contains(T_Demos.Basic_Demos_EID,EID_included{subji}));
Age_included(subji,1) = T_Demos.Basic_Demos_Age(si);
Sex_included(subji,1) = T_Demos.Basic_Demos_Sex(si); % 0= Male, 1= Female
end
%% Identify demographic information
for subji = 1:length(EID_included_asd)
si = find(contains(T_Demos.Basic_Demos_EID,EID_included_asd{subji}));
Age_included_asd(subji,1) = T_Demos.Basic_Demos_Age(si);
Sex_included_asd(subji,1) = T_Demos.Basic_Demos_Sex(si); % 0= Male, 1= Female
end
%%
% Identify NIH toolbox scores
for subji = 1:length(EID_included)
si = find(contains(T_NIH.NIH_Scores_EID,EID_included{subji}));
if isempty(si)
NIH_included(subji,:) = NaN(1,6);
else
NIH_included(subji,:) = [T_NIH.NIH_Scores_NIH7_Card(si) T_NIH.NIH_Scores_NIH7_Comp(si) T_NIH.NIH_Scores_NIH7_Flanker(si) T_NIH.NIH_Scores_NIH7_List(si) T_NIH.NIH_Scores_NIH7_Pattern(si) T_NIH.NIH_Scores_NIH7_Picture(si)];
end
end
%% Identify NIH toolbox scores
for subji = 1:length(EID_included_asd)
si = find(contains(T_NIH.NIH_Scores_EID,EID_included{subji}));
if isempty(si)
NIH_included(subji,:) = NaN(1,6);
else
NIH_included(subji,:) = [T_NIH.NIH_Scores_NIH7_Card(si) T_NIH.NIH_Scores_NIH7_Comp(si) T_NIH.NIH_Scores_NIH7_Flanker(si) T_NIH.NIH_Scores_NIH7_List(si) T_NIH.NIH_Scores_NIH7_Pattern(si) T_NIH.NIH_Scores_NIH7_Picture(si)];
end
end
%%
% Plot results
figure; scatter(Age_included,NIH_included(:,5),20,Sex_included,'filled'); colormap([1 0 0; 0 0 1])
xlabel('Age');ylabel('NIH All Test')
ylim([0 200]);
legend('male','female')
title('No diagnosis')
md2 = fitlm(Age_included,NIH_included(:,6));
%% Stats of Analysis
%T(:,ismember(T.Properties.VariableNames, {""list of variables""})
a = contains(T_NIH.Properties.VariableNames,'NIH7_Card');
b = contains(T_NIH.Properties.VariableNames,'NIH7_Comp');
d = contains(T_NIH.Properties.VariableNames,'NIH7_Flanker');
e = contains(T_NIH.Properties.VariableNames,'NIH7_List');
f = contains(T_NIH.Properties.VariableNames,'NIH7_Pattern');
g = contains(T_NIH.Properties.VariableNames,'NIH7_Picture');
Cb = T_NIH(:,a);
type = T_NIH(:,g);
type1 = table2array(type);
RT = T_NIH(:,f);
N = T_NIH.NIH_Scores_EID; %specifying EID of T_flanker
O = ismember(N,EID_included);%logical array of EID of T_box == EID_included
O1 = ismember(N,EID_included_asd);
Q = ismember(EID_included,N);%logical array of EID_included == EID of T_Box
Q1 = ismember(EID_included_asd,N);
x = Age_included(Q);
x1 = Age_included(Q1);
y1 = var(O);
y2 = var(O1);
z = Sex_included(Q);
z2 = Sex_included(Q1);
y = Sex_included(O);
y_asd = Sex_included_asd(O1);
%%
scatter(x,y1,20,Sex_included(Q),'filled');
colormap([1 0 0; 0 0 1])
hold on
scatter(x1,y2,20,Sex_included(Q1),'filled');
title('NIH')
xlabel('Age');ylabel('RCS')
legend('no diagnosis','asd')
md1 = fitlm(x,y1);
md2 = fitlm(x1,y2);
plot(md1)
plot(md2)
%% Variance Data
%based on gender
male = Sex_included(Q);
female = ~male;
male_asd = Sex_included_asd(Q1);
female_asd = ~male_asd;
male_variance = var(logical(male));
male_variance_asd = var(logical(male_asd));
female_variance = var(female);
female_variance_asd = var(female_asd);
x_male = x(logical(male));
x_male_asd = x1(logical(male_asd));
x_female = x(female);
x_female_asd = x1(female_asd);
figure(4)
scatter(x_male,male_variance,20,'filled','r');
hold on
scatter(x_male_asd,male_variance_asd,20,'filled','b');
md_male = fitlm(x_male,male_variance);
md_male_asd = fitlm(x_male_asd,male_variance_asd);
plot(x_male,md_male.Coefficients.Estimate(1)+md_male.Coefficients.Estimate(2)*x_male,'r');
md_male_asd = fitlm(x_male_asd,male_variance_asd);
plot(x_male_asd,md_male_asd.Coefficients.Estimate(1)+md_male_asd.Coefficients.Estimate(2)*x_male_asd,'b');
legend('no diagnosis male','asd male','fitlm male','fitlm male asd')
figure(5)
scatter(x_female,female_variance,20,'filled','r');
hold on
scatter(x_female_asd,female_variance_asd,20,'filled','b');
md_female = fitlm(x_female,female_variance);
md_female_asd = fitlm(x_female_asd,female_variance_asd);
plot(x_female,md_female.Coefficients.Estimate(1)+md_female.Coefficients.Estimate(2)*x_female,'r');
md_male_asd = fitlm(x_female_asd,female_variance_asd);
plot(x_female_asd,md_female_asd.Coefficients.Estimate(1)+md_female_asd.Coefficients.Estimate(2)*x_female_asd,'b');
legend('no diagnosis female','asd female','fitlm female','fitlm female asd')
figure(6)
x_male_total = [x_male; x_male_asd];
male_variance_total = [male_variance; male_variance_asd];
scatter(x_male_total,male_variance_total,20,'filled','b');
hold on
md_male_total = fitlm(x_male_total,male_variance_total);
plot(x_male_total,md_male_total.Coefficients.Estimate(1)+md_male_total.Coefficients.Estimate(2)*x_male_total,'b');
x_female_total = [x_female; x_female_asd];
female_variance_total = [female_variance; female_variance_asd];
scatter(x_female_total,female_variance_total,20,'filled','r');
hold on
md_female_total = fitlm(x_female_total,female_variance_total);
plot(x_female_total,md_female_total.Coefficients.Estimate(1)+md_female_total.Coefficients.Estimate(2)*x_female_total,'r');
legend('all male','fitlm male','all female','fitlm female')
T = [mean(variance) mean(variance_asd) mean(male_variance) mean(male_variance_asd) mean(female_variance) mean(female_variance_asd) mean(male_variance_total) mean(female_variance_total)];
Names ={'variance','variance_asd','male_variance','male_variance_asd','female_variance','female_variance_asd','male_variance_total','female_variance_total'};
  2 件のコメント
Roxanna Ruiz
Roxanna Ruiz 2021 年 10 月 17 日
Yes, i got rid of all the congruents and i have the data. I edited my code.

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 10 月 17 日
You had a persistent problem where you were assuming that var() of a vector would give you a result the same size as the vector. In the below, I coded repmat() to take copies of the variance so that the statements could at least proceed.
It is doubtful that the results are what you would expect, as you are plotting several graphs with the same y value for each point, since the duplicated variance is going to be the same value for each point.
The following version of the code will do something without crashing right down to the second last line,
T = [mean(variance) mean(variance_asd) mean(male_variance) mean(male_variance_asd) mean(female_variance) mean(female_variance_asd) mean(male_variance_total) mean(female_variance_total)];
at which point it has to fail because you did not define variance or variance_asd .
Several of those mean() are taking the mean of scalars.
The ones that are taking means of vectors are questionable, as the vectors consist of repeated copies of scalar variances.
But at least the crashes (before the second last line) are gone so now you can concentrate on looking at the results and fixing them.
Note that you repeatedly do fitting of vector x values against what would be a scalar variance, now duplicated to be the same length. This seems like a waste of time: the obvious linear model in each case is just to declare y = 0*x + the_scalar_variance
% load info
Demos = load('Basic_Demos_data-2021-09-16T20_15_30.480Z.mat');
Diagnosis = load('Diagnosis_ClinicianConsensus_data-2021-09-16T20_18_13.004Z.mat');
NIH = load('NIHScoresdata.mat');
%%
% Load demographic information
T_Demos = readtable('Basic_Demos_data-2021-09-16T20_15_30.480Z.csv');
% Load diagnosis information
T_Diagnosis = readtable('Diagnosis_ClinicianConsensus_data-2021-09-16T20_18_13.004Z.csv');
T_NIH = readtable('NIH_Scores_data-2021-09-16T20_21_01.301Z.csv');
T_Data = readtable('NIH_data-2021-09-16T20_22_07.111Z.csv');
%%
% Identify healthy control participants
diagnosis = strcmp('No Diagnosis Given',T_Diagnosis.Diagnosis_ClinicianConsensus_DX_01);
diagnosis_asd = strcmp('Autism Spectrum Disorder',T_Diagnosis.Diagnosis_ClinicianConsensus_DX_01);
EID_included = T_Diagnosis.Diagnosis_ClinicianConsensus_EID(diagnosis);
fprintf('%d\n', length(EID_included));
EID_included_asd = T_Diagnosis.Diagnosis_ClinicianConsensus_EID(diagnosis_asd);
Basic_Demos_Age = T_Demos(:,3);
% Identify demographic information
for subji = 1:length(EID_included)
si = find(contains(T_Demos.Basic_Demos_EID,EID_included{subji}));
Age_included(subji,1) = T_Demos.Basic_Demos_Age(si);
Sex_included(subji,1) = T_Demos.Basic_Demos_Sex(si); % 0= Male, 1= Female
end
%% Identify demographic information
for subji = 1:length(EID_included_asd)
si = find(contains(T_Demos.Basic_Demos_EID,EID_included_asd{subji}));
Age_included_asd(subji,1) = T_Demos.Basic_Demos_Age(si);
Sex_included_asd(subji,1) = T_Demos.Basic_Demos_Sex(si); % 0= Male, 1= Female
end
%%
% Identify NIH toolbox scores
for subji = 1:length(EID_included)
si = find(contains(T_NIH.NIH_Scores_EID,EID_included{subji}));
if isempty(si)
NIH_included(subji,:) = NaN(1,6);
else
NIH_included(subji,:) = [T_NIH.NIH_Scores_NIH7_Card(si) T_NIH.NIH_Scores_NIH7_Comp(si) T_NIH.NIH_Scores_NIH7_Flanker(si) T_NIH.NIH_Scores_NIH7_List(si) T_NIH.NIH_Scores_NIH7_Pattern(si) T_NIH.NIH_Scores_NIH7_Picture(si)];
end
end
%% Identify NIH toolbox scores
for subji = 1:length(EID_included_asd)
si = find(contains(T_NIH.NIH_Scores_EID,EID_included{subji}));
if isempty(si)
NIH_included(subji,:) = NaN(1,6);
else
NIH_included(subji,:) = [T_NIH.NIH_Scores_NIH7_Card(si) T_NIH.NIH_Scores_NIH7_Comp(si) T_NIH.NIH_Scores_NIH7_Flanker(si) T_NIH.NIH_Scores_NIH7_List(si) T_NIH.NIH_Scores_NIH7_Pattern(si) T_NIH.NIH_Scores_NIH7_Picture(si)];
end
end
%%
% Plot results
figure; scatter(Age_included,NIH_included(:,5),20,Sex_included,'filled'); colormap([1 0 0; 0 0 1])
xlabel('Age');ylabel('NIH All Test')
ylim([0 200]);
legend('male','female')
title('No diagnosis')
md2 = fitlm(Age_included,NIH_included(:,6));
%% Stats of Analysis
%T(:,ismember(T.Properties.VariableNames, {""list of variables""})
a = contains(T_NIH.Properties.VariableNames,'NIH7_Card');
b = contains(T_NIH.Properties.VariableNames,'NIH7_Comp');
d = contains(T_NIH.Properties.VariableNames,'NIH7_Flanker');
e = contains(T_NIH.Properties.VariableNames,'NIH7_List');
f = contains(T_NIH.Properties.VariableNames,'NIH7_Pattern');
g = contains(T_NIH.Properties.VariableNames,'NIH7_Picture');
Cb = T_NIH(:,a);
type = T_NIH(:,g);
type1 = table2array(type);
RT = T_NIH(:,f);
N = T_NIH.NIH_Scores_EID; %specifying EID of T_flanker
%{
O = ismember(N,EID_included);%logical array of EID of T_box == EID_included
O1 = ismember(N,EID_included_asd);
%}
O = ismember(EID_included,N);%logical array of EID of T_box == EID_included
O1 = ismember(EID_included_asd,N);
Q = ismember(EID_included,N);%logical array of EID_included == EID of T_Box
Q1 = ismember(EID_included_asd,N);
x = Age_included(Q);
x1 = Age_included(Q1);
y1 = var(O);
y2 = var(O1);
z = Sex_included(Q);
z2 = Sex_included(Q1);
y = Sex_included(O);
y_asd = Sex_included_asd(O1);
y1dup = repmat(y1, size(x));
y2dup = repmat(y2, size(x1));
%%
scatter(x,y1dup,20,z,'filled');
colormap([1 0 0; 0 0 1])
hold on
scatter(x1,y2dup,20,z2,'filled');
title('NIH')
xlabel('Age');ylabel('RCS')
legend('no diagnosis','asd')
md1 = fitlm(x,y1dup);
md2 = fitlm(x1,y2dup);
plot(md1)
plot(md2)
%% Variance Data
%based on gender
male = Sex_included(Q);
female = ~male;
male_asd = Sex_included_asd(Q1);
female_asd = ~male_asd;
male_variance = var(logical(male));
male_variance_asd = var(logical(male_asd));
female_variance = var(female);
female_variance_asd = var(female_asd);
x_male = x(logical(male));
x_male_asd = x1(logical(male_asd));
x_female = x(female);
x_female_asd = x1(female_asd);
figure(4)
mvdup = repmat(male_variance, size(x_male));
mvadup = repmat(male_variance_asd, size(x_male_asd));
scatter(x_male,mvdup,20,'filled','r');
hold on
scatter(x_male_asd,mvadup,20,'filled','b');
md_male = fitlm(x_male,mvdup);
md_male_asd = fitlm(x_male_asd,mvadup);
plot(x_male,md_male.Coefficients.Estimate(1)+md_male.Coefficients.Estimate(2)*x_male,'r');
plot(x_male_asd,md_male_asd.Coefficients.Estimate(1)+md_male_asd.Coefficients.Estimate(2)*x_male_asd,'b');
legend('no diagnosis male','asd male','fitlm male','fitlm male asd')
figure(5)
fvdup = repmat(female_variance, size(x_female));
fvadup = repmat(female_variance_asd, size(x_female_asd));
scatter(x_female,fvdup,20,'filled','r');
hold on
scatter(x_female_asd,fvadup,20,'filled','b');
md_female = fitlm(x_female,fvdup);
md_female_asd = fitlm(x_female_asd,fvadup);
plot(x_female,md_female.Coefficients.Estimate(1)+md_female.Coefficients.Estimate(2)*x_female,'r');
plot(x_female_asd,md_female_asd.Coefficients.Estimate(1)+md_female_asd.Coefficients.Estimate(2)*x_female_asd,'b');
legend('no diagnosis female','asd female','fitlm female','fitlm female asd')
figure(6)
x_male_total = [x_male; x_male_asd];
male_variance_total = [mvdup; mvadup];
scatter(x_male_total,male_variance_total,20,'filled','b');
hold on
md_male_total = fitlm(x_male_total,male_variance_total);
plot(x_male_total,md_male_total.Coefficients.Estimate(1)+md_male_total.Coefficients.Estimate(2)*x_male_total,'b');
x_female_total = [x_female; x_female_asd];
female_variance_total = [fvdup; fvadup];
scatter(x_female_total,female_variance_total,20,'filled','r');
hold on
md_female_total = fitlm(x_female_total,female_variance_total);
plot(x_female_total,md_female_total.Coefficients.Estimate(1)+md_female_total.Coefficients.Estimate(2)*x_female_total,'r');
legend('all male','fitlm male','all female','fitlm female')
T = [mean(variance) mean(variance_asd) mean(male_variance) mean(male_variance_asd) mean(female_variance) mean(female_variance_asd) mean(male_variance_total) mean(female_variance_total)];
Names ={'variance','variance_asd','male_variance','male_variance_asd','female_variance','female_variance_asd','male_variance_total','female_variance_total'};

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by