-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulation.m
More file actions
330 lines (258 loc) · 11.6 KB
/
Copy pathSimulation.m
File metadata and controls
330 lines (258 loc) · 11.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
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
329
330
%% simulation studies
% This file contains examples about how to use the functions to fit the
% relative shift models (RS-ES, RS-L1, RS-CL2, RS-DL2) and reproduce simulation results in the manuscript
%
% Full comparison with competiting methods (LC-Lasso, Lin et al., 2014; KPR
% Randolph et al., 2018) requires exporting simulated data into R and
% implementing the methods in R
% by Gen Li
clear all;
close all;
clc;
rng(20200630)
addpath(genpath(['.',filesep]))
%% Study 1: Equi-Sparsity Setting
% 1. RS-ES
% 2. CLR+lasso
% 3. (in R) LR+Lasso, CLR+ridge, CLR+ridge/UniFrac
rng(20200630)
n=500; % 100 training, 400 testing
p=100;
% true and obs compositional matrix
temp=randn(n,p-1);
X_true=bsxfun(@rdivide, [exp(temp),ones(n,1)],sum(exp(temp),2)+1); % logistic normal distribuition, no zero
thres=0.005;
X_obs=Threshold(X_true,thres); % get observed X with excessive zeros
disp(['Avg zero proportion in the observed data is ', num2str(mean(sum(X_obs==0,2)/p))]); % avg zero percentage in each sample, about 25%
% true coefficient (with equi-sparsity)
beta=[-1*ones(20,1);2*ones(10,1);zeros(70,1)];
figure(1);clf;
plot(beta)
% parameters that can be tweaked (SNR, eps, thres)
SNR=1;sigma=sqrt(var(X_true*beta)/SNR)
% sigma=0.1; SNR=var(X_true*beta)/sigma^2
eps=1e-3; % surrogate of zeros for LC
% repeat 100 times
nsim=100;
rec_RSS=zeros(nsim,2); % in-sample prediction
rec_PMSE=zeros(nsim,2); % out-sample prediction
rec_time=zeros(nsim,2); % computing time
% data for R
Ytrain_forR=zeros(100,nsim);
Xtrain_forR=zeros(100,p,nsim);
Ytest_forR=zeros(400,nsim);
Xtest_forR=zeros(400,p,nsim);
for isim=1:nsim
disp(['Running sim #', num2str(isim)]);
E=randn(n,1)*sigma;
Y=X_true*beta+E; % relative shift model, from true X
% separate into 100 training and 400 testing (using obs X)
Ytrain=Y(1:100);
Xtrain=X_obs(1:100,:);
Ytest=Y(101:n);
Xtest=X_obs(101:n,:);
% for LR and CLR methods, replace 0 by eps and renormalize
Xtrain_nozero=max(Xtrain,eps);
Xtrain_nozero=Xtrain_nozero./sum(Xtrain_nozero,2);
Xtest_nozero=max(Xtest,eps);
Xtest_nozero=Xtest_nozero./sum(Xtest_nozero,2);
% RS with equi-sparsity (5-fold CV for tuning selection)
Tstart=tic;
W=ones(p,p); % weight matrix for graph (complete graph without edge-specific weights)
[C, CNorm]=mat2SPGgraph(W);
option.mu=1e-02; % Smoothing Parameter, fixed
option.gammarange=(5.5:0.5:15)*1E-4; % tuning parameter
option.fig=1;
% option.gammarange=0; % no penalty -> vanilla relative shift model
% option.gammarange=0.1; % too much penalty -> beta all equal
[beta_RS,~,gamma_opt,CV] = cv_SPG_cvrt('graph',Ytrain, Xtrain,[], C, CNorm, option);
T1=toc(Tstart);
% figure(1); clf;
% plot(beta,'k'); hold on;
% plot(beta_RS,'r')
% CLR regression with lasso (5-fold CV tuning selection)
% (a naive competitor created by myself)
Tstart=tic;
Xtrain_CLR=log(Xtrain_nozero)-mean(log(Xtrain_nozero),2); % CLR
[beta_naive,info]=lasso(Xtrain_CLR,Ytrain,'cv',5); % lasso with CV for tuning selection
beta_Lasso=beta_naive(:,info.IndexMinMSE);
beta_Lasso0=info.Intercept(info.IndexMinMSE);
T2=toc(Tstart);
% implementation of LC with lasso -- R package CVS (Lin et al, 2014)
% implementation of KPR -- R package KPR (Randolph et al, 2018)
% Use *5-fold CV* and *10-value tuning range* if possible.
% compare
% in-sample residual
res_RS=Ytrain-Xtrain*beta_RS;
res_CLRlasso=Ytrain-beta_Lasso0-Xtrain_CLR*beta_Lasso;
rec_RSS(isim,:)=[sum(res_RS.^2),sum(res_CLRlasso.^2)];
% out-sample prediction
Xtest_CLR=log(Xtest_nozero)-mean(log(Xtest_nozero),2); % CLR
rec_PMSE(isim,:)=[sum((Ytest-Xtest*beta_RS).^2),sum((Ytest-Xtest_CLR*beta_Lasso-beta_Lasso0).^2)];
% time
rec_time(isim,:)=[T1,T2];
% save data for R (no-zero compositional data)
Xtrain_forR(:,:,isim)=Xtrain_nozero;
Xtest_forR(:,:,isim)=Xtest_nozero;
Ytrain_forR(:,isim)=Ytrain;
Ytest_forR(:,isim)=Ytest;
end
save('Sim1.mat','Xtrain_forR','Xtest_forR','Ytrain_forR','Ytest_forR','rec_RSS','rec_PMSE','rec_time');
%% Study 2: Tree-Guided Equi-Sparsity Setting
% 1. RS-DL2
% 2. RS-CL2
% 3. RS-L1
% 4. RS-ES
% 5. CLR+lasso
% 6. (in R) LR+Lasso, CLR+ridge, CLR+ridge/UniFrac, CLR+tree (patristic kernel), CLR+tree/UniFrac
rng(20200630)
n=500; % 100 training, 400 testing
p=100;
% true and obs compositional matrix
temp=randn(n,p-1);
X_true=bsxfun(@rdivide, [exp(temp),ones(n,1)],sum(exp(temp),2)+1); % logistic normal distribuition, no zero
thres=0.005;
X_obs=Threshold(X_true,thres); % get observed X with excessive zeros
disp(['Avg zero proportion in the observed data is ', num2str(mean(sum(X_obs==0,2)/p))]); % avg zero percentage in each sample, about 25%
% true coefficient (tree-guided)
taxonomy=[(1:100)',kron((1:10)',ones(10,1)),kron((1:5)',ones(20,1)), [ones(40,1);2*ones(40,1);3*ones(20,1)]];
beta=[ones(20,1);-2*ones(10,1);0.5*ones(10,1);2*ones(40,1);randn(20,1)]; % some grouping structure according to the tree
beta_group=[20,10,10,40,ones(1,20)];
% rootless reparameterization (remember to center Y and cvrt)
[A_tree,C_tree1,CNorm_tree1,g_idx_tree1,node,M2, ~,~]=mat2SPGtree_rootless(taxonomy,1); % tree-derived parameters for SPG
figure();
treeplot(node);
title('Trimmed tree (without redundant nodes) -- ready for analysis')
[x,y] = treelayout(node);
for i=1:length(x)
text(x(i),y(i),num2str(i))
end
% coeff for node111, node103, node104, node117, node81-100
figure();clf;
plot(beta)
[~,C_tree2,CNorm_tree2,g_idx_tree2,~,~, ~,~]=mat2SPGtree_rootless(taxonomy,2);
[~,C_tree3,CNorm_tree3,g_idx_tree3,~,~, ~,~]=mat2SPGtree_rootless(taxonomy,3);
% graph parameters for SPG
W=ones(p,p); % weight matrix for graph (complete graph without edge-specific weights)
[C_graph, CNorm_graph]=mat2SPGgraph(W);
% parameters that can be tweaked (SNR, eps, thres)
SNR=1; sigma=sqrt(var(X_true*beta)/SNR)
% sigma=0.1; SNR=var(X_true*beta)/sigma^2; % SNR=2.8
eps=1e-3; % surrogate of zeros for LC
% repeat 100 times
nsim=100;
rec_RSS=zeros(nsim,5);
rec_PMSE=zeros(nsim,5);
rec_time=zeros(nsim,5); % computing time
rec_es=zeros(nsim,4); % equi-sparsity measure
% data for R
Ytrain_forR=zeros(100,nsim);
Xtrain_forR=zeros(100,p,nsim);
Ytest_forR=zeros(400,nsim);
Xtest_forR=zeros(400,p,nsim);
for isim=1:nsim
disp(['Running sim #', num2str(isim)]);
E=randn(n,1)*sigma;
Y=X_true*beta+E; % relative shift model, from true X
% separate into 100 training and 400 testing (using obs X)
Ytrain=Y(1:100);
Xtrain=X_obs(1:100,:);
Ytest=Y(101:n);
Xtest=X_obs(101:n,:);
% for LR and CLR methods, replace 0 by eps and renormalize
Xtrain_nozero=max(Xtrain,eps);
Xtrain_nozero=Xtrain_nozero./sum(Xtrain_nozero,2);
Xtest_nozero=max(Xtest,eps);
Xtest_nozero=Xtest_nozero./sum(Xtest_nozero,2);
% NOTE: remember to center Y since we have removed the root node!!!
Ymean=mean(Ytrain);
Ytrain_c=Ytrain-Ymean;
% RS-DL2
Tstart=tic;
option.mu=1e-02; % Smoothing Parameter, fixed
option.gammarange=(0.5:.5:10)*1E-2;
option.fig=1;
[coeff_RS1,~,gamma_opt1,CV1] = cv_SPG_cvrt('group',Ytrain_c, Xtrain*A_tree, [], C_tree1, CNorm_tree1, option,g_idx_tree1);
beta_RS_tree1=A_tree*coeff_RS1;
ES1=beta_group(1)*var(beta_RS_tree1(1:20),1)+...
beta_group(2)*var(beta_RS_tree1(21:30),1)+...
beta_group(3)*var(beta_RS_tree1(31:40),1)+...
beta_group(4)*var(beta_RS_tree1(41:80),1);
T1_1=toc(Tstart);
%check equi-sparsity
figure(1);clf;subplot(2,2,1);plot(beta_RS_tree1);title('Descendant L2')
% RS-CL2
Tstart=tic;
option.mu=1e-02; % Smoothing Parameter, fixed
option.gammarange=(0.5:.5:10)*1E-2;
option.fig=1;
[coeff_RS2,~,gamma_opt2,CV2] = cv_SPG_cvrt('group',Ytrain_c, Xtrain*A_tree,[], C_tree2, CNorm_tree2, option,g_idx_tree2);
beta_RS_tree2=A_tree*coeff_RS2;
ES2=beta_group(1)*var(beta_RS_tree2(1:20),1)+...
beta_group(2)*var(beta_RS_tree2(21:30),1)+...
beta_group(3)*var(beta_RS_tree2(31:40),1)+...
beta_group(4)*var(beta_RS_tree2(41:80),1);
T1_2=toc(Tstart);
%check equi-sparsity
figure(1);subplot(2,2,2);plot(beta_RS_tree2);title('Child L2')
% RS-L1
Tstart=tic;
option.mu=1e-02; % Smoothing Parameter, fixed
option.gammarange=(0.5:.5:10)*1E-2;
option.fig=1;
[coeff_RS3,~,gamma_opt3,CV3] = cv_SPG_cvrt('group',Ytrain_c, Xtrain*A_tree,[], C_tree3, CNorm_tree3, option,g_idx_tree3);
beta_RS_tree3=A_tree*coeff_RS3;
ES3=beta_group(1)*var(beta_RS_tree3(1:20),1)+...
beta_group(2)*var(beta_RS_tree3(21:30),1)+...
beta_group(3)*var(beta_RS_tree3(31:40),1)+...
beta_group(4)*var(beta_RS_tree3(41:80),1);
T1_3=toc(Tstart);
%check equi-sparsity
figure(1);subplot(2,2,3);plot(beta_RS_tree3);title('Node L1')
% RS-ES
Tstart=tic;
option.mu=1e-02; % Smoothing Parameter, Carefully choose mu, a larger mu may not converge while a smaller one leads to slow convergence
option.gammarange=(0.25:0.25:5)*1E-3;
option.fig=1;
[beta_RS_graph,~,gamma_opt,CV] = cv_SPG_cvrt('graph',Ytrain, Xtrain,[], C_graph, CNorm_graph, option);
ES4=beta_group(1)*var(beta_RS_graph(1:20),1)+...
beta_group(2)*var(beta_RS_graph(21:30),1)+...
beta_group(3)*var(beta_RS_graph(31:40),1)+...
beta_group(4)*var(beta_RS_graph(41:80),1);
T2=toc(Tstart);
%check equi-sparsity
figure(1);subplot(2,2,4);plot(beta_RS_graph);title('Equi-Sparsity')
% CLR regression with lasso (with CV tuning selection)
% (a naive competitor created by myself)
Tstart=tic;
Xtrain_CLR=log(Xtrain_nozero)-mean(log(Xtrain_nozero),2); % CLR
[beta_naive,info]=lasso(Xtrain_CLR,Ytrain,'cv',5); % lasso with CV for tuning selection
beta_Lasso=beta_naive(:,info.IndexMinMSE);
beta_Lasso0=info.Intercept(info.IndexMinMSE);
T3=toc(Tstart);
% implementation of LC with lasso -- R package CVS (Lin et al, 2014)
% implementation of KPR -- R package KPR (Randolph et al, 2018)
% Use *5-fold CV* and *10-value tuning range* if possible.
% compare
% in-sample residual
res_RS_tree1=Ytrain_c-Xtrain*beta_RS_tree1;
res_RS_tree2=Ytrain_c-Xtrain*beta_RS_tree2;
res_RS_tree3=Ytrain_c-Xtrain*beta_RS_tree3;
res_RS_graph=Ytrain-Xtrain*beta_RS_graph;
res_Lasso=Ytrain-beta_Lasso0-Xtrain_CLR*beta_Lasso;
rec_RSS(isim,:)=[sum(res_RS_tree1.^2),sum(res_RS_tree2.^2),sum(res_RS_tree3.^2),sum(res_RS_graph.^2),sum(res_Lasso.^2)];
% out-sample prediction
Xtest_CLR=log(Xtest_nozero)-mean(log(Xtest_nozero),2); % CLR
rec_PMSE(isim,:)=[sum((Ytest-Ymean-Xtest*beta_RS_tree1).^2),sum((Ytest-Ymean-Xtest*beta_RS_tree2).^2),sum((Ytest-Ymean-Xtest*beta_RS_tree3).^2),...
sum((Ytest-Xtest*beta_RS_graph).^2),sum((Ytest-Xtest_CLR*beta_Lasso-beta_Lasso0).^2)];
% time
rec_time(isim,:)=[T1_1,T1_2,T1_3,T2,T3];
% equi-sparsity
rec_es(isim,:)=[ES1,ES2,ES3,ES4];
% save data for R (no-zero compositional data)
Xtrain_forR(:,:,isim)=Xtrain_nozero;
Xtest_forR(:,:,isim)=Xtest_nozero;
Ytrain_forR(:,isim)=Ytrain;
Ytest_forR(:,isim)=Ytest;
end
save('Sim2.mat','Xtrain_forR','Xtest_forR','Ytrain_forR','Ytest_forR','rec_RSS','rec_PMSE','rec_es', 'rec_time','taxonomy');