#Estimaciones de Modelos Espaciales, intento uno: 

#Cargar la libreria spdep

library(spdep) # Econometria espacial 
## Loading required package: sp
## Loading required package: Matrix
## Loading required package: spData
## To access larger datasets in this package, install the spDataLarge
## package with: `install.packages('spDataLarge',
## repos='https://nowosad.github.io/drat/', type='source'))`
library(maptools) # leer archivos shapfiles y elaborar mapas 
## Checking rgeos availability: FALSE
##      Note: when rgeos is not available, polygon geometry     computations in maptools depend on gpclib,
##      which has a restricted licence. It is disabled by default;
##      to enable gpclib, type gpclibPermit()
library(RColorBrewer) # Eleccion de colores
library(classInt) # m??todos para clasificar
library(sf) #adicion
## Linking to GEOS 3.6.1, GDAL 2.2.3, proj.4 4.9.3
library(rgdal) #adicion
## rgdal: version: 1.2-20, (SVN revision 725)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.2.3, released 2017/11/20
##  Path to GDAL shared files: C:/Users/Sergio Mora/Documents/R/win-library/3.5/rgdal/gdal
##  GDAL binary built with GEOS: TRUE 
##  Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
##  Path to PROJ.4 shared files: C:/Users/Sergio Mora/Documents/R/win-library/3.5/rgdal/proj
##  Linking to sp version: 1.2-7
#Cambiar el directorio de trabajo
setwd("C:/Users/Sergio Mora/Downloads")
#Leer archivos shapes y transformarlo en objeto Shape y DataFrame 
empleo <- readOGR("Zona_Centro.shp")
## OGR data source with driver: ESRI Shapefile 
## Source: "C:\Users\Sergio Mora\Downloads\Zona_Centro.shp", layer: "Zona_Centro"
## with 174 features
## It has 39 fields
## Integer64 fields read as strings:  ID
summary(empleo)
## Object of class SpatialPolygonsDataFrame
## Coordinates:
##         min       max
## x -87.84584 -87.17459
## y  24.56797  25.16453
## Is projected: NA 
## proj4string : [NA]
## Data attributes:
##        ID          CVEGEO                NOM_ENT   
##  266    :  1   09002  :  1   Distrito Federal: 16  
##  267    :  1   09003  :  1   Morelos         : 33  
##  268    :  1   09004  :  1   MÚxico          :125  
##  269    :  1   09005  :  1                         
##  270    :  1   09006  :  1                         
##  271    :  1   09007  :  1                         
##  (Other):168   (Other):168                         
##                    NOM_MUN        POBTOT            POBMAS      
##  Zacualpan             :  2   Min.   :   4051   Min.   :  2012  
##  -lvaro Obreg¾n        :  1   1st Qu.:  18469   1st Qu.:  9054  
##  Acambay               :  1   Median :  44852   Median : 22188  
##  Acolman               :  1   Mean   : 148300   Mean   : 71778  
##  Aculco                :  1   3rd Qu.: 136118   3rd Qu.: 67958  
##  Almoloya de Alquisiras:  1   Max.   :1815786   Max.   :880998  
##  (Other)               :167                                     
##      POBFEM          PROM_HNV       GRAPROES_M       GRAPROES_F    
##  Min.   :  2018   Min.   :1.360   Min.   : 5.610   Min.   : 4.720  
##  1st Qu.:  9407   1st Qu.:2.160   1st Qu.: 7.508   1st Qu.: 7.183  
##  Median : 22614   Median :2.360   Median : 8.515   Median : 8.175  
##  Mean   : 76522   Mean   :2.421   Mean   : 8.514   Mean   : 8.172  
##  3rd Qu.: 68157   3rd Qu.:2.618   3rd Qu.: 9.385   3rd Qu.: 9.027  
##  Max.   :934788   Max.   :3.590   Max.   :14.110   Max.   :13.050  
##                                                                    
##       PEA             PEA_M            PEA_F           PE_INAC      
##  Min.   :  1350   Min.   :  1035   Min.   :   189   Min.   :  1405  
##  1st Qu.:  6742   1st Qu.:  4962   1st Qu.:  1738   1st Qu.:  7071  
##  Median : 15462   Median : 11788   Median :  4514   Median : 16165  
##  Mean   : 62670   Mean   : 39849   Mean   : 22821   Mean   : 51245  
##  3rd Qu.: 51654   3rd Qu.: 35943   3rd Qu.: 16036   3rd Qu.: 45507  
##  Max.   :792297   Max.   :491236   Max.   :301061   Max.   :626317  
##                                                                     
##    PE_INAC_M        PE_INAC_F         POCUPADA        POCUPADA_M    
##  Min.   :   385   Min.   :  1017   Min.   :  1158   Min.   :   953  
##  1st Qu.:  1642   1st Qu.:  5380   1st Qu.:  6426   1st Qu.:  4643  
##  Median :  4012   Median : 12258   Median : 14470   Median : 10912  
##  Mean   : 14365   Mean   : 36880   Mean   : 59568   Mean   : 37554  
##  3rd Qu.: 12075   3rd Qu.: 34270   3rd Qu.: 49053   3rd Qu.: 34220  
##  Max.   :183133   Max.   :443184   Max.   :752268   Max.   :463081  
##                                                                     
##    POCUPADA_F        PDESOCUP         PDESOCUP_M        PDESOCUP_F      
##  Min.   :   183   Min.   :   33.0   Min.   :   28.0   Min.   :    4.00  
##  1st Qu.:  1708   1st Qu.:  313.2   1st Qu.:  263.8   1st Qu.:   41.25  
##  Median :  4406   Median :  860.0   Median :  739.0   Median :  129.00  
##  Mean   : 22014   Mean   : 3102.0   Mean   : 2295.0   Mean   :  807.03  
##  3rd Qu.: 15687   3rd Qu.: 2642.5   3rd Qu.: 2103.0   3rd Qu.:  431.25  
##  Max.   :289187   Max.   :40029.0   Max.   :28155.0   Max.   :11874.00  
##                                                                         
##     PSINDER          PDER_SS          PDER_IMSS        PDER_ISTE       
##  Min.   :   591   Min.   :   1971   Min.   :    43   Min.   :    22.0  
##  1st Qu.:  6758   1st Qu.:  11189   1st Qu.:  1311   1st Qu.:   453.5  
##  Median : 17835   Median :  26248   Median :  5560   Median :  1121.0  
##  Mean   : 55903   Mean   :  89534   Mean   : 45848   Mean   : 11061.9  
##  3rd Qu.: 47760   3rd Qu.:  83030   3rd Qu.: 34660   3rd Qu.:  5621.8  
##  Max.   :699848   Max.   :1096323   Max.   :593406   Max.   :193469.0  
##                                                                        
##    PDER_ISTEE        PDER_SEGP         GRAPROES        N_CLASE_CR
##  Min.   :    1.0   Min.   :   713   Min.   : 5.150   Min.   :1   
##  1st Qu.:  222.2   1st Qu.:  6550   1st Qu.: 7.353   1st Qu.:1   
##  Median :  570.0   Median : 15484   Median : 8.395   Median :1   
##  Mean   : 1851.9   Mean   : 23533   Mean   : 8.334   Mean   :1   
##  3rd Qu.: 1511.8   3rd Qu.: 30102   3rd Qu.: 9.217   3rd Qu.:1   
##  Max.   :57934.0   Max.   :274958   Max.   :13.520   Max.   :1   
##                                                                  
##    ESCOLA_15          POB_15             CORE           BOHEMIOS      
##  Min.   : 3.130   Min.   :   2613   Min.   :    80   Min.   :   0.00  
##  1st Qu.: 5.270   1st Qu.:  12779   1st Qu.:   802   1st Qu.:  30.25  
##  Median : 6.165   Median :  29019   Median :  1854   Median : 107.00  
##  Mean   : 6.373   Mean   : 108714   Mean   : 15905   Mean   : 734.32  
##  3rd Qu.: 7.008   3rd Qu.:  93237   3rd Qu.:  8286   3rd Qu.: 477.00  
##  Max.   :12.680   Max.   :1353286   Max.   :209994   Max.   :8220.00  
##                                                                       
##    NO_CREATIV           CORE_P         NO_CREAT_P         BOHE_P      
##  Min.   :    28.0   Min.   : 1.160   Min.   : 0.410   Min.   :0.0000  
##  1st Qu.:   326.0   1st Qu.: 5.760   1st Qu.: 2.315   1st Qu.:0.2100  
##  Median :   955.5   Median : 8.230   Median : 4.180   Median :0.4050  
##  Mean   :  9149.6   Mean   : 9.492   Mean   : 4.868   Mean   :0.4764  
##  3rd Qu.:  4809.0   3rd Qu.:11.670   3rd Qu.: 6.140   3rd Qu.:0.6575  
##  Max.   :111629.0   Max.   :38.260   Max.   :21.080   Max.   :1.9900  
##                                                                       
##       OID        
##  Min.   :  1.00  
##  1st Qu.: 44.25  
##  Median : 87.50  
##  Mean   : 87.50  
##  3rd Qu.:130.75  
##  Max.   :174.00  
## 
# Logaritmo del Empleo 
lempleo <- log(empleo$POCUPADA)
# Capital Humano y logaritmo del capital humano, a??os de escolaridad
ch <- empleo$ESCOLA_15
lch <- log(empleo$ESCOLA_15)

