-
Notifications
You must be signed in to change notification settings - Fork 11
/
demo_glm.m
519 lines (468 loc) · 16.9 KB
/
demo_glm.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
%% Sparse generalized linear model (GLM)
% A demonstration of sparse GLM regression using SparseReg toolbox.
% Sparsity is in the general sense: variable selection, total variation
% regularization, polynomial trend filtering, and others. Various penalties
% are implemented: elestic net (enet), power family (bridge regression),
% log penalty, SCAD, and MCP.
%% Sparse logistic regression (n>p)
% Simulate a sample data set (n=500, p=50)
clear;
n = 500;
p = 50;
X = randn(n,p); % generate a random design matrix
X = bsxfun(@rdivide, X, sqrt(sum(X.^2,1))); % normalize predictors
X = [ones(size(X,1),1) X];
b = zeros(p+1,1); % true signalfirst ten predictors are 5
b(2:6) = 1; % first 5 predictors are 1
b(7:11) = -1; % next 5 predictors are -1
inner = X*b; % linear parts
prob = 1./(1+exp(-inner));
y = double(rand(n,1)<prob);
%%
% Sparse logistic regression at a fixed tuning parameter value
model = 'logistic'; % set model to logistic
penidx = [false; true(size(X,2)-1,1)]; % leave intercept unpenalized
penalty = 'enet'; % set penalty to lasso
penparam = 1;
lambdastart = 0; % find the maximum tuning parameter to start
for j=1:size(X,2)
if (penidx(j))
lambdastart = max(lambdastart, ...
glm_maxlambda(X(:,j),y,model,'penalty',penalty,'penparam',penparam));
end
end
disp(lambdastart);
lambda = 0.9*lambdastart; % tuning parameter value
betahat = ... % sparse regression
glm_sparsereg(X,y,lambda,model,'penidx',penidx,'penalty',penalty,...
'penparam',penparam);
figure; % plot penalized estimate
bar(0:length(betahat)-1,betahat);
xlabel('j');
ylabel('\beta_j');
xlim([-1,length(betahat)]);
title([penalty '(' num2str(penparam) '), \lambda=' num2str(lambda,2)]);
lambda = 0.5*lambdastart; % tuning parameter value
betahat = ... % sparse regression
glm_sparsereg(X,y,lambda,model,'penidx',penidx,'penalty',penalty,...
'penparam',penparam);
figure; % plot penalized estimate
bar(0:length(betahat)-1,betahat);
xlabel('j');
ylabel('\beta_j');
xlim([-1,length(betahat)]);
title([penalty '(' num2str(penparam) '), \lambda=' num2str(lambda,2)]);
%%
% Solution path for lasso
model = 'logistic'; % do logistic regression
penalty = 'enet'; % set penalty to lasso
penparam = 1;
penidx = [false; true(size(X,2)-1,1)]; % leave intercept unpenalized
tic;
[rho_path,beta_path,eb_path] = ... % compute solution path
glm_sparsepath(X,y,model,'penidx',penidx,'penalty',penalty, ...
'penparam',penparam);
timing = toc;
[~,ebidx] = min(eb_path); % locate the best model by empirical Bayes crit.
figure;
plot(rho_path,eb_path);
xlabel('\rho');
ylabel('Emp. Bayes. Criterion');
xlim([min(rho_path),max(rho_path)]);
title([penalty '(' num2str(penparam) '), ' num2str(timing,2) ' sec']);
line([rho_path(ebidx), rho_path(ebidx)], ylim);
figure;
plot(rho_path,beta_path);
xlabel('\rho');
ylabel('\beta(\rho)');
xlim([min(rho_path),max(rho_path)]);
title([penalty '(' num2str(penparam) '), ' num2str(timing,2) ' sec']);
line([rho_path(ebidx), rho_path(ebidx)], ylim);
%%
% Solution path for power (0.5)
penalty = 'power'; % set penalty function to power
penparam = 0.5;
tic;
[rho_path,beta_path,eb_path] = ... % compute solution path
glm_sparsepath(X,y,model,'penidx',penidx,'penalty',penalty, ...
'penparam',penparam);
timing = toc;
[~,ebidx] = min(eb_path);
figure;
plot(rho_path,eb_path);
xlabel('\rho');
ylabel('Emp. Bayes. Criterion');
xlim([min(rho_path),max(rho_path)]);
title([penalty '(' num2str(penparam) '), ' num2str(timing,2) ' sec']);
line([rho_path(ebidx), rho_path(ebidx)], ylim);
figure;
plot(rho_path,beta_path);
xlabel('\rho');
ylabel('\beta(\rho)');
xlim([min(rho_path),max(rho_path)]);
title([penalty '(' num2str(penparam) '), ' num2str(timing,2) ' sec']);
line([rho_path(ebidx), rho_path(ebidx)], ylim);
%%
% Compare solution paths from different penalties
penalty = {'enet' 'enet' 'enet' 'power' 'power' 'log' 'log' 'mcp' 'scad'};
penparam = [1 1.5 2 0.5 1 0 1 1 3.7];
penidx = [false; true(size(X,2)-1,1)]; % leave intercept unpenalized
figure;
for i=1:length(penalty)
tic;
[rho_path,beta_path] = ...
glm_sparsepath(X,y,model,'penidx',penidx,'penalty',penalty{i}, ...
'penparam',penparam(i));
timing = toc;
subplot(3,3,i);
plot(rho_path,beta_path);
if (i==8)
xlabel('\rho');
end
if (i==4)
ylabel('\beta(\rho)');
end
xlim([min(rho_path),max(rho_path)]);
title([penalty{i} '(' num2str(penparam(i)) '), ' num2str(timing,1) 's']);
end
%% Fused logistic regression
% Fused logistic regression (fusing the first 10 predictors)
D = zeros(9,size(X,2)); % regularization matrix for fusing first 10 preds
D(10:10:90) = 1;
D(19:10:99) = -1;
disp(D(1:9,1:11));
model = 'logistic';
penalty = 'enet'; % set penalty function to lasso
penparam = 1;
tic;
[rho_path,beta_path,eb_path] = glm_regpath(X,y,D,model,'penalty',penalty, ...
'penparam',penparam);
timing = toc;
[~,ebidx] = min(eb_path);
figure;
plot(rho_path,eb_path);
xlabel('\rho');
ylabel('EBC');
xlim([min(rho_path),max(rho_path)]);
title([penalty '(' num2str(penparam) '), ' num2str(timing,2) ' sec']);
line([rho_path(ebidx), rho_path(ebidx)], ylim);
figure;
plot(rho_path,beta_path(2:11,:));
xlabel('\rho');
ylabel('\beta(\rho)');
xlim([min(rho_path),max(rho_path)]);
title([penalty '(' num2str(penparam) '), ' num2str(timing,2) ' sec']);
line([rho_path(ebidx), rho_path(ebidx)], ylim);
%%
% Same fusion problem, but with power, log, MCP, and SCAD penalty
penalty = {'enet' 'power' 'log' 'mcp'};
penparam = [1.5 0.5 1 1];
for i=1:length(penalty)
disp(penalty(i))
tic;
[rho_path, beta_path,eb_path] = glm_regpath(X,y,D,model,'penalty',penalty{i},...
'penparam',penparam(i));
timing = toc;
[~,ebidx] = min(eb_path);
subplot(2,2,i);
plot(rho_path,beta_path(2:11,:));
xlim([min(rho_path),max(rho_path)]);
title([penalty{i} '(' num2str(penparam(i)) '), ' num2str(timing,1) 's']);
line([rho_path(ebidx), rho_path(ebidx)], ylim);
end
%% Sparse logistic regression (n<p)
% Simulate another sample data set (n=100, p=1000)
clear;
n = 100;
p = 1000;
X = randn(n,p); % generate a random design matrix
X = bsxfun(@rdivide, X, sqrt(sum(X.^2,1))); % normalize predictors
X = [ones(size(X,1),1),X]; % add intercept
b = zeros(p+1,1); % true signal
b(2:6) = 5; % first 5 predictors are 5
b(7:11) = -5; % next 5 predictors are -5
inner = X*b; % linear parts
prob = 1./(1+exp(-inner));
y = binornd(1,prob); % generate binary response
%%
% Solution path for lasso
maxpreds = 51; % request path to the first 51 predictors
model = 'logistic'; % do logistic regression
penalty = 'enet'; % set penalty to lasso
penparam = 1;
penidx = [false; true(size(X,2)-1,1)]; % leave intercept unpenalized
tic;
[rho_path,beta_path,eb_path] = ... % compute solution path
glm_sparsepath(X,y,model,'penidx',penidx,'maxpreds',maxpreds, ...
'penalty',penalty,'penparam',penparam);
timing = toc;
[~,ebidx] = min(eb_path);
figure;
plot(rho_path,eb_path);
xlabel('\rho');
ylabel('Emp. Bayes. Criterion');
xlim([min(rho_path),max(rho_path)]);
title([penalty '(' num2str(penparam) '), ' num2str(timing,2) ' sec']);
line([rho_path(ebidx), rho_path(ebidx)], ylim);
figure;
plot(rho_path,beta_path);
xlabel('\rho');
ylabel('\beta(\rho)');
xlim([min(rho_path),max(rho_path)]);
title([penalty '(' num2str(penparam) '), ' num2str(timing,2) ' sec']);
line([rho_path(ebidx), rho_path(ebidx)], ylim);
%%
% Solution path for power (0.5)
penalty = 'power'; % set penalty function to power
penparam = 0.5;
tic;
[rho_path,beta_path,eb_path] = ... % compute solution path
glm_sparsepath(X,y,model,'penidx',penidx,'maxpreds',maxpreds, ...
'penalty',penalty,'penparam',penparam);
timing = toc;
[~,ebidx] = min(eb_path);
figure;
plot(rho_path,eb_path);
xlabel('\rho');
ylabel('EBC');
xlim([min(rho_path),max(rho_path)]);
title([penalty '(' num2str(penparam) '), ' num2str(timing,2) ' sec']);
line([rho_path(ebidx), rho_path(ebidx)], ylim);
figure;
plot(rho_path,beta_path);
xlabel('\rho');
ylabel('\beta(\rho)');
xlim([min(rho_path),max(rho_path)]);
title([penalty '(' num2str(penparam) '), ' num2str(timing,2) ' sec']);
line([rho_path(ebidx), rho_path(ebidx)], ylim);
%% Sparse loglinear (Poisson) regression (n>p)
% Simulate a sample data set (n=500, p=50)
clear;
n = 500;
p = 50;
X = randn(n,p); % generate a random design matrix
X = bsxfun(@rdivide, X, sqrt(sum(X.^2,1))); % normalize predictors
X = [ones(size(X,1),1) X]; % add intercept
b = zeros(p+1,1); % true signal: first ten predictors are 3
b(2:6) = 1; % first 5 predictors are 1
b(7:11) = -1; % next 5 predictors are -1
inner = X*b; % linear parts
y = poissrnd(exp(inner)); % generate response from Poisson
%%
% Sparse loglinear regression at a fixed tuning parameter value
model = 'loglinear'; % set model to logistic
penidx = [false; true(size(X,2)-1,1)]; % leave intercept unpenalized
penalty = 'enet'; % set penalty to lasso
penparam = 1;
lambdastart = 0; % find the maximum tuning parameter to start
for j=1:size(X,2)
if (penidx(j))
lambdastart = max(lambdastart, ...
glm_maxlambda(X(:,j),y,model,'penalty',penalty,'penparam',penparam));
end
end
disp(lambdastart);
lambda = 0.9*lambdastart; % tuning parameter value
betahat = ... % sparse regression
glm_sparsereg(X,y,lambda,model,'penidx',penidx,'penalty',penalty, ...
'penparam',penparam);
figure; % plot penalized estimate
bar(1:length(betahat),betahat);
xlabel('j');
ylabel('\beta_j');
xlim([0,length(betahat)+1]);
title([penalty '(' num2str(penparam) '), \lambda=' num2str(lambda,2)]);
lambda = 0.5*lambdastart; % tuning parameter value
betahat = ... % sparse regression
glm_sparsereg(X,y,lambda,model,'penidx',penidx,'penalty',penalty, ...
'penparam',penparam);
figure; % plot penalized estimate
bar(1:length(betahat),betahat);
xlabel('j');
ylabel('\beta_j');
xlim([0,length(betahat)+1]);
title([penalty '(' num2str(penparam) '), \lambda=' num2str(lambda,2)]);
%%
% Solution path for lasso
model = 'loglinear'; % do logistic regression
penalty = 'enet'; % set penalty to lasso
penparam = 1;
penidx = [false; true(size(X,2)-1,1)]; % leave intercept unpenalized
tic;
[rho_path,beta_path,eb_path] = ... % compute solution path
glm_sparsepath(X,y,model,'penidx',penidx,'penalty',penalty, ...
'penparam',penparam);
timing = toc;
[~,ebidx] = min(eb_path);
figure;
plot(rho_path,eb_path);
xlabel('\rho');
ylabel('EBC');
xlim([min(rho_path),max(rho_path)]);
title([penalty '(' num2str(penparam) '), ' num2str(timing,2) ' sec']);
line([rho_path(ebidx), rho_path(ebidx)], ylim);
figure;
plot(rho_path,beta_path);
xlabel('\rho');
ylabel('\beta(\rho)');
xlim([min(rho_path),max(rho_path)]);
title([penalty '(' num2str(penparam) '), ' num2str(timing,2) ' sec']);
line([rho_path(ebidx), rho_path(ebidx)], ylim);
%%
% Solution path for power (0.5)
penalty = 'power'; % set penalty function to power
penparam = 0.5;
tic;
[rho_path,beta_path,eb_path] = ... % compute solution path
glm_sparsepath(X,y,model,'penidx',penidx,'penalty',penalty, ...
'penparam',penparam);
timing = toc;
[~,ebidx] = min(eb_path);
figure;
plot(rho_path,eb_path);
xlabel('\rho');
ylabel('EBC');
xlim([min(rho_path),max(rho_path)]);
title([penalty '(' num2str(penparam) '), ' num2str(timing,2) ' sec']);
line([rho_path(ebidx), rho_path(ebidx)], ylim);
figure;
plot(rho_path,beta_path);
xlabel('\rho');
ylabel('\beta(\rho)');
xlim([min(rho_path),max(rho_path)]);
title([penalty '(' num2str(penparam) '), ' num2str(timing,2) ' sec']);
line([rho_path(ebidx), rho_path(ebidx)], ylim);
%%
% Compare solution paths from different penalties
penalty = {'enet' 'enet' 'enet' 'power' 'power' 'log' 'log' 'mcp' 'scad'};
penparam = [1 1.5 2 0.5 1 0 1 1 3.7];
penidx = [false; true(size(X,2)-1,1)]; % leave intercept unpenalized
figure;
for i=1:length(penalty)
disp(penalty(i));
tic;
[rho_path,beta_path,eb_path] = glm_sparsepath(X,y,model,'penidx',penidx, ...
'penalty',penalty{i},'penparam',penparam(i));
timing = toc;
[~,ebidx] = min(eb_path);
subplot(3,3,i);
plot(rho_path,beta_path);
if (i==8)
xlabel('\rho');
end
if (i==4)
ylabel('\beta(\rho)');
end
xlim([min(rho_path),max(rho_path)]);
title([penalty{i} '(' num2str(penparam(i)) '), ' num2str(timing,1) 's']);
line([rho_path(ebidx), rho_path(ebidx)], ylim);
end
%% Fused loglinear (Poisson) regression
% Fused loglinear regression (fusing the first 10 predictors)
D = zeros(9,size(X,2)); % regularization matrix for fusing first 10 preds
D(10:10:90) = 1;
D(19:10:99) = -1;
disp(D(1:9,1:11));
model = 'loglinear';
penalty = 'enet'; % set penalty function to lasso
penparam = 1;
tic;
[rho_path,beta_path,eb_path] = glm_regpath(X,y,D,model,'penalty',penalty, ...
'penparam',penparam);
timing = toc;
[~,ebidx] = min(eb_path);
figure;
plot(rho_path,eb_path);
xlabel('\rho');
ylabel('EBC');
xlim([min(rho_path),max(rho_path)]);
title([penalty '(' num2str(penparam) '), ' num2str(timing,2) ' sec']);
line([rho_path(ebidx), rho_path(ebidx)], ylim);
figure;
plot(rho_path,beta_path(2:11,:));
xlabel('\rho');
ylabel('\beta(\rho)');
xlim([min(rho_path),max(rho_path)]);
title([penalty '(' num2str(penparam) '), ' num2str(timing,2) ' sec']);
line([rho_path(ebidx), rho_path(ebidx)], ylim);
%%
% Same fusion problem, but with enet, power, MCP, and SCAD penalty
penalty = {'enet' 'power' 'mcp' 'scad'};
penparam = [1.5 0.5 1 3.7];
for i=1:length(penalty)
disp(penalty(i));
tic;
[rho_path,beta_path,eb_path] = glm_regpath(X,y,D,model,'penalty',penalty{i}, ...
'penparam',penparam(i));
timing = toc;
[~,ebidx] = min(eb_path);
subplot(2,2,i);
plot(rho_path,beta_path(2:11,:));
xlim([min(rho_path),max(rho_path)]);
title([penalty{i} '(' num2str(penparam(i)) '), ' num2str(timing,1) 's']);
line([rho_path(ebidx), rho_path(ebidx)], ylim);
end
%% Sparse loglinear (Poisson) regression (n<<p)
% Simulate a sample data set (n=500, p=50)
clear;
n = 100;
p = 1000;
X = randn(n,p); % generate a random design matrix
X = bsxfun(@rdivide, X, sqrt(sum(X.^2,1))); % normalize predictors
X = [ones(size(X,1),1) X]; % add intercept
b = zeros(p+1,1); % true signal: first ten predictors are 3
b(2:6) = 3; % first 5 predictors are 3
b(7:11) = -3; % next 5 predictors are -3
inner = X*b; % linear parts
y = poissrnd(exp(inner)); % generate response from Poisson
%%
% Solution path for lasso
maxpreds = 51; % obtain solution path to top 50 predictors
model = 'loglinear'; % do Poisson regression
penalty = 'enet'; % set penalty to lasso
penparam = 1;
penidx = [false; true(size(X,2)-1,1)]; % leave intercept unpenalized
tic;
[rho_path,beta_path,eb_path] = ... % compute solution path
glm_sparsepath(X,y,model,'penidx',penidx,'penalty',penalty, ...
'penparam',penparam,'maxpreds',maxpreds);
timing = toc;
[~,ebidx] = min(eb_path);
figure;
plot(rho_path,eb_path);
xlabel('\rho');
ylabel('EBC');
xlim([min(rho_path),max(rho_path)]);
title([penalty '(' num2str(penparam) '), ' num2str(timing,2) ' sec']);
line([rho_path(ebidx), rho_path(ebidx)], ylim);
figure;
plot(rho_path,beta_path);
xlabel('\rho');
ylabel('\beta(\rho)');
xlim([min(rho_path),max(rho_path)]);
title([penalty '(' num2str(penparam) '), ' num2str(timing,2) ' sec']);
line([rho_path(ebidx), rho_path(ebidx)], ylim);
%%
% Solution path for power (0.5)
penalty = 'power'; % set penalty function to power
penparam = 0.5;
tic;
[rho_path,beta_path,eb_path] = ... % compute solution path
glm_sparsepath(X,y,model,'penidx',penidx,'penalty',penalty, ...
'penparam',penparam,'maxpreds',maxpreds);
timing = toc;
[~,ebidx] = min(eb_path);
figure;
plot(rho_path,eb_path);
xlabel('\rho');
ylabel('EBC');
xlim([min(rho_path),max(rho_path)]);
title([penalty '(' num2str(penparam) '), ' num2str(timing,2) ' sec']);
line([rho_path(ebidx), rho_path(ebidx)], ylim);
figure;
plot(rho_path,beta_path);
xlabel('\rho');
ylabel('\beta(\rho)');
xlim([min(rho_path),max(rho_path)]);
title([penalty '(' num2str(penparam) '), ' num2str(timing,2) ' sec']);
line([rho_path(ebidx), rho_path(ebidx)], ylim);