library(wooldridge)
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

##Modelo a estimar

library(stargazer)
## 
## Please cite as:
##  Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##  R package version 5.2.3. https://CRAN.R-project.org/package=stargazer
modelo_hrpice <- lm(formula = price ~ lotsize + sqrft + bdrms, data = hprice1)
stargazer(modelo_hrpice, title = "Modelo estimado" , type = "text" , digits = 5)
## 
## Modelo estimado
## ===============================================
##                         Dependent variable:    
##                     ---------------------------
##                                price           
## -----------------------------------------------
## lotsize                     0.00207***         
##                              (0.00064)         
##                                                
## sqrft                       0.12278***         
##                              (0.01324)         
##                                                
## bdrms                        13.85252          
##                              (9.01015)         
##                                                
## Constant                     -21.77031         
##                             (29.47504)         
##                                                
## -----------------------------------------------
## Observations                    88             
## R2                            0.67236          
## Adjusted R2                   0.66066          
## Residual Std. Error     59.83348 (df = 84)     
## F Statistic          57.46023*** (df = 3; 84)  
## ===============================================
## Note:               *p<0.1; **p<0.05; ***p<0.01

Indice de condicion

matriz_x <- model.matrix(modelo_hrpice)
matriz_xx <- t(matriz_x)%*%matriz_x
print(matriz_xx)
##             (Intercept)     lotsize      sqrft   bdrms
## (Intercept)          88      793748     177205     314
## lotsize          793748 16165159010 1692290257 2933767
## sqrft            177205  1692290257  385820561  654755
## bdrms               314     2933767     654755    1182

normalizada

options(scipen = 999)
matriz_normalizacion <- solve(diag(sqrt(diag(matriz_xx))))
print(matriz_normalizacion)
##           [,1]           [,2]          [,3]       [,4]
## [1,] 0.1066004 0.000000000000 0.00000000000 0.00000000
## [2,] 0.0000000 0.000007865204 0.00000000000 0.00000000
## [3,] 0.0000000 0.000000000000 0.00005091049 0.00000000
## [4,] 0.0000000 0.000000000000 0.00000000000 0.02908649

Matriz transpuesta normalizada

matriz_normalizacion_t <- (matriz_normalizacion%*%matriz_xx)%*%matriz_normalizacion
print(matriz_normalizacion_t)
##           [,1]      [,2]      [,3]      [,4]
## [1,] 1.0000000 0.6655050 0.9617052 0.9735978
## [2,] 0.6655050 1.0000000 0.6776293 0.6711613
## [3,] 0.9617052 0.6776293 1.0000000 0.9695661
## [4,] 0.9735978 0.6711613 0.9695661 1.0000000

Autovalores normalizada

autovalor <- eigen(matriz_xx, symmetric = TRUE)
print (autovalor)
## eigen() decomposition
## $values
## [1] 16344612181.206480   206368582.800828          73.018955           3.973713
## 
## $vectors
##                [,1]          [,2]            [,3]             [,4]
## [1,] -0.00004943579 -0.0004483067  0.194276588766  0.9809466874580
## [2,] -0.99442458432  0.1054502064 -0.000007308921 -0.0000004752379
## [3,] -0.10545003681 -0.9944231045 -0.001721880551 -0.0001187612469
## [4,] -0.00018271796 -0.0016559655  0.980945279886 -0.1942770760042

K

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

#Cálculo del Indice de Condición usando librería “mctest”

library(mctest)
X_mat<-model.matrix(modelo_hrpice)
mctest(mod = modelo_hrpice)
## 
## 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

#Cálculo del Indice de Condición usando librería “olsrr”

library(olsrr)
## 
## Attaching package: 'olsrr'
## The following object is masked from 'package:wooldridge':
## 
##     cement
## The following object is masked from 'package:datasets':
## 
##     rivers
ols_eigen_cindex(model = modelo_hrpice)
##   Eigenvalue Condition Index   intercept      lotsize       sqrft       bdrms
## 1 3.48158596        1.000000 0.003663034 0.0277802824 0.004156293 0.002939554
## 2 0.45518380        2.765637 0.006800735 0.9670803174 0.006067321 0.005096396
## 3 0.03851083        9.508174 0.472581427 0.0051085488 0.816079307 0.016938178
## 4 0.02471941       11.867781 0.516954804 0.0000308514 0.173697079 0.975025872

