Importación de datos

library(wooldridge)
data(hprice1)
head(force(hprice1),n=5)
price assess bdrms lotsize sqrft colonial lprice lassess llotsize lsqrft
300 349.1 4 6126 2438 1 5.703783 5.855359 8.720297 7.798934
370 351.5 3 9903 2076 1 5.913503 5.862210 9.200593 7.638198
191 217.7 3 5200 1374 0 5.252274 5.383118 8.556414 7.225481
195 231.8 3 4600 1448 1 5.273000 5.445875 8.433811 7.277938
373 319.1 4 6095 2514 1 5.921578 5.765504 8.715224 7.829630

1. Estimación del modelo

library(stargazer)
modelo_estimado<-lm(formula = price~lotsize+sqrft+bdrms, data = hprice1)
stargazer(modelo_estimado, title = "Ejercicio de pruebas de Multicolinealidad", type = "text")
## 
## Ejercicio de pruebas de Multicolinealidad
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                                price           
## -----------------------------------------------
## lotsize                      0.002***          
##                               (0.001)          
##                                                
## sqrft                        0.123***          
##                               (0.013)          
##                                                
## bdrms                         13.853           
##                               (9.010)          
##                                                
## Constant                      -21.770          
##                              (29.475)          
##                                                
## -----------------------------------------------
## Observations                    88             
## R2                             0.672           
## Adjusted R2                    0.661           
## Residual Std. Error      59.833 (df = 84)      
## F Statistic           57.460*** (df = 3; 84)   
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01

2. Verificación de la evidencia de la independencia de los regresores

Indice de Condición

Calculo Manual

Se basa la matriz X^t X:

library(stargazer)
X_mat<-model.matrix(modelo_estimado)
stargazer(head(X_mat, n=6), type = "text")
## 
## =================================
##   (Intercept) lotsize sqrft bdrms
## ---------------------------------
## 1      1       6,126  2,438   4  
## 2      1       9,903  2,076   3  
## 3      1       5,200  1,374   3  
## 4      1       4,600  1,448   3  
## 5      1       6,095  2,514   4  
## 6      1       8,566  2,754   5  
## ---------------------------------
XX_mattrix<-t(X_mat)%*%X_mat
stargazer(XX_mattrix, type = "text")
## 
## ==============================================================
##             (Intercept)    lotsize         sqrft       bdrms  
## --------------------------------------------------------------
## (Intercept)     88         793,748        177,205       314   
## lotsize       793,748   16,165,159,010 1,692,290,257 2,933,767
## sqrft         177,205   1,692,290,257   385,820,561   654,755 
## bdrms           314       2,933,767       654,755      1,182  
## --------------------------------------------------------------

Calculo de la matriz de normalización

library(stargazer)
options(scipen = 999)
Sn<-solve(diag(sqrt(diag(XX_mattrix))))
stargazer(Sn, type = "text")
## 
## ==========================
## 0.107    0      0      0  
## 0     0.00001   0      0  
## 0        0    0.0001   0  
## 0        0      0    0.029
## --------------------------

X^t X Normalizada

library(stargazer)
XX_norm<-(Sn%*%XX_mattrix)%*%Sn
stargazer(XX_norm, type = "text", digits = 4)
## 
## ===========================
## 1      0.6655 0.9617 0.9736
## 0.6655   1    0.6776 0.6712
## 0.9617 0.6776   1    0.9696
## 0.9736 0.6712 0.9696   1   
## ---------------------------

Autovalores de X^t X Normalizada

library(stargazer)
#autovalores 
lambdas<-eigen(XX_norm, symmetric = TRUE)
stargazer(lambdas$values, type = "text")
## 
## =======================
## 3.482 0.455 0.039 0.025
## -----------------------

Calculo de K(x)

K<-sqrt(max(lambdas$values)/min(lambdas$values))
print(K)
## [1] 11.86778

Como K(x) es inferior a 20, 11.86778 < 20, la multicolinealidad es leve, lo cual no se considera un problema.

Calculo del Indice de Condicion con “mctest”

library(mctest)
X_mat<-model.matrix(modelo_estimado)
mctest(mod = modelo_estimado)
## 
## Call:
## omcdiag(mod = mod, Inter = TRUE, detr = detr, red = red, conf = conf, 
##     theil = theil, cn = cn)
## 
## 
## Overall Multicollinearity Diagnostics
## 
##                        MC Results detection
## Determinant |X'X|:         0.6918         0
## Farrar Chi-Square:        31.3812         1
## Red Indicator:             0.3341         0
## Sum of Lambda Inverse:     3.8525         0
## Theil's Method:           -0.7297         0
## Condition Number:         11.8678         0
## 
## 1 --> COLLINEARITY is detected by the test 
## 0 --> COLLINEARITY is not detected by the test

Usando “olsrr”

library(olsrr)
ols_eigen_cindex(model= modelo_estimado)
Eigenvalue Condition Index intercept lotsize sqrft bdrms
3.4815860 1.000000 0.0036630 0.0277803 0.0041563 0.0029396
0.4551838 2.765637 0.0068007 0.9670803 0.0060673 0.0050964
0.0385108 9.508174 0.4725814 0.0051085 0.8160793 0.0169382
0.0247194 11.867781 0.5169548 0.0000309 0.1736971 0.9750259

