3

This is a question about extracting fit statistics from the lmfit fit_report()(1) object

In this lmfit example, the following partial output is returned:

[[Model]]
    Model(gaussian)
[[Fit Statistics]]
    # function evals   = 31
    # data points      = 101
    # variables        = 3
    chi-square         = 3.409
    reduced chi-square = 0.035
    Akaike info crit   = -336.264
    Bayesian info crit = -328.418
.
.
.
.
.
.

I am trying to extract all the quantities in the Fit Statistics section as separate variables.

eg. to extract the model parameters, we could use (per 1,2):

for key in fit.params:
    print(key, "=", fit.params[key].value, "+/-", fit.params[key].stderr)

However, this only gives the model parameters; it does not give the fit statistics parameters, which are also useful. I cannot seem to find this in the documentation.

Is there a similar way to extract the Fit Statistics parameters (chi-square, reduced chi-square, function evals, etc.) separately?

edesz
  • 11,756
  • 22
  • 75
  • 123

2 Answers2

5

result holds all the fit statistics. you can get the required parameters as shown below

result = gmodel.fit(y, x=x, amp=5, cen=5, wid=1)
# print number of function efvals
print result.nfev
# print number of data points
print result.ndata
# print number of variables
print result.nvarys
# chi-sqr
print result.chisqr
# reduce chi-sqr
print result.redchi
#Akaike info crit
print result.aic
#Bayesian info crit
print result.bic
plasmon360
  • 4,109
  • 1
  • 16
  • 19
  • Thanks, but how did you figure this out? Is there a way to check the attributes of the `results` object? – edesz Apr 13 '17 at 01:24
  • 2
    I looked at code (https://github.com/lmfit/lmfit-py/blob/master/lmfit/printfuncs.py) of fit_report() and was able to see how it was getting all those parameters. Alternatively, you can use dir(result) to see all the attributes. In addition, if you are using ipython notebook, you can type 'result.' and press tab, there will be a drop down menu will all the attributes. – plasmon360 Apr 13 '17 at 01:49
  • I tried `result.attributes()` but this did not work. Thanks for your post. This question is answered. – edesz Apr 13 '17 at 15:40
2

Yes, sorry the reporting of the fit statistics in Model.fit_report() was inadvertenly turned off in the 0.9.6 release. This is fixed in the main github repo.

As user1753919 has shown, you can get at these individual values. These attributes of ModelResult are documented at https://lmfit.github.io/lmfit-py/model.html#modelresult-attributes

M Newville
  • 7,486
  • 2
  • 16
  • 29