##### Calculating Variance Inflation Factors (VIF) in R ###Import Data datum=read.csv(file.choose()) head(datum) ### Run a GLM containing all the x variables of interest results=lm(Size~Age+Sex+MotherSize+FatherSize,data=datum) ###linear model summary(results) #notice that p-values and standard errors are really high #estimated effects are within 2 se of truth #notice that estimated residual error is spot on ### variance inflation library(car) ###function is in the 'car' package vif(results) ###calculates vif for all independent variables #note that y variable is disregarded in vif analysis - doesn't matter what it is #Note that like an ANOVA p-value, vif doesn't tell you which variables are collinear # and not all of them may be collinear. VIF just tells you that the collinearity is there ### Relationship between VIF and r^2 results=lm(Age~Sex+MotherSize+FatherSize,data=datum) summary(results) ### look at r^2 #VIF = 1/(1-r^2) ### Calculate Collinearity between individual, independent variables results=lm(Age~Sex,data=datum) ### to get R^2 between sex and Age summary(results) ### look at r^2 # Repeat for all variables ### Correlation matrix for calculating collinearity among all individual variables datum2=data.frame(Age=datum$Age,MotherSize=datum$MotherSize,FatherSize=datum$FatherSize) #New data frame with only continuous variables head(datum2) #make sure it worked cor(datum2) #correlation matrix - provides r (not r^2) among all continuous variables cor(datum2)^2 ### R^2 among all variables