Prueba FG

Calculo Manual

Calculo de |R|

library(stargazer)
Zn<-scale(X_mat[,-1])
stargazer(head(Zn, n=6), type = "text")
## 
## =======================
##   lotsize sqrft  bdrms 
## -----------------------
## 1 -0.284  0.735  0.513 
## 2  0.087  0.108  -0.675
## 3 -0.375  -1.108 -0.675
## 4 -0.434  -0.980 -0.675
## 5 -0.287  0.867  0.513 
## 6 -0.045  1.283  1.702 
## -----------------------

Calcular la matriz R

library(stargazer)
n<-nrow(Zn)
R<-(t(Zn)%*%Zn)*(1/(n-1))
stargazer(R, type = "text", digits = 4)
## 
## =============================
##         lotsize sqrft  bdrms 
## -----------------------------
## lotsize    1    0.1838 0.1363
## sqrft   0.1838    1    0.5315
## bdrms   0.1363  0.5315   1   
## -----------------------------

Calcular |R|

determinante_R<-det(R)
print(determinante_R)
## [1] 0.6917931

Aplicando la prueba de Farrer Glaubar

Estadistico X^2~FG

m<-ncol(X_mat[,-1])
n<-nrow(X_mat[,-1])
chi_FG<--(n-1-(2*m+5)/6)*log(determinante_R)
print(chi_FG)
## [1] 31.38122

Valor critico

gl<-m*(m-1)/2
VC<-qchisq(p=0.95,df=gl)
print(VC)
## [1] 7.814728

Regla de desición:

Como X^2FG es >= V.C. se rechaza H~0, es decir, que existe evidencia de colinealidad en los regresores.

Cálculo de FG usando “mctest”

library(mctest)
mctest::omcdiag(mod = modelo_estimado)
## 
## Call:
## mctest::omcdiag(mod = modelo_estimado)
## 
## 
## Overall Multicollinearity Diagnostics
## 
##                        MC Results detection
## Determinant |X'X|:         0.6918         0
## Farrar Chi-Square:        31.3812         1
## Red Indicator:             0.3341         0
## Sum of Lambda Inverse:     3.8525         0
## Theil's Method:           -0.7297         0
## Condition Number:         11.8678         0
## 
## 1 --> COLLINEARITY is detected by the test 
## 0 --> COLLINEARITY is not detected by the test

Cálculo de FG usando “psych”

library(psych)
FG_test<-cortest.bartlett(X_mat[,-1])
print(FG_test)
## $chisq
## [1] 31.38122
## 
## $p.value
## [1] 0.0000007065806
## 
## $df
## [1] 3

Presentando resultados en forma grafica

library(fastGraph)
alphan_sig<-0.05
chi_FG<- -(n-1-(2*m+5)/6)*log(determinante_R)
gl<-m*(m-1)/2
VC<-qchisq(p = 0.95,df = gl)
shadeDist(chi_FG,ddist = "dchisq",
          parm1 = gl,
          lower.tail = FALSE, xmin = 0,
          sub=paste("VC:", round(VC,2)," ","chi_FG", round(chi_FG,2)))

3. Factores Inflacionarios de la Varianza

Referencia entre R^2~j

library(dplyr)
R.cuadrado.regresores<-c(0,0.5,.8,.9)
as.data.frame(R.cuadrado.regresores) %>% mutate(VIF=1/(1-R.cuadrado.regresores))
R.cuadrado.regresores VIF
0.0 1
0.5 2
0.8 5
0.9 10

Calculo Manual

print(R)
##           lotsize     sqrft     bdrms
## lotsize 1.0000000 0.1838422 0.1363256
## sqrft   0.1838422 1.0000000 0.5314736
## bdrms   0.1363256 0.5314736 1.0000000

Inversa de la matriz de Correlación R~-1

inversa_R<-solve(R)
print(inversa_R)
##             lotsize      sqrft       bdrms
## lotsize  1.03721145 -0.1610145 -0.05582352
## sqrft   -0.16101454  1.4186543 -0.73202696
## bdrms   -0.05582352 -0.7320270  1.39666321

VIF´s para el modelo estimado.

VIFs<-diag(inversa_R)
print(VIFs)
##  lotsize    sqrft    bdrms 
## 1.037211 1.418654 1.396663

Usando “performance”

library(performance)
VIFs<-multicollinearity(x = modelo_estimado,verbose = FALSE)
VIFs
Term VIF VIF_CI_low VIF_CI_high SE_factor Tolerance Tolerance_CI_low Tolerance_CI_high
lotsize 1.037211 1.000138 11.019148 1.018436 0.9641236 0.0907511 0.9998618
sqrft 1.418654 1.178849 1.979999 1.191073 0.7048934 0.5050508 0.8482854
bdrms 1.396663 1.165161 1.952659 1.181805 0.7159922 0.5121221 0.8582509

Calculo de los VIFs usando “car”

library(car)
VIFs_car<-vif(modelo_estimado)
print(VIFs_car)
##  lotsize    sqrft    bdrms 
## 1.037211 1.418654 1.396663

Cálculo de los VIFs usando “mctest”

library(mctest)
mc.plot(mod = modelo_estimado, vif = 2)