gpt4 book ai didi

python - 从 OLS 回归结果中获取偏斜和峰度

转载 作者:行者123 更新时间:2023-12-01 00:45:15 27 4
gpt4 key购买 nike

我正在尝试访问 skew 的值和kurtosis使用 statsmodels.formula.api.ols 进行 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

如上面的代码所示,我在从模型访问这些特定项目时遇到问题。

最佳答案

我假设您正在寻找残差的偏度。您的 mod 是一个 RegressionResults 对象,它没有 diagn 属性(请参阅 docs )。相反,您可以使用 scipy 中的 skew 函数

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com