library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.4 v dplyr 1.0.7
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 2.0.1 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(sf)
## Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(tmap)
library(spatstat)
## Loading required package: spatstat.data
## Loading required package: spatstat.geom
## Registered S3 method overwritten by 'spatstat.geom':
## method from
## print.boxx cli
## spatstat.geom 2.2-2
## Loading required package: spatstat.core
## Loading required package: nlme
##
## Attaching package: 'nlme'
## The following object is masked from 'package:dplyr':
##
## collapse
## Loading required package: rpart
## spatstat.core 2.3-0
## Loading required package: spatstat.linnet
## spatstat.linnet 2.3-0
##
## spatstat 2.2-0 (nickname: 'That's not important right now')
## For an introduction to spatstat, type 'beginner'
library(maptools)
## Loading required package: sp
## Checking rgeos availability: FALSE
## Please note that 'maptools' will be retired by the end of 2023,
## plan transition at your earliest convenience;
## some functionality will be moved to 'sp'.
## Note: when rgeos is not available, polygon geometry computations in maptools depend on gpclib,
## which has a restricted licence. It is disabled by default;
## to enable gpclib, type gpclibPermit()
library(sqldf)
## Loading required package: gsubfn
## Loading required package: proto
## Loading required package: RSQLite
library(ggplot2)
library(spatstat.geom)
library(leaflet.providers)
library(markdown)
library(polyclip)
## polyclip 1.10-0 built from Clipper C++ version 6.4.0
library(ggmap)
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(dplyr)
library(jsonlite)
##
## Attaching package: 'jsonlite'
## The following object is masked from 'package:purrr':
##
## flatten
library(devtools)
## Loading required package: usethis
library(yelpr)
hotel_resp <- read.csv("hotel_response.csv")
grocery_resp <- read.csv("grocery_response.csv")
#Organize: stack the data & create new column called "term" that's either called hotel or grocery.
hotel_resp$type <- "hotel"
grocery_resp$type <- "grocery"
resp_stack <- rbind(hotel_resp, grocery_resp)
#Convert resp into a spatial file
stack_sf <- st_as_sf(resp_stack, coords = c("coordinates.longitude","coordinates.latitude"),
dim = "XY", crs = 4326) %>%
na.omit()
#Filter out anomaly
stack_sf <- filter(stack_sf, id != "dw0dQNw3d6MlYHO7psLRNA")
tmap_mode('view')
## tmap mode set to interactive viewing
tm_shape(stack_sf) + tm_dots(col = "type", id = "name")
grocery_sf <- filter(stack_sf, type == "grocery")
hotel_sf <- filter(stack_sf, type == "hotel")
grocery.proj <- st_transform(grocery_sf, crs = 26967)
grocery.sp <- as(grocery.proj, "Spatial") # Create Spatial Object
grocery.ppp <- as(grocery.sp, "ppp")
hotel.proj <- st_transform(hotel_sf, crs = 26967)
hotel.sp <- as(hotel.proj, "Spatial") # Create Spatial Object
hotel.ppp <- as(hotel.sp, "ppp")
#Q1 - Report on the Chi Squared Test results for the hotels and grocery stores
hotel.qc <- quadratcount(hotel.ppp, nx=4, ny=4)
hotel.qt <- quadrat.test(hotel.qc)
print(hotel.qt)
##
## 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
grocery.qc <- quadratcount(grocery.ppp, nx=4, ny=4)
grocery.qt <- quadrat.test(grocery.qc)
print(grocery.qt)
##
## 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
#Question 2 - Show quadrant maps for Hotel and Grocery Stores
plot(hotel.qc, main = "Quadrant Analysis of POI type : hotel")
plot(grocery.qc, main = "Quadrant Analysis of POI type : grocery")
#Question 3 - What are the three issues with quadrat analysis?
• A change in Quadrat count completely changes the statistical analysis. • It does not take account of the context very well. The formation of the boxes does not follow the constraints, boundaries, forms of cities or regions. • It does not provide information about clustering or dispersal within a box. • It can measure primarily static data. It might not be suitable to analyze parameters like movement.
#Question 4 - Create a Nearest Neighbor Plot (G Plot) for Hotels and Grocery Stores. Describe which is clustered the most and how you know ?
hotel.G <- Gest(hotel.ppp)
grocery.G <- Gest(grocery.ppp)
plot(hotel.G, . ~ r, col = "red", main = "Nearest Neighbor for Hotels in Red and Grocery Stores in Black", legend = FALSE)
plot(grocery.G, . ~ r, col = "black", legend = FALSE, add = TRUE )
#Hotels cluster more than grocery stores (G(r) increases at a greater rate with respect to r for red line (Hotels)