如何在python中仅对某些模型变量执行部分F测试 [英] How to perform a partial F test in a python on only some of the model variables

查看:45
本文介绍了如何在python中仅对某些模型变量执行部分F测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 python 中构建一个具有 65 个变量的回归模型.然后,我只想对 python 中的 2 个选定变量执行部分 F 测试.

I want to build a regression model with 65 variables in python. Then, I want to perform a partial F test only on 2 selected variables in python.

在偏 F 检验中,H0 将是两个变量的两个 Beta(模型中所选变量的系数)等于 0 的假设.H1 将是 Beta 值中至少有一个不等于 0 的假设.

In the partial F test, H0 will be the hypothesis that the two Beta (coefficient of the selected variable in the model) of the two variables are equal to 0. H1 will be the hypothesis that at least one of the Beta is not equal to 0.

我到处搜索,但没有找到我的问题的答案.我很乐意为您提供帮助.

I searched everywhere and found no answer to my question. I would be happy for your help.

推荐答案

更多详情,可以查看关于 statsmodels 结果的 f 测试的帮助页面.

例如你的数据是这样的(我只使用了 5 个变量):

For example your data is like this (i use only 5 variables):

import numpy as np
import pandas as pd
import statsmodels.api as sm

np.random.seed(999)

data = pd.DataFrame(np.random.uniform(0,1,(50,6)),
                   columns=['x1','x2','x3','x4','x5','y'])

我们可以拟合回归,结果如下:

We can fit the regression and results look like:

results = sm.OLS(endog= data['y'],exog=sm.add_constant(data.iloc[:,:5])).fit()
results.summary()

    coef    std err t   P>|t|   [0.025  0.975]
const   0.7432  0.201   3.700   0.001   0.338   1.148
x1  -0.0345 0.147   -0.235  0.816   -0.331  0.262
x2  -0.1758 0.151   -1.165  0.250   -0.480  0.128
x3  -0.1472 0.150   -0.982  0.331   -0.449  0.155
x4  -0.2735 0.144   -1.905  0.063   -0.563  0.016
x5  0.1143  0.135   0.845   0.403   -0.158  0.387

建立假设,在这种情况下 x3 和 x4 为零,然后执行测试:

Set up the hypothesis, in this case x3 and x4 are zero, then perform the test:

hypotheses = '(x3 = 0), (x4 = 0)'
f_test = results.f_test(hypotheses)
print(f_test)

<F test: F=array([[2.64119819]]), p=0.08255414803527926, df_denom=44, df_num=2>

这篇关于如何在python中仅对某些模型变量执行部分F测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