Reputation: 411
I am using glmnet in MATLAB 2019a on my Macbook to do logistic regression.
Algorithm:
log(pi/(1-pi))=b0+X*b
pi=P(Y=2|X_i)=1-P(Y=1|X_i)
Code:
Y = [2;1;2;1;2;1;2;1;1;2];
X = [0.1451 0.1176 0.0872 0.0544 0.0197 -0.0164 -0.0533 -0.0907;
0.5096 0.7240 0.9038 1.0515 1.1694 1.2599 1.3253 1.3681;
-0.0593 -0.1683 -0.2738 -0.3754 -0.4730 -0.5660 -0.6543 -0.7376;
-1.0128 -0.9539 -0.9004 -0.8522 -0.8089 -0.7701 -0.7355 -0.7047;
0.7533 0.5640 0.4054 0.2752 0.1709 0.0900 0.0302 -0.0109;
0.2014 0.2595 0.3070 0.3444 0.3724 0.3918 0.4032 0.4074;
0.9174 0.8706 0.8260 0.7834 0.7423 0.7025 0.6636 0.6253;
0.7643 0.6115 0.4789 0.3653 0.2693 0.1897 0.1252 0.0744;
-0.3299 -0.5078 -0.6507 -0.7615 -0.8430 -0.8981 -0.9294 -0.9399;
-0.2141 -0.1472 -0.0818 -0.0179 0.0443 0.1045 0.1626 0.2183];
lambda=0.5;
family='binomial';
options.weights = [];
options.alpha = 1;
options.nlambda = 100;
options.lambda_min = 0;
options.lambda = lambda;
options.standardize = false;%true
options.thresh = 1E-4;
options.dfmax = 0;
options.pmax = 0;
options.exclude = [];
options.penalty_factor = [];
options.maxit = 100;
options.HessianExact = false;
options.type = 'naive';
fit = glmnet(X,Y,family,options);
Outcome:
a0 0
label [1;2]
beta [0;0;0;0;0;0;0;0]
dev 0
nulldev 13.8629
df 0
lambda 0.5000
npasses 1
jerr 0
dim [8,1]
class 'lognet'
No matter how I changed my input and output, the function always returns coefficients with all 0. I picked these options carefully so I am really confused how it comes to this outcome.
Is it because the data I picked are special or the parameters are wrong?
Upvotes: 0
Views: 111
Reputation: 411
I have just found where the problem is. The data here is small, so lambda=0.5
implements an overly strong penalty to it.
Changing lambda to less than 0.01 solves the problem. Plus, options.pmax = 0
should be deleted, otherwise it asks for all coefficients to be 0.
Upvotes: 1