# Caracteristicas estadisticas del empleo y capital humano
summary(empleo$POCUPADA)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    1158    6426   14470   59568   49053  752268
summary(empleo$ESCOLA_15)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   3.130   5.270   6.165   6.373   7.008  12.680
summary(lempleo)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   7.054   8.768   9.580   9.882  10.801  13.531
summary(ch)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   3.130   5.270   6.165   6.373   7.008  12.680
summary(lch)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.141   1.662   1.819   1.821   1.947   2.540
class(empleo)
## [1] "SpatialPolygonsDataFrame"
## attr(,"package")
## [1] "sp"
# Construir lista de vecinos tipo Queen de poligonos 
pr.nb <- poly2nb(empleo, queen=TRUE)
# Matriz de ponderacion W estandarizada
wqueen <- nb2listw(pr.nb, style="W")

# Caracteristicas de la Matriz W tipo Queen
summary(wqueen)
## Characteristics of weights list object:
## Neighbour list object:
## Number of regions: 174 
## Number of nonzero links: 950 
## Percentage nonzero weights: 3.137799 
## Average number of links: 5.45977 
## Link number distribution:
## 
##  1  2  3  4  5  6  7  8  9 10 11 14 
##  3  3 19 41 32 26 20 16  9  3  1  1 
## 3 least connected regions:
## 76 94 120 with 1 link
## 1 most connected region:
## 116 with 14 links
## 
## Weights style: W 
## Weights constants summary:
##     n    nn  S0       S1       S2
## W 174 30276 174 70.54671 724.6509
# Grafica con la conexicion espacial  
cent <- coordinates(empleo)
plot(empleo, border="grey", lwd=1.5)
plot(pr.nb,cent, add=T, col="darkred")

