-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathglmnetPlot.m
121 lines (111 loc) · 3.4 KB
/
glmnetPlot.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
function glmnetPlot( x, xvar, label )
%--------------------------------------------------------------------------
% glmnetPlot.m: plot coefficients from a "glmnet" object
%--------------------------------------------------------------------------
%
% DESCRIPTION:
% Produces a coefficient profile plot fo the coefficient paths for a
% fitted "glmnet" object.
%
% USAGE:
% glmnetPlot(fit);
% glmnetPlot(fit, xvar);
% glmnetPlot(fit, xvar, label);
%
% INPUT ARGUMENTS:
% x fitted "glmnet" model.
% xvar What is on the X-axis. "norm" plots against the L1-norm of
% the coefficients, "lambda" against the log-lambda sequence,
% and "dev" against the percent deviance explained.
% label if TRUE, label the curves with variable sequence numbers.
%
% DETAILS:
% A coefficient profile plot is produced. If x is a multinomial model, a
% coefficient plot is produced for each class.
%
% LICENSE: GPL-2
%
% DATE: 14 Jul 2009
%
% AUTHORS:
% Algorithm was designed by Jerome Friedman, Trevor Hastie and Rob Tibshirani
% Fortran code was written by Jerome Friedman
% R wrapper (from which the MATLAB wrapper was adapted) was written by Trevor Hasite
% MATLAB wrapper was written and maintained by Hui Jiang, [email protected]
% Department of Statistics, Stanford University, Stanford, California, USA.
%
% REFERENCES:
% Friedman, J., Hastie, T. and Tibshirani, R. (2009)
% Regularization Paths for Generalized Linear Models via Coordinate Descent.
% Journal of Statistical Software, 33(1), 2010
%
% SEE ALSO:
% glmnet, glmnetSet, glmnetPrint, glmnetPredict and glmnetCoef methods.
%
% EXAMPLES:
% x=randn(100,20);
% y=randn(100,1);
% g2=randsample(2,100,true);
% g4=randsample(4,100,true);
% fit1=glmnet(x,y);
% glmnetPlot(fit1);
% glmnetPlot(fit1, 'lambda', true);
% fit3=glmnet(x,g4,'multinomial');
% glmnetPlot(fit3);
%
% DEVELOPMENT: 14 Jul 2009: Original version of glmnet.m written.
if nargin < 2
xvar = 'norm';
end
if nargin < 3
label = false;
end
if strcmp(x.class,'multnet')
beta=x.beta;
if strcmp(xvar,'norm')
norm = 0;
for i=1:length(beta);
which = nonzeroCoef(beta{i});
beta{i} = beta{i}(which,:);
norm = norm + sum(abs(beta{i}),1);
end
else
norm = 0;
end
dfmat=x.dfmat;
ncl=size(dfmat,1);
for i=1:ncl
plotCoef(beta{i},norm,x.lambda,dfmat(i,:),x.dev,label,xvar,'',sprintf('Coefficients: Class %d', i));
end
else
plotCoef(x.beta,[],x.lambda,x.df,x.dev,label,xvar,'','Coefficients');
end
%----------------------------------------------------------------
% End function glmnetPlot
%----------------------------------------------------------------
function plotCoef(beta,norm,lambda,df,dev,label,xvar,xlab,ylab)
which = nonzeroCoef(beta);
beta = beta(which,:);
if strcmp(xvar, 'norm')
if isempty(norm)
index = sum(abs(beta),1);
else
index = norm;
end
iname = 'L1 Norm';
elseif strcmp(xvar, 'lambda')
index=log(lambda);
iname='Log Lambda';
elseif strcmp(xvar, 'dev')
index=dev;
iname='Fraction Deviance Explained';
end
if isempty(xlab)
xlab = iname;
end
plot(index,transpose(beta));
xlabel(xlab);
ylabel(ylab);
%----------------------------------------------------------------
% End private function plotCoef
%----------------------------------------------------------------