pkgs <-c("tidyverse","sf", "tmap", "jsonlite","devtools","yelpr", "spatstat","maptools")
sapply(pkgs, library, character.only = TRUE)
hotel_resp <- read.csv("hotel_response.csv")
grocery_resp <- read.csv("grocery_response.csv")
hotel_resp$type <- "hotel"
grocery_resp$type <- "grocery"
resp_stack <- rbind(hotel_resp, grocery_resp)
Convert resp into a sf (spatial file)
stack_sf <- st_as_sf(resp_stack, coords = c("coordinates.longitude","coordinates.latitude"),
dim = "XY", crs = 4326) %>%
na.omit()
Now we need to filter
stack_sf <- filter(stack_sf, id != "dw0dQNw3d6MlYHO7psLRNA")
Map Viz to show the different points
The Chi Square Test score for grocery is 164.46 at a degree of freedom of 15 with a p-value of <2.2e^-16
The Chi Square Test score for hotel is 375.85 at a degree of freedom of 15 with a p-value of <2.2e^-16
Conducting Chi Square Test for Grocery
# Converting sf object to ppp_g
input_sf_g <- filter(stack_sf, type == "grocery")
p.proj_g <- st_transform(input_sf_g, crs = 26967)
p.sp_g <- as(p.proj_g, "Spatial") # Create Spatial Object
p.ppp_g <- as(p.sp_g, "ppp")
Create Quadrat Count
quadrat_grocery <- quadratcount(p.ppp_g, nx = 4, ny = 4)
qt_g <- quadrat.test(quadrat_grocery)
print(qt_g)
##
## Chi-squared test of CSR using quadrat counts
##
## data:
## X2 = 164.46, df = 15, p-value < 2.2e-16
## alternative hypothesis: two.sided
##
## Quadrats: 4 by 4 grid of tiles
Conducting Chi Square Test for Hotels
# Converting sf object to ppp_h
input_sf_h <- filter(stack_sf, type == "hotel")
p.proj_h <- st_transform(input_sf_h, crs = 26967)
p.sp_h <- as(p.proj_h, "Spatial") # Create Spatial Object
p.ppp_h <- as(p.sp_h, "ppp")
Create Quadrat Count
quadrat_h <- quadratcount(p.ppp_h, nx = 4, ny = 4)
qt_h <- quadrat.test(quadrat_h)
print(qt_h)
##
## Chi-squared test of CSR using quadrat counts
##
## data:
## X2 = 375.85, df = 15, p-value < 2.2e-16
## alternative hypothesis: two.sided
##
## Quadrats: 4 by 4 grid of tiles
The quadrat analysis of grocery POI indicates that the stores are more clustered in quadrat 3, 6, 7 and 8, but more dispersed in quadrat 5 and 16. Also, for quadrat 1, the observed is 29, but the estimated/expected is 17.5.
The quadrat analysis of hotel POI indicates that the hotels are more dispersed in quadrat 9 to 12 but more clustered in quadrat 3 and 7. Also, for quadrat 1, the observed is 9, but the estimated/expected is 14.2.
Create quadrat map for grocery
plot(p.ppp_g, use.marks = FALSE, cols = 'grey', main = paste("Quadrat analysis of POI type: Grocery"))
plot(qt_g, add = TRUE) # overlay: actual, expected, residual (O-E)/(E^2)
Create quadrat map for hotel
plot(p.ppp_h, use.marks = FALSE, cols = 'grey', main = paste("Quadrat analysis of POI type: Hotel"))
plot(qt_h, add = TRUE) # overlay: actual, expected, residual (O-E)/(E^2)
From the chart hotel is represented by red color and grocery stores is represented by black color. The plot shows that hotels are more clustered than grocery in our study area. This is because the curve of hotel rises faster than that of grocery with increase distance.
grocery <- Gest(p.ppp_g)
hotel <- Gest(p.ppp_h)
plot(grocery, . ~ r, col = "black", main = "Nearest Neighbor for Hotels (red) and Grocery Stores (black)", legend = FALSE)
plot(hotel, . ~ r, col = "red", legend = FALSE, add = TRUE)