我怎样才能在 Python 中进行 wald 测试?
How can I do test wald in Python?
我想检验一个假设 "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()
一开始我觉得hypothesis_3是对的。
但hypothesis_1的结果与回归的F检验相同,表示假设'intercept = 0 and beta = 0'。
所以,我认为模块,'wald_test' 默认设置 'intercept = 0'。
我不确定哪个是正确的。
你能给我一个答案吗?
假设 3 是 wald 检验的正确联合零假设。
假设 1 与汇总输出中的 F 检验相同,即所有斜率系数都为零的假设。
我将示例更改为使用人工数据,因此我们可以看到不同 "true" beta 系数的效果。
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 来检查类似的结果。
我想检验一个假设 "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()
一开始我觉得hypothesis_3是对的。
但hypothesis_1的结果与回归的F检验相同,表示假设'intercept = 0 and beta = 0'。
所以,我认为模块,'wald_test' 默认设置 'intercept = 0'。
我不确定哪个是正确的。
你能给我一个答案吗?
假设 3 是 wald 检验的正确联合零假设。 假设 1 与汇总输出中的 F 检验相同,即所有斜率系数都为零的假设。
我将示例更改为使用人工数据,因此我们可以看到不同 "true" beta 系数的效果。
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 来检查类似的结果。