library(wooldridge)
## Warning: package 'wooldridge' was built under R version 4.0.5
data("hprice1")
head(force(hprice1),n=5)
## price assess bdrms lotsize sqrft colonial lprice lassess llotsize lsqrft
## 1 300 349.1 4 6126 2438 1 5.703783 5.855359 8.720297 7.798934
## 2 370 351.5 3 9903 2076 1 5.913503 5.862210 9.200593 7.638198
## 3 191 217.7 3 5200 1374 0 5.252274 5.383118 8.556414 7.225482
## 4 195 231.8 3 4600 1448 1 5.273000 5.445875 8.433811 7.277938
## 5 373 319.1 4 6095 2514 1 5.921578 5.765504 8.715224 7.829630
#Estimamos el modelo
library(stargazer)
##
## Please cite as:
## Hlavac, Marek (2018). stargazer: Well-Formatted Regression and Summary Statistics Tables.
## R package version 5.2.2. https://CRAN.R-project.org/package=stargazer
modelo_precio<-lm(formula = price~lotsize+sqrft+bdrms,data = hprice1)
stargazer(modelo_precio,title = "Precio del Lote",type = "text")
##
## Precio del Lote
## ===============================================
## 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
Calculamos X’X
library(stargazer)
x_mat<-model.matrix(modelo_precio)
xx_mat<-t(x_mat)%*%(x_mat)
stargazer(xx_mat,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
## --------------------------------------------------------------
Normalizamos X’X.
library(stargazer)
# Obtenemos la matriz de de normalizacion Sn.
sn<-solve(diag(sqrt(diag(xx_mat))))
stargazer(sn,type = "text")
##
## ==========================
## 0.107 0 0 0
## 0 0.00001 0 0
## 0 0 0.0001 0
## 0 0 0 0.029
## --------------------------
# Procedemos con la normalizacion de X'X.
xx_norm<-(sn%*%xx_mat)%*%sn
stargazer(xx_norm,type = "text")
##
## =======================
## 1 0.666 0.962 0.974
## 0.666 1 0.678 0.671
## 0.962 0.678 1 0.970
## 0.974 0.671 0.970 1
## -----------------------
Para poder calcular el Indice de Condicion antes debemos obtner los autovalores de la matriz X’X normalizada.
library(stargazer)
lamdas<-eigen(xx_norm)$values
stargazer(lamdas,type = "text")
##
## =======================
## 3.482 0.455 0.039 0.025
## -----------------------
Ahora ya podemos obtener el Indice de Condicion
k<-sqrt(max(lamdas)/min(lamdas))
print(k)
## [1] 11.86778
Como podemos apreciar, nos hemos encontrado con un indice de condicion con valor menor a 20, lo que sugiere una multicolinealidad leve y no considerada como un problema.
Prueba de Farrar-Glaubar.
Normalizaremos la matriz X
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
## -----------------------
Calculamos 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
## -----------------------------
Calculamos |R|.
determinante_R<-det(R)
print(determinante_R)
## [1] 0.6917931
Posteriormente procedes con la realizacion de la preba de FG
-Estadistico de prueba
m<-ncol(x_mat[,-1])
n<-nrow(x_mat[,-1])
#Aplicamos la formula para obtener el estadistico de prueba
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
Dado que el estadistico de prueba es superior al valor critico se rechaza H0, por lo que se sugiere que hay evidencias en favor de la existencia de cierto grado de colinealidad en los regresores del modelo.
Prueba de Farrar-Glaubar usando libreria mctest
library(mctest)
mctest(modelo_precio)
##
## 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
Prueba de Farrar-Glaubar usando libreria Psych.
library(psych)
## Warning: package 'psych' was built under R version 4.0.5
FG_test<-cortest.bartlett(x_mat[,-1])
## R was not square, finding R from data
print(FG_test)
## $chisq
## [1] 31.38122
##
## $p.value
## [1] 7.065806e-07
##
## $df
## [1] 3
-Resultados de prueba FG usando fastGraph
library(fastGraph)
## Warning: package 'fastGraph' was built under R version 4.0.5
shadeDist(xshade = chi_FG, ddist = "dchisq", parm1 = gl, lower.tail = FALSE, sub =paste("VC", VC, "FG",chi_FG ))
B)FACTORES INFLACIONARIOS DE LA VARIANZA
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 matirz de correlacion 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 la libreria “car”.
library(car)
## Warning: package 'car' was built under R version 4.0.5
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:psych':
##
## logit
VIFs_car<-vif(modelo_precio)
print(VIFs_car)
## lotsize sqrft bdrms
## 1.037211 1.418654 1.396663
Grafica
library(mctest)
mc.plot(modelo_precio,vif = 2)