prueba de Farrar-Glaubar

fg <- scale(matriz_x[,-1])
print(fg)
##         lotsize       sqrft      bdrms
## 1  -0.284432952  0.73512302  0.5132184
## 2   0.086801976  0.10794824 -0.6752874
## 3  -0.375447923 -1.10828571 -0.6752874
## 4  -0.434420906 -0.98007871 -0.6752874
## 5  -0.287479889  0.86679507  0.5132184
## 6  -0.044609488  1.28260155  1.7017243
## 7  -0.001952363  0.09235550 -0.6752874
## 8  -0.276176734 -0.48977357 -0.6752874
## 9  -0.296817278 -0.42740260 -0.6752874
## 10 -0.602297331 -0.21430178 -0.6752874
## 11 -0.296817278  0.55840526  0.5132184
## 12 -0.193909423  1.07469831  1.7017243
## 13  0.316206880  2.35850081 -0.6752874
## 14 -0.251604658 -0.19870903 -0.6752874
## 15 -0.245805648  0.51682462 -0.6752874
## 16 -0.533004076 -0.43953029  0.5132184
## 17 -0.304483766 -0.02372381  0.5132184
## 18 -0.186439512 -0.41527491 -0.6752874
## 19 -0.332004492 -1.10482065 -0.6752874
## 20 -0.041071109 -0.30959076  0.5132184
## 21 -0.346551161  0.05943749 -0.6752874
## 22 -0.119898329  0.19110954 -0.6752874
## 23 -0.296522414 -0.42567007 -0.6752874
## 24 -0.373678733 -0.48804104  0.5132184
## 25  0.039820167 -0.99393893 -0.6752874
## 26 -0.285612412 -0.14153564 -0.6752874
## 27 -0.227032582 -0.14153564 -0.6752874
## 28 -0.043528316  0.15992405 -0.6752874
## 29 -0.060925346  2.62530997  4.0787359
## 30  0.074024497  0.06463507  0.5132184
## 31 -0.414173515 -0.76351284  0.5132184
## 32  0.596230262  1.41254107  0.5132184
## 33 -0.320111607 -0.66475880 -0.6752874
## 34 -0.259172858 -0.30092813  0.5132184
## 35 -0.001952363  0.09062297  0.5132184
## 36 -0.542538041 -0.54001685  0.5132184
## 37  0.184009110  1.27567144  0.5132184
## 38  0.650092253  3.23342695  1.7017243
## 39 -0.257501956 -0.27667275  0.5132184
## 40 -0.013746960 -1.02685694 -1.8637932
## 41 -0.265954751 -0.60931793 -0.6752874
## 42  1.888230032  2.28226963  1.7017243
## 43 -0.193614558 -0.61971309  0.5132184
## 44 -0.365127650 -1.45998869 -0.6752874
## 45 -0.234207628  0.48390660  1.7017243
## 46 -0.116556527 -0.43260018 -0.6752874
## 47 -0.788258804  1.30685693 -0.6752874
## 48 -0.089232378  2.97874548  0.5132184
## 49 -0.311560524 -0.82761633 -0.6752874
## 50 -0.231946997 -0.65089858  0.5132184
## 51 -0.234207628 -0.07223456 -0.6752874
## 52  0.614020445 -0.92810290 -1.8637932
## 53 -0.380755491 -1.04937979 -0.6752874
## 54 -0.295146377 -0.34943888 -0.6752874
## 55 -0.059942463 -0.50536631 -0.6752874
## 56 -0.333675393 -0.40487975  0.5132184
## 57 -0.336132600 -0.58852761  0.5132184
## 58 -0.245215918 -0.28360286  0.5132184
## 59 -0.290919980 -0.15366333 -0.6752874
## 60 -0.342128187  0.57053295  0.5132184
## 61 -0.142897793 -0.77390800 -0.6752874
## 62 -0.347534044 -0.60585288  0.5132184
## 63 -0.360901253 -1.08749538  2.8902301
## 64  0.669749914  1.04524535  1.7017243
## 65 -0.098078326  0.53241736  0.5132184
## 66  0.289570750  1.08162842  0.5132184
## 67 -0.055028048 -0.17098860  0.5132184
## 68  0.598097739  0.99673459  0.5132184
## 69  0.180765596  1.20463783  0.5132184
## 70 -0.267330787 -0.73925746 -0.6752874
## 71  0.249075968 -0.55387707 -0.6752874
## 72 -0.296817278 -0.82761633 -0.6752874
## 73  2.160390349  2.85573606  1.7017243
## 74 -0.488086320 -0.48111093 -0.6752874
## 75  1.148020806  0.33144423 -1.8637932
## 76 -0.343504223 -0.88652225 -0.6752874
## 77  8.222911296 -0.55041201  0.5132184
## 78 -0.082745350  0.29852621 -0.6752874
## 79 -0.302321424 -0.14846575  0.5132184
## 80  0.965007982 -1.24688787 -0.6752874
## 81 -0.462433073 -0.82934886  0.5132184
## 82 -0.378691437 -0.05837435 -0.6752874
## 83 -0.110757517  0.13220362  0.5132184
## 84 -0.291313133 -0.30612571 -0.6752874
## 85 -0.313722867 -0.51749400 -0.6752874
## 86 -0.263300966 -0.76178031 -0.6752874
## 87 -0.261236912 -1.43573331 -1.8637932
## 88 -0.400019999 -0.41527491  0.5132184
## attr(,"scaled:center")
##     lotsize       sqrft       bdrms 
## 9019.863636 2013.693182    3.568182 
## attr(,"scaled:scale")
##       lotsize         sqrft         bdrms 
## 10174.1504141   577.1915827     0.8413926

