我正在尝试使用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


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

最佳答案

我假设您正在寻找残差的偏斜度。您的modRegressionResults对象,并且没有diagn属性(请参见docs)。相反,您可以使用skew中的scipy函数

from scipy.stats import skew

records = []

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

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

    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,
            'Skew': skew(mod.resid_pearson),
        }

        records.append(rec)
        i+=1

关于python - 从OLS回归结果访问偏斜和峰度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57031932/

10-12 19:38