Packages

library(sf)
## Warning: package 'sf' was built under R version 4.0.4
## Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.3     v purrr   0.3.4
## v tibble  3.1.0     v dplyr   1.0.5
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.1
## Warning: package 'tibble' was built under R version 4.0.4
## Warning: package 'readr' was built under R version 4.0.4
## Warning: package 'dplyr' was built under R version 4.0.4
## Warning: package 'forcats' was built under R version 4.0.4
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(censusxy)
## Warning: package 'censusxy' was built under R version 4.0.4
library(tmap)
## Warning: package 'tmap' was built under R version 4.0.4
library(readxl)
library(tidycensus)
library(qgisprocess)
## Using 'qgis_process' at 'C:/Program Files/QGIS 3.16/bin/qgis_process-qgis.bat'.
## Run `qgis_configure()` for details.
qgis_configure()
## getOption('qgisprocess.path') was not found.
## Sys.getenv('R_QGISPROCESS_PATH') was not found.
## Trying 'qgis_process' on PATH
## Error in rethrow_call(c_processx_exec, command, c(command, args), pty, : Command 'qgis_process' not found @win/processx.c:982 (processx_exec)
## Found 1 QGIS installation containing 'qgis_process':
##  C:/Program Files/QGIS 3.16/bin/qgis_process-qgis.bat
## Trying command 'C:/Program Files/QGIS 3.16/bin/qgis_process-qgis.bat'
## Success!

Data

df <- read_excel("Data/grocery_west_side.xls")
## New names:
## * Source -> Source...1
## * Source -> Source...223
head(df)
## # A tibble: 6 x 223
##   Source...1   Date     `Obsolescence Da~ `Business Name`       `Legal Name`    
##   <chr>        <chr>    <chr>             <chr>                 <chr>           
## 1 AtoZDatabas~ 03/03/2~ 09/03/2021        H-E-B LP              HEB Grocery Com~
## 2 AtoZDatabas~ 03/03/2~ 09/03/2021        Dean Foods            <NA>            
## 3 AtoZDatabas~ 03/03/2~ 09/03/2021        Surlean Foods         <NA>            
## 4 AtoZDatabas~ 03/03/2~ 09/03/2021        Super Target          <NA>            
## 5 AtoZDatabas~ 03/03/2~ 09/03/2021        Botello Food Store    <NA>            
## 6 AtoZDatabas~ 03/03/2~ 09/03/2021        Marios & Brother Cor~ Mario's Grocery 
## # ... with 218 more variables: Physical Address <chr>,
## #   Physical Address Number <chr>, Physical Pre Direction <chr>,
## #   Physical Address Name <chr>, Physical Address Suffix <chr>,
## #   Physical Post Direction <lgl>, Physical City <chr>, Physical State <chr>,
## #   Physical ZIP <chr>, Physical ZIP 4 <chr>, Key Executive Name <chr>,
## #   First Name <chr>, Middle Initial <chr>, Last Name <chr>, Title <chr>,
## #   Gender <chr>, Location Employee Size <chr>, Corporate Employee Size <chr>,
## #   Revenue / Yr <chr>, Mailing Address <chr>, Mailing Address Number <chr>,
## #   Mailing Pre Direction <chr>, Mailing Address Name <chr>,
## #   Mailing Address Suffix <chr>, Mailing Post Direction <lgl>,
## #   Mailing City <chr>, Mailing State <chr>, Mailing ZIP <chr>,
## #   Mailing ZIP 4 <chr>, Phone <chr>, Fax <chr>, Toll-Free <chr>,
## #   County Name <chr>, County Population <chr>, Metro Area <chr>,
## #   Latitude <chr>, Longitude <chr>, EIN <chr>, Main Line of Business <chr>,
## #   Location Type <chr>, Importer or Exporter <lgl>, Manufacturer <chr>,
## #   Primary SIC <chr>, Primary SIC Description <chr>, SIC02 <chr>,
## #   SIC02.Description <chr>, SIC03 <chr>, SIC03.Description <chr>, SIC04 <chr>,
## #   SIC04.Description <chr>, SIC05 <chr>, SIC05.Description <chr>, SIC06 <chr>,
## #   SIC06.Description <chr>, SIC07 <chr>, SIC07.Description <chr>, SIC08 <chr>,
## #   SIC08.Description <chr>, SIC09 <chr>, SIC09.Description <chr>, SIC10 <chr>,
## #   SIC10.Description <chr>, NAICS 1 <chr>, NAICS 1 Description <chr>,
## #   NAICS 2 <chr>, NAICS 2 Description <chr>, NAICS 3 <chr>,
## #   NAICS 3 Description <chr>, NAICS 4 <chr>, NAICS 4 Description <chr>,
## #   NAICS 5 <chr>, NAICS 5 Description <chr>, NAICS 6 <chr>,
## #   NAICS 6 Description <chr>, NAICS 7 <chr>, NAICS 7 Description <chr>,
## #   NAICS 8 <chr>, NAICS 8 Description <chr>, NAICS 9 <chr>,
## #   NAICS 9 Description <chr>, NAICS 10 <lgl>, NAICS 10 Description <lgl>,
## #   Non-Profit <chr>, Number of PCs <chr>, Public / Private <chr>,
## #   Small Business <chr>, Square Footage <chr>, Website <chr>,
## #   Women Owned <chr>, Year Established <chr>, Ticker Symbol <lgl>,
## #   Stock Exchange <lgl>, Fortune 1000 Ranking <lgl>, Credit Score <chr>,
## #   2019 Revenue/Yr <chr>, 2018 Revenue/Yr <chr>, 2017 Revenue/Yr <chr>,
## #   2018 % Sales Growth <chr>, 2017 % Sales Growth <chr>, 2019 Employees <chr>,
## #   ...

