I am using lmfit to fit a function in Python. This is my fit:
where the histogram is my data and the dashed line the fit. The error bars are simply the sqrt of the number of counts.
I would like to decide on whether it is a good fit automatically, for this I am using the chi squared test. Despite the fit looks decent to me, I always have to reject it when performing the chi squared test. So either I am doing the test wrong or my 'eye calibration for what a good fit looks like' is wrong.
I am obtaining the value of chi square from the fit by doing result.chisqr as stated in the documentation of lmfit. For this fit I get a value of 162.8 = result.chisqr. Now to select the critical value for chi I am using scipy.stats.chi2.ppf(.95, degrees_of_freedom) where degrees_of_freedom = result.nfree (for this example it is 22 = degrees_of_freedom). This gives me a critical chi squared value of 33.9 and since result.chisqr is larger, I have to consider that this is a bad fit.
Is this actually a bad fit or am I doing the test wrongly?
I noticed that if I use result.redchi, i.e. the reduced chi squared statistic, then this seems to work. I am not sure if I have to use this or it is just a coincidence.


chisq.testinR, for instance.) – whuber Apr 13 '23 at 13:45