-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethods.R
218 lines (155 loc) · 7.39 KB
/
methods.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
library(sf)
library(sfdep)
library(tidyverse)
library(tmap)
# read census tracts - downloaded from here https://www.ine.es/ss/Satellite?c=Page&cid=1259952026632&p=1259952026632&pagename=ProductosYServicios%2FPYSLayout
sc <- st_read("/pathtofile/España_Seccionado2022_ETRS89H30/SECC_CE_20220101.shp")
# filter - tracts in Catalonia from municipalities w/ at least 30 tracts
sc_filtered <- subset(sc, CCA=="09")
sc_per_muni <- table(as.factor(sc_filtered$NMUN))
sc_filtered <- subset(sc_filtered, NMUN %in% names(sc_per_muni[which(sc_per_muni >= 30)]))
# read income indicators - filter: net income per household, only values per census tracts
# downloaded from here - https://www.ine.es/experimental/atlas/exp_atlas_tab.htm (Indicadores de renta media y mediana)
inds <- read.csv2("30824.csv", sep = "\t")
inds <- subset(inds, (Periodo == "2019" & Indicadores.de.renta.media == 'Renta neta media por hogar' & Secciones != ""))
# transform field with value: remove decimal point and transform to numeric
inds$Total <- as.numeric(str_remove(inds$Total, "\\."))
# create field with census tract unique code for later merge
inds$CUSEC <- substring(inds$Secciones, 1,10)
inds <- inds[!is.na(inds$Total),c("CUSEC", "Total")]
# merge
sc_inds <- inner_join(sc_filtered, inds, by = "CUSEC")
# plot
tm_shape(sc_inds) +
tm_polygons(col = "Total",
title = "Renda neta mitjana per llar (€)",
legend.format = list(text.separator = "-"),
palette = "viridis",
style = "jenks",
border.alpha = 0.2) +
tm_facets(by = "NMUN")
#' Get spatial lags for a single column
#' By defect it uses queen contiguity (only one point necessary) & row-standardised weights
#' It cannot use KNN, distance band or kernel weights - may add it later on.
#' Designed to use with by or similar tool to get lagged values for groups of polygons within a sf object
#'
#' @param table a sf polygon or multipolygon object
#' @param col a column of table for which values lags will be computed (e.g. income)
#' @param queen logical, whether to use queen contiguity if TRUE. Uses rook contiguity if FALSE.
#' @param style character, type of weight style to use, by defect row-standardised. See sfdep::st_weights
#' @return lags (table)
lags_t <- function(table, col, queen = TRUE, style = "W"){
# nb list
nb <- sfdep::st_contiguity(table, queen = queen)
w <- sfdep::st_weights(nb, style = style)
x <- sf::st_drop_geometry(table)
x <- x[,col] |>
as.vector()
lags <- sfdep::st_lag(x = x, nb = nb, wt = w, na_ok = TRUE)
t <- cbind(x, lags) |> as.data.frame()
colnames(t) <- c("Value", "Lag")
t$CUSEC <- table$CUSEC
return(t)
}
lags <- moran_table <- by(data = sc_inds, INDICES = list(sc_inds$NMUN), FUN = lags_t, col = "Total") |>
cbind() |>
as.data.frame()
lags$muni <- row.names(lags)
lags_unnested <- unnest(lags)
ggplot(lags_unnested, aes(x = Value/1000, y = Lag/1000)) +
geom_point() +
geom_smooth(method = "lm") +
theme_minimal() +
ylab("Lag renda (milers d'€)") +
xlab("Renda (milers d'€)") +
facet_wrap(~muni, scales = "free")
#' Calculate Moran's I for a given column of a sf polygon object.
#' By defect it uses queen contiguity (only one point necessary) & row-standardised weights
#' It cannot use KNN, distance band or kernel weights - may add it later on.
#' Designed to use with by or similar tool to get Moran's i for groups of polygons within a sf object
#'
#' @param table a sf polygon or multipolygon object
#' @param col a column of table for which values Moran's I will be computed (e.g. income)
#' @param queen logical, whether to use queen contiguity if TRUE. Uses rook contiguity if FALSE.
#' @param style character, type of weight style to use, by defect row-standardised. See sfdep::st_weights
#' @return Global Moran's I (numeric)
moran_t <- function(table, col, queen = TRUE, style = "W"){
# nb list
nb <- sfdep::st_contiguity(table, queen = queen)
w <- sfdep::st_weights(nb, style = style)
x <- sf::st_drop_geometry(table)
x <- x[,col] |>
as.vector()
m <- sfdep::global_moran(x = x, nb = nb, wt = w)
m <- m$I
return(m)
}
# get moran's I for each city
moran_table <- by(data = sc_inds, INDICES = list(sc_inds$NMUN), FUN = moran_t, col = "Total") |>
cbind() |>
as.data.frame()
# format the DF
moran_table$city <- row.names(moran_table)
row.names(moran_table) <- NULL
moran_table <- moran_table[,c("city", "V1")]
moran_table <- rename(moran_table, "Moran's I" = "V1")
ggplot(moran_table, aes(y = reorder(city, `Moran's I`), x= `Moran's I`)) +
geom_bar(stat = "identity", fill = "blue") +
theme_light() +
xlab("Ciutat") +
ylab("I de Moran")
#' Calculate Local Moran's I for a given column of a sf polygon object.
#' By defect it uses queen contiguity (only one point necessary) & row-standardised weights
#' It cannot use KNN, distance band or kernel weights - may add it later on.
#' Designed to use with by or similar tool to get Moran's i for groups of polygons within a sf object
#'
#' @param table a sf polygon or multipolygon object
#' @param col a column of table for which values Moran's I will be computed (e.g. income)
#' @param queen logical, whether to use queen contiguity if TRUE. Uses rook contiguity if FALSE.
#' @param style character, type of weight style to use, by defect row-standardised. See sfdep::st_weights
#' @return Local Moran's I (table)
local_moran_t <- function(table, col, queen = TRUE, style = "W") {
nb <- sfdep::st_contiguity(table, queen = queen)
w <- sfdep::st_weights(nb, style = style)
x <- sf::st_drop_geometry(table)
x <- x[,col] |>
as.vector()
m <- sfdep::local_moran(x = x, nb = nb, wt = w)
m$CUSEC <- table$CUSEC
return(m)
}
lm <- by(data = sc_inds, INDICES = list(sc_inds$NMUN), FUN = local_moran_t, col = "Total") |>
cbind() |>
as.data.frame() |>
unnest()
# generate quadrants based on significance
lm <- mutate(lm, "quadrant" = case_when(p_ii_sim < 0.05 ~ as.character(pysal),
TRUE ~ "Not significant"),
"quadrant" = as.factor(quadrant),
"quadrant" = fct_relevel(quadrant, "High-High", "High-Low", "Low-Low", "Low-High", "Not significant"))
# plot
lm_plot <- left_join(sc_inds[,c("NMUN","CUSEC")], lm[,c("CUSEC", "quadrant")], by = "CUSEC")
palette <- c(rgb(1,0,0, alpha = 1), rgb(1,0,0, alpha = 0.5), rgb(0,0,1, alpha = 1), rgb(0,0,1, alpha = 0.5), rgb(0,0,0, alpha = 0.2))
tm_shape(lm_plot) +
tm_polygons(col = "quadrant",
title = "quadrant",
palette = palette,
border.alpha = 0.5) +
tm_facets(by = "NMUN")
# moran scatterplot w/ quadrants
lags_unnested_quadrant <- left_join(lags_unnested, lm[,c("CUSEC", "quadrant")], by = "CUSEC")
group.center <- function(var,grp) {
return((var-tapply(var,grp,mean,na.rm=T)[grp])/tapply(var,grp,sd,na.rm=T)[grp])
}
lags_unnested_quadrant$centervalue <- unname(group.center(lags_unnested_quadrant$Value, lags_unnested_quadrant$muni))
lags_unnested_quadrant$centerlag <- unname(group.center(lags_unnested_quadrant$Lag, lags_unnested_quadrant$muni))
ggplot(lags_unnested_quadrant) +
geom_point(aes(x = centervalue, y = centerlag, colour = quadrant)) +
scale_colour_manual(values = palette) +
geom_smooth(method = "lm", formula = y ~ x, aes(x = centervalue, y = centerlag)) +
geom_vline(xintercept = 0) +
geom_hline(yintercept = 0) +
theme_minimal() +
ylab("Lag renda std") +
xlab("Renda std") +
facet_wrap(~muni)