Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何在Python中测试wald?_Python 3.x_Statsmodels - Fatal编程技术网

Python 3.x 如何在Python中测试wald?

Python 3.x 如何在Python中测试wald?,python-3.x,statsmodels,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)'

我想测试一个假设“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,β=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与模拟数据不一致

瓦尔德检验结果拒绝了错误的假设,而不拒绝了真实的假设,因为样本量和效应量应导致高功率

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>
H0:(截距=0,GNP=0)
H0:(国民生产总值=0)
H0:(国民生产总值=1)
H0:(截距=0,GNP=1)

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

F-回归检验是所有斜率系数都为零,但截距不受限制,即与本例中的假设_1相同。一般来说,是的,为什么需要检查截距?为什么需要检查截距为0?关于IV/DV关系,非零截距意味着什么?在某些情况下,我们可能需要测试回归是否经过原点,即具有零截距。但这不是很常见的情况。