-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUneven_vectors_accuracy.m
213 lines (200 loc) · 5.38 KB
/
Uneven_vectors_accuracy.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
%% Uneven_vectors_accuracy.m
% Script to compare the prediction accuracy of D3 & D4 using uneven vector techniques.
% With a few modifications (removing the reading process of a dataset)
% it can calculate the accuracy of different data-sets.
% The predictions change substantially to the published due to yield normalization
% for confidenciality protection.
%% Copyright
% Carlos Alberto Duran-Villalobos June 2020 University of Manchester.
% Data provided by UCL and Sutro
% Copyright (c) Future Targeted Healthcare Manufacturing Hub
% Reference: "Multivariate statistical data analysis of cell-free protein synthesis towards monitoring and control", AIChE
clear all;
%% Initialize variables
X=[]; %Matrix of PCA variables
Y=[]; %Response variables
%% Read process measurements D3
yield3=xlsread('D3',1,'M2:M25'); %Read files
rtime3= xlsread('D3',1,'L2:L25');
DO = xlsread('D3',2,'B2:Y490')';
pH = xlsread('D3',3,'B2:Y490')';
T = xlsread('D3',4,'B2:Y490')';
% Averaging every hour
time=8;
sampling=480;
step=floor(sampling/time);
DO23=[];
pH23=[];
T23=[];
for i=1:step:sampling-step+1
DO23=[DO23 mean(DO(:,i:i+step-1),2) ];
pH23=[pH23 mean(pH(:,i:i+step-1),2) ];
T23=[T23 mean(T(:,i:i+step-1),2) ];
end
%% Read process measurements D4
yield4=xlsread('D4',1,'M2:M25'); %Read files
yield4(4)=[]; %Remove not measured opbservations
rtime4= xlsread('D4',1,'L2:L25');
rtime4(4)=[];
DO = xlsread('D4',2,'B2:Y721')';
DO(4,:)=[];
pH = xlsread('D4',3,'B2:Y721')';
pH(4,:)=[];
T = xlsread('D4',4,'B2:Y721')';
T(4,:)=[];
% Averaging every hour
time=12;
sampling=720;
step=floor(sampling/time)-1;
DO24=[];
pH24=[];
T24=[];
for i=1:step:sampling-step+1
DO24=[DO24 mean(DO(:,i:i+step-1),2) ];
pH24=[pH24 mean(pH(:,i:i+step-1),2) ];
T24=[T24 mean(T(:,i:i+step-1),2) ];
end
yield=[yield3;yield4];
rtime=[rtime3;rtime4];
%% Preditions using fist 8h (Approach 1)
time=8;
X=[];
for i=1:1:length(yield3)
Xn=[];
for n=1:1:time
Xn = [Xn pH23(i,n) DO23(i,n) T23(i,n)];
end
X=[X;Xn];
end
for i=1:1:length(yield4)
Xn=[];
for n=1:1:time
Xn = [Xn pH24(i,n) DO24(i,n) T24(i,n)];
end
X=[X;Xn];
end
X=[X, rtime];
batches=length(yield);
Y=[];
for i=1:1:batches
Yn=[];
Yn = [Yn yield(i)];
Y=[Y;Yn];
end
[X2,xmean,xstd]=zscore(X);
[Y2,ymean,ystd]=zscore(Y);
cv=plscv(X2,Y2,5,10); %10 fold cross validation
lv=cv.optLV; %Obtain the best LV
%calculate predictions
yest=[];
yest2=[];
yest3=[];
for i=1:1:batches
yest=[yest; predm(X,Y,i,batches,lv) ]; %PLS
yest2=[yest2; predpoly(X,Y,i,lv,2) ]; %QPLS
yest3=[yest3; predlinear(X,Y,i,batches) ];%OLS
end
%calculate SMAPE
msemlrf=100*sum(abs(Y-yest3)./(0.5*(abs(Y)+abs(yest3))))/batches;
mseplsf=100*sum(abs(Y-yest)./(0.5*(abs(Y)+abs(yest))))/batches;
mseqplsf=100*sum(abs(Y-yest2)./(0.5*(abs(Y)+abs(yest2))))/batches;
%% Preditions using last 8h (Approach 2)
time=8;
X=[];
for i=1:1:length(yield3)
Xn=[];
for n=1:1:time
Xn = [Xn pH23(i,n) DO23(i,n) T23(i,n)];
end
X=[X;Xn];
end
time=12;
for i=1:1:length(yield4)
Xn=[];
for n=5:1:time
Xn = [Xn pH24(i,n) DO24(i,n) T24(i,n)];
end
X=[X;Xn];
end
X=[X, rtime];
Y=[];
for i=1:1:batches
Yn=[];
Yn = [Yn yield(i)];
Y=[Y;Yn];
end
[X2,xmean,xstd]=zscore(X);
[Y2,ymean,ystd]=zscore(Y);
cv=plscv(X2,Y2,5,10); %10 fold cross validation
lv=cv.optLV; %Obtain the best LV
%calculate predictions
yest=[];
yest2=[];
yest3=[];
for i=1:1:batches
yest=[yest; predm(X,Y,i,batches,lv) ]; %PLS
yest2=[yest2; predpoly(X,Y,i,lv,2) ]; %QPLS
yest3=[yest3; predlinear(X,Y,i,batches) ];%OLS
end
%calculate SMAPE
msemlrl=100*sum(abs(Y-yest3)./(0.5*(abs(Y)+abs(yest3))))/batches;
mseplsl=100*sum(abs(Y-yest)./(0.5*(abs(Y)+abs(yest))))/batches;
mseqplsl=100*sum(abs(Y-yest2)./(0.5*(abs(Y)+abs(yest2))))/batches;
%% Preditions using missing data techniques (Approach 3)
time=12;
X=[];
for i=1:1:23
Xn=[];
for n=1:1:time
Xn = [Xn pH24(i,n) DO24(i,n) T24(i,n)];
end
X=[X;Xn];
end
[X2,xmean,xstd]=zscore(X);
%calculate future values for D3
[P,T,latent,tsquared,explained,mu] = pca(X2);
pc=3; %Number of PC from explained
Xnew=[];
time=8;
for i=1:1:24
Xn=[];
for n=1:1:time
Xn = [Xn pH23(i,n) DO23(i,n) T23(i,n)];
end
Xnew=[Xnew;Xn];
end
Xnew2=(Xnew-xmean(1:24))./xstd(1:24);
Tnew=(P(1:24,1:3)'*P(1:24,1:3))\P(1:24,1:3)'*Xnew2';
Xnewfuture=Tnew'*P(25:end,1:3)';
Xnewf=Xnewfuture.*xstd(25:end)+xmean(25:end);
Xn2=[Xnew Xnewf]; %Add future values to D3
X=[X;Xn2];
X=[X,rtime];
[X2,xmean,xstd]=zscore(X);
[Y2,ymean,ystd]=zscore(Y);
cv=plscv(X2,Y2,5,10); %10 fold cross validation
lv=cv.optLV; %Obtain the best LV
%calculate predictions
yest=[];
yest2=[];
yest3=[];
for i=1:1:batches
yest=[yest; predm(X,Y,i,batches,lv) ]; %PLS
yest2=[yest2; predpoly(X,Y,i,lv,2) ]; %QPLS
yest3=[yest3; predlinear(X,Y,i,batches) ];%OLS
end
%calculate SMAPE
msemlrm=100*sum(abs(Y-yest3)./(0.5*(abs(Y)+abs(yest3))))/batches;
mseplsm=100*sum(abs(Y-yest)./(0.5*(abs(Y)+abs(yest))))/batches;
mseqplsm=100*sum(abs(Y-yest2)./(0.5*(abs(Y)+abs(yest2))))/batches;
%% Plot accurace errors
mse=[msemlrf mseplsf mseqplsf ; msemlrl mseplsl mseqplsl ; msemlrm mseplsm mseqplsm ];
figure(5)
bar(mse)
hold on;
xticklabels({'First 8h','Last 8h','Missing data'})
xtickangle(45)
ylabel('RMSE of prediction %');
legend({'MLR','PLS','QPLS'})
grid on
set(findall(gcf,'-property','FontSize'),'FontSize',28)