# Implementing a VIF test on Panel Data in R

#### StudentCBS

##### New Member
I'm currently writing my thesis about the performance of Ethical Companies.

I'm runnning a Fama Macbeth Cross Sectional Regression as in the picture attached.

The problem is that ESG score is correlated with Firm Size, and when I include firm size the alpha(1) changes a lot (turns from stat sign. negative to stat.sign. positive). I therefore fear that I have multicollinearity in the model.

I therefore want to run a VIF test, but as I am not to good in R I have problems implementing the code in R.

I tried to use the vif function in the car package, but just got error (I assume it is wrong coded):

Code:
vif(Y~X)
Error in vcov.default(mod) :
there is no vcov() method for models of class formula
In is.na(coef(mod)) :
is.na() applied to non-(list or vector) of type 'NULL'

Here is the code that I use to run the regression, after I average and run t tests:

Code:
for (t in 1:T){
Y=ZRet[t,]
X=cbind(matrix(1,N,1),ESG[t,],Beta[t,],MarketCap[t,],PriceToBook[t,],Momentum[t,],BasicMaterialsDummy[t,],ConsumerGoodsDummy[t,],ConsumerServicesDummy[t,],HealthCareDummy[t,],IndustrialsDummy[t,],OilAndGasDummy[t,],TechnologyDummy[t,],TelecommunicationsDummy[t,],UtilitiesDummy[t,])
Theta=solve(t(X)%*%X)%*%t(X)%*%Y
gamma0[t,]=Theta[1,]
gamma1[t,]=Theta[2,]
gamma2[t,]=Theta[3,]
gamma3[t,]=Theta[4,]
gamma4[t,]=Theta[5,]
gamma5[t,]=Theta[6,]
gamma6[t,]=Theta[7,]
gamma7[t,]=Theta[8,]
gamma8[t,]=Theta[9,]
gamma9[t,]=Theta[10,]
gamma10[t,]=Theta[11,]
gamma11[t,]=Theta[12,]
gamma12[t,]=Theta[13,]
gamma13[t,]=Theta[14,]
gamma14[t,]=Theta[15,]
}
Hope anyone can help!

Thanks,
Anders

#### rogojel

##### TS Contributor
hi,
I guess you need to run the regression, save the results in an object and call the vif function on THAT object, not on the formula.

regards

#### StudentCBS

##### New Member
Got it now!

Had to transform my data from wide-form to long-form.

Then ran:

Code:
mydata=read.csv("US Panel Data.csv")

attach(mydata)

Y=cbind(Return)

pdata=plm.data(mydata,index=c("id","t"))

model=plm(Y ~ 1+ESG+Beta+Market.Cap+PTBV+Momentum+Dummy1+Dummy2+Dummy3+Dummy4+Dummy5+Dummy6+Dummy7+Dummy8+Dummy9,data=pdata,model="pooling")

vif(model)
This video was of help: https://www.youtube.com/watch?v=1pST2lUx6QM

/Anders