library(wooldridge)
## Warning: package 'wooldridge' was built under R version 4.3.3
data(hprice1)
head(force(hprice1),n=5) #mostrar las primeras 5 observaciones
## 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_estimado <- lm( formula = price ~ lotsize + sqrft + bdrms, data = hprice1)
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
stargazer(modelo_estimado, title = 'Modelo estimado', 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 |
a) Indice de condición y prueba de FG, presente sus resultados de manera tabular en ambos casos y para la prueba de FG presente también sus resultados de forma gráfica usando la librería fastGraph
matrizX <- model.matrix(modelo_estimado)
MatrizXX <- t(matrizX) %*% matrizX
#Sn matriz de normailizacion
Sn <- solve(diag(sqrt(diag(MatrizXX))))
print( Sn)
## [,1] [,2] [,3] [,4]
## [1,] 0.1066004 0.000000e+00 0.000000e+00 0.00000000
## [2,] 0.0000000 7.865204e-06 0.000000e+00 0.00000000
## [3,] 0.0000000 0.000000e+00 5.091049e-05 0.00000000
## [4,] 0.0000000 0.000000e+00 0.000000e+00 0.02908649
##Matriz normalizada
Matriz_norm <- (Sn %*% MatrizXX) %*% Sn
print(Matriz_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
#Encontrar los autovalores
lambdas <- eigen(Matriz_norm, symmetric = TRUE)$values
#Calculando K (indice de correlacion)
K <- sqrt(max(lambdas)/ min(lambdas))
print(K)
## [1] 11.86778
#con libreria
library(mctest)
indice_condicion<- eigprop( mod = modelo_estimado)
print(indice_condicion)
##
## Call:
## eigprop(mod = modelo_estimado)
##
## Eigenvalues CI (Intercept) lotsize sqrft bdrms
## 1 3.4816 1.0000 0.0037 0.0278 0.0042 0.0029
## 2 0.4552 2.7656 0.0068 0.9671 0.0061 0.0051
## 3 0.0385 9.5082 0.4726 0.0051 0.8161 0.0169
## 4 0.0247 11.8678 0.5170 0.0000 0.1737 0.9750
##
## ===============================
## Row 2==> lotsize, proportion 0.967080 >= 0.50
## Row 3==> sqrft, proportion 0.816079 >= 0.50
## Row 4==> bdrms, proportion 0.975026 >= 0.50
#Prueba FG con libreria
library(psych)
## Warning: package 'psych' was built under R version 4.3.2
FG <- cortest.bartlett(matrizX[,-1])
## R was not square, finding R from data
print(FG)
## $chisq
## [1] 31.38122
##
## $p.value
## [1] 7.065806e-07
##
## $df
## [1] 3
library(fastGraph)
## Warning: package 'fastGraph' was built under R version 4.3.3
vC_1 <-qchisq(0.05, FG$df, lower.tail = FALSE)
#Prueba FG a paso
library(fastGraph)
m <- ncol(matrizX[,-1])
n <- nrow(matrizX)
Determinante_R <- det(cor(matrizX[,- 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(0.05,gl, lower.tail = FALSE)
#Graficar FG
shadeDist(xshade = Chi_FG, ddist = "dchisq", parm1 = gl, lower.tail = FALSE, sub = paste("VC:", Vc))
b) Factores inflacionarios de la varianza, presente sus resultados de forma tabular y de forma gráfica.
#VIF manual
VIFmanual <- diag(solve(cor(matrizX[,-1])))
print(VIFmanual)
## lotsize sqrft bdrms
## 1.037211 1.418654 1.396663
#VIF libreria car
library(car)
## Warning: package 'car' was built under R version 4.3.2
## Loading required package: carData
##
## Attaching package: 'car'
## The following object is masked from 'package:psych':
##
## logit
VIF <- vif(modelo_estimado)
print(VIF)
## lotsize sqrft bdrms
## 1.037211 1.418654 1.396663
#VIF con libreria mctest (FORMA GRAFICA)
library(mctest)
mc.plot(modelo_estimado, vif = 2)