Bug 408535 - regression: wrong calculation of chi^2 reduced for fits
Summary: regression: wrong calculation of chi^2 reduced for fits
Status: RESOLVED NOT A BUG
Alias: None
Product: LabPlot2
Classification: Applications
Component: backend (other bugs)
Version First Reported In: latest
Platform: Microsoft Windows Microsoft Windows
: NOR grave
Target Milestone: ---
Assignee: Stefan Gerlach
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-06-10 15:27 UTC by Uwe Stöhr
Modified: 2019-06-17 00:28 UTC (History)
2 users (show)

See Also:
Latest Commit:
Version Fixed/Implemented In:
Sentry Crash Report:


Attachments
example plot (17.19 KB, application/gzip)
2019-06-10 15:27 UTC, Uwe Stöhr
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Uwe Stöhr 2019-06-10 15:27:43 UTC
Created attachment 120756 [details]
example plot

The main task for me with LabPlot is curve fitting but in version 2.6 there is a bug leading to wrong calculations of the chi^2:

- open the attached plot
- recalculate the fit and look at the results: LP tells me I have a chi^2 reduced of 441. This is obviously wrong since the fit is not that bad. It looks like it is around 2. And indeed, when performing the same fit with another program I get a Chi^2 reduced of 1.9617.
Comment 1 Uwe Stöhr 2019-06-10 16:13:08 UTC
Here is the fit result of MagicPlot for comparison:

LAST FIT
Data Points Number: 35
Fitted Curves Number: 1
Parameters Number: 2
Degrees of Freedom (DoF): 33
Data Total Sum of Squares (TSS): 6.4226e8
Weighting: Standard Error (w = 1 / stdErr^2)

DATA INTERVALS
From	To
0	1200

RESULTS
Iterations: 5
Convergence: 1.8829e-13
Residual Sum of Squares (RSS, χ^2): 64.737
Reduced RSS: 1.9617
Residual Standard Deviation: 1.4006
Coefficient of Determination R^2: 1
Adjusted R^2: 1

FITTED CURVES
Custom Curve	Fit curve	A=557.4639±22.049	B=468.3198±26.5106	y(x) = A*exp(-B/x)

FITTED PARAMETERS
Parameter	Fit curve A	Fit curve B	
Value	557.4639	468.3198	
Std. Dev.	22.049	26.5106	

FIT FORMULA WITH VALUES
y(x) = 557.4639*exp(-468.3198/x)

FIT FORMULA WITH VARIABLES
y(x) = A*exp(-B/x)

A = 557.4639
B = 468.3198

s_A = 22.049
s_B = 26.5106

CORRELATION MATRIX
Parameter	Fit curve A	Fit curve B	
Fit curve A	1	0.9415	
Fit curve B	0.9415	1
Comment 2 Stefan Gerlach 2019-06-14 23:02:40 UTC
It looks like you did an unweighted fit. In this case the calculated chi^2 is correct. To do a weighted fit, you have to select "Instrumental" for y-Weight and the error column in the dock widget.
There were two related bugs that are fixed now:
1. Weighted fitting was possible only for data from spreadsheet (not from a curve)
2. weighted fitting failed when the given error was zero

I will release a Windows package for 2.7.0rc3 tomorrow on https://theorie.physik.uni-konstanz.de/gerlach/labplot/.
Let me know if that works for you.
Comment 3 uwestoehr 2019-06-16 21:57:54 UTC
> To do a weighted fit, you have to select "Instrumental" for
> y-Weight and the error column in the dock widget.

I cannot find this setting. What I did was:
- setting the third column in the spreadsheet as y-error
- in the advanced fitting dialog I checked "use given data errors"

So why does this then not result in a weighted fit?
Comment 4 uwestoehr 2019-06-16 22:14:19 UTC
I sent you a private mail with the testing result of LP 2.7.RC3: I cannot test properly because of crashes.
Comment 5 uwestoehr 2019-06-17 00:28:57 UTC
OK, now I found it. To get a weighted fit one has to set that LP should really take the given error column. It would be very useful if this would be the default if there is an error column.
Moreover the setting section "Weight" is by default collapsed and thus very hard to find. it should be unfolded by default like all other sections in the fit dialog.