计算VIF(差异膨胀因子)时出错

时间:2016-10-21 02:29:05

标签: r predict feature-selection variance

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

  

" as.vector(y)中的错误 - 二进制的均值(y)非数字参数   操作员"

数据集:80个障碍物。和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 ob​​s的样本数据集。

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")

3 个答案:

答案 0 :(得分:4)

vif包中的VIF函数不会估算差异通货膨胀因子(VIF)。 “它选择线性模型的变量”和“返回用于构建线性模型的变量子集。”;有关说明,请参阅here

您想要的是vif包中的car功能。

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

编辑:我不会在统计方面做出具体评论,但似乎你有一个完美的契合,一些非常不寻常的东西,暗示你的数据存在一些问题。

答案 1 :(得分:1)

有2个R库“汽车” “ VIF” ,它们具有相同的函数vif(),但定义不同。您的结果/错误取决于您在当前会话中加载的软件包。

如果您在会话中使用“ 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中的所有列都是数字。希望有帮助

答案 2 :(得分:0)

你给了vif错误的输入。它需要响应y和预测变量x

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

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