Perform the following analysis of these data: 1. Mean Center of the points - Make a map showing this. 2. Convex hull of the points - Make a map showing this. 3. Spatial join of the points to the ACS layer (in sa_tracts_2019.gpkg). Set the Symbology to Graduated, Pretty Breaks, 5 Classes, for the variable ppov. - Make a map showing this. 4. Nearest Neighbor Analysis of the bar layer. Report the Z-score

#download A to Z databases drinking venues information
GIS_drinking_venues <- read_csv("C:/Users/rlutt/OneDrive/Spring 2022/GIS drinking venues.csv")
## New names:
## Rows: 23 Columns: 227
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (75): Source...1, Date, Obsolescence Date, Business Name, Legal Name, P... dbl
## (41): Physical Address Number, Physical ZIP, Physical ZIP 4, Location E... lgl
## (111): Physical Post Direction, Corporate Employee Size, Mailing Post Di...
## ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
## Specify the column types or set `show_col_types = FALSE` to quiet this message.
## • `Source` -> `Source...1`
## • `Source` -> `Source...227`
GIS_drinking_venues<-GIS_drinking_venues[c(6, 12:14)]
names(GIS_drinking_venues)<-c("street", "city", "st", "zip")
head(GIS_drinking_venues)
## # A tibble: 6 × 4
##   street             city        st      zip
##   <chr>              <chr>       <chr> <dbl>
## 1 108 King William   San Antonio TX    78204
## 2 905 Nogalitos St   San Antonio TX    78204
## 3 507 Ruiz St        San Antonio TX    78207
## 4 514 W Commerce St  San Antonio TX    78207
## 5 5721 W Commerce St San Antonio TX    78237
## 6 254 Hobart St      San Antonio TX    78237
#project data
results<-cxy_geocode(GIS_drinking_venues,
                     street = "street",
                     city = "city",
                     state ="st",
                     zip = "zip",
                     class="sf",
                     output = "simple")
## 2 rows removed to create an sf object. These were addresses that the geocoder could not match.
results.proj<-st_transform(results,
                           crs = 2278)
mapview(results.proj)
#calculate mean 

mean_feature<-apply(st_coordinates(results.proj), MARGIN = 2, FUN = mean)
mean_feature<-data.frame(place="meanfeature", x=mean_feature[1], y= mean_feature[2])
mean_feature<-st_as_sf(mean_feature, coords = c("x", "y"), crs= 2278)
mapview(mean_feature, col.regions="pink")+mapview( results)
#calculate median
median_feature<-apply(st_coordinates(results.proj), MARGIN = 2, FUN = median)
median_feature<-data.frame(place="medianfeature", x=median_feature[1], y= median_feature[2])
median_feature<-st_as_sf(median_feature, coords = c("x", "y"), crs= 2278)
mapview(median_feature, col.regions="yellow")+
  mapview(mean_feature, col.regions="orange")+
  mapview( results)
#buffer points
wicbuff<- st_buffer(results.proj, dist = 2500)
mapview(wicbuff)+mapview(results.proj, col.regions="purple")
#convex hull
chull <- st_convex_hull(st_union(results))
mapview(chull)+
  mapview(results, col.regions = "blue")
library(tidycensus)
#spatial join data with acs

#load census tract data
sa_acs<-get_acs(geography = "tract",
                state="TX",
                county = "Bexar", 
                year = 2019,
                variables=c( "DP05_0001E", "DP03_0009P", "DP03_0062E", "DP03_0119PE",
                           "DP05_0001E","DP02_0009PE","DP02_0008PE","DP02_0040E","DP02_0038E",
                            "DP02_0066PE","DP02_0067PE","DP02_0080PE","DP02_0092PE",
                        "DP03_0005PE","DP03_0028PE","DP03_0062E","DP03_0099PE","DP03_0101PE",
                            "DP03_0119PE","DP04_0046PE","DP05_0072PE","DP05_0073PE",
                            "DP05_0066PE", "DP05_0072PE", "DP02_0113PE") ,
                geometry = T, output = "wide")
