计算VIF(方差膨胀因子)时出错 [英] Error in calculating VIF (Variance Inflation Factor)

查看:201
本文介绍了计算VIF(方差膨胀因子)时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am getting the following error when calculating VIF on a small dataset in Rstudio. Could anyone help? I can provide more information on the dataset if needed.

"as.vector(y)中的错误-二进制的mean(y)非数字参数运算符".

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

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

Dataset: 80 obs. and 15 variables (all variables are numeric)

已执行的步骤:

   # 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个观察点.

Here is a sample dataset with 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 包中的 vif 函数不会估计方差通货膨胀系数(VIF).它为线性模型选择变量",并且返回用于构建线性模型的变量子集".

The vif function from the VIF package does not estimates the Variance Inflation Factor(VIF). "It selects variables for a linear model" and "returns a subset of variables for building a linear model."; see here for the description.

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

What you want is the vif function from the car package.

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

我不会在统计方面专门发表评论,但是看来您很合适,这很不寻常,说明您的数据中存在一些问题.

I won't comment specifically on the statistics side, but it seems like you have a perfect fit, something quite unusual, suggesting some problem in your data.

这篇关于计算VIF(方差膨胀因子)时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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