从OLS回归结果访问偏斜和峰度

时间:2019-07-14 23:36:50

标签: python pandas statsmodels

我正在尝试使用skew从OLS回归中访问kurtosisstatsmodels.formula.api.ols的值

这是我在示例数据框上的工作:

# First initialize the df
import pandas as pd
import numpy as np

np.random.seed(11)
df = pd.DataFrame({'Group':np.random.randint(1,4,100),'Score_1':np.random.randint(1,100,100),'Score_2':np.random.randint(1,200,100)})
df['Score_1'] = df['Score_1']*df['Group'] * np.random.random_sample(100)
df['Score_2'] = df['Score_1']*df['Score_2']

# -----------------------------------

# Next, apply ols regression loopwise:
from statsmodels.formula.api import ols

records = []

for col in ['Score_1','Score_2']:

    mod = ols(f'{col} ~ C(Group)',data=df).fit()

    # If we only care about significant differences
    # if (mod.f_pvalue<=0.05):

    i = 0
    for gen in sorted(df['Group'].unique()):
        rec = {'variable':col,
               'f_pvalue': mod.f_pvalue,
               'group': gen,
               'mean':mod.params[i],
               'conf int lower':mod.conf_int().values[i][0],
               'conf int upper':mod.conf_int().values[i][1],
               'p value': mod.pvalues[i],
               'Log-Likelihood':mod.llf,
# **I'm trying to access the value for the item below:**
#                    'Skew':mod.diagn['skew'],
              }

        records.append(rec)
        i+=1

如上面的代码所示,我无法从模型访问这些特定项。

1 个答案:

答案 0 :(得分:2)

我假设您正在寻找残差的偏斜度。您的natcasesort($fileList);mod对象,没有RegressionResults属性(请参见docs)。相反,您可以使用diagn中的skew函数

scipy