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
options(scipen = 9999)
library(stargazer)
modelo.price <- lm(formula = price~lotsize+sqrft+bdrms,data = hprice1)
stargazer(modelo.price,title = "Modelo Price", type = "html")
| 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 |
# Cálculo Manual
#Matriz X
x_mat<-model.matrix(modelo.price)
#Sigma Matriz
xx_mat<-t(x_mat)%*%x_mat
print(xx_mat)
## (Intercept) lotsize sqrft bdrms
## (Intercept) 88 793748 177205 314
## lotsize 793748 16165159010 1692290257 2933767
## sqrft 177205 1692290257 385820561 654755
## bdrms 314 2933767 654755 1182
#Construccion de Sn
Sn<-solve(diag(sqrt(diag(xx_mat))))
print(Sn)
## [,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
#Sn normalizada
xx_mat.norm<-(Sn%*%xx_mat)%*%Sn
print(xx_mat.norm)
## [,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
#Atovalores
lambdas<-eigen(xx_mat.norm,symmetric = TRUE)$values
print(lambdas)
## [1] 3.48158596 0.45518380 0.03851083 0.02471941
#Indice de Condicion
k<-sqrt(max(lambdas)/min(lambdas))
print(k)
## [1] 11.86778
library(mctest)
mctest(modelo.price)
##
## 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
library(psych)
library(fastGraph)
FG<-cortest.bartlett(x_mat[,-1])
print(FG)
## $chisq
## [1] 31.38122
##
## $p.value
## [1] 0.0000007065806
##
## $df
## [1] 3
vc<-qchisq(0.05,FG$df,lower.tail = FALSE)
shadeDist(xshade = FG$chisq,
ddist = "dchisq",
parm1 = FG$df,
lower.tail = FALSE,
sub=paste("VC:",vc,"FG:",FG$chisq))
#VIF de forma tabular
library(car)
VIF<-vif(modelo.price)
print(VIF)
## lotsize sqrft bdrms
## 1.037211 1.418654 1.396663
#VIF de forma gráfica
library(mctest)
mc.plot(modelo.price,vif = 2)