knitr::opts_chunk$set(echo = TRUE, message = FALSE, comment = NA, warning = FALSE)
knitr::opts_knit$set(root.dir = "C:/Users/tosses/Desktop/DIPLOMADO BIG DATA/MODULO4/datos_tarea")
ipak <- function(pkg){
new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if (length(new.pkg))
install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}
# usage
packages <- c("rgdal","sf", "raster", # manejo de datos espaciales
"ggmap", # Geocodificacion
"ggplot2", "viridis", # cartografias estaticas
"leaflet","RColorBrewer", # cartografias dinamicas
"spatstat", "spdep", "gstat", # analisis estadistico espacial
"leaps", "plyr","dplyr") # manipulacion y organizacion de datos
ipak(packages)
rgdal sf raster ggmap ggplot2 viridis
TRUE TRUE TRUE TRUE TRUE TRUE
leaflet RColorBrewer spatstat spdep gstat leaps
TRUE TRUE TRUE TRUE TRUE TRUE
plyr dplyr
TRUE TRUE
setwd("C:/Users/tosses/Desktop/DIPLOMADO BIG DATA/MODULO4/datos_tarea")
#Leer archivos para la tarea
violencia<-readRDS(file="casos_violencia.rds")
head(violencia)
id x y
4423 1 351501.2 6301276
4836 2 359214.9 6303245
4837 3 351866.6 6301303
4844 4 351303.1 6301359
4916 5 351536.6 6301306
5125 6 357105.9 6301723
coordenadas<-readRDS(file="crs_utm.rds")
coordenadas
[1] "+proj=utm +zone=19 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"
# Definir proyecciones espaciales según datos crs_utm.rds
crs_utm <- "+proj=utm +zone=19 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"
## Transformar objeto data2 (points) en formato espacial de sf
data_sf_utm <- st_as_sf(violencia, coords = c("x", "y"), crs = crs_utm)
data_sf_utm$geometry
Geometry set for 6047 features
geometry type: POINT
dimension: XY
bbox: xmin: 350490 ymin: 6299685 xmax: 361083.1 ymax: 6307023
epsg (SRID): 32719
proj4string: +proj=utm +zone=19 +south +datum=WGS84 +units=m +no_defs
First 5 geometries:
## Guardar los puntos en formato shapefile
violencia2<-st_write(data_sf_utm, "C:/Users/tosses/Desktop/DIPLOMADO BIG DATA/MODULO4/datos_tarea/base_points.shp", delete_dsn = TRUE)
Deleting source `C:\Users\tosses\Desktop\DIPLOMADO BIG DATA\MODULO4\datos_tarea\base_points.shp' using driver `ESRI Shapefile'
Writing layer `base_points' to data source `C:\Users\tosses\Desktop\DIPLOMADO BIG DATA\MODULO4\datos_tarea\base_points.shp' using driver `ESRI Shapefile'
features: 6047
fields: 1
geometry type: Point
violencia2
NULL
violencia2<-readOGR(dsn = "C:/Users/tosses/Desktop/DIPLOMADO BIG DATA/MODULO4/datos_tarea",
layer="base_points")
OGR data source with driver: ESRI Shapefile
Source: "C:\Users\tosses\Desktop\DIPLOMADO BIG DATA\MODULO4\datos_tarea", layer: "base_points"
with 6047 features
It has 1 fields
violencia2
class : SpatialPointsDataFrame
features : 6047
extent : 350490, 361083.1, 6299685, 6307023 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=19 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
variables : 1
names : id
min values : 1
max values : 6047
## Evitar notacion cientifica codigo INE
options(scipen=999)
# Registrar coordenadas contenedoras de la data espacial
ext <- extent(violencia2)
x_min <- ext[1] - 500
x_max <- ext[2] + 500
y_min <- ext[3] - 500
y_max <- ext[4] + 500
w <- as.owin(c(x_min,x_max, y_min, y_max)) # ventana que define espacio de trabajo
plot(w)
# generar mapas de calor
pts <- coordinates(violencia2)
p <- ppp(pts[,1], pts[,2], window = w)
# densidad calculada en radio medio
ds_violencia <- stats::density(p, adjust=.50) # parametro de radio de kernel
plot(ds_violencia, main='Hotspot de eventos de violencia en Las Condes')
#Se debe colocar ID a identificador para que todas lñas variables tengan nombre
manzanas<-readRDS(file="manzanas_lc.rds")
manzanas
class : SpatialPolygonsDataFrame
features : 1753
extent : 350547.5, 362097.9, 6299612, 6307020 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=19 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
variables : 2
names : CODINE011, pob
min values : 13114011001006, 0
max values : 13114161004042, 3209
Censo_ed<-readRDS(file="censo_lc.rds")
head(Censo_ed)
IDMZ DSOST EDUC
11194875 13114011001006 1 17
11194876 13114011001006 0 14
11194877 13114011001006 0 10
11194878 13114011001006 0 13
11194879 13114011001006 0 15
11194880 13114011001006 1 18
Censo_ed
IDMZ DSOST EDUC
11194875 13114011001006 1 17
11194876 13114011001006 0 14
11194877 13114011001006 0 10
11194878 13114011001006 0 13
11194879 13114011001006 0 15
11194880 13114011001006 1 18
11194881 13114011001006 0 19
11194882 13114011001006 0 0
11194883 13114011001006 0 3
11194884 13114011001006 0 13
11194885 13114011001006 1 15
11194886 13114011001006 1 21
11194887 13114011001006 0 21
11194888 13114011001006 0 0
11194889 13114011001006 1 13
11194890 13114011001006 0 19
11194891 13114011001006 1 17
11194892 13114011001006 0 15
11194893 13114011001006 1 13
11194894 13114011001006 0 18
11194895 13114011001006 1 14
11194896 13114011001006 0 19
11194897 13114011001006 0 7
11194898 13114011001006 0 10
11194899 13114011001006 1 19
11194900 13114011001006 0 16
11194901 13114011001006 0 9
11194902 13114011001006 0 10
11194903 13114011001006 1 21
11194904 13114011001006 0 21
11194905 13114011001006 0 13
11194906 13114011001006 0 21
11194907 13114011001006 0 18
11194908 13114011001006 0 17
11194909 13114011001006 0 0
11194910 13114011001006 1 18
11194911 13114011001006 0 15
11194912 13114011001006 0 12
11194913 13114011001006 0 17
11194914 13114011001006 0 18
11194915 13114011001006 0 12
11194916 13114011001006 1 17
11194917 13114011001006 0 13
11194918 13114011001006 0 17
11194919 13114011001006 1 21
11194920 13114011001006 0 21
11194921 13114011001006 0 19
11194922 13114011001006 1 18
11194923 13114011001006 0 18
11194924 13114011001006 0 15
11194925 13114011001006 0 18
11194926 13114011001006 0 17
11194927 13114011001006 0 0
11194928 13114011001006 1 18
11194929 13114011001006 0 15
11194930 13114011001006 0 12
11194931 13114011001006 0 17
11194932 13114011001006 0 18
11194933 13114011001006 0 12
11194934 13114011001006 1 17
11194935 13114011001006 0 13
11194936 13114011001006 0 17
11194937 13114011001006 0 20
11194938 13114011001006 1 20
11194939 13114011001006 0 0
11194940 13114011001006 1 21
11194941 13114011001006 0 21
11194942 13114011001006 0 19
11194943 13114011001006 1 18
11194944 13114011001006 0 18
11194945 13114011001006 0 17
11194946 13114011001006 0 0
11194947 13114011001006 1 18
11194948 13114011001006 0 15
11194949 13114011001006 0 12
11194950 13114011001006 0 17
11194951 13114011001006 0 18
11194952 13114011001006 1 18
11194953 13114011001006 1 21
11194954 13114011001006 0 21
11194955 13114011001006 0 19
11194956 13114011001006 0 16
11194957 13114011001006 0 17
11194958 13114011001006 1 17
11194959 13114011001006 0 18
11194960 13114011001006 0 17
11194961 13114011001006 0 0
11194962 13114011001006 1 18
11194963 13114011001006 0 15
11194964 13114011001006 0 12
11194965 13114011001006 0 17
11194966 13114011001006 0 18
11194967 13114011001006 1 21
11194968 13114011001006 0 21
11194969 13114011001006 0 19
11194970 13114011001006 0 20
11194971 13114011001006 1 20
11194972 13114011001006 0 0
11194973 13114011001006 0 16
11194974 13114011001006 0 17
11194975 13114011001006 1 17
11194976 13114011001006 0 20
11194977 13114011001006 1 20
11194978 13114011001006 0 0
11194979 13114011001006 1 18
11194980 13114011001006 0 17
11194981 13114011001006 0 4
11194982 13114011001006 0 7
11194983 13114011001006 0 15
11194984 13114011001006 0 18
11194985 13114011001006 1 18
11194986 13114011001006 0 17
11194987 13114011001006 0 4
11194988 13114011001006 0 7
11194989 13114011001006 0 15
11194990 13114011001006 0 18
11194991 13114011001006 0 14
11194992 13114011001006 0 13
11194993 13114011001006 0 9
11194994 13114011001006 0 12
11194995 13114011001006 1 17
11194996 13114011001006 0 13
11194997 13114011001006 0 17
11194998 13114011001006 1 18
11194999 13114011001006 0 14
11195000 13114011001006 0 13
11195001 13114011001006 0 9
11195002 13114011001006 1 18
11195003 13114011001006 0 18
11195004 13114011001006 0 15
11195005 13114011001006 0 18
11195006 13114011001006 0 13
11195007 13114011001006 1 18
11195008 13114011001006 0 12
11195009 13114011001006 1 17
11195010 13114011001006 0 13
11195011 13114011001006 0 17
11195012 13114011001006 1 18
11195013 13114011001006 1 17
11195014 13114011001006 0 19
11195015 13114011001006 0 1
11195016 13114011001006 1 18
11195017 13114011001006 0 18
11195018 13114011001006 0 15
11195019 13114011001006 1 18
11195020 13114011001006 0 17
11195021 13114011001006 0 10
11195022 13114011001006 1 19
11195023 13114011001006 0 17
11195024 13114011001006 0 17
11195025 13114011001006 0 18
11195026 13114011001006 1 18
11195027 13114011001006 1 19
11195028 13114011001006 0 17
11195029 13114011001006 0 17
11195030 13114011001006 0 20
11195031 13114011001006 1 14
11195032 13114011001006 0 4
11195033 13114011001006 0 6
11195034 13114011001006 0 17
11195035 13114011001006 1 14
11195036 13114011001006 0 13
11195037 13114011001006 0 13
11195038 13114011001006 0 12
11195039 13114011001006 1 12
11195040 13114011001006 0 15
11195041 13114011001006 0 1
11195042 13114011001006 0 17
11195043 13114011001006 1 14
11195044 13114011001006 0 13
11195045 13114011001006 0 13
11195046 13114011001006 0 12
11195047 13114011001006 0 13
11195048 13114011001006 1 13
11195049 13114011001006 0 12
11195050 13114011001006 0 13
11195051 13114011001006 1 13
11195052 13114011001006 0 12
11195053 13114011001006 1 19
11195054 13114011001006 1 16
11195055 13114011001006 0 2
11195056 13114011001006 0 9
11195057 13114011001006 0 13
11195058 13114011001006 1 16
11195059 13114011001006 0 2
11195060 13114011001006 0 9
11195061 13114011001006 0 13
11195062 13114011001006 0 11
11195063 13114011001006 0 13
11195064 13114011001006 0 16
11195065 13114011001006 0 19
11195066 13114011001006 0 18
11195067 13114011001006 0 2
11195068 13114011001006 0 17
11195069 13114011001006 0 17
11195070 13114011001006 1 20
11195071 13114011001006 0 20
11195072 13114011001006 0 19
11195073 13114011001006 0 16
11195074 13114011001006 0 12
11195075 13114011001006 0 18
11195076 13114011001006 0 11
11195077 13114011001006 0 13
11195078 13114011001006 0 16
11195079 13114011001006 0 19
11195080 13114011001006 0 18
11195081 13114011001006 0 2
11195082 13114011001006 0 17
11195083 13114011001006 0 17
11195084 13114011001006 1 15
11195085 13114011001006 0 17
11195086 13114011001006 0 5
11195087 13114011001006 0 7
11195088 13114011001006 1 20
11195089 13114011001006 0 20
11195090 13114011001006 0 19
11195091 13114011001006 0 16
11195092 13114011001006 0 12
11195093 13114011001006 0 18
11195094 13114011001006 1 15
11195095 13114011001006 0 17
11195096 13114011001006 0 5
11195097 13114011001006 0 7
11195098 13114011001006 1 19
11195099 13114011001006 0 13
11195100 13114011001006 1 19
11195101 13114011001006 0 18
11195102 13114011001006 0 3
11195103 13114011001006 0 5
11195104 13114011001006 1 20
11195105 13114011001006 0 16
11195106 13114011001006 0 16
11195107 13114011001006 0 20
11195108 13114011001006 1 13
11195109 13114011001006 0 15
11195110 13114011001006 0 18
11195111 13114011001006 0 17
11195112 13114011001006 0 2
11195113 13114011001006 1 19
11195114 13114011001006 1 18
11195115 13114011001006 0 19
11195116 13114011001006 0 18
11195117 13114011001006 1 18
11195118 13114011001006 0 18
11195119 13114011001006 0 0
11195120 13114011001006 0 1
11195121 13114011001006 0 18
11195122 13114011001006 1 13
11195123 13114011001006 0 13
11195124 13114011001006 0 12
11195125 13114011001006 1 18
11195126 13114011001006 0 18
11195127 13114011001006 0 0
11195128 13114011001006 0 1
11195129 13114011001006 0 18
11195130 13114011001006 1 13
11195131 13114011001006 0 18
11195132 13114011001006 1 18
11195133 13114011001006 0 13
11195134 13114011001006 0 0
11195135 13114011001006 0 1
11195136 13114011001006 0 17
11195137 13114011001006 1 18
11195138 13114011001006 0 17
11195139 13114011001006 1 15
11195140 13114011001006 1 19
11195141 13114011001006 0 18
11195142 13114011001006 1 19
11195143 13114011001006 0 18
11195144 13114011001006 0 0
11195145 13114011001006 0 1
11195146 13114011001006 1 18
11195147 13114011001006 0 18
11195148 13114011001006 0 0
11195149 13114011001006 0 1
11195150 13114011001006 1 19
11195151 13114011001006 0 18
11195152 13114011001006 1 18
11195153 13114011001006 0 15
11195154 13114011001006 1 18
11195155 13114011001006 0 15
11195156 13114011001006 1 18
11195157 13114011001006 0 18
11195158 13114011001006 1 20
11195159 13114011001006 0 18
11195160 13114011001006 0 16
11195161 13114011001006 1 17
11195162 13114011001006 0 17
11195163 13114011001006 1 18
11195164 13114011001006 0 18
11195165 13114011001006 0 17
11195166 13114011001006 0 19
11195167 13114011001006 0 0
11195168 13114011001006 0 1
11195169 13114011001006 1 20
11195170 13114011001006 0 17
11195171 13114011001006 0 9
11195172 13114011001006 0 12
11195173 13114011001006 1 18
11195174 13114011001006 0 20
11195175 13114011001006 0 10
11195176 13114011001006 1 19
11195177 13114011001006 0 16
11195178 13114011001006 0 2
11195179 13114011001006 0 7
11195180 13114011001006 0 18
11195181 13114011001006 0 18
11195182 13114011001006 0 21
11195183 13114011001006 0 21
11195184 13114011001006 1 18
11195185 13114011001006 0 18
11195186 13114011001006 0 17
11195187 13114011001006 0 19
11195188 13114011001006 0 17
11195189 13114011001006 0 18
11195190 13114011001006 0 19
11195191 13114011001006 0 17
11195192 13114011001006 0 15
11195193 13114011001006 0 19
11195194 13114011001006 0 16
11195195 13114011001006 0 16
11195196 13114011001006 0 13
11195197 13114011001006 0 15
11195198 13114011001006 0 19
11195199 13114011001006 0 13
11195200 13114011001006 1 18
11195201 13114011001006 0 20
11195202 13114011001006 0 18
11195203 13114011001006 0 16
11195204 13114011001006 1 13
11195205 13114011001006 0 13
11195206 13114011001006 1 18
11195207 13114011001006 1 18
[ reached getOption("max.print") -- omitted 282639 rows ]
# filtrar sostenedores de hogar
sostenedores = Censo_ed[Censo_ed$DSOST == 1, ]
## Agregar datos por manzana
nived = aggregate(sostenedores[c("EDUC")], list(sostenedores$IDMZ), mean)
names(nived)[1] = "CODINE011"
nived$CODINE011=as.character(nived$CODINE011)
## Imputar datos a manzanas
manzanas@data <- left_join(manzanas@data, nived, by=c("CODINE011"))
manzanas@data
CODINE011 pob EDUC
1 13114041001006 382 16.676768
2 13114041001008 51 17.300000
3 13114041001007 174 16.769231
4 13114041001014 61 17.076923
5 13114041001022 109 17.307692
6 13114041001013 166 17.529412
7 13114041001012 81 18.705882
8 13114041001023 81 16.133333
9 13114041001001 164 17.900000
10 13114041001028 33 18.200000
11 13114041001031 39 18.833333
12 13114041003025 239 18.260870
13 13114041001029 74 17.250000
14 13114041001030 49 17.900000
15 13114041003023 392 17.044444
16 13114041003021 687 16.798122
17 13114041003020 269 17.416667
18 13114041003015 948 17.092784
19 13114041001042 22 19.000000
20 13114041003013 0 NA
21 13114041003016 107 17.296296
22 13114041001057 168 17.687500
23 13114041001061 135 17.370370
24 13114041003006 656 17.286385
25 13114041001048 111 18.333333
26 13114041001049 32 18.400000
27 13114041003026 231 16.888889
28 13114041001051 154 17.193548
29 13114041001050 76 17.647059
30 13114041003028 158 18.194444
31 13114041003018 50 18.100000
32 13114041003017 54 17.153846
33 13114041003019 104 17.869565
34 13114041001052 51 19.285714
35 13114041003029 54 17.615385
36 13114041003032 364 17.071429
37 13114041003005 455 17.343511
38 13114041003036 359 17.212500
39 13114041003034 0 NA
40 13114041003030 82 17.533333
41 13114041003027 0 NA
42 13114151002002 386 17.724490
43 13114041001067 0 NA
44 13114041003033 38 18.500000
45 13114041003042 0 NA
46 13114041001071 0 NA
47 13114151002001 342 17.961538
48 13114151002027 224 18.025641
49 13114041003004 156 16.769231
50 13114041003037 208 17.318182
51 13114041003043 94 16.375000
52 13114151002015 0 NA
53 13114151002013 0 NA
54 13114041003044 104 16.700000
55 13114161002005 199 16.375000
56 13114151002008 0 NA
57 13114041003045 385 16.810127
58 13114041003038 105 17.392857
59 13114151002009 0 NA
60 13114151002024 69 17.363636
61 13114151002023 0 NA
62 13114151001001 302 17.636364
63 13114041003003 0 NA
64 13114151002029 0 NA
65 13114041003041 59 18.750000
66 13114151002036 0 NA
67 13114041002001 0 NA
68 13114151002044 1021 18.122549
69 13114151002033 255 18.591837
70 13114041002002 86 17.631579
71 13114151002031 0 NA
72 13114151001018 204 17.938776
73 13114041003047 95 17.941176
74 13114041002003 156 17.111111
75 13114151001016 399 17.985915
76 13114041002021 490 16.680556
77 13114041003039 0 NA
78 13114041003046 0 NA
79 13114151001017 0 NA
80 13114041002004 301 17.434783
81 13114161002004 191 17.918919
82 13114041002006 407 17.219780
83 13114151001015 103 18.833333
84 13114021002053 592 17.027875
85 13114161002006 0 NA
86 13114151001023 0 NA
87 13114151001002 75 18.666667
88 13114041002019 623 17.857143
89 13114151001014 0 NA
90 13114151001004 54 18.583333
91 13114151001024 35 18.500000
92 13114151002037 0 NA
93 13114041002009 562 17.239130
94 13114151001003 53 19.750000
95 13114151001013 96 18.200000
96 13114041002020 196 18.000000
97 13114151002072 259 18.264151
98 13114161002009 438 17.701493
99 13114161002008 422 17.951807
100 13114151001005 80 18.562500
101 13114151001036 120 17.481481
102 13114151001006 92 17.500000
103 13114041002022 92 16.826087
104 13114151001026 117 18.043478
105 13114151001012 61 18.083333
106 13114151001007 107 17.526316
107 13114151002070 337 17.569231
108 13114151001037 95 17.761905
109 13114041002018 61 17.625000
110 13114151001008 78 17.937500
111 13114151002039 0 NA
112 13114041002017 62 15.571429
113 13114041002023 48 17.250000
114 13114041002008 50 14.142857
115 13114151002041 0 NA
116 13114151001009 80 17.500000
117 13114151001011 84 18.111111
118 13114151002040 0 NA
119 13114151001038 78 17.368421
120 13114151002067 268 17.862069
121 13114041002010 266 17.200000
122 13114151001010 0 NA
123 13114151001035 162 17.966667
124 13114041002024 32 15.400000
125 13114041002025 51 17.062500
126 13114021002049 247 17.408602
127 13114151002071 250 17.226415
128 13114151002059 485 18.489583
129 13114151001027 108 18.529412
130 13114041002029 87 16.500000
131 13114151001039 0 NA
132 13114041002027 71 17.130435
133 13114021002052 175 16.157143
134 13114161002007 301 18.160714
135 13114151002069 0 NA
136 13114041002014 244 16.849057
137 13114041002036 52 16.750000
138 13114151001029 69 18.642857
139 13114041002026 89 17.185185
140 13114151001043 58 18.300000
141 13114051001021 595 17.565934
142 13114071003032 89 9.428571
143 13114061002014 0 NA
144 13114131002003 662 16.757143
145 13114131002012 549 16.801887
146 13114051001020 916 17.538028
147 13114071004018 0 NA
148 13114111002027 110 17.700000
149 13114111003003 119 17.666667
150 13114131006018 234 17.333333
151 13114131004013 347 17.589744
152 13114071003035 0 NA
153 13114071003033 138 11.351351
154 13114051001026 162 16.825397
155 13114131002002 653 16.881057
156 13114061002042 100 16.620690
157 13114141003031 0 NA
158 13114081002014 59 17.500000
159 13114141001011 192 11.148148
160 13114111003016 51 14.142857
161 13114061002018 76 16.181818
162 13114141001023 93 12.064516
163 13114081002012 20 17.333333
164 13114111003010 65 17.125000
165 13114141001012 0 NA
166 13114081002011 52 16.600000
167 13114141001014 99 9.807692
168 13114061002017 0 NA
169 13114131008005 305 17.020408
170 13114141001013 154 11.577778
171 13114111001002 3131 16.980189
172 13114081002010 34 18.444444
173 13114101002007 422 16.985816
174 13114121005004 1406 16.919840
175 13114091002004 0 NA
176 13114081001034 70 16.823529
177 13114141004023 0 NA
178 13114091002011 49 17.071429
179 13114121004001 1239 16.605996
180 13114081001035 79 16.227273
181 13114081003033 84 16.880000
182 13114081001037 51 17.300000
183 13114091002003 90 17.458333
184 13114081001038 0 NA
185 13114091002005 97 16.642857
186 13114081001029 97 17.148148
187 13114091002002 40 16.000000
188 13114141004028 0 NA
189 13114151001044 55 18.250000
190 13114151002068 148 17.906250
191 13114041002030 122 15.928571
192 13114161002002 136 18.464286
193 13114151001045 78 17.352941
194 13114041002035 74 16.952381
195 13114041002015 29 18.500000
196 13114041002037 258 17.871795
197 13114041002028 0 NA
198 13114151001046 298 17.574074
199 13114161002010 301 17.758621
200 13114151002066 161 17.657143
201 13114041002031 87 16.450000
202 13114041002012 162 17.043478
203 13114151002043 115 18.400000
204 13114151001053 70 15.357143
205 13114151001040 0 NA
206 13114151002065 0 NA
207 13114021002005 173 17.135593
208 13114151001051 0 NA
209 13114041002034 75 16.050000
210 13114151001050 0 NA
211 13114151001055 123 17.833333
212 13114021002051 127 16.762712
213 13114021002009 142 16.250000
214 13114021002006 75 16.916667
215 13114041002047 47 16.071429
216 13114041002032 50 16.263158
217 13114041002033 60 16.615385
218 13114151001056 71 17.529412
219 13114041002046 73 16.736842
220 13114151001047 0 NA
221 13114151002109 603 17.944954
222 13114151001057 68 17.285714
223 13114021002018 135 17.588235
224 13114021002010 66 16.733333
225 13114151001058 65 17.812500
226 13114151002057 0 NA
227 13114041002045 92 15.807692
228 13114021002008 96 17.500000
229 13114151002049 141 17.074074
230 13114021002011 221 16.612500
231 13114151001059 54 18.800000
232 13114161002001 73 16.444444
233 13114151002050 159 18.161290
234 13114151002051 143 18.375000
235 13114021002019 147 17.000000
236 13114021002017 115 16.740741
237 13114151001048 0 NA
238 13114151002052 127 18.444444
239 13114021002016 29 16.888889
240 13114151002046 76 18.235294
241 13114151001074 0 NA
242 13114021002050 25 12.600000
243 13114041002043 89 17.043478
244 13114151002106 0 NA
245 13114151001060 398 17.780822
246 13114161001006 217 16.714286
247 13114041002042 75 15.583333
248 13114151002053 86 18.714286
249 13114021002015 38 15.363636
250 13114151001049 0 NA
251 13114021002027 197 15.890411
252 13114021002020 113 17.214286
253 13114021002014 43 17.083333
254 13114151002054 57 18.083333
255 13114151002104 0 NA
256 13114021001006 198 17.012987
257 13114151002101 0 NA
258 13114151002108 0 NA
259 13114021002026 98 17.083333
260 13114161003001 84 15.538462
261 13114021002012 56 16.647059
262 13114161001005 96 17.521739
263 13114161003003 273 16.716667
264 13114021002021 0 NA
265 13114151001068 72 18.000000
266 13114021001007 532 17.466321
267 13114151001061 133 18.821429
268 13114151001067 0 NA
269 13114151002110 62 17.833333
270 13114021002025 49 17.062500
271 13114021002028 55 17.368421
272 13114151002103 0 NA
273 13114151001062 0 NA
274 13114021002013 69 17.380952
275 13114021002022 62 16.650000
276 13114151001066 53 17.600000
277 13114151001065 62 17.600000
278 13114151002116 342 17.682540
279 13114021002029 48 16.000000
280 13114161001004 294 17.402985
281 13114151001064 55 16.333333
282 13114161001007 191 17.951220
283 13114021002030 138 17.075000
284 13114151002085 0 NA
285 13114151001063 0 NA
286 13114021002024 133 16.761905
287 13114021002023 97 16.542857
288 13114021001008 484 17.465241
289 13114151002095 0 NA
290 13114021002039 119 16.657143
291 13114021001032 594 17.279279
292 13114151002113 0 NA
293 13114021002040 21 15.625000
294 13114021002031 56 16.900000
295 13114151002096 0 NA
296 13114161003002 194 17.325581
297 13114151001071 0 NA
298 13114151002119 0 NA
299 13114161001014 0 NA
300 13114151002114 0 NA
301 13114021001031 121 16.916667
302 13114151002094 0 NA
303 13114161003005 424 17.532609
304 13114021001009 742 17.445652
305 13114161001013 126 17.266667
306 13114151001070 0 NA
307 13114021002033 59 16.631579
308 13114161003006 368 17.602941
309 13114021001030 109 16.968750
310 13114021002032 0 NA
311 13114031002001 441 17.810526
312 13114151001077 106 18.045455
313 13114021002034 60 17.500000
314 13114161003007 459 17.213592
315 13114021002035 223 17.205479
316 13114151002118 0 NA
317 13114151001078 201 17.301587
318 13114151002115 0 NA
319 13114021001029 0 NA
320 13114161001012 111 16.678571
321 13114021002038 47 16.800000
322 13114161001009 0 NA
323 13114161003008 491 17.820225
324 13114151002117 0 NA
325 13114151001083 32 18.555556
326 13114151001076 145 16.485714
327 13114021001028 0 NA
328 13114151002092 0 NA
329 13114151002102 0 NA
330 13114151001087 98 17.681818
331 13114161003009 415 18.166667
332 13114021001027 70 18.100000
333 13114021002037 52 16.266667
[ reached getOption("max.print") -- omitted 1420 rows ]
## Transformacion de Poligonos a Puntos
manzanasp = SpatialPointsDataFrame(manzanas, data = manzanas@data, proj4string = CRS(proj4string(manzanas)))
class(manzanasp)
[1] "SpatialPointsDataFrame"
attr(,"package")
[1] "sp"
## Eliminar NAs
manzanasp = manzanasp[!is.na(manzanasp$EDUC), ]
## crear variable id identica a numero de filas
rownames(manzanasp@data) = manzanasp$id = 1:nrow(manzanasp)
## Numero de vecinos a considerar para calculo de autocorrelacion
nvec=10
## Crear matriz de pesos espaciales
nb <- nb2listw(neighbours = knn2nb(knn = knearneigh( x = manzanasp, k = nvec, longlat = F)),
style = "W")
plot(manzanasp)
plot(nb,coordinates(manzanasp),add=T)
## Asignar pesos por nivel educacional de cada manzana
nb$weights = lapply(1:nrow(manzanasp@data), function(i)
manzanasp@data$pob[manzanasp@data$id %in% nb$neighbours[[i]]] / sum(manzanasp@data$pob[manzanasp@data$id %in%
nb$neighbours[[i]]]))
## Test de Moran de autocorrelacion global
test = spdep::moran.test(x = manzanasp$EDUC, listw = nb)
moran.plot(x = manzanasp$EDUC, listw = nb, labels=as.character(manzanasp$id))
# Calcular Local Moran
lmoran=localmoran(manzanasp$EDUC,nb)
manzanasp$EDUC=as.numeric(scale(manzanasp$EDUC))
manzanasp$lag_s_EDUC=lag.listw(nb,manzanasp$EDUC)
manzanasp@data=cbind(manzanasp@data,lmoran=as.data.frame(lmoran)[,5])
# Define quadrants
manzanasp[(manzanasp$EDUC>=0&manzanasp$lag_s_EDUC>=0)&(manzanasp$lmoran<=0.05),"clusterM"]="HH" # plot
manzanasp[(manzanasp$EDUC<=0&manzanasp$lag_s_EDUC<=0)&(manzanasp$lmoran<=0.05),"clusterM"]="LL" # plot
manzanasp[(manzanasp$EDUC>=0&manzanasp$lag_s_EDUC<=0)&(manzanasp$lmoran<= 0.05),"clusterM"]="HL"
manzanasp[(manzanasp$EDUC<=0&manzanasp$lag_s_EDUC>=0)&(manzanasp$lmoran<=0.05),"clusterM"]="LH"
manzanasp[(lmoran[,5]>0.05),"clusterM"]="NS"
table(manzanasp$clusterM)# Resultado
HH LL NS
49 130 1231
## Imputar informacion de puntos a manzanas
manzanas@data <- left_join(manzanas@data, manzanasp@data[,c("CODINE011","clusterM")], by=c("CODINE011"))
#### Visualizacion Dinamica Local Moran LAS MANZANAS HH Y LL DE NIVEL DE ESTUDIOS EN LAS CONDES (SEGREGACIÓN EDUCACIONAL)
## Filtar los puntos que fueron clasificados HH y LL
HH_LL <- subset(manzanas, clusterM == "HH" | clusterM == "LL")
crs_latlon <- "+proj=longlat +datum=WGS84 +no_defs"
## Reproyectar a latitud-longitud
base_fil <- spTransform(HH_LL, CRS(crs_latlon))
## Definir paleta de color
pal3 <- colorFactor(palette = "RdBu", domain = base_fil$clusterM)
## Generar mapa interactivo
leaflet(data = base_fil) %>%
addProviderTiles(providers$CartoDB.Positron)%>%
addPolygons(
color = ~ pal3(clusterM),
stroke = FALSE,
fillOpacity = 0.5,
label = ~ as.character(clusterM)
) %>%
addLegend("bottomright", pal = pal3, values = ~clusterM,
title = "Clas. L. Moran", opacity = 1)
# Generacion de raster espacial desde imagen de kernel: Hotspot de eventos de violencia en Las Condes
r2 <- raster(ds_violencia, crs = crs_utm)
# reproyectar raster espacial
r2 <- projectRaster(r2, crs = crs_latlon )
## Filtar los puntos que fueron clasificados HH y LL
HH_LL <- subset(manzanas, clusterM == "Cluster altos niveles de educación (HH)" | clusterM == "Cluster bajos nievles de educación LL")
crs_latlon <- "+proj=longlat +datum=WGS84 +no_defs"
## Definir paleta de color
pal3 <- colorFactor(palette = "RdBu", domain = base_fil$clusterM)
pal2 <- colorNumeric(c("#FFFFCC", "#41B6C4", "#0C2C84"), values(r2),
na.color = "transparent")
## Generar mapa interactivo
leaflet(data = base_fil) %>%
addProviderTiles(providers$CartoDB.Positron)%>%
addRasterImage(r2, colors = pal2, opacity = 0.5, group = "kernel") %>%
addPolygons(
color = ~ pal3(clusterM),
stroke = FALSE,
fillOpacity = 0.5,
label = ~ as.character(clusterM)
) %>%
addLegend("bottomright", pal = pal3, values = ~clusterM,
title = "Clasificación L. Moran combinado con concentración de violencia en la comuna de Las Condes", opacity = 1)
La segregación socioespacial en Las Condes es evidente en el territorio, pues el nivel educativo alto (HH) se concentra significativamente al oriente de la comuna, mientras el nivel más bajo (LL) en el poniente. En éstas últimas, es donde mayor concentración de eventos violentos se observan en la comuna.