-
Notifications
You must be signed in to change notification settings - Fork 0
/
fitlm_model.m
55 lines (39 loc) · 1.45 KB
/
fitlm_model.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
function Results = fitlm_model(cal,val,rem_outlier,show)
if ~exist('show', 'var')
show = 0;
end
tr_mdl = fitlm(cal(:,2:end),cal(:,1),'RobustOpts','on');%
pred_val = predict(tr_mdl,val(:,2:end));
pred_cal = predict(tr_mdl,cal(:,2:end));
if rem_outlier
cal_residuals = cal(:,1)-pred_cal;
val_residuals = val(:,1)-pred_val;
disp('outlier index')
idx_cal = find(isoutlier(cal_residuals,'grubbs'))
idx_val = find(isoutlier(val_residuals,'grubbs'))
cal(idx_cal,:)=[];
val(idx_val,:)=[];
num_outlier = length([idx_cal;idx_val]);
tr_mdl = fitlm(cal(:,2:end),cal(:,1),'RobustOpts','on');% ,'RobustOpts','on'
pred_val = predict(tr_mdl,val(:,2:end));
% pred_cal = predict(tr_mdl,cal(:,2:end));
else
num_outlier = [];
end
mdl_pr = fitlm(pred_val,val(:,1),'RobustOpts','on');
Rc2 = tr_mdl.Rsquared.Ordinary;
Rp2 = mdl_pr.Rsquared.Ordinary;
RMSEc = tr_mdl.RMSE;
RMSEp = mdl_pr.RMSE;
Results = [];
Results.model = tr_mdl;
Results.cal.real = cal;
Results.cal.prediction = tr_mdl.Fitted;
Results.val.real = val;
Results.val.prediction = pred_val;
Results.performance = [Rc2,RMSEc,Rp2,RMSEp];
Result.numOutlier = num_outlier;
if show == 1
plot_results(Results, '%')
plot_residuals(Results)
end