我正在Lalonde数据集上进行逻辑回归以估计倾向得分。我使用了logit
中的statsmodels.statsmodels.formula.api
函数,并用C()
包裹了协变量,使它们成为分类变量。将age
和educ
视为连续变量会导致成功收敛,但是将它们分类将引发错误
Warning: Maximum number of iterations has been exceeded.
Current function value: 0.617306
Iterations: 35
---------------------------------------------------------------------------
LinAlgError Traceback (most recent call last)
<ipython-input-29-bae905b632a4> in <module>
----> 1 psmodel = fsms.logit('treatment ~ 1 + C(age) + C(educ) + C(black) + C(hisp) + C(married) + C(nodegr)', tdf).fit()
2 tdf['ps'] = psmodel.predict()
3 tdf.head()
~/venv/lib/python3.7/site-packages/statsmodels/discrete/discrete_model.py in fit(self, start_params, method, maxiter, full_output, disp, callback, **kwargs)
1832 bnryfit = super(Logit, self).fit(start_params=start_params,
1833 method=method, maxiter=maxiter, full_output=full_output,
-> 1834 disp=disp, callback=callback, **kwargs)
1835
1836 discretefit = LogitResults(self, bnryfit)
~/venv/lib/python3.7/site-packages/statsmodels/discrete/discrete_model.py in fit(self, start_params, method, maxiter, full_output, disp, callback, **kwargs)
218 mlefit = super(DiscreteModel, self).fit(start_params=start_params,
219 method=method, maxiter=maxiter, full_output=full_output,
--> 220 disp=disp, callback=callback, **kwargs)
221
222 return mlefit # up to subclasses to wrap results
~/venv/lib/python3.7/site-packages/statsmodels/base/model.py in fit(self, start_params, method, maxiter, full_output, disp, fargs, callback, retall, skip_hessian, **kwargs)
471 Hinv = cov_params_func(self, xopt, retvals)
472 elif method == 'newton' and full_output:
--> 473 Hinv = np.linalg.inv(-retvals['Hessian']) / nobs
474 elif not skip_hessian:
475 H = -1 * self.hessian(xopt)
~/venv/lib/python3.7/site-packages/numpy/linalg/linalg.py in inv(a)
549 signature = 'D->D' if isComplexType(t) else 'd->d'
550 extobj = get_linalg_error_extobj(_raise_linalgerror_singular)
--> 551 ainv = _umath_linalg.inv(a, signature=signature, extobj=extobj)
552 return wrap(ainv.astype(result_t, copy=False))
553
~/venv/lib/python3.7/site-packages/numpy/linalg/linalg.py in _raise_linalgerror_singular(err, flag)
95
96 def _raise_linalgerror_singular(err, flag):
---> 97 raise LinAlgError("Singular matrix")
98
99 def _raise_linalgerror_nonposdef(err, flag):
LinAlgError: Singular matrix
要进行复制,请加载Lalonde dataset(您可以从R data(lalonde)
写入csv)并运行以下代码
import numpy as np
import pandas as pd
from statsmodels.formula import api as fsms
filename = 'lalonde.csv'
df = pd.read_csv(filename)
tdf = df.drop(['re74', 're75', 'u74', 'u75'], axis=1)
formula = 'treat ~ 1 + C(age) + C(educ) + C(black) + C(hisp) + C(married) + C(nodegr)'
psmodel = fsms.logit(formula, tdf).fit()
不知道为什么在训练过程中无法收敛/变成奇异的黑森州。
有趣的是,我在网上找到了一些有关因果推断和lalonde数据集的示例,这些示例并未将变量归类,这对我来说毫无意义。一个示例是Microsoft DoWhy,它直接使用了来自sklearn的LogisticRegression。看起来它并没有对变量进行分类。
还有其他一些类似的示例,它们涉及在Lalonde数据集上运行逻辑回归而不将变量归类。这些是数据中的数字,但不应将其视为连续的值。至少我认为,如果每个值都不属于一个类别,则应将它们放入垃圾箱。但这是一个不同的问题,在CrossValidated上更合适。有人可以帮助我理解为什么会出现此错误,以及消除该错误的正确方法是什么?