6

我在对包含大约 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: zero-size array to reduce operation maximum,它没有标识。

---------------------------------------------------------------------------
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

我怀疑错误是由于目标变量(即 KPI6)包含一些 NaN 而引起的,所以我尝试像这样删除所有带有 KPI6 = NaN 的行,但问题仍然存在:

KPI.dropna(subset = ['KPI6'])

我还尝试删除仅包含 NaN 值的所有行,但问题仍然存在:

KPI.dropna(how = 'all')

我结合了上述两个步骤,问题仍然存在。消除此错误的唯一方法是用某些东西(例如 0、平均值、中值等)实际估算缺失的数据。但是,我希望尽可能避免这种方法,因为我想对原始数据执行 OLS 回归。

当我尝试仅选择几个变量作为预测变量时,OLS 回归也有效,但这又不是我的目标。我想将除 KPI6 之外的所有其他变量作为预测变量。

有什么解决办法吗?一个星期以来,我真的很紧张。任何帮助表示赞赏。我不是专业的 Python 编码员,所以如果您能用外行的方式解决问题(并提出解决方案),我将不胜感激。

提前非常感谢。

4

1 回答 1

4

使用公式时默认的缺失处理是删除任何包含至少一个 nan 的行。如果每一行都包含一个 nan,那么就没有观察值了。我认为这就是追溯结束的ValueError: zero-size array意思。

如果总体上有足够的数据,那么您可以尝试使用 MICE 进行估算和估算,这将迭代地估算每个变量的缺失值。

于 2017-08-07T13:06:09.073 回答