我在包含大约7500个数据点的数据集上执行“多次回归”时遇到问题,这些数据点在某些列和行中缺少数据(NaN)。每行至少有一个NaN值。有些行只包含NaN值。
我正在使用OLS Statsmodel进行回归分析。我试图不使用Scikit Learn来执行OLS回归,因为(我可能错了)但是我必须将数据集中的缺失数据归咎于数据集,这会在一定程度上扭曲数据集。
我的数据集如下所示: KPI
这就是我所做的(目标变量是KP6,预测变量是剩余的变量):
est2 = ols(formula = KPI.KPI6.name + ' ~ ' + ' + '.join(KPI.drop('KPI6', axis = 1).columns.tolist()), data = KPI).fit()
它返回一个ValueError:零大小数组到减少操作最大值,没有标识。
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-207-b24ba316a452> in <module>()
3 #test = KPI.dropna(how='all')
4 #test = KPI.fillna(0)
----> 5 est2 = ols(formula = KPI.KPI6.name + ' ~ ' + ' + '.join(KPI.drop('KPI6', axis = 1).columns.tolist()), data = KPI).fit()
6 print(est2.summary())
/Users/anhtran/anaconda/lib/python3.6/site-packages/statsmodels/base/model.py in from_formula(cls, formula, data, subset, drop_cols, *args, **kwargs)
172 'formula': formula, # attach formula for unpckling
173 'design_info': design_info})
--> 174 mod = cls(endog, exog, *args, **kwargs)
175 mod.formula = formula
176
/Users/anhtran/anaconda/lib/python3.6/site-packages/statsmodels/regression/linear_model.py in __init__(self, endog, exog, missing, hasconst, **kwargs)
629 **kwargs):
630 super(OLS, self).__init__(endog, exog, missing=missing,
--> 631 hasconst=hasconst, **kwargs)
632 if "weights" in self._init_keys:
633 self._init_keys.remove("weights")
/Users/anhtran/anaconda/lib/python3.6/site-packages/statsmodels/regression/linear_model.py in __init__(self, endog, exog, weights, missing, hasconst, **kwargs)
524 weights = weights.squeeze()
525 super(WLS, self).__init__(endog, exog, missing=missing,
--> 526 weights=weights, hasconst=hasconst, **kwargs)
527 nobs = self.exog.shape[0]
528 weights = self.weights
/Users/anhtran/anaconda/lib/python3.6/site-packages/statsmodels/regression/linear_model.py in __init__(self, endog, exog, **kwargs)
93 """
94 def __init__(self, endog, exog, **kwargs):
---> 95 super(RegressionModel, self).__init__(endog, exog, **kwargs)
96 self._data_attr.extend(['pinv_wexog', 'wendog', 'wexog', 'weights'])
97
/Users/anhtran/anaconda/lib/python3.6/site-packages/statsmodels/base/model.py in __init__(self, endog, exog, **kwargs)
210
211 def __init__(self, endog, exog=None, **kwargs):
--> 212 super(LikelihoodModel, self).__init__(endog, exog, **kwargs)
213 self.initialize()
214
/Users/anhtran/anaconda/lib/python3.6/site-packages/statsmodels/base/model.py in __init__(self, endog, exog, **kwargs)
61 hasconst = kwargs.pop('hasconst', None)
62 self.data = self._handle_data(endog, exog, missing, hasconst,
---> 63 **kwargs)
64 self.k_constant = self.data.k_constant
65 self.exog = self.data.exog
/Users/anhtran/anaconda/lib/python3.6/site-packages/statsmodels/base/model.py in _handle_data(self, endog, exog, missing, hasconst, **kwargs)
86
87 def _handle_data(self, endog, exog, missing, hasconst, **kwargs):
---> 88 data = handle_data(endog, exog, missing, hasconst, **kwargs)
89 # kwargs arrays could have changed, easier to just attach here
90 for key in kwargs:
/Users/anhtran/anaconda/lib/python3.6/site-packages/statsmodels/base/data.py in handle_data(endog, exog, missing, hasconst, **kwargs)
628 klass = handle_data_class_factory(endog, exog)
629 return klass(endog, exog=exog, missing=missing, hasconst=hasconst,
--> 630 **kwargs)
/Users/anhtran/anaconda/lib/python3.6/site-packages/statsmodels/base/data.py in __init__(self, endog, exog, missing, hasconst, **kwargs)
77
78 # this has side-effects, attaches k_constant and const_idx
---> 79 self._handle_constant(hasconst)
80 self._check_integrity()
81 self._cache = resettable_cache()
/Users/anhtran/anaconda/lib/python3.6/site-packages/statsmodels/base/data.py in _handle_constant(self, hasconst)
129 # detect where the constant is
130 check_implicit = False
--> 131 const_idx = np.where(self.exog.ptp(axis=0) == 0)[0].squeeze()
132 self.k_constant = const_idx.size
133
ValueError: zero-size array to reduction operation maximum which has no identity
我怀疑错误是由于包含一些NaN的目标变量(即KPI6)引起的,所以我尝试使用KPI6 = NaN这样丢弃所有行,但问题仍然存在:
KPI.dropna(subset = ['KPI6'])
我还尝试删除仅包含NaN值的所有行,但问题仍然存在:
KPI.dropna(how = 'all')
我结合上述两个步骤,问题仍然存在。消除此错误的唯一方法是用某些东西(例如0,平均值,中位数等)实际输入缺失的数据。但是,我希望尽可能避免使用此方法,因为我想对原始数据执行OLS回归。
当我尝试仅选择少数变量作为预测变量时,OLS回归也有效,但这不再是我的目标。我想包括除KPI6之外的所有其他变量作为预测变量。
这有什么解决方案吗?我已经非常紧张了一个星期。任何帮助表示赞赏。我不是一个专业的Python编码器,所以如果你能用外行的术语解决问题(并建议一个解决方案),我会很感激。
非常感谢。
答案 0 :(得分:2)
使用公式时的默认缺失处理是删除包含至少一个nan的任何行。如果每行包含一个nan,则没有任何观察结果。我认为回溯ValueError: zero-size array
的结尾意味着什么。
如果您有足够的数据,那么您可以尝试使用MICE进行估算和估算,这将迭代地计算每个变量的缺失值。