Instalar los paquetes necesarios install.packages(“REAT”) #Es una herramienta para realizar análisis económicos regionales install.packpages(“spdep) install.packpages(”gridExtra”) install.packpages(“sp”) #un paquete que proporciona clases y métodos para datos espaciales install.packpages(“readxl”) install.packpages(“RColorBrewer”) #es una herramienta para crear paletas de colores de alta calidad, diseñadas para gráficos de datos. install.packpages(“RColorBrewer”) install.packpages(“spdep”) install.packpages(“leaflet”) #es una herramienta para crear mapas interactivos en la web install.packpages(“magrittr”) #se utiliza para simplificar la manipulación de datos y la creación de gráficos. install.packpages(“dplyr”) #ggplot2 install.packpages(“ggplot2”) #spatialEco install.packpages(“spatialEco”) #proporciona herramientas para el análisis ecológico espacial, específicamente diseñado para trabajar con datos raster y datos vectoriales. install.packpages(“stargazer”) install.packpages(“readr”) install.packpages(“sf”) permite trabajar con datos geoespaciales vectoriales. install.packages(“leaflet.extras”)
setwd(“C:/Users/yomara.ruiz/Desktop/clase dem 07/nxcantones_V2-20250608T013535Z-1-001/nxcantones_V2”)
library(spdep)
Cargando paquete requerido: spData
To access larger datasets in this package, install the spDataLarge
package with: `install.packages('spDataLarge',
repos='https://nowosad.github.io/drat/', type='source')`
Cargando paquete requerido: sf
Linking to GEOS 3.13.1, GDAL 3.11.0, PROJ 9.6.0; sf_use_s2() is TRUE
shape_canton <-st_read("NXCantones.shp")
Reading layer `NXCantones' from data source
`C:\Users\LILIANA\Desktop\Examen\nxcantones_V2\NXCantones.shp'
using driver `ESRI Shapefile'
Simple feature collection with 221 features and 12 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -732143.5 ymin: 9445216 xmax: 1147852 ymax: 10189400
Projected CRS: WGS 84 / UTM zone 17S
D_R D_R_B D_R_A DPA_VALOR
Length:221 Length:221 Length:221 Min. :0
Class :character Class :character Class :character 1st Qu.:0
Mode :character Mode :character Mode :character Median :0
Mean :0
3rd Qu.:0
Max. :0
DPA_ANIO DPA_CANTON ID_CANTON DPA_DESCAN
Length:221 Length:221 Min. : 101 Min. : NA
Class :character Class :character 1st Qu.: 704 1st Qu.: NA
Mode :character Mode :character Median :1107 Median : NA
Mean :1100 Mean :NaN
3rd Qu.:1411 3rd Qu.: NA
Max. :2403 Max. : NA
NA's :221
DPA_PROVIN DPA_DESPRO CODIGO CANTON
Length:221 Length:221 Min. : 101 Length:221
Class :character Class :character 1st Qu.: 704 Class :character
Mode :character Mode :character Median :1106 Mode :character
Mean :1093
3rd Qu.:1410
Max. :2403
geometry
MULTIPOLYGON :221
epsg:32717 : 0
+proj=utm ...: 0
Warning: st_centroid assumes attributes are constant over geometries
Creo un objeto con una matriz de los puntos de los 5 vecinos
vc_k5 <-knn2nb(knearneigh(coords_centroides, k =5))
print(coords_centroides)
Simple feature collection with 221 features and 2 fields
Geometry type: POINT
Dimension: XY
Bounding box: xmin: -643716.7 ymin: 9463821 xmax: 1070598 ymax: 10110140
Projected CRS: WGS 84 / UTM zone 17S
First 10 features:
D_R D_R_B geometry
1 <NA> <NA> POINT (697860 9683563)
2 <NA> <NA> POINT (704541.8 9648385)
3 <NA> <NA> POINT (747169 9677155)
4 <NA> <NA> POINT (709009.6 9631756)
5 <NA> <NA> POINT (751048.5 9695427)
6 <NA> <NA> POINT (663618.7 9647021)
7 <NA> <NA> POINT (692468.1 9653579)
8 <NA> <NA> POINT (681845.5 9645897)
9 <NA> <NA> POINT (738510.3 9653811)
10 <NA> <NA> POINT (707668.6 9614130)
Crear una matriz de pesos espaciales w
Una matriz de pesos espaciales (también conocida como matriz W) es una representación matemática de las relaciones espaciales entre diferentes entidades en un conjunto de datos. Esta matriz se utiliza en diversos análisis espaciales para representar cómo una unidad geográfica afecta a sus vecinos.
ID_CANTON CANTON PROVINCIA lnproduct_s12019
Min. : 101 Length:221 Length:221 Min. : 3.651
1st Qu.: 704 Class :character Class :character 1st Qu.: 6.462
Median :1107 Mode :character Mode :character Median : 7.198
Mean :1100 Mean : 7.233
3rd Qu.:1411 3rd Qu.: 7.996
Max. :2403 Max. :12.309
lnproduct_s22019 lnproduct_s32019 lnproduct_tot2019
Min. : 5.092 Min. :7.264 Min. :14.79
1st Qu.: 6.151 1st Qu.:7.924 1st Qu.:15.39
Median : 6.669 Median :8.178 Median :15.73
Mean : 6.888 Mean :8.242 Mean :15.84
3rd Qu.: 7.452 3rd Qu.:8.520 3rd Qu.:16.11
Max. :11.045 Max. :9.741 Max. :19.24
View(datosmoran)
Unir shape cantonal con datos de Moran
shape_merge <-merge(shape_canton, datosmoran , by ="ID_CANTON")
Moran I test under randomisation
data: datosmoran$lnproduct_s32019
weights: wk5
Moran I statistic standard deviate = 2.0286, p-value = 0.02125
alternative hypothesis: greater
sample estimates:
Moran I statistic Expectation Variance
0.075795024 -0.004545455 0.001568422
Análisis: la productividad de un cantón genere un aumento en la productividad del vecino. (efecto contagio)
#transformar shape con datos shape_canton <-st_transform(shape_merge, crs =4326)