-
Notifications
You must be signed in to change notification settings - Fork 2
/
TechDemo-Oct2022.R
246 lines (183 loc) · 7.52 KB
/
TechDemo-Oct2022.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
## Libraries
library(tidyverse) # Data wrangling
library(sf) # (Spatial) data wrangling and GIS
library(tidycensus) # Census API
library(tmap) # Interactive mapping
## Options and Settings
tmap_mode("view")
census_api_key("e0f3cdfa0e1e13ab3a4d9dc32c67984d0be5523b")
# 1. Workshop Data --------------------------------------------------------
## SafeGraph places - Atlanta MSA
places <- st_read("Data/places.gpkg", quiet = TRUE)
### What does the data look like?
glimpse(places)
### Map
places %>%
filter(top_category == "Grocery Stores") %>%
tm_shape() +
tm_dots(col = "brands", title = "Brand", size = 0.05) +
tm_layout(legend.outside = TRUE)
################################################################################
## US Geodemographic Classification (Spielman and Singleton) - Atlanta MSA
geodemo <- st_read("Data/geodemographic.gpkg", quiet = TRUE)
## What does the data look like?
glimpse(geodemo)
### Map
tm_shape(geodemo) +
tm_fill(col = "TractGroup", alpha = 0.5, palette = "Pastel1") +
tm_borders(col = "black", lwd = 0.25) +
tm_layout(legend.outside = TRUE)
################################################################################
## Population Data - Tidycensus
pop <- get_decennial(geography = "Tract", variables = "P001001",
state = "GA", year = 2010)
### What does the data look like?
glimpse(pop)
### Merge w/ geodemographic
gd_pop <- merge(geodemo, pop, by = "GEOID")
glimpse(gd_pop)
### Map
tm_shape(gd_pop) +
tm_fill(col = "value", title = "TotalPopulation", alpha = 0.5, style = "jenks") +
tm_borders(col = "black", lwd = 0.25) +
tm_layout(legend.outside = TRUE)
################################################################################
# 2. Store Catchments -----------------------------------------------------
### Focusing on two different store brands in the U.S.
wholefoods <- places %>%
filter(brands == "Whole Foods Market")
ollies <- places %>%
filter(brands == "Ollie's Bargain Outlet")
### Map
q1 <- tm_shape(wholefoods) +
tm_dots(col = "blue", size = 0.1, alpha = 0.5) +
tm_text("brands", size = 1, just = "top") +
tm_shape(ollies) +
tm_dots(col = "orange", size = 0.1, alpha = 0.5) +
tm_text("brands", size = 1, just = "top")
q1
### Map (w/ Geodemographic underneath)
tm_shape(gd_pop) +
tm_fill(col = "TractGroup", alpha = 0.75, palette = "Pastel1") +
tm_borders(col = "black", lwd = 0.1) +
tm_layout(legend.outside = TRUE) +
q1
### Build a 2.5k buffer for the stores
wholefoods_catch <- st_buffer(wholefoods, 2500)
ollies_catch <- st_buffer(ollies, 2500)
### Map
q2 <- tm_shape(wholefoods_catch) +
tm_polygons(col = "red", alpha = 0.3) +
tm_shape(ollies_catch) +
tm_polygons(col = "red", alpha = 0.3) +
q1
q2
########################################################################
# 3. Catchment Profiling -------------------------------------------------
### Join the geodemographics with the store catchments
wholefoods_gd <- st_join(wholefoods_catch, gd_pop)
head(wholefoods_gd)
### Calculate total population in each store catchment
wholefood_profile <- wholefoods_gd %>%
as.data.frame() %>%
select(-c(geom)) %>%
group_by(location_name, placekey) %>%
summarise(catch_pop = sum(value))
### Calculate total geodemographic population in each store catchment
wholefood_gd_profile <- wholefoods_gd %>%
as.data.frame() %>%
select(-c(geom)) %>%
group_by(location_name, placekey, TractGroup) %>%
summarise(catch_gd_pop = sum(value))
### Bring together
wholefood_profile <- merge(wholefood_profile, wholefood_gd_profile, by = c("location_name", "placekey"), all.x = TRUE)
### Calculate proportion of catchment population occupied by each geodemographic group
wholefood_profile_final <- wholefood_profile %>%
mutate(gd_prop = (catch_gd_pop / catch_pop) * 100) %>%
select(location_name, placekey, TractGroup, gd_prop)
head(wholefood_profile_final)
### Visualise breakdown of catchment geodemographic population by store
wholefood_profile_final %>%
ggplot(aes(fill = TractGroup, x = placekey, y = gd_prop)) +
xlab("Store") +
ylab("Catchment Proportion (%)") +
scale_fill_brewer(palette = "Pastel1") +
geom_bar(stat = "identity") +
theme(axis.text.x = element_blank())
###############################################################################
# 4. Catchment Income Profile ---------------------------------------------
### Weights for each geodemographic group
zscores <- read.csv("Data/z-scores.csv")
zscores
### Merge weights onto profiles
wholefood_profile_final <- merge(wholefood_profile_final, zscores[, c("TractGroup", "z.score")], all.x = TRUE)
### Weight the catchment proportions
wholefood_profile_final <- wholefood_profile_final %>%
arrange(location_name, placekey) %>%
mutate(w_gd_prop = gd_prop * z.score)
### Calculate a weighted score, sums the weighted proportions to provide an Income Profile score:
wholefood_out <- wholefood_profile_final %>%
select(location_name, placekey, w_gd_prop) %>%
group_by(location_name, placekey) %>%
summarise(exp = sum(w_gd_prop))
wholefood_out
### Mean Income Profile score for wholefoods
mean(wholefood_out$exp)
##############################################################################
# 5. Why my code is awful -------------------------------------------------
### Function that does all of these steps
getProfile <- function(brand) {
## Data
x <- places %>%
filter(brands == brand)
x_catch <- st_buffer(x, 2500)
## Spatial Join
x_gd <- st_join(x_catch, gd_pop)
## Calculate populations
x_profile <- x_gd %>%
as.data.frame() %>%
select(-c(geom)) %>%
group_by(location_name, placekey) %>%
summarise(catch_pop = sum(value))
x_gd_profile <- x_gd %>%
as.data.frame() %>%
select(-c(geom)) %>%
group_by(location_name, placekey, TractGroup) %>%
summarise(catch_gd_pop = sum(value))
x_profile <- merge(x_profile, x_gd_profile, by = c("location_name", "placekey"), all.x = TRUE)
x_profile_final <- x_profile %>%
mutate(gd_prop = (catch_gd_pop / catch_pop) * 100) %>%
select(location_name, placekey, TractGroup, gd_prop)
## Calculate Income Profile
x_profile_final <- merge(x_profile_final, zscores[, c("TractGroup", "z.score")], all.x = TRUE)
x_profile_final <- x_profile_final %>%
arrange(location_name, placekey) %>%
mutate(w_gd_prop = gd_prop * z.score)
x_out <- x_profile_final %>%
select(location_name, placekey, w_gd_prop) %>%
group_by(location_name, placekey) %>%
summarise(exp = sum(w_gd_prop))
x_income_prof <- x_out %>%
group_by(location_name) %>%
drop_na() %>%
summarise(incomeProfile = mean(exp))
return(x_income_prof)}
### Now we can supply a brand name and return it's mean Income Profile score
getProfile("T.J. Maxx")
getProfile("Burger King")
### What if we want to test it across multiple brands at the same time, instead
### of line-by-line???
### First set up a list of brands you want to look at?
brand_ls <- c("Banana Republic", "Michael Kors", "T.J. Maxx", "JCPenney")
### Then lapply()
profiles <- lapply(brand_ls, getProfile)
glimpse(profiles)
### df
do.call(rbind, profiles)
### If working on linux, or within one of the lab machines
#mclapply(brand_ls, getProfile, mc.cores = 4)
###############################################################################
# 6. US Retail Centres example --------------------------------------------
##############################################################################
# 7. GitHub demo ----------------------------------------------------------
###############################################################################