# Estadistico de Moran 
moran_lempleo <- moran.test(lempleo, wqueen,randomisation=TRUE, alternative="two.sided", na.action=na.exclude)
moran_ch <- moran.test(ch, wqueen,randomisation=TRUE, alternative="two.sided", na.action=na.exclude)
moran_lch <- moran.test(lch, wqueen,randomisation=TRUE, alternative="two.sided", na.action=na.exclude)

#Ver resultados
print(moran_lempleo)
## 
##  Moran I test under randomisation
## 
## data:  lempleo  
## weights: wqueen    
## 
## Moran I statistic standard deviate = 12.466, p-value < 2.2e-16
## alternative hypothesis: two.sided
## sample estimates:
## Moran I statistic       Expectation          Variance 
##       0.587496960      -0.005780347       0.002264968
print(moran_ch)
## 
##  Moran I test under randomisation
## 
## data:  ch  
## weights: wqueen    
## 
## Moran I statistic standard deviate = 14.652, p-value < 2.2e-16
## alternative hypothesis: two.sided
## sample estimates:
## Moran I statistic       Expectation          Variance 
##       0.687457813      -0.005780347       0.002238704
print(moran_lch)
## 
##  Moran I test under randomisation
## 
## data:  lch  
## weights: wqueen    
## 
## Moran I statistic standard deviate = 14.843, p-value < 2.2e-16
## alternative hypothesis: two.sided
## sample estimates:
## Moran I statistic       Expectation          Variance 
##       0.698986435      -0.005780347       0.002254493
# Grafica de diagrama de dispersion de Moran 
moran.plot(lempleo, wqueen, pch=20)

moran.plot(ch, wqueen, pch=20)

moran.plot(lch, wqueen, pch=20)

#Clasificador con quintiles y colores especificos
# Mapa de quintiles del logaritmo del empleo
brks <- round(quantile(lempleo, probs=seq(0,1,0.25)), digits=2)
colours <- brewer.pal(4,"Reds")

plot(empleo, col=colours[findInterval(lempleo, brks, all.inside=TRUE)],
     axes=F)
legend(x=-87.9, y=25.2, legend=leglabs(brks), fill=colours, bty="n")

#invisible(title(main=paste("EMPLEO", sep="\n")))
#box()

# Mapa de quintiles del logaritmo del capital humano
brks <- round(quantile(lch, probs=seq(0,1,0.25)), digits=2)
colours <- brewer.pal(4,"Blues")

plot(empleo, col=colours[findInterval(lch, brks, all.inside=TRUE)],
     axes=F)
legend(x=-87.9, y=25.2, legend=leglabs(brks), fill=colours, bty="n")

#invisible(title(main=paste("CAPITAL HUMANO", sep="\n")))
#box()

# An??lisis LISA

# Valores de referencia z de la distribuci??n t
z <- c(1.65, 1.96)
zc <- c(2.8284, 3.0471)

# Estimaci??n de indice de Moran local (Ii)
f.Ii <- localmoran(lempleo, wqueen)
zIi <- f.Ii[,"Z.Ii"] # Asignaci??n de la distribuci??n Z del Ii
mx <- max(zIi) 
mn <- min(zIi)

