python计算方差膨胀因子_Python中的方差膨胀因子

I'm trying to calculate the variance inflation factor (VIF) for each column in a simple dataset in python:

a b c d

1 2 4 4

1 2 6 3

2 3 7 4

3 2 8 5

4 1 9 4

I have already done this in R using the vif function from the usdm library which gives the following results:

a

b

c

d

df

vif_df

print(vif_df)

Variables VIF

a 22.95

b 3.00

c 12.95

d 3.00

However, when I do the same in python using the statsmodel vif function, my results are:

a = [1, 1, 2, 3, 4]

b = [2, 2, 3, 2, 1]

c = [4, 6, 7, 8, 9]

d = [4, 3, 4, 5, 4]

ck = np.column_stack([a, b, c, d])

vif = [variance_inflation_factor(ck, i) for i in range(ck.shape[1])]

print(vif)

Variables VIF

a 47.136986301369774

b 28.931506849315081

c 80.31506849315096

d 40.438356164383549

The results are vastly different, even though the inputs are the same. In general, results from the statsmodel VIF function seem to be wrong, but I'm not sure if this is because of the way I am calling it or if it is an issue with the function itself.

I was hoping someone could help me figure out whether I was incorrectly calling the statsmodel function or explain the discrepancies in the results. If it's an issue with the function then are there any VIF alternatives in python?

解决方案

I believe the reason for this is due to a difference in Python's OLS. OLS, which is used in the python variance inflation factor calculation, does not add an intercept by default. You definitely want an intercept in there however.

What you'd want to do is add one more column to your matrix, ck, filled with ones to represent a constant. This will be the the intercept term of the equation. Once this is done, your values should match out properly.

Edited: replaced zeroes with ones

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值