-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolyMultipleLin.m
More file actions
284 lines (228 loc) · 9.06 KB
/
Copy pathpolyMultipleLin.m
File metadata and controls
284 lines (228 loc) · 9.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
%redirecting folder to correct path. clear.
clear all; clc;
%define measure
measure = 'fa';
%insert local path of Tshort.csv and Tlong.csv file
Tshort = '/Users/land/Desktop/projectTrackProfiles/supportFiles/Tshort.csv';
Tlong = '/Users/land/Desktop/projectTrackProfiles/supportFiles/Tlong.csv';
colorProfiles = '/Users/land/Desktop/projectTrackProfiles/supportFiles/colorProfiles.csv';
rsqTableAdj = '/Users/land/Desktop/projectTrackProfiles/supportFiles/rsqTableAdj.csv';
rsqTableOrd = '/Users/land/Desktop/projectTrackProfiles/supportFiles/rsqTableOrd.csv';
aicTable = '/Users/land/Desktop/projectTrackProfiles/supportFiles/aicTable.csv';
%convert csv into a table.
Tshort = readtable(Tshort);
Tlong = readtable(Tlong);
colorProfiles = readtable(colorProfiles);
rsqTableAdj = readtable(rsqTableAdj);
rsqTableOrd = readtable(rsqTableOrd);
aicTable = readtable(aicTable);
%============== Generate Plots ==============
%generate column of tracts of interest ids
mask = ismember(Tlong.structureID, colorProfiles{:, 1});
tractIDs = Tlong(mask, :);
tractIDs = unique(tractIDs.structureID);
rsqSimpleLin = table(tractIDs);
%close all previous plots
close all
for t = 1:length(tractIDs)
f = figure(t);
%startingx, startingy, width height
f.Position = [1000 1000 800 700];
hold on
%defining variables
Age = Tshort.Age;
%define sex as a categorical variable.
Sex = categorical(Tshort.Sex);
yVar = Tshort.(char(tractIDs(t)));
tbl = table(Age, Sex, yVar);
tbl(any(ismissing(tbl), 2), :) = [];
Age = tbl.Age;
Sex = tbl.Sex;
yVar = tbl.yVar;
%replace all outliers with zero
%remove outliers from yVar that is more than 3 sd from the mean
yVar = filloutliers(yVar, 0, "mean");
tbl = table(Age, Sex, yVar);
%delete rows with missing data and yVar = 0 (outliers)
tbl(any(ismissing(tbl), 2), :) = [];
tbl(~yVar, :) = [];
%defining the line to fit the model to
Q = 'yVar ~ Age + Sex';
%generating the model
mdl = fitlm(tbl, Q);
%get appropriate RGB color for tract by indexing into colorProfiles.csv
idx = find(strcmp(colorProfiles.NameOfTrack, char(tractIDs(t))) == 1);
markerColor = [colorProfiles.Red(idx)/255, colorProfiles.Green(idx)/255, colorProfiles.Blue(idx)/255];
%plotting the model
h = plotAdjustedResponse(mdl, 'Age', 'MarkerEdgeColor', markerColor, 'MarkerFaceColor', markerColor);
pltLeg = legend('', '', '');
set(pltLeg,'visible','off')
%get data for plotting the confidence intervals and add CI to plot.
j = array2table(cat(2, h(1).XData', h(1).YData')); j.Properties.VariableNames = {'x', 'y'};
mdlci = fitlm(j, 'y~x');
clear f;
%======================================================================
%Outliers
outliers = [];
% Examine model residuals: boxplot of raw residuals.
figure(t + length(tractIDs)); k = figure('visible', 'off');
m = mdlci.Residuals.Raw;
e = eps(max(m(:)));
boxplot(m)
% ylabel('Raw Residuals')
% Suppress figure display.
set(gcf,'Visible','off');
set(0,'DefaultFigureVisible','off');
%
% Get indices of the outliers.
h1 = flipud(findobj(gcf,'tag','Outliers')); % flip order of handles
for jj = 1 : length( h1 )
x = get( h1(jj), 'XData' );
y = get( h1(jj), 'YData' );
for ii = 1 : length( x )
if not( isnan( x(ii) ) )
ix = find( abs( m(:,jj)-y(ii) ) < e );
outliers = cat(1, outliers, ix);
% text( x(ii), y(ii), sprintf( '\\leftarrowY%02d', ix ) )
end
end
end
%
k = gcf; close(k);
%
% Examine robust weights: boxplot of robust weights.
figure(t + length(tractIDs) + 1); k = figure('visible', 'off');
m = mdlci.Robust;
e = eps(max(m(:)));
boxplot(m);
% ylabel('Robust Beta-Weights')
% Suppress figure display.
set(gcf, 'Visible', 'off');
set(0, 'DefaultFigureVisible', 'off');
%
% Get indices of the outliers.
h1 = flipud(findobj(gcf,'tag','Outliers')); % flip order of handles
for jj = 1 : length( h1 )
x = get( h1(jj), 'XData' );
y = get( h1(jj), 'YData' );
for ii = 1 : length( x )
if not( isnan( x(ii) ) )
ix = find( abs( m(:,jj)-y(ii) ) < e );
outliers = cat(1, outliers, ix);
% text( x(ii), y(ii), sprintf( '\\leftarrowY%02d', ix ) )
end
end
end
%
outliers = sort(outliers);
%
k = gcf; close(k);
%
k = figure('visible', 'on');
set(gcf, 'Visible', 'off');
set(0, 'DefaultFigureVisible', 'off');
%
%======================================================================
clf;
%Remove outliers
tbl(outliers, :) = [];
%recalculate the model
%generating the model
mdl = fitlm(tbl, Q);
%plotting the model
h = plotAdjustedResponse(mdl, 'Age', 'MarkerEdgeColor', markerColor, 'MarkerFaceColor', markerColor);
pltLeg = legend('', '', '');
set(pltLeg,'visible','off')
z = get(gca, 'children');
set(0, 'DefaultFigureVisible', 'off');
%get data for plotting the confidence intervals and add CI to plot.
j = array2table(cat(2, h(1).XData', h(1).YData')); j.Properties.VariableNames = {'x', 'y'};
mdlci = fitlm(j, 'y~x');
clf(figure(t));
f = figure(t);
%startingx, startingy, width height
f.Position = [1000 1000 800 700];
hold on
pci = plot(mdlci);
set(pci, 'MarkerEdgeColor', 'white', 'MarkerFaceColor', markerColor, 'MarkerSize', 12, 'Marker', 'o')
x = tbl.Age; y = tbl.yVar; CI = (tbl.yVar)/2;
%fill in confidence interval
cbHandles = findobj(pci,'DisplayName','Confidence bounds');
cbHandles = findobj(pci,'LineStyle', cbHandles.LineStyle, 'Color', cbHandles.Color);
upperCBHandle = cbHandles(2,:);
lowerCBHandle = cbHandles(1,:);
xData = upperCBHandle.XData;
k = patch([xData xData(end:-1:1) xData(1)], [lowerCBHandle.YData upperCBHandle.YData(end:-1:1) lowerCBHandle.YData(1)], 'b');
set(k, 'EdgeColor', 'none', 'FaceColor', [markerColor(1)*0.55 markerColor(2)*0.55 markerColor(3)*0.55], 'FaceAlpha', '0.2')
%grab trendline and datapoints
dataHandle = findobj(h,'DisplayName','data');
fitHandle = findobj(h,'DisplayName','fit');
dataHandle2 = findobj(pci,'DisplayName','Data');
fitHandle2 = findobj(pci,'DisplayName','Fit');
%style the trendline
set(fitHandle2, 'Color', [markerColor(1) markerColor(2) markerColor(3)], 'LineWidth', 3)
%w = plot(mdl, 'Marker', 'o', 'MarkerFaceColor', markerColor, 'MarkerSize', 12);
pltLeg = legend('', '', '');
set(fitHandle2, 'Marker', 'none')
set(pltLeg,'visible','off')
%set(fitHandle, 'Visible', 'off')
%set(dataHandle, 'Visible', 'off')
%set(h, 'Visible', 'off')
plot(pci(1).XData, pci(1).YData, 'MarkerEdgeColor', 'white', 'MarkerFaceColor', markerColor, 'MarkerSize', 12, 'Marker', 'o', 'LineStyle', 'none')
%delete confidence bounds border
delete(pci(3))
delete(pci(4))
%===========================================================================
% Set up plot and measure-specific details.
capsize = 0;
marker = 'o';
linewidth = 1.5;
linestyle = 'none';
markersize = 100;
xtickvalues = [1 2 3 4];
xlim_lo = min(xtickvalues)-0.5; xlim_hi = max(xtickvalues)+0.5;
fontname = 'Arial';
fontsize = 50;
fontangle = 'italic';
yticklength = 0;
xticklength = 0.02;
% xaxis
xax = get(gca, 'xaxis');
xax.TickDirection = 'out';
xax.TickLength = [xticklength xticklength];
set(gca, 'XLim', [3 22], 'XTick', [3 12.5 22]);
xax.FontName = fontname;
xax.FontSize = fontsize;
% yaxis
yax = get(gca,'yaxis');
yax.TickDirection = 'out';
yax.TickLength = [yticklength yticklength];
set(gca, 'YLim', [0.3 0.6], 'YTick', [0.3 0.45 0.6]);
yax.FontName = fontname;
yax.FontSize = fontsize;
yax.FontAngle = fontangle;
%change figure background to white
set(gcf, 'color', 'w')
%===========================================================================
hold off
%adding title and color to the model
plotTitle = {char(tractIDs(t))};
plotTitle = strjoin(['Multiple Linear Model for', plotTitle]);
title(plotTitle);
xlabel('Age (years)');
ylabel(measure);
%add adjusted r squared and aic to table.
rsqTableAdj.MultLin(t) = mdlci.Rsquared.Adjusted;
rsqTableOrd.MultLin(t) = mdlci.Rsquared.Ordinary;
aicTable.MultLin(t)= mdlci.ModelCriterion.AIC;
end
%============== Export rsqTables and aicTable as a csv ==============
%local path to save table:
mainpath = '/Users/land/Desktop/projectTrackProfiles/supportFiles';
table_path_format_rsqTAdj = fullfile(mainpath, 'rsqTableAdj.csv');
table_path_format_rsqTOrd = fullfile(mainpath, 'rsqTableOrd.csv');
table_path_format_aicTable = fullfile(mainpath, 'aicTable.csv');
%funally, save tables
writetable(rsqTableAdj, table_path_format_rsqTAdj);
writetable(rsqTableOrd, table_path_format_rsqTOrd);
writetable(aicTable, table_path_format_aicTable);