statsmodels中高多重共线性的捕获

2024-09-28 03:23:40 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我在statsmodels中安装了一个模型

mod = smf.ols('dependent ~ first_category + second_category + other', data=df).fit()

当我做mod.summary()时,我可能会看到以下内容:

Warnings:
[1] The condition number is large, 1.59e+05. This might indicate that there are
strong multicollinearity or other numerical problems.

有时警告是不同的(例如,基于设计矩阵的特征值)。如何捕获变量中的高多重共线性条件?此警告是否存储在模型对象中的某个位置?

另外,在哪里可以找到summary()中字段的描述?


Tags: 模型mod警告dfdatasummaryfitfirst
2条回答

可以通过检查相关矩阵的特征值来检测高多重共线性。很低的特征值表明数据是共线的,对应的特征向量显示哪些变量是共线的。

如果数据中不存在共线性,则期望特征值都不接近零:

>>> xs = np.random.randn(100, 5)      # independent variables
>>> corr = np.corrcoef(xs, rowvar=0)  # correlation matrix
>>> w, v = np.linalg.eig(corr)        # eigen values & eigen vectors
>>> w
array([ 1.256 ,  1.1937,  0.7273,  0.9516,  0.8714])

但是,如果说x[4] - 2 * x[0] - 3 * x[2] = 0,那么

>>> noise = np.random.randn(100)                      # white noise
>>> xs[:,4] = 2 * xs[:,0] + 3 * xs[:,2] + .5 * noise  # collinearity
>>> corr = np.corrcoef(xs, rowvar=0)
>>> w, v = np.linalg.eig(corr)
>>> w
array([ 0.0083,  1.9569,  1.1687,  0.8681,  0.9981])

其中一个特征值(这里是第一个)接近于零。对应的特征向量为:

>>> v[:,0]
array([-0.4077,  0.0059, -0.5886,  0.0018,  0.6981])

忽略几乎为零的系数,上面基本上说x[0]x[2]x[4]是共线的(如预期)。如果将xs值标准化并乘以该特征向量,结果将在0附近徘徊,且方差很小:

>>> std_xs = (xs - xs.mean(axis=0)) / xs.std(axis=0)  # standardized values
>>> ys = std_xs.dot(v[:,0])
>>> ys.mean(), ys.var()
(0, 0.0083)

注意ys.var()基本上是接近于零的本征值。

因此,为了获得高的多线性度,可以考虑相关矩阵的特征值。

基于R的similar question,还有一些其他选项可以帮助人们。我在寻找一个能捕捉共线性的数字,选项包括相关矩阵的行列式和条件数。

根据其中一个R答案,相关矩阵的行列式将“从0(完全共线)到1(无共线)”。我发现有界范围很有用。

行列式的翻译示例:

import numpy as np
import pandas as pd

# Create a sample random dataframe
np.random.seed(321)
x1 = np.random.rand(100)
x2 = np.random.rand(100)
x3 = np.random.rand(100)
df = pd.DataFrame({'x1': x1, 'x2': x2, 'x3': x3})

# Now create a dataframe with multicollinearity
multicollinear_df = df.copy()
multicollinear_df['x3'] = multicollinear_df['x1'] + multicollinear_df['x2']

# Compute both correlation matrices
corr = np.corrcoef(df, rowvar=0)
multicollinear_corr = np.corrcoef(multicollinear_df, rowvar=0)

# Compare the determinants
print np.linalg.det(corr) . # 0.988532159861
print np.linalg.det(multicollinear_corr) . # 2.97779797328e-16

同样地,协方差矩阵的条件数将以完全线性相关逼近无穷大。

print np.linalg.cond(corr) . # 1.23116253259
print np.linalg.cond(multicollinear_corr) . # 6.19985218873e+15

相关问题 更多 >

    热门问题