Skip to content
Advertisement

Python lmfit – how to calculate R squared?

This may be a stupid question, but I didn’t find an answer to it anywhere in lmfit’s documentation. My question is simple: how do I retrieve R squared? (I know I can calculate it manually with 1 - SS_res / SS_tot)

Update: I tried calculating R squared myself and compared it to the R squared from statsmodels. Parameters are the same in both estimations, but R squared is not.

Code:

JavaScript

Advertisement

Answer

I don’t see an included rsquared in lmfit, but we can reuse either the residuals or the redchi

I am using a similar example where y contains additional noise

lmfit result (assuming the mean residual equals zero, which is always true for linear regression)

JavaScript

compared to OLS results:

JavaScript

This is the definition of rsquared when we compare to a model with just an intercept and without weights.

The calculations for rsquared in statsmodels are adjusted for the case when the regression does not include an intercept, and they takes weights into account for weighted least squares.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement