有谁知道是否有Stata nl
(非线性最小二乘)包的实现Python
?我尝试使用lmfit
以及optimize.leastsq
from scipy
,但两者似乎都不起作用。
回归方程为
Y = x1 + b1 + 0.3*log(x2-b2)*b3 - 0.7*x3*b3 + b5*x2
其中Y
是因变量,x's
是自变量,b's
是要估计的系数。
使用该lmfit
软件包,我尝试了以下操作:
from lmfit import minimize, Parameters, Parameter, report_fit
import pandas as pd
import numpy as np
inputfile = "testdata.csv"
df = pd.read_csv(inputfile)
x1= df['x1']
x2 = df['x2']
x3= df['x3']
y= df['y']
def fcn2min(params, x1, x2, x3, y):
b1 = params['b1'].value
b2 = params['b2'].value
b3 = params['b3'].value
b5 = params['b5'].value
model = x1 + b1 + (0.3)*np.log(x2-b2)*b3 - (0.7)*x3*b3 + b5*x2
return model - y
params = Parameters()
params.add('b1', value= 10)
params.add('b2', value= 1990)
params.add('b3', value= 5)
params.add('b5', value= 12)
result = minimize(fcn2min, params, args=(x1, x2, x3, y))
print report_fit(result)
结果,所有参数估计为NaN。谁能解释我做错了什么?或者,在 Python 中有没有很好的实现 Stata 的 nl 函数?非常感谢!
这是 CSV 文件中的数据:
x1,x2,x3,y
1981,15.2824955,14.56475067,2.936807632
1982,15.2635746,15.52343941,2.908272743
1983,15.30461597,16.30871582,2.940227509
1984,15.37490845,16.76519966,3.001846313
1985,15.41295338,17.04235458,3.030970573
1986,15.44680405,17.25271797,3.055702209
1987 ,15.48135281,17.44781876,3.081344604
1988,15.52259159,17.62217331,3.113491058
1989,15.5565939,17.71343422,3.138068199
1990,15.57392025,17.81187439,3.144176483
1991,15.57197666,17.89474106,3.128887177
1992,15.60479259,17.98217583,3.14837265
1993,15.63134575,18.06685829,3.161927223
1994, 15.67116165,18.16578865,3.18959713
1995,15.69621944,18.27449799,3.202876091
1996,15.7329874,18.38712311,3.228042603
1997,15.77698135,18.50685883,3.260077477
1998,15.81788635,18.63579178,3.289312363
1999,15.86141682,18.76427078,3.321393967
2000,15.89737129,18.89691544,3.34650898
2001,15.90485096,18.99729347,3.344522476
2002,15.92070866,19.06253433,3.351119995