Repository navigation
Expand file tree
/
Copy pathfast_fs.m
More file actions
112 lines (90 loc) · 2.6 KB
/
Copy pathfast_fs.m
File metadata and controls
112 lines (90 loc) · 2.6 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
function output = fast_fs(tabela, dep, n, T, sfa, init_var, dec_cr)
%init_var = 2; %number of fixed vars including the constant
% dec_cr = 0; % BIC based + ML
% dec_crit = 1 mdd + bayesian estimates
% dec_crit = 2 aic + ML
% opt = 0; opt = 1 is mdd;
% sfa - rodzaj modelu
% init_var - ile zmiennych x ma być na siłę uwzględnione w modelu
%n
%T
if dec_cr == 1
if_mdd = 1;
else
if_mdd = 0;
end
% data prep
if istable(tabela)
%kowersja do tablicy
X = table2array(tabela);
nazwy = tabela.Properties.VariableNames;
%typowanie zmiennych ważnych; tu zakładam x0 (zawsze) oraz x5
else
X = tabela;
end
if istable(dep)
%kowersja do tablicy
y = table2array(dep);
%y_lab = dep.Properties.VariableNames;
%typowanie zmiennych ważnych; tu zakładam x0 (zawsze) oraz x5
else
y = dep;
end
%na początku są tylko zmienne które muszą być w modelu
X0 = X(:,1:init_var);
X_cand = X(:,init_var+1:end);
p = size(X_cand,2);
r = corr(X_cand, y);
[~, idx] = sort(abs(r), 'descend');
included = zeros(1,p);
remaining = idx; % ordered candidate list
current_vars = [];
%initial model:
mdl = sfa_fit_rep(X0,y,n,T,sfa,dec_cr,if_mdd);
current_BIC = mdl.inf_cr;
cykli = 0;
%%
tic;
while ~isempty(remaining)
best_BIC = Inf;
best_var = -1;
% Try each remaining variable (in correlation order)
for t = 1:length(remaining)
cykli = cykli+1;
j = remaining(t);
vars = [current_vars j];
Xtemp = [X0 X_cand(:,vars)];
mdl_curr = sfa_fit_rep(Xtemp,y,n,T,sfa,dec_cr,if_mdd);
BIC = mdl_curr.inf_cr;
if BIC < best_BIC
best_BIC = BIC;
best_var = j;
end
end
% Check improvement
if best_BIC < current_BIC
current_BIC = best_BIC;
current_vars = [current_vars best_var];
included(best_var) = 1;
% remove from remaining
remaining(remaining == best_var) = [];
else
%this is were we break, so not a full FS!
break
end
end
X_best = [X0, X_cand(:,logical(included))];
mdl_best = sfa_fit_rep(X_best,y,n,T,sfa,dec_cr,if_mdd);
if istable(tabela)
labels_best = nazwy(logical([ones(1,init_var) included]));
else
labels_best = [ones(1,init_var) included];
end
%output- strukura z wynikami:
%output.best_model - mdl
%output.included - zm. sztuczne z uwględnionymi zmiennymi
%output.labels_bets - etykiety tych zmiennych
output.best_model = mdl_best;
output.included = [ones(1,init_var) included];
output.labels_best = labels_best;
end