如何手动计算方差膨胀因子(VIF)?

huangapple go评论84阅读模式
英文:

How to manually calculate Variance Inflation Factor (VIF)?

问题

我想手动计算mtcars数据集的方差膨胀因子(VIF)。
当我使用包中的函数进行计算时,我得到以下结果:

  1. install.packages('car')
  2. library(car)
  3. fit <- lm(mtcars[,1] ~ ., mtcars[,-1])
  4. summary(fit)
  5. cyl disp hp drat wt qsec
  6. 15.373833 21.620241 9.832037 3.374620 15.164887 7.527958
  7. vs am gear carb
  8. 4.965873 4.648487 5.357452 7.908747

然后,我逐个为每一列进行计算:
对于第一个列cyl,一切都很正常:

  1. fit <- lm(mtcars[,2] ~ ., mtcars[,- c(1:2)])
  2. summary(fit)$r.squared
  3. 1/(1-summary(fit)$r.squared)
  4. [1] 15.37383

然而,对于disp,已经有一个差异。

  1. fit2 <- lm(mtcars[,3] ~ ., mtcars[,- c(1:3)])
  2. summary(fit2)$r.squared
  3. 1/(1-summary(fit2)$r.squared)
  4. [1] 20.08864 # 但它应该是 21.620241

问题出在哪里?

英文:

I would like to manually calculate the Variance Inflation Factor (VIF) for mtcars dataset.
When I do it using the function from package I have the following result:

install.packages('car')
library(car)

  1. fit &lt;- lm(mtcars[,1] ~ ., mtcars[,-1])
  2. summary(fit)
  3. cyl disp hp drat wt qsec
  4. 15.373833 21.620241 9.832037 3.374620 15.164887 7.527958
  5. vs am gear carb
  6. 4.965873 4.648487 5.357452 7.908747

Then I do it one by one for each column:
For the first one cyl it's perfectly ok:

  1. fit &lt;- lm(mtcars[,2] ~ ., mtcars[,- c(1:2)])
  2. summary(fit)$r.squared
  3. 1/(1-summary(fit)$r.squared)
  4. [1] 15.37383

While for disp there's a difference already.

  1. fit2 &lt;- lm(mtcars[,3] ~ ., mtcars[,- c(1:3)])
  2. summary(fit2)$r.squared
  3. 1/(1-summary(fit2)$r.squared)
  4. [1] 20.08864 # but it must be 21.620241

What's wrong?

答案1

得分: 1

正确的VIF公式是:

  1. fit2 <- lm(mtcars[,3] ~ ., mtcars[,-c(1,3)])
  2. 1/(1-summary(fit2)$r.squared)
  3. 21.62024

请注意,1:3 表示 c(1,2,3)。我们不想在设计矩阵中排除第二列。因此,我们应该指定 c(1,3)

英文:

The correct formula for VIF is:

  1. fit2 &lt;- lm(mtcars[,3] ~ ., mtcars[,- c(1,3)])
  2. 1/(1-summary(fit2)$r.squared)
  3. 21.62024

Note that 1:3 means c(1,2,3). We don't want to exclude the second column in the design matrix. Therefore, we should specify c(1,3) instead.

huangapple
  • 本文由 发表于 2023年7月10日 22:14:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76654621.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定