Matriz R

nR <- nrow(fg)
matrizR <- (t(fg)%*%fg)*(1/(nR-1))
print(matrizR)
##           lotsize     sqrft     bdrms
## lotsize 1.0000000 0.1838422 0.1363256
## sqrft   0.1838422 1.0000000 0.5314736
## bdrms   0.1363256 0.5314736 1.0000000

Determinante R

D_R <- det(matrizR)
print(D_R)
## [1] 0.6917931

FG

P_FG <- ncol(matriz_x[,-1])
n_R <- nrow(matriz_x[,-1])
chi_FG <- -(n_R-1-(2*P_FG+5)/6)*log(D_R)
print(chi_FG)
## [1] 31.38122

VC

gl_FG <- P_FG*(P_FG-1)/2
vc_FG <- qchisq(p=0.95,df= gl_FG)
print(vc_FG)
## [1] 7.814728

FG

library(ggplot2)
df <- data.frame(
  Test = c("Chi_FG", "VC_FG"),
  Resultado = c(chi_FG, vc_FG)
)
ggplot(df, aes(x=Test, y=Resultado)) +
  geom_bar(stat="identity", fill="steelblue") +
  theme_minimal() +
  labs(title="Resultados de la prueba Farrar-Glaubar", x="Test", y="Resultado") +
  geom_text(aes(label=Resultado), vjust=-0.3, size=3.5)

FIV

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
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
## 1                   0.0   1
## 2                   0.5   2
## 3                   0.8   5
## 4                   0.9  10
print(matrizR)
##           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

inversa_R <- solve(matrizR)
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

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

##Gráfico

library(ggplot2)
library(dplyr)
R.cuadrado.regresores <- c(0, 0.5, .8, .9)
datos <- as.data.frame(R.cuadrado.regresores) %>% mutate(VIF = 1 / (1 - R.cuadrado.regresores))

ggplot(datos, aes(x = R.cuadrado.regresores, y = VIF)) +
  geom_line() +
  labs(title = "Factores Inflacionarios de la Varianza (FIV)",
       x = "R cuadrado de los regresores",
       y = "VIF") +
  theme_minimal()

## Cálculo de los VIF’s usando “performance”

library(performance)
VIFs<-multicollinearity(x = modelo_hrpice,verbose = FALSE)
VIFs
## # Check for Multicollinearity
## 
## Low Correlation
## 
##     Term  VIF    VIF 95% CI Increased SE Tolerance Tolerance 95% CI
##  lotsize 1.04 [1.00, 11.02]         1.02      0.96     [0.09, 1.00]
##    sqrft 1.42 [1.18,  1.98]         1.19      0.70     [0.51, 0.85]
##    bdrms 1.40 [1.17,  1.95]         1.18      0.72     [0.51, 0.86]

Cálculo de los VIF’s “car”

library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
VIFs_car<-vif(modelo_hrpice)
print(VIFs_car)
##  lotsize    sqrft    bdrms 
## 1.037211 1.418654 1.396663

Cálculo de los VIF’s usando “mctest

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