# Mapa de signficancia para los z-scores
pal <- c("white", "green", "darkgreen")
z3.Ii <- classIntervals(zIi, n=3, style="fixed", fixedBreaks=c(mn, z, mx))
cols.Ii <- findColours(z3.Ii, pal)  
plot(empleo, col=cols.Ii)
brks <- round(z3.Ii$brks,4)
leg <- paste(brks[-4], brks[-1], sep=" - ")
legend(x=-87.9, y=25.2, fill=pal, legend=leg, bty="n")         

# Mapa de los grupos de cluster
pal.rb <- c("skyblue1","white","blue","red")
z4.Ii <- classIntervals(zIi, n=4, style="fixed", fixedBreaks=c(min(f.Ii), -z[1], z[1], z[2], max(f.Ii)))
cols.Ii <- findColours(z4.Ii, pal.rb)               
plot(empleo, col=cols.Ii)
brks <- round(z4.Ii$brks,4)
leg <- paste(brks[-5], brks[-1], sep=" - ")
legend(x=-87.9, y=25.2, fill= pal.rb, legend=leg, bty="n")

# Modelo OLS 
ModeloEmpleo_OLS <- lm(lempleo ~ lch , data=empleo)
summary(ModeloEmpleo_OLS)
## 
## Call:
## lm(formula = lempleo ~ lch, data = empleo)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.3948 -0.8243  0.0286  0.7611  2.8910 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   3.8775     0.6763   5.734 4.33e-08 ***
## lch           3.2969     0.3680   8.960 5.26e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.2 on 172 degrees of freedom
## Multiple R-squared:  0.3182, Adjusted R-squared:  0.3142 
## F-statistic: 80.27 on 1 and 172 DF,  p-value: 5.264e-16
# Prueba de Moran a residuales del modelo OLS
I_Moran <- lm.morantest(ModeloEmpleo_OLS,wqueen)
print(I_Moran)
## 
##  Global Moran I for regression residuals
## 
## data:  
## model: lm(formula = lempleo ~ lch, data = empleo)
## weights: wqueen
## 
## Moran I statistic standard deviate = 8.0244, p-value = 5.1e-16
## alternative hypothesis: greater
## sample estimates:
## Observed Moran I      Expectation         Variance 
##      0.368395591     -0.009877828      0.002222204
#Pruebas de Multiplicadores de Lagranges lm.LMtests(columbus.lm,col.listw,test=c("LMerr","RLMerr","LMlag","RLMlag","SARMA"))
lm.LMtests(ModeloEmpleo_OLS,wqueen,test=c("LMerr","RLMerr","LMlag","RLMlag","SARMA"))
## 
##  Lagrange multiplier diagnostics for spatial dependence
## 
## data:  
## model: lm(formula = lempleo ~ lch, data = empleo)
## weights: wqueen
## 
## LMerr = 58.244, df = 1, p-value = 2.32e-14
## 
## 
##  Lagrange multiplier diagnostics for spatial dependence
## 
## data:  
## model: lm(formula = lempleo ~ lch, data = empleo)
## weights: wqueen
## 
## RLMerr = 0.0032553, df = 1, p-value = 0.9545
## 
## 
##  Lagrange multiplier diagnostics for spatial dependence
## 
## data:  
## model: lm(formula = lempleo ~ lch, data = empleo)
## weights: wqueen
## 
## LMlag = 70.722, df = 1, p-value < 2.2e-16
## 
## 
##  Lagrange multiplier diagnostics for spatial dependence
## 
## data:  
## model: lm(formula = lempleo ~ lch, data = empleo)
## weights: wqueen
## 
## RLMlag = 12.482, df = 1, p-value = 0.0004109
## 
## 
##  Lagrange multiplier diagnostics for spatial dependence
## 
## data:  
## model: lm(formula = lempleo ~ lch, data = empleo)
## weights: wqueen
## 
## SARMA = 70.726, df = 2, p-value = 4.441e-16
# Modelos Espaciales 
# Estimar el  Modelo Rezago Espacial 
ModeloEmpleo_lag <- lagsarlm(lempleo ~ lch , data=empleo,wqueen)
summary(ModeloEmpleo_lag)
## 
## Call:lagsarlm(formula = lempleo ~ lch, data = empleo, listw = wqueen)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -3.1783841 -0.5655122 -0.0045741  0.5698724  2.4373878 
## 
## Type: lag 
## Coefficients: (asymptotic standard errors) 
##             Estimate Std. Error z value  Pr(>|z|)
## (Intercept)  0.73366    0.61410  1.1947    0.2322
## lch          1.32893    0.32970  4.0307 5.562e-05
## 
## Rho: 0.67009, LR test value: 65.113, p-value: 6.6613e-16
## Asymptotic standard error: 0.06377
##     z-value: 10.508, p-value: < 2.22e-16
## Wald statistic: 110.42, p-value: < 2.22e-16
## 
## Log likelihood: -245.0376 for lag model
## ML residual variance (sigma squared): 0.87624, (sigma: 0.93607)
## Number of observations: 174 
## Number of parameters estimated: 4 
## AIC: 498.08, (AIC for lm: 561.19)
## LM test for residual autocorrelation
## test value: 0.11791, p-value: 0.73132
# Estimar el modelo de Error Espacial 
ModeloEmpleo_err <- errorsarlm(lempleo ~ lch , data=empleo,wqueen)
summary(ModeloEmpleo_err)
## 
## Call:errorsarlm(formula = lempleo ~ lch, data = empleo, listw = wqueen)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -3.229495 -0.603003  0.050277  0.613023  2.550370 
## 
## Type: error 
## Coefficients: (asymptotic standard errors) 
##             Estimate Std. Error z value  Pr(>|z|)
## (Intercept)  6.47006    0.94208  6.8679 6.516e-12
## lch          1.64366    0.49579  3.3152 0.0009157
## 
## Lambda: 0.71817, LR test value: 58.604, p-value: 1.9318e-14
## Asymptotic standard error: 0.061095
##     z-value: 11.755, p-value: < 2.22e-16
## Wald statistic: 138.18, p-value: < 2.22e-16
## 
## Log likelihood: -248.2923 for error model
## ML residual variance (sigma squared): 0.89031, (sigma: 0.94356)
## Number of observations: 174 
## Number of parameters estimated: 4 
## AIC: 504.58, (AIC for lm: 561.19)
# Estimar modelo SARAR
ModeloEmpleo_sarar <- sacsarlm(lempleo ~ lch , data=empleo, wqueen, type="sac")
summary(ModeloEmpleo_sarar)
## 
## Call:sacsarlm(formula = lempleo ~ lch, data = empleo, listw = wqueen, 
##     type = "sac")
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -3.142804 -0.581190 -0.016665  0.575159  2.415785 
## 
## Type: sac 
## Coefficients: (asymptotic standard errors) 
##             Estimate Std. Error z value Pr(>|z|)
## (Intercept)  0.56642    0.74371  0.7616 0.446294
## lch          1.24960    0.41976  2.9770 0.002911
## 
## Rho: 0.70235
## Asymptotic standard error: 0.11906
##     z-value: 5.899, p-value: 3.6575e-09
## Lambda: -0.073671
## Asymptotic standard error: 0.24171
##     z-value: -0.30479, p-value: 0.76053
## 
## LR test value: 65.214, p-value: 6.8834e-15
## 
## Log likelihood: -244.9869 for sac model
## ML residual variance (sigma squared): 0.86265, (sigma: 0.92879)
## Number of observations: 174 
## Number of parameters estimated: 5 
## AIC: 499.97, (AIC for lm: 561.19)
#Estimar el modelo de Durbin Rezago Espacial
ModeloEmpleo_lag_durbin <- lagsarlm(lempleo ~ lch , data=empleo,wqueen, type="mixed")
summary(ModeloEmpleo_lag_durbin)
## 
## Call:lagsarlm(formula = lempleo ~ lch, data = empleo, listw = wqueen, 
##     type = "mixed")
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -3.059210 -0.599797 -0.023718  0.585009  2.417886 
## 
## Type: mixed 
## Coefficients: (asymptotic standard errors) 
##             Estimate Std. Error z value Pr(>|z|)
## (Intercept)  0.54567    0.67171  0.8124  0.41659
## lch          1.03039    0.55554  1.8547  0.06363
## lag.lch      0.50523    0.71928  0.7024  0.48242
## 
## Rho: 0.6515, LR test value: 52.636, p-value: 4.0157e-13
## Asymptotic standard error: 0.06901
##     z-value: 9.4407, p-value: < 2.22e-16
## Wald statistic: 89.127, p-value: < 2.22e-16
## 
## Log likelihood: -244.8002 for mixed model
## ML residual variance (sigma squared): 0.88042, (sigma: 0.93831)
## Number of observations: 174 
## Number of parameters estimated: 5 
## AIC: 499.6, (AIC for lm: 550.24)
## LM test for residual autocorrelation
## test value: 0.024694, p-value: 0.87513
#Estimar el modelo de Durbin Error Espacial
ModeloEmpleo_err_durbin <- errorsarlm(lempleo ~ lch , data=empleo,wqueen, etype="emixed")
summary(ModeloEmpleo_err_durbin)
## 
## Call:errorsarlm(formula = lempleo ~ lch, data = empleo, listw = wqueen, 
##     etype = "emixed")
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -3.127211 -0.570143 -0.040392  0.575190  2.423206 
## 
## Type: error 
## Coefficients: (asymptotic standard errors) 
##             Estimate Std. Error z value Pr(>|z|)
## (Intercept)  2.62363    1.52942  1.7154 0.086264
## lch          1.35737    0.50232  2.7022 0.006888
## lag.lch      2.47167    0.86244  2.8659 0.004158
## 
## Lambda: 0.65455, LR test value: 52.865, p-value: 3.5738e-13
## Asymptotic standard error: 0.069289
##     z-value: 9.4467, p-value: < 2.22e-16
## Wald statistic: 89.24, p-value: < 2.22e-16
## 
## Log likelihood: -244.6857 for error model
## ML residual variance (sigma squared): 0.87821, (sigma: 0.93713)
## Number of observations: 174 
## Number of parameters estimated: 5 
## AIC: 499.37, (AIC for lm: 550.24)
#Comparar modelos de error espacial y durbin error espacial(Hipoteis nula son iguales), con pruebas de Razon de Verosimilitud
LR.sarlm(ModeloEmpleo_err_durbin, ModeloEmpleo_err)
## 
##  Likelihood ratio for spatial linear models
## 
## data:  
## Likelihood ratio = 7.2132, df = 1, p-value = 0.007237
## sample estimates:
## Log likelihood of ModeloEmpleo_err_durbin 
##                                 -244.6857 
##        Log likelihood of ModeloEmpleo_err 
##                                 -248.2923
#Comparar modelos de rezago espacial y durbin rezago espacial(Hipoteis nula son iguales), con pruebas de Razon de Verosimilitud
LR.sarlm(ModeloEmpleo_lag_durbin, ModeloEmpleo_lag)
## 
##  Likelihood ratio for spatial linear models
## 
## data:  
## Likelihood ratio = 0.47491, df = 1, p-value = 0.4907
## sample estimates:
## Log likelihood of ModeloEmpleo_lag_durbin 
##                                 -244.8002 
##        Log likelihood of ModeloEmpleo_lag 
##                                 -245.0376
#Probar la hipotesis de factor com?n  
durbin.test1 <- LR.sarlm(ModeloEmpleo_lag_durbin,ModeloEmpleo_err)
print(durbin.test1)
## 
##  Likelihood ratio for spatial linear models
## 
## data:  
## Likelihood ratio = 6.9842, df = 1, p-value = 0.008223
## sample estimates:
## Log likelihood of ModeloEmpleo_lag_durbin 
##                                 -244.8002 
##        Log likelihood of ModeloEmpleo_err 
##                                 -248.2923
1 - pchisq(durbin.test1[[1]][1],2)
## Likelihood ratio 
##        0.0304362
#Comparar modeloss con ANOVA
anova(ModeloEmpleo_lag, ModeloEmpleo_lag_durbin)
##                         Model df    AIC  logLik Test L.Ratio p-value
## ModeloEmpleo_lag            1  4 498.08 -245.04    1                
## ModeloEmpleo_lag_durbin     2  5 499.60 -244.80    2 0.47491 0.49074
#Breusch-Pagan test for spatial models
bptest.sarlm(ModeloEmpleo_lag, studentize=FALSE)
## 
##  Breusch-Pagan test
## 
## data:  
## BP = 6.5471, df = 1, p-value = 0.01051
# Evaluar impactos en modelos de Rezago Espacial y Durbidn Rezago Espacial
#Matrices para el calculo
W <- as(as_dgRMatrix_listw(wqueen), "CsparseMatrix")
trMatc <- trW(W, type="mult")
trMC <- trW(W, type="MC")