Making sf

dfsf <- st_as_sf(df,coords = c("Longitude", "Latitude"), crs = 4269, agr="constant")

dfsf.proj <- st_transform(dfsf, crs = 2278) #important for analysis is to project data

Buffers

grocerybuff<- st_buffer(dfsf.proj, dist = 2500)
tm_shape(grocerybuff)+
  tm_polygons(alpha = .25) +
tm_shape(dfsf.proj) +
  tm_dots()

Convex Hull of points

chull <-  st_convex_hull(st_union(dfsf.proj)) #need to have st_union to work
tm_shape(chull)+
  tm_polygons()+
  tm_shape(dfsf.proj)+
  tm_dots()

Spatial join

sa_acs<-get_acs(geography = "tract",
                state="TX",
                county = "Bexar", 
                year = 2018,
                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 2014-2018 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%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |============                                                          |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |============================                                          |  41%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |===================================                                   |  51%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  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)
join <- st_join(dfsf.proj, sa_acs2) #adds polygon features to data points (order matters)
## New names:
## * Source...223 -> Source...221
plot(join["punemp"])

Nearest Neighbor

library(spatstat)
## Warning: package 'spatstat' was built under R version 4.0.4
## Loading required package: spatstat.data
## Warning: package 'spatstat.data' was built under R version 4.0.4
## Loading required package: spatstat.geom
## Warning: package 'spatstat.geom' was built under R version 4.0.4
## Registered S3 method overwritten by 'spatstat.geom':
##   method     from
##   print.boxx cli
## spatstat.geom 2.0-1
## Loading required package: spatstat.core
## Loading required package: nlme
## Warning: package 'nlme' was built under R version 4.0.4
## 
## Attaching package: 'nlme'
## The following object is masked from 'package:dplyr':
## 
##     collapse
## Loading required package: rpart
## spatstat.core 2.0-0
## Loading required package: spatstat.linnet
## Warning: package 'spatstat.linnet' was built under R version 4.0.4
## spatstat.linnet 2.0-0
## 
## spatstat 2.0-1       (nickname: 'Caution: contains small parts') 
## For an introduction to spatstat, type 'beginner'
wic.pp<-as.ppp(as(dfsf.proj, "Spatial"))

plot(nearest.neighbour(wic.pp))

wic_nn<-qgis_run_algorithm(alg="native:nearestneighbouranalysis",
         INPUT=dfsf.proj,
        OUTPUT_HTML_FILE=file.path(tempdir(), "wicnn.html"),
         load_output = TRUE)
## Ignoring unknown input 'load_output'
## Running "C:/Program Files/QGIS 3.16/bin/qgis_process-qgis.bat" run \
##   "native:nearestneighbouranalysis" \
##   "--INPUT=C:\Users\victo\AppData\Local\Temp\RtmpYDluoB\fileb84df533db\fileb841adad6b.gpkg" \
##   "--OUTPUT_HTML_FILE=C:\Users\victo\AppData\Local\Temp\RtmpYDluoB/wicnn.html"
## 
## ----------------
## Inputs
## ----------------
## 
## INPUT:   C:\Users\victo\AppData\Local\Temp\RtmpYDluoB\fileb84df533db\fileb841adad6b.gpkg
## OUTPUT_HTML_FILE:    C:\Users\victo\AppData\Local\Temp\RtmpYDluoB/wicnn.html
## 
## 
## 0...10...20...30...40...50...60...70...80...90...100 - done.
## 
## ----------------
## Results
## ----------------
## 
## EXPECTED_MD: 1654.757340237235
## NN_INDEX:    0.8046634820351501
## OBSERVED_MD: 1331.5228033185172
## OUTPUT_HTML_FILE:    C:\Users\victo\AppData\Local\Temp\RtmpYDluoB/wicnn.html
## POINT_COUNT: 59
## Z_SCORE: -2.8703861806878765