1
$\begingroup$

I asked this question in stack Overflow, but no one gave me an answer.I managed to optimize a line in order to get a line of best fit using curve_fit, but I can't seem to get the R squared value the way I can for linear regression, this is my code:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
from scipy.optimize import *
from scipy.integrate import *
from scipy.interpolate import * 
df=pd.read_csv('F:/Data32.csv')
df2=df['Temperature']
df3=df['CO2-Rh']
def f(x,a,b,c) :
   return a*np.exp(b*x)+c
params, extras = curve_fit(f, df2, df3)
print('a=%g,b=%g, c=%g' %(params[0],df2[1],df3[2]))
plt.plot(df2,df3,'o')
plt.plot(df2,f(df2,params[0],params[1],params[2]))
plt.legend(['data','fit'],loc='best')
plt.show()
$\endgroup$
3
  • $\begingroup$ I do not see any code which tries and fails to get $r^2$ although this may be because I do not use Python. $\endgroup$
    – mdewey
    Commented Aug 5, 2016 at 7:33
  • $\begingroup$ This is what i use for linear regression @mdewey: result = sm.ols(formula="x ~ y", data=df2).fit() print (result.params) print (result.summary()) $\endgroup$ Commented Aug 5, 2016 at 7:35
  • $\begingroup$ you probably don't want to compute $R^2$ for a non-linear fit, see here: stackoverflow.com/a/14530791/6353406 $\endgroup$ Commented Aug 5, 2016 at 8:35

1 Answer 1

0
$\begingroup$

You can obtain an estimate of $R^2$ for non linear regression by calculate the square of the correlation value between the fitted values and the real values of the response variable.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.