我怎样才能在Python中测试wald?

파워뿡*_*뿡뿡이 6 python-3.x statsmodels

我想测试一个假设“截距 = 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()
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

请您给我一个答案,哪一个是正确的?

Jos*_*sef 5

假设 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)
Run Code Online (Sandbox Code Playgroud)

在 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>
Run Code Online (Sandbox Code Playgroud)

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