#Impactos modelo rezago espacial
impacts(ModeloEmpleo_lag, listw=wqueen)
## Impact measures (lag, exact):
##       Direct Indirect  Total
## lch 1.513396 2.514804 4.0282
impacts(ModeloEmpleo_lag, tr=trMatc)
## Impact measures (lag, trace):
##       Direct Indirect    Total
## lch 1.513396  2.51478 4.028176
impacts(ModeloEmpleo_lag, tr=trMC)
## Impact measures (lag, trace):
##      Direct Indirect    Total
## lch 1.51193 2.516246 4.028176
summary(impacts(ModeloEmpleo_lag, tr=trMatc, R=1000), zstats=TRUE, short=TRUE)
## Impact measures (lag, trace):
##       Direct Indirect    Total
## lch 1.513396  2.51478 4.028176
## ========================================================
## Simulation results (asymptotic variance matrix):
## ========================================================
## Simulated z-values:
##       Direct Indirect    Total
## lch 4.314809  3.32023 4.056422
## 
## Simulated p-values:
##     Direct     Indirect   Total    
## lch 1.5974e-05 0.00089943 4.983e-05
summary(impacts(ModeloEmpleo_lag, tr=trMatc, R=1000, Q=10), zstats=TRUE, short=TRUE,reportQ=TRUE)
## Impact measures (lag, trace):
##       Direct Indirect    Total
## lch 1.513396  2.51478 4.028176
## =================================
## Impact components
## $direct
##             lch
## Q1  1.328928126
## Q2  0.000000000
## Q3  0.111150166
## Q4  0.024136769
## Q5  0.022837893
## Q6  0.009817296
## Q7  0.006782716
## Q8  0.003663321
## Q9  0.002352407
## Q10 0.001386173
## 
## $indirect
##            lch
## Q1  0.00000000
## Q2  0.89050654
## Q3  0.48557277
## Q4  0.37572359
## Q5  0.24510606
## Q6  0.16973030
## Q7  0.11353102
## Q8  0.07695817
## Q9  0.05167156
## Q10 0.03481495
## 
## $total
##            lch
## Q1  1.32892813
## Q2  0.89050654
## Q3  0.59672293
## Q4  0.39986035
## Q5  0.26794396
## Q6  0.17954759
## Q7  0.12031373
## Q8  0.08062149
## Q9  0.05402396
## Q10 0.03620112
## 
## ========================================================
## Simulation results (asymptotic variance matrix):
## ========================================================
## Simulated z-values:
##       Direct Indirect    Total
## lch 4.495309  3.49212 4.339877
## 
## Simulated p-values:
##     Direct     Indirect  Total     
## lch 6.9469e-06 0.0004792 1.4256e-05
## ========================================================
## Simulated impact components z-values:
## $Direct
##          lch
## Q1  4.216556
## Q2       NaN
## Q3  4.403352
## Q4  3.636735
## Q5  2.941886
## Q6  2.416673
## Q7  2.027602
## Q8  1.733148
## Q9  1.503590
## Q10 1.319433
## 
## $Indirect
##          lch
## Q1       NaN
## Q2  4.743845
## Q3  4.403352
## Q4  3.636735
## Q5  2.941886
## Q6  2.416673
## Q7  2.027602
## Q8  1.733148
## Q9  1.503590
## Q10 1.319433
## 
## $Total
##          lch
## Q1  4.216556
## Q2  4.743845
## Q3  4.403352
## Q4  3.636735
## Q5  2.941886
## Q6  2.416673
## Q7  2.027602
## Q8  1.733148
## Q9  1.503590
## Q10 1.319433
## 
## 
## Simulated impact components p-values:
## $Direct
##     lch       
## Q1  2.4806e-05
## Q2  NA        
## Q3  1.0659e-05
## Q4  0.00027612
## Q5  0.00326220
## Q6  0.01566307
## Q7  0.04260087
## Q8  0.08306934
## Q9  0.13268699
## Q10 0.18702445
## 
## $Indirect
##     lch       
## Q1  NA        
## Q2  2.0970e-06
## Q3  1.0659e-05
## Q4  0.00027612
## Q5  0.00326220
## Q6  0.01566307
## Q7  0.04260087
## Q8  0.08306934
## Q9  0.13268699
## Q10 0.18702445
## 
## $Total
##     lch       
## Q1  2.4806e-05
## Q2  2.0970e-06
## Q3  1.0659e-05
## Q4  0.00027612
## Q5  0.00326220
## Q6  0.01566307
## Q7  0.04260087
## Q8  0.08306934
## Q9  0.13268699
## Q10 0.18702445
#Impactos modelo Durbin Rezago Espacial
impacts(ModeloEmpleo_lag_durbin, listw=wqueen)
## Impact measures (mixed, exact):
##       Direct Indirect    Total
## lch 1.260805 3.145558 4.406363
impacto2 <- impacts(ModeloEmpleo_lag_durbin, tr=trMatc,R=200,Q=5)
summary(impacto2)
## Impact measures (mixed, trace):
##       Direct Indirect    Total
## lch 1.260805 3.145547 4.406352
## ========================================================
## Simulation results (asymptotic variance matrix):
## Direct:
## 
## Iterations = 1:200
## Thinning interval = 1 
## Number of chains = 1 
## Sample size per chain = 200 
## 
## 1. Empirical mean and standard deviation for each variable,
##    plus standard error of the mean:
## 
##     Mean     SD Naive SE Time-series SE
## lch 1.34 0.4781   0.0338         0.0338
## 
## 2. Quantiles for each variable:
## 
##       2.5%   25%   50%   75% 97.5%
## lch 0.2842 1.063 1.371 1.689 2.106
## 
## ========================================================
## Indirect:
## 
## Iterations = 1:200
## Thinning interval = 1 
## Number of chains = 1 
## Sample size per chain = 200 
## 
## 1. Empirical mean and standard deviation for each variable,
##    plus standard error of the mean:
## 
##      Mean    SD Naive SE Time-series SE
## lch 3.052 1.148   0.0812         0.0812
## 
## 2. Quantiles for each variable:
## 
##       2.5%   25%   50%   75% 97.5%
## lch 0.8779 2.348 2.957 3.731 5.295
## 
## ========================================================
## Total:
## 
## Iterations = 1:200
## Thinning interval = 1 
## Number of chains = 1 
## Sample size per chain = 200 
## 
## 1. Empirical mean and standard deviation for each variable,
##    plus standard error of the mean:
## 
##      Mean    SD Naive SE Time-series SE
## lch 4.392 1.094  0.07735        0.07735
## 
## 2. Quantiles for each variable:
## 
##      2.5%  25%   50%   75% 97.5%
## lch 2.291 3.76 4.318 5.024 6.658
impacts(ModeloEmpleo_lag_durbin, tr=trMC)
## Impact measures (mixed, trace):
##       Direct Indirect    Total
## lch 1.259124 3.147228 4.406352
summary(impacto2, zstats=TRUE, short=TRUE, reportQ=TRUE)
## Impact measures (mixed, trace):
##       Direct Indirect    Total
## lch 1.260805 3.145547 4.406352
## =================================
## Impact components
## $direct
##           lch
## Q1 1.03038525
## Q2 0.06131180
## Q3 0.09440862
## Q4 0.02910761
## Q5 0.02079919
## 
## $indirect
##          lch
## Q1 0.5052339
## Q2 0.9391435
## Q3 0.5573877
## Q4 0.3955374
## Q5 0.2558569
## 
## $total
##          lch
## Q1 1.5356192
## Q2 1.0004553
## Q3 0.6517963
## Q4 0.4246450
## Q5 0.2766561
## 
## ========================================================
## Simulation results (asymptotic variance matrix):
## ========================================================
## Simulated z-values:
##       Direct Indirect    Total
## lch 2.803908 2.657586 4.015445
## 
## Simulated p-values:
##     Direct    Indirect  Total     
## lch 0.0050487 0.0078702 5.9334e-05
## ========================================================
## Simulated impact components z-values:
## $Direct
##          lch
## Q1 2.1937551
## Q2 0.5399261
## Q3 2.8388334
## Q4 2.4968814
## Q5 2.4867013
## 
## $Indirect
##          lch
## Q1 0.5922279
## Q2 4.6961090
## Q3 3.4074928
## Q4 3.3543396
## Q5 2.6838827
## 
## $Total
##         lch
## Q1 3.405669
## Q2 3.998737
## Q3 3.904515
## Q4 3.316421
## Q5 2.706860
## 
## 
## Simulated impact components p-values:
## $Direct
##    lch      
## Q1 0.0282530
## Q2 0.5892480
## Q3 0.0045279
## Q4 0.0125291
## Q5 0.0128934
## 
## $Indirect
##    lch       
## Q1 0.55369801
## Q2 2.6516e-06
## Q3 0.00065563
## Q4 0.00079555
## Q5 0.00727726
## 
## $Total
##    lch       
## Q1 0.00066002
## Q2 6.3681e-05
## Q3 9.4415e-05
## Q4 0.00091178
## Q5 0.00679228
summary(impacts(ModeloEmpleo_lag_durbin, tr=trMatc, R=200, Q=5), zstats=TRUE, short=TRUE,reportQ=TRUE)
## Impact measures (mixed, trace):
##       Direct Indirect    Total
## lch 1.260805 3.145547 4.406352
## =================================
## Impact components
## $direct
##           lch
## Q1 1.03038525
## Q2 0.06131180
## Q3 0.09440862
## Q4 0.02910761
## Q5 0.02079919
## 
## $indirect
##          lch
## Q1 0.5052339
## Q2 0.9391435
## Q3 0.5573877
## Q4 0.3955374
## Q5 0.2558569
## 
## $total
##          lch
## Q1 1.5356192
## Q2 1.0004553
## Q3 0.6517963
## Q4 0.4246450
## Q5 0.2766561
## 
## ========================================================
## Simulation results (asymptotic variance matrix):
## ========================================================
## Simulated z-values:
##       Direct Indirect    Total
## lch 2.290723 2.634592 4.064209
## 
## Simulated p-values:
##     Direct   Indirect  Total     
## lch 0.021979 0.0084238 4.8196e-05
## ========================================================
## Simulated impact components z-values:
## $Direct
##          lch
## Q1 1.7294140
## Q2 0.6402196
## Q3 2.7467847
## Q4 2.6329493
## Q5 2.8223840
## 
## $Indirect
##          lch
## Q1 0.6605046
## Q2 4.5906632
## Q3 3.5094829
## Q4 3.5905733
## Q5 2.8912818
## 
## $Total
##         lch
## Q1 3.451879
## Q2 4.018689
## Q3 4.037380
## Q4 3.539447
## Q5 2.926229
## 
## 
## Simulated impact components p-values:
## $Direct
##    lch      
## Q1 0.0837350
## Q2 0.5220298
## Q3 0.0060183
## Q4 0.0084647
## Q5 0.0047668
## 
## $Indirect
##    lch       
## Q1 0.50893010
## Q2 4.4184e-06
## Q3 0.00044898
## Q4 0.00032995
## Q5 0.00383674
## 
## $Total
##    lch       
## Q1 0.00055670
## Q2 5.8523e-05
## Q3 5.4052e-05
## Q4 0.00040097
## Q5 0.00343098
#basado en:
#Econometria Espacial, 2015
#Profesor: Miguel Angel Mendoza, FE-UNAM