计算 VIF 时出错(方差 Inflation 因子)

Error in calculating VIF (Variance Inflation Factor)

在 Rstudio 中计算小型数据集的 VIF 时出现以下错误。有人能帮忙吗?如果需要,我可以提供有关数据集的更多信息。

"Error in as.vector(y) - mean(y) non-numeric argument to binary operator".

数据集:80 obs。和 15 个变量(所有变量都是数字)

遵循的步骤:

   # 1. Determine correlation  
    library(corrplot)  
    cor.data <- cor(train)  
    corrplot(cor.data, method = 'color')  
    cor.data    


# 2. Build Model  

    model2 <- lm(Volume~., train)  
    summary(model2)  

# 3. Calculate VIF  

    library(VIF)  
    vif(model2) 

这是一个包含 20 个 obs 的示例数据集。

train <- structure(list(Price = c(949, 2249.99, 399, 409.99, 1079.99, 
114.22, 379.99, 65.29, 119.99, 16.99, 6.55, 15, 52.5, 21.08, 
18.98, 3.6, 3.6, 174.99, 9.99, 670), X.5.Star.Reviews. = c(3, 
2, 3, 49, 58, 83, 11, 33, 16, 10, 21, 75, 10, 313, 349, 8, 11, 
170, 15, 20), X.4.Star.Reviews. = c(3, 1, 0, 19, 31, 30, 3, 19, 
9, 1, 2, 25, 8, 62, 118, 6, 5, 100, 12, 2), X.3.Star.Reviews. = c(2, 
0, 0, 8, 11, 10, 0, 12, 2, 1, 2, 6, 5, 13, 27, 3, 2, 23, 4, 4
), X.2.Star.Reviews. = c(0, 0, 0, 3, 7, 9, 0, 5, 0, 0, 4, 3, 
0, 8, 7, 2, 2, 20, 0, 2), X.1.Star.Reviews. = c(0, 0, 0, 9, 36, 
40, 1, 9, 2, 0, 15, 3, 1, 16, 5, 1, 1, 20, 4, 4), X.Positive.Service.Review.   = c(2, 
1, 1, 7, 7, 12, 3, 5, 2, 2, 2, 9, 2, 44, 57, 0, 0, 310, 3, 4), 
    X.Negative.Service.Review. = c(0, 0, 0, 8, 20, 5, 0, 3, 1, 
    0, 1, 2, 0, 3, 3, 0, 0, 6, 1, 3), X.Would.consumer.recommend.product. = c(0.9, 
    0.9, 0.9, 0.8, 0.7, 0.3, 0.9, 0.7, 0.8, 0.9, 0.5, 0.2, 0.8, 
    0.9, 0.9, 0.8, 0.8, 0.8, 0.8, 0.7), X.Shipping.Weight..lbs.. = c(25.8, 
    50, 17.4, 5.7, 7, 1.6, 7.3, 12, 1.8, 0.75, 1, 2.2, 1.1, 0.35, 
    0.6, 0.01, 0.01, 1.4, 0.4, 0.25), X.Product.Depth. = c(23.94, 
    35, 10.5, 15, 12.9, 5.8, 6.7, 7.9, 10.6, 10.7, 7.3, 21.3, 
    15.6, 5.7, 1.7, 11.5, 11.5, 13.8, 11.1, 5.8), X.Product.Width. = c(6.62, 
    31.75, 8.3, 9.9, 0.3, 4, 10.3, 6.7, 9.4, 13.1, 7, 1.8, 3, 
    3.5, 13.5, 8.5, 8.5, 8.2, 7.6, 1.4), X.Product.Height. = c(16.89, 
    19, 10.2, 1.3, 8.9, 1, 11.5, 2.2, 4.7, 0.6, 1.6, 7.8, 15, 
    8.3, 10.2, 0.4, 0.4, 0.4, 0.5, 7.8), X.Profit.margin. = c(0.15, 
    0.25, 0.08, 0.08, 0.09, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 
    0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.15), Volume = c(12, 
    8, 12, 196, 232, 332, 44, 132, 64, 40, 84, 300, 40, 1252, 
    1396, 32, 44, 680, 60, 80)), .Names = c("Price", "X.5.Star.Reviews.", 
"X.4.Star.Reviews.", "X.3.Star.Reviews.", "X.2.Star.Reviews.", 
"X.1.Star.Reviews.", "X.Positive.Service.Review.", "X.Negative.Service.Review.", 
"X.Would.consumer.recommend.product.", "X.Shipping.Weight..lbs..", 
"X.Product.Depth.", "X.Product.Width.", "X.Product.Height.", 
"X.Profit.margin.", "Volume"), row.names = c(NA, 20L), class = "data.frame")

vif 的输入有误。它需要响应 y 和预测变量 x:

vif(train$Volume,subset(train,select=-Volume),subsize=19)

我必须将 subsize 参数设置为 <= 观察次数(默认值为 200)的值。

VIF 包中的 vif 函数不估计方差 Inflation 因子 (VIF)。 "It selects variables for a linear model" 和 "returns a subset of variables for building a linear model.";参见 here 的描述。

您想要的是 car 包中的 vif 函数。

install.packages("car")
library(car)
vif(model2) # This should do it

编辑:我不会具体评论统计数据方面的问题,但看起来你非常适合,这很不寻常,表明你的数据存在问题。

有2个R库"car""VIF"定义了相同的函数vif()不同的。您的 result/error 取决于您在当前会话中加载的包。

如果您在会话中使用 "VIF" 库并将线性模型作为参数传递给 vif() 函数,那么您将得到初始查询中给出的错误,如下所示:

> model1 = lm(Satisfaction~., data1)
> library(VIF)

Attaching package: ‘VIF’

The following object is masked from ‘package:car’:

 vif

> vif(model1)
Error in as.vector(y) - mean(y) : non-numeric argument to binary operator
In addition: Warning message:
In mean.default(y) : argument is not numeric or logical: returning NA

如果您在 R 会话中加载 "car" 库而不是 "VIF",那么您将获得线性模型预期的 vif 数,如下所示:

> model1 = lm(Satisfaction~., data1)
> library(car)
Loading required package: carData

Attaching package: ‘car’

The following object is masked from ‘package:psych’:

    logit

> vif(model1)
   ProdQual        Ecom     TechSup     CompRes Advertising    ProdLine SalesFImage  ComPricing 
   1.635797    2.756694    2.976796    4.730448    1.508933    3.488185    3.439420    1.635000 
 WartyClaim  OrdBilling    DelSpeed 
   3.198337    2.902999    6.516014 

data1 中的所有列都是数字。希望有帮助