## Getting data from the 2015-2019 5-year ACS
## Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
## Using the ACS Data Profile
## Using the ACS Data Profile
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |=========                                                             |  14%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  54%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================| 100%
#rename variables and filter missing cases
sa_acs2<-sa_acs%>%
  mutate(totpop= DP05_0001E, pwhite=DP05_0072PE, 
         pblack=DP05_0073PE , phisp=DP05_0066PE,
         phsormore=DP02_0066PE,punemp=DP03_0009PE, medhhinc=DP03_0062E,
         ppov=DP03_0119PE)%>%
  dplyr::select(GEOID, totpop, pblack, pwhite, phisp, punemp, medhhinc, ppov)
sa_acs2<-st_transform(sa_acs2, crs = 2278)
sa_trol<-st_cast(sa_acs2, "MULTILINESTRING")

spjoin<-st_join(results.proj, sa_acs2)
head(spjoin)
## Simple feature collection with 6 features and 12 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: 2100089 ymin: 13694870 xmax: 2130647 ymax: 13705940
## Projected CRS: NAD83 / Texas South Central (ftUS)
##                street        city st   zip       GEOID totpop pblack pwhite
## 2    108 King William San Antonio TX 78204 48029192100   2299    0.7   42.4
## 23   905 Nogalitos St San Antonio TX 78204 48029150100   5588    0.8   83.1
## 12        507 Ruiz St San Antonio TX 78207 48029110600   5293    1.1   65.0
## 14  514 W Commerce St San Antonio TX 78207 48029110100   3160    0.5   52.0
## 15 5721 W Commerce St San Antonio TX 78237 48029171200   4065    4.6   89.1
## 9       254 Hobart St San Antonio TX 78237 48029171501   2609    0.0   77.5
##    phisp punemp medhhinc ppov                 geometry
## 2    0.0    3.8    73194  1.2 POINT (2130647 13699644)
## 23   2.3    7.8    40302 14.5 POINT (2124919 13694869)
## 12   6.2   16.1    15250 37.8 POINT (2125077 13705944)
## 14   0.4    2.0    50865 24.7 POINT (2128147 13702613)
## 15   3.2    4.0    28284 22.3 POINT (2104639 13705051)
## 9    0.2    6.6    35194 23.6 POINT (2100089 13698413)
#Set the Symbology to Graduated, Pretty Breaks, 5 Classes, for the variable ppov 

library(tmap)
library(tmaptools)
map5 <- tm_shape(sa_acs2)+
  tm_polygons()+
tm_shape(spjoin)+
  tm_dots("ppov", title="% in Poverty",
              palette="Greens",
              style="pretty",
              n=5,
              size=0.3)+
  tm_format("World",
            main.title=" Poverty Estimates- (SA 2019) - Pretty Breaks",
            main.title.position=c('center','top'),
            main.title.size=1,
            title="Author: R.Luttinen \nSource: ACS 2019",
            legend.title.size=1.7,
            legend.outside=T,
            legend.text.size= 1)+
  tm_scale_bar(position = c("left","bottom"))+
  tm_compass()
map5

#Nearest Neighbor Analysis of the bar layer
library(spatstat)
## Loading required package: spatstat.data
## Loading required package: spatstat.geom
## spatstat.geom 3.0-3
## Loading required package: spatstat.random
## spatstat.random 3.0-1
## Loading required package: spatstat.explore
## Loading required package: nlme
## 
## Attaching package: 'nlme'
## The following object is masked from 'package:dplyr':
## 
##     collapse
## spatstat.explore 3.0-5
## Loading required package: spatstat.model
## Loading required package: rpart
## spatstat.model 3.0-2
## Loading required package: spatstat.linnet
## spatstat.linnet 3.0-3
## 
## spatstat 3.0-2 
## For an introduction to spatstat, type 'beginner'
library(maptools)
## Loading required package: sp
## Checking rgeos availability: TRUE
## Please note that 'maptools' will be retired during 2023,
## plan transition at your earliest convenience;
## some functionality will be moved to 'sp'.
bar.pp<-as.ppp(as(results.proj, "Spatial"))
plot(nearest.neighbour(bar.pp))

I am unable to get the rest of the nearest neighborhood analysis code to run so I will interpret the NNI and z-score provided on blackboard. An NNI of 0.43 means the pattern of bars/drinking venues in San Antonio is clustered and a Z-score of -9.8 would mean the value is significant. An NNI less than one indicates a clustered pattern and a z score greater any absolute value greater than 1.96 indicates the result is significant.