5
$\begingroup$

I use curve_fit from scipy to estimate parameter values from a specific function.

from scipy.optimize import curve_fit
import numpy as np

x =np.linspace(0,5,100) 
noise = np.random.normal(0,1,100) 
y= (1.5 * x + 2) + noise

def f(x,b0,b1):
    return b0 + (b1 * x)

parameters, cov= curve_fit(f, x, y)
print(parameters)

How to know if the estimated parameters (b0 and b1) are significants ? (i.e., p-value < 0.05 ? (in other software such as stata, t-value and p-value are generally provided)

PS: f could be any type of function (linear and non-linear).

$\endgroup$

1 Answer 1

4
$\begingroup$

Here is example code adapted from my zunzun.com curve fitting web site. Note that your original data gives very small or zero p-values.

from scipy.optimize import curve_fit
import numpy as np
import scipy.odr
import scipy.stats

x = np.array([5.357, 5.797, 5.936, 6.161, 6.697, 6.731, 6.775, 8.442, 9.861])
y = np.array([0.376, 0.874, 1.049, 1.327, 2.054, 2.077, 2.138, 4.744, 7.104])

def f(x,b0,b1):
    return b0 + (b1 * x)


def f_wrapper_for_odr(beta, x): # parameter order for odr
    return f(x, *beta)

parameters, cov= curve_fit(f, x, y)

model = scipy.odr.odrpack.Model(f_wrapper_for_odr)
data = scipy.odr.odrpack.Data(x,y)
myodr = scipy.odr.odrpack.ODR(data, model, beta0=parameters,  maxit=0)
myodr.set_job(fit_type=2)
parameterStatistics = myodr.run()
df_e = len(x) - len(parameters) # degrees of freedom, error
cov_beta = parameterStatistics.cov_beta # parameter covariance matrix from ODR
sd_beta = parameterStatistics.sd_beta * parameterStatistics.sd_beta
ci = []
t_df = scipy.stats.t.ppf(0.975, df_e)
ci = []
for i in range(len(parameters)):
    ci.append([parameters[i] - t_df * parameterStatistics.sd_beta[i], parameters[i] + t_df * parameterStatistics.sd_beta[i]])

tstat_beta = parameters / parameterStatistics.sd_beta # coeff t-statistics
pstat_beta = (1.0 - scipy.stats.t.cdf(np.abs(tstat_beta), df_e)) * 2.0    # coef. p-values

for i in range(len(parameters)):
    print('parameter:', parameters[i])
    print('   conf interval:', ci[i][0], ci[i][1])
    print('   tstat:', tstat_beta[i])
    print('   pstat:', pstat_beta[i])
    print()
$\endgroup$
1
  • 2
    $\begingroup$ would be nice to have more comments explaining a bit what each line does $\endgroup$
    – Yev Guyduy
    Commented Feb 16, 2022 at 15:47

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