python-3.x - 我怎样才能在Python中测试wald?

标签 python-3.x statsmodels

我想测试一个假设“intercept = 0,beta = 1”,所以我应该进行 Wald 测试并使用模块“statsmodel.formula.api”。

但是我不确定在进行wald测试时哪个代码是正确的。

from statsmodels.datasets import longley
import statsmodels.formula.api as smf
data = longley.load_pandas().data

hypothesis_0 = '(Intercept = 0, GNP = 0)'
hypothesis_1 = '(GNP = 0)'
hypothesis_2 = '(GNP = 1)'
hypothesis_3 = '(Intercept = 0, GNP = 1)'
results = smf.ols('TOTEMP ~ GNP', data).fit()
wald_0 = results.wald_test(hypothesis_0)
wald_1 = results.wald_test(hypothesis_1)
wald_2 = results.wald_test(hypothesis_2)
wald_3 = results.wald_test(hypothesis_3)

print(wald_0)
print(wald_1)
print(wald_2)
print(wald_3)

results.summary()

一开始我认为假设_3是正确的。

但假设_1的结果与回归的F检验相同,表示假设“截距= 0且beta = 0”。

所以,我认为模块“wald_test”默认设置了“intercept = 0”。

我不确定哪一个是正确的。

您能给我一个正确的答案吗?

最佳答案

假设 3 是 Wald 检验的正确联合原假设。 假设1与汇总输出中的F检验相同,即所有斜率系数均为零的假设。

我更改了示例以使用人工数据,因此我们可以看到不同“真实”贝塔系数的效果。

import numpy as np
import pandas as pd
nobs = 100
np.random.seed(987125)
yx = np.random.randn(nobs, 2)
beta0 = 0
beta1 = 1
yx[:, 0] += beta0 + beta1 * yx[:, 1]
data = pd.DataFrame(yx, columns=['TOTEMP', 'GNP'])

hypothesis_0 = '(Intercept = 0, GNP = 0)'
hypothesis_1 = '(GNP = 0)'
hypothesis_2 = '(GNP = 1)'
hypothesis_3 = '(Intercept = 0, GNP = 1)'
results = smf.ols('TOTEMP ~ GNP', data).fit()
wald_0 = results.wald_test(hypothesis_0)
wald_1 = results.wald_test(hypothesis_1)
wald_2 = results.wald_test(hypothesis_2)
wald_3 = results.wald_test(hypothesis_3)

print('H0:', hypothesis_0)
print(wald_0)
print()
print('H0:', hypothesis_1)
print(wald_1)
print()
print('H0:', hypothesis_2)
print(wald_2)
print()
print('H0:', hypothesis_3)
print(wald_3)

在 beta0=0 且 beta1=1 的情况下,假设 2 和 3 均成立。假设0和1与模拟数据不一致。

wald 检验结果拒绝错误的假设,但不拒绝真实的假设,考虑到样本量和效应大小应产生高功效。

H0: (Intercept = 0, GNP = 0)
<F test: F=array([[ 58.22023709]]), p=2.167936332972888e-17, df_denom=98, df_num=2>

H0: (GNP = 0)
<F test: F=array([[ 116.33149937]]), p=2.4054199668085043e-18, df_denom=98, df_num=1>

H0: (GNP = 1)
<F test: F=array([[ 0.1205935]]), p=0.7291363441993846, df_denom=98, df_num=1>

H0: (Intercept = 0, GNP = 1)
<F test: F=array([[ 0.0623734]]), p=0.9395692694166834, df_denom=98, df_num=2>

通过改变 beta0 和 beta1 可以检查类似的结果。

关于python-3.x - 我怎样才能在Python中测试wald?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50117157/

相关文章:

python - 如何将文件数据存储为类对象?

python - 任何 Python 库都会生成发布风格回归表

python - 当 fit_intercept=False 时,为什么 Sklearn R 平方与 statsmodels 不同?

python - statsmodel线性回归(ols)的稳健性问题 - Python

python - Python 中的模型输出 `to_excel`?

python - 如何使用 statsmodels 中的 Linear_model.OLS 使用 LinearRegression 预测数据

python - 全局覆盖打印语句python

python-3.x - python / Pandas : why the empty dataframe when using isin?

python - 如何创建字典类型列?

Python 程序查找 .txt 文件中最常见的单词