-
Notifications
You must be signed in to change notification settings - Fork 3
/
Cholesterol_predict.m
328 lines (255 loc) · 8.01 KB
/
Cholesterol_predict.m
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
% ============================================================================
% Problem: Current practice of cholesterol measurements is based on separating
% cholesterol into fractions. However, it was suggested that the
% cholesterol levels could be measured directly from measurements of
% the spectral content of a blood serum sample. In this case study,
% we can use measurements of 21 wavelengths of the spectrum for 264
% patients, and reference measurements of cholesterol in lipoprotein
% fractions for the same patients.
% We are required to select a data mining tool to solve this problem.
% ============================================================================
%
% Hit any key to continue.
pause
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Part 1: The ANN application
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% It appears that a back-propagation neural network can be trained to predict
% cholesterol levels in HDL (high density), LDL (low density) and VLDL (very
% low density) fractions given the measurements of 21 wavelengths.
%
% Hit any key to load the cholesterol data.
pause
[p,t] = Cholesterol_data;
echo off
for i=1:21
for j=1:263
p(j,i)=p(j,i)-mean(p(:,i));
end
end
p=p'; t=t';
rand('seed',2336);
echo on
% Hit any key to randomly divide the data into training (60%), validation (20%)
% and test (20%) sets. A validation set is used to stop training early -
% the network is trained on the training data until its performance on the
% validation data fails to improve for a number of consecutive epochs.
pause
[trainP,valP,testV,trainInd,valInd,testInd] = dividerand(p,0.6,0.2,0.2);
[trainT,valT,testT] = divideind(t,trainInd,valInd,testInd);
% Hit any key to define the network architecture.
pause
s1=7; % Five neurons in the hidden layer
% Hit any key to create the network, initialise its weights and biases,
% and set up training parameters.
pause
net = newff(p,t,s1);
net.trainParam.show = 20; % Number of epochs between showing the progress
net.trainParam.epochs = 1000; % Maximum number of epochs
net.trainParam.goal = 0.01; % Performance goal
net.trainParam.lr=0.1; % Learning rate
% Hit any key to train the back-propagation network.
pause
[net,tr] = train(net,p,t);
plotperform(tr);
% Hit any key to perform regression analysis between the desired outputs
% and the actual outputs of the network.
pause
test_p=p(:,tr.testInd);
test_t=t(:,tr.testInd);
a=sim(net,test_p);
figure
h=plot(a(1,:),test_t(1,:),'bo','markersize',3);
lsline;
set(refline([1 1]),'Color','r');
R1 = corrcoef(a(1,:),test_t(1,:)); R1=R1(1,2);
axis('image');
title(['r = ',num2str(R1)]);
xlabel('HDL Desired output');
ylabel('HDL Actual output');
% Hit any key to continue.
pause
figure
plot(a(2,:),test_t(2,:),'bo','markersize',3);
lsline;
set(refline([1 1]),'Color','r');
R2 = corrcoef(a(2,:),test_t(2,:)); R2=R2(1,2);
axis('image');
title(['r = ',num2str(R2)]);
xlabel('LDL Desired output');
ylabel('LDL Actual output');
% Hit any key to continue.
pause
figure
plot(a(3,:),test_t(3,:),'bo','markersize',3);
lsline;
set(refline([1 1]),'Color','r');
R3 = corrcoef(a(3,:),test_t(3,:)); R3=R3(1,2);
axis('image');
title(['r = ',num2str(R3)]);
xlabel('VLDL Desired output');
ylabel('VLDL Actual output');
% To improve the accuracy of the cholesterol level prediction, we can use
% an ANFIS model. However, before we can apply it, we need to reduce the
% number of inputs. This task can be accomplished by PCA.
%
% Hit any key to continue.
pause
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Part 2: The PCA & ANFIS application
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Hit any key to load the cholesterol data.
pause
[p,t] = Cholesterol_data;
echo off
for i=1:21
for j=1:263
x(j,i)=p(j,i)-mean(p(:,i));
end
end
echo on
% Hit any key to perform PCA.
pause
C=cov(x);
lambda=eig(C); % The eigenvalues
lambda=sort(lambda,'descend');
variance=lambda*100/sum(lambda);
% Hit any key to display the scree plot for the 21-dimensional spectrum data set.
pause
figure
plot(variance); axis([1 21 0 100]);
title('Scree plot for the 21-dimensional spectrum data set');
xlabel('Eigenvalue number');
ylabel('Variance, %');
[V,D] = eig(C); % V is the matrix with the eigenvectors
V=V(:,21:-1:1);
echo off
fin=x*V;
data=fin*V';
for i=1:21
for j=1:263
data(j,i)=data(j,i)+mean(p(:,i));
end
end
V1=[V(:,1) V(:,2) V(:,3) V(:,4)];
fin1=x*V1;
data1=fin1*V1';
for i=1:21
for j=1:263
data1(j,i)=data1(j,i)+mean(p(:,i));
end
end
echo on
% Hit any key to continue.
pause
% Hit any key to randomly select inputs and outputs to be used in ANFIS training.
% For this study, we randomly split the transformed data into two sets: training
% (80%) and test (20%).
pause
echo off
tr_p=[]; tr_t=[]; test_p=[]; test_t=[];
[a b]=size(fin1);
for n=1:a
if rand(1)>1/3
tr_p=[tr_p; fin1(n,:)];
tr_t=[tr_t; t(n,:)];
else
test_p=[test_p; fin1(n,:)];
test_t=[test_t; t(n,:)];
end
end
[m n]=size(test_p);
disp(' ')
fprintf(1,' The training data set contains %.0f elements.\n',(a-m));
fprintf(1,' The test data set contains %.0f elements.\n',m);
disp(' ')
echo on
% Hit any key to define the number of membership functions to be assigned to
% input variable, type of the membership function, and the number of
% training epochs.
pause
numMFs = 2; % Number of membership functions assigned to each input variable
mfType = 'gbellmf'; % Type of the membership function
epoch_n = 10; % Number of training epochs
% As an ANFIS architecture permits only one output, we will use three identical
% models, each predicting cholesterol in a single lipoprotein fraction.
%
% Hit any key to generate the ANFIS model for HDL prediction.
pause
trnData1 = [tr_p tr_t(:,1)];
in_fismat1 = genfis1(trnData1,numMFs,mfType);
% Hit any key to train the ANFIS to predict HDL.
pause
out_fismat1 = anfis(trnData1,in_fismat1,epoch_n);
% Hit any key to generate the ANFIS model for LDL prediction.
pause
trnData2 = [tr_p tr_t(:,2)];
in_fismat2 = genfis1(trnData2,numMFs,mfType);
% Hit any key to train the ANFIS to predict LDL.
pause
out_fismat2 = anfis(trnData2,in_fismat2,epoch_n);
% Hit any key to generate the ANFIS model for VLDL prediction.
pause
trnData3 = [tr_p tr_t(:,3)];
in_fismat3 = genfis1(trnData3,numMFs,mfType);
% Hit any key to train the ANFIS to predict VLDL.
pause
out_fismat3 = anfis(trnData3,in_fismat3,epoch_n);
% Hit any key to perform regression analysis between the desired outputs
% and the actual outputs of the ANFIS models.
pause
echo off
t_p=test_p; t_t=test_t; test_p=[]; test_t=[]; num=0;
[a b]=size(t_p);
for nrow=1:a
for ncol=1:b
if t_p(nrow,ncol)>=min(tr_p(:,b))& t_p(nrow,ncol)<=max(tr_p(:,b));
num=num+1;
end
if num==b;
test_p=[test_p; t_p(nrow,:)];
test_t=[test_t; t_t(nrow,:)];
end
end
num=0;
end
test_p=[test_p(:,:); tr_p(:,:)]; test_t=[test_t(:,:);tr_t(:,:)];
a1=evalfis(test_p,out_fismat1);
a2=evalfis(test_p,out_fismat2);
a3=evalfis(test_p,out_fismat3);
echo on
figure
plot(a1,test_t(:,1),'bo','markersize',3);
lsline;
set(refline([1 1]),'Color','r');
R1 = corrcoef(a1,test_t(:,1)); R1=R1(1,2);
axis('image');
title(['r = ',num2str(R1)]);
xlabel('HDL Desired output');
ylabel('HDL Actual output');
% Hit any key to continue.
pause
figure
plot(a2,test_t(:,2),'bo','markersize',3);
lsline;
set(refline([1 1]),'Color','r');
R2 = corrcoef(a2,test_t(:,2)); R2=R2(1,2);
axis('image');
title(['r = ',num2str(R2)]);
xlabel('LDL Desired output');
ylabel('LDL Actual output');
% Hit any key to continue.
pause
figure
plot(a3,test_t(:,3),'bo','markersize',3);
lsline;
set(refline([1 1]),'Color','r');
R3 = corrcoef(a3,test_t(:,3)); R3=R3(1,2);
axis('image');
title(['r = ',num2str(R3)]);
xlabel('VLDL Desired output');
ylabel('VLDL Actual output');
echo off
disp('end of Cholesterol_predict')