2

I am trying to find the Variance inflation factor (VIF) from a Structural Equation Model (SEM). My model is:

# load the data
library(readxl)
Log_And_SurveyResult <- read_excel("C:/Users/Aakash/Desktop/analysis/Today/Mot-Log.xlsx")

# load lavaan
library(lavaan)

#scale the variables
Log_And_SurveyResult$Time <-
scale(Log_And_SurveyResult$TotalActivity)

model <-
Ct =~ CT1 + CT2 + CT3
R =~ R1 + R2 + R3
B =~ B1 + B2 + B3
UserActivity =~ Time + TotalActivity

fit <- sem(model,data = Log_And_SurveyResult, std.lv = TRUE)
summary(fit, standardized=T)

Here is a sample of my data:

Time,TotalActivity,CT1,CT2,CT3,R1,R2,R3,B1,B2,B3
-0.4923798,-0.09991485,4,4,4,3,3,3,3,3,2
-1.0519708,-1.12771752,3,2,2,2,2,3,4,2,3
-0.5330384,-0.06320762,4,4,5,5,4,4,4,4,4
-1.0134522,-0.67805386,5,4,4,5,5,4,4,5,5
-1.1568273,-1.18277838,4,3,4,3,2,3,3,4,4
-0.8561675,-0.12744528,3,4,4,4,4,3,3,3,3

When I run vif(fit), I get error:

Error: $ operator not defined for this S4 class

I am following the examples from the: http://minato.sip21c.org/msb/man/VIF.html

Most of the example and tutorial on VIF has been generated by using a multiple regression model. But how can I get the VIF for my SEM model?

Werner Hertzog
  • 2,002
  • 3
  • 24
  • 36
User_4373
  • 379
  • 1
  • 3
  • 17
  • You're running `vif()` on a data frame. You're supposed to use it on a regression model object. First run a regression with `model <- lm(x ~ y, data=df)` then `vif(model)`. – Werner Hertzog Sep 24 '20 at 16:54
  • I tried to model as you suggested but that could be x and y here? since all of the data variables I have are dependent variables. – User_4373 Sep 24 '20 at 16:58
  • `vif()` is used to measure the variance inflation factor of models. You can't get the VIF of dependent variables alone. I'll post an example as an answer, see if that helps. – Werner Hertzog Sep 24 '20 at 17:00
  • can you help with my code, how to approach to create the model? – User_4373 Sep 24 '20 at 17:01

1 Answers1

6

To estimate the VIF of a model produced with the sem() function of package lavaan you can create a binary dummy variable, regress it against the independent variables in his model, and then use vif() to estimate the Variance Inflation Factors. Example:

## Create random binary variable
Log_And_SurveyResult$randomvar <- rbinom(nrow(Log_And_SurveyResult), 1, 0.5)

## Model and VIF
Model <- lm(randomvar ~ CT1 + CT2 + CT3, data=Log_And_SurveyResult)
vif(Model)
Werner Hertzog
  • 2,002
  • 3
  • 24
  • 36
  • gives me the error as: `Error in eval(predvars, data, env) : object 'UserActivity' not found` – User_4373 Sep 24 '20 at 17:05
  • I'm having a hard time understanding what you're trying to do. You need to specify what your dependent and independent variables are. You can't run `vif()` on dependent variables alone, you need to have a model with both dependent and independent variables. – Werner Hertzog Sep 24 '20 at 17:13
  • You need to specify what `UserActivity `, `Ct`, `R`, and `B` are also. I'm not seeing these variables on your data frame. – Werner Hertzog Sep 24 '20 at 17:15
  • sorry, I do not have the dependent variables in my data object. That is only the data I have. – User_4373 Sep 24 '20 at 17:15
  • so can I average the CT1+CT2+CT3 to generate the independent variables? since they are the Likert scales measuring values from 1 to 5? – User_4373 Sep 24 '20 at 17:17
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/222032/discussion-between-werner-and-user-4373). – Werner Hertzog Sep 24 '20 at 17:20