3

There are many MATLAB functions that do some kind of statistical model fitting, such as fitglm(). These model fits can fail to converge for various reasons; this question is NOT about what can cause such failures or about how to prevent them.

My question is: is there a way, other than by looking at the console output, to determine if a given call to fitglm() converged? The obvious way to do this would seem to be through some property of the output arguments, but the list of properties of the Linear Model class doesn't seem to contain this basic information.

A minimal example (inspired by this question):

x = [7 0;0 0;8 0;9 0;7 1;8 0;7 0;4 0;7 0;2 0];
y = [0 0 1 1 1 0 0 1 0 0]';
m = fitglm(x,y,'distr','binomial');

Warning: Iteration limit reached.

What, if anything, about the output m tells us that the iteration limit was reached?

Matt Mizumi
  • 1,193
  • 1
  • 11
  • 27

1 Answers1

2

I have yet to find any such information in the GeneralizedLinearModel class object returned from fitglm. However, one option which technically avoids looking at the console output is to capture information about the last warning using either lastwarn or warning:

>> [lastMsg, lastID] = lastwarn

lastMsg =

Iteration limit reached.

lastID =

stats:glmfit:IterationLimit


>> w = warning('query', 'last')

w = 

  struct with fields:

    identifier: 'stats:glmfit:IterationLimit'
         state: 'on'

Note that this will still work even if warnings have been suppressed (i.e. warnings are still generated and stored, but not displayed).

One way you could use this is to first set the last warning to a dummy message (using lastwarn), then fit your model, then get the last warning again and compare it to your dummy message. If it's different, a new warning was thrown when fitting the model:

lastwarn('Nothing to see here', 'this:is:not:a:warning');
m = fitglm(x, y, 'distr', 'binomial');
[lastMsg, lastID] = lastwarn;
warningWasThrown = ~strcmp(lastID, 'this:is:not:a:warning');

In addition, you could have a switch statement that checks the value of the warning identifier lastID and takes different actions accordingly, like attempting the model fit with an alternate set of parameters.

gnovice
  • 125,304
  • 15
  • 256
  • 359
  • 1
    @MattMizumi: Yeah, I was a little surprised they didn't return this kind of information, either as another output or a property of the model object. Maybe they intended for the warning messages alone to be used for this. – gnovice Sep 26 '18 at 15:56