Plotting Data

## Warning: package 'mapview' was built under R version 4.2.3
## Warning: package 'sf' was built under R version 4.2.3
## Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
## Warning: package 'dplyr' was built under R version 4.2.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
## 2 rows removed to create an sf object. These were addresses that the geocoder could not match.

Mean - Average of Coordinates

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="red")+mapview( results)

Convex Hull Plot

chull <- st_convex_hull(st_union(results))
mapview(chull)+
  mapview(results, col.regions = "green")

Spatial Join

library(tidycensus)
## Warning: package 'tidycensus' was built under R version 4.2.3
library(dplyr)
#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
#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)
mapview(spjoin["ppov"])+mapview(sa_trol)

Spacial Join with Scale and North Arrow

library(tmap)
## Warning: package 'tmap' was built under R version 4.2.3
library(tmaptools)
map5 <- tm_shape(sa_acs2)+
  tm_polygons()+
tm_shape(spjoin)+
  tm_dots("ppov", title="Percent in Poverty",
              palette="Reds",
              style="pretty",
              n=5,
              size=0.3)+
  tm_format("World",
            main.title="San Antonio Poverty Estimates in 2019",
            main.title.position=c('center','top'),
            main.title.size=1.5,
            title=" \nSource: ACS 2019",
            legend.title.size=1.7,
            legend.outside=T,
            legend.text.size=1.2)+
  tm_scale_bar(position = c("left","bottom"))+
  tm_compass()
map5

Nearest Neighbor Analysis & Explanation of Z-Score

#NOTE: this chunk was set to eval=F; it will not run for me. See link above for what Dr. Sparks got.
library(spatstat)
drinking_places.pp<-as.ppp(as(results.proj, "Spatial"))
plot(nearest.neighbour(drinking_places.pp))
library(qgisprocess)
## Using 'qgis_process' at 'C:/Program Files/QGIS 3.28.2/bin/qgis_process-qgis.bat'.
## QGIS version: 3.28.2-Firenze
## Configuration loaded from 'C:\Users\gomez\AppData\Local/R-qgisprocess/R-qgisprocess/Cache/cache-0.0.0.9000.rds'
## Run `qgis_configure(use_cached_data = TRUE)` to reload cache and get more details.
## >>> If you need another installed QGIS version, run `qgis_configure()`;
##     see its documentation if you need to preset the path of qgis_process.
## - Using JSON for input serialization.
## - Using JSON for output serialization.
qgis_configure()
## getOption('qgisprocess.path') was not found.
## Sys.getenv('R_QGISPROCESS_PATH') was not found.
## Trying 'qgis_process' on PATH...
## 'qgis_process' is not available on PATH.
## Found 1 QGIS installation containing 'qgis_process':
##  C:/Program Files/QGIS 3.28.2/bin/qgis_process-qgis.bat
## Trying command 'C:/Program Files/QGIS 3.28.2/bin/qgis_process-qgis.bat'
## Success!
## QGIS version: 3.28.2-Firenze
## Saving configuration to 'C:\Users\gomez\AppData\Local/R-qgisprocess/R-qgisprocess/Cache/cache-0.0.0.9000.rds'
## Metadata of 1265 algorithms queried and stored in cache.
## Run `qgis_algorithms()` to see them.
## - Using JSON for input serialization.
## - Using JSON for output serialization.
algs<-qgis_algorithms()
algs[grepl(pattern = "density", x = algs$algorithm ),]
qgis_show_help("qgis:heatmapkerneldensityestimation")
## Heatmap (Kernel Density Estimation) (qgis:heatmapkerneldensityestimation)
## 
## ----------------
## Description
## ----------------
## Creates a density (heatmap) raster of an input point vector layer using kernel density estimation. Heatmaps allow easy identification of hotspots and clustering of points.
## The density is calculated based on the number of points in a location, with larger numbers of clustered points resulting in larger values.
## 
## 
## ----------------
## Arguments
## ----------------
## 
## INPUT: Point layer
##  Argument type:  source
##  Acceptable values:
##      - Path to a vector layer
## RADIUS: Radius
##  Default value:  100
##  Argument type:  distance
##  Acceptable values:
##      - A numeric value
## RADIUS_FIELD: Radius from field (optional)
##  Argument type:  field
##  Acceptable values:
##      - The name of an existing field
##      - ; delimited list of existing field names
## PIXEL_SIZE: Output raster size
##  Default value:  0.1
##  Argument type:  number
##  Acceptable values:
##      - A numeric value
## WEIGHT_FIELD: Weight from field (optional)
##  Argument type:  field
##  Acceptable values:
##      - The name of an existing field
##      - ; delimited list of existing field names
## KERNEL: Kernel shape
##  Default value:  0
##  Argument type:  enum
##  Available values:
##      - 0: Quartic
##      - 1: Triangular
##      - 2: Uniform
##      - 3: Triweight
##      - 4: Epanechnikov
##  Acceptable values:
##      - Number of selected option, e.g. '1'
##      - Comma separated list of options, e.g. '1,3'
## DECAY: Decay ratio (Triangular kernels only) (optional)
##  Default value:  0
##  Argument type:  number
##  Acceptable values:
##      - A numeric value
## OUTPUT_VALUE: Output value scaling
##  Default value:  0
##  Argument type:  enum
##  Available values:
##      - 0: Raw
##      - 1: Scaled
##  Acceptable values:
##      - Number of selected option, e.g. '1'
##      - Comma separated list of options, e.g. '1,3'
## OUTPUT: Heatmap
##  Argument type:  rasterDestination
##  Acceptable values:
##      - Path for new raster layer
## 
## ----------------
## Outputs
## ----------------
## 
## OUTPUT: <outputRaster>
##  Heatmap
algs[grepl(pattern = "nearest", x = algs$algorithm ),]
qgis_show_help("native:nearestneighbouranalysis")
## Nearest neighbour analysis (native:nearestneighbouranalysis)
## 
## ----------------
## Description
## ----------------
## This algorithm performs nearest neighbor analysis for a point layer.
## 
## The output describes how the data are distributed (clustered, randomly or distributed).
## 
## Output is generated as an HTML file with the computed statistical values.
## 
## ----------------
## Arguments
## ----------------
## 
## INPUT: Input layer
##  Argument type:  source
##  Acceptable values:
##      - Path to a vector layer
## OUTPUT_HTML_FILE: Nearest neighbour (optional)
##  Argument type:  fileDestination
##  Acceptable values:
##      - Path for new file
## 
## ----------------
## Outputs
## ----------------
## 
## OUTPUT_HTML_FILE: <outputHtml>
##  Nearest neighbour
## OBSERVED_MD: <outputNumber>
##  Observed mean distance
## EXPECTED_MD: <outputNumber>
##  Expected mean distance
## NN_INDEX: <outputNumber>
##  Nearest neighbour index
## POINT_COUNT: <outputNumber>
##  Number of points
## Z_SCORE: <outputNumber>
##  Z-score
drinking_places_nn<-qgis_run_algorithm(alg="native:nearestneighbouranalysis",
         INPUT=results.proj,
        OUTPUT_HTML_FILE=file.path(tempdir(), "drinking_places_nn.html"),
         load_output = TRUE)
## Ignoring unknown input 'load_output'
## JSON input ----
## {
##   "inputs": {
##     "INPUT": "C:\\Users\\gomez\\AppData\\Local\\Temp\\RtmpArHUs9\\file64c756bfa\\file64c45101b7e.gpkg",
##     "OUTPUT_HTML_FILE": "C:\\Users\\gomez\\AppData\\Local\\Temp\\RtmpArHUs9/drinking_places_nn.html"
##   }
## }
## 
## Running cmd.exe /c call \
##   "C:/Program Files/QGIS 3.28.2/bin/qgis_process-qgis.bat" --json run \
##   "native:nearestneighbouranalysis" -
drinking_places_nn
## <Result of `qgis_run_algorithm("native:nearestneighbouranalysis", ...)`>
## List of 6
##  $ EXPECTED_MD     : num 2045
##  $ NN_INDEX        : num 1.03
##  $ OBSERVED_MD     : num 2113
##  $ OUTPUT_HTML_FILE: 'qgis_outputHtml' chr "C:\\Users\\gomez\\AppData\\Local\\Temp\\RtmpArHUs9/drinking_places_nn.html"
##  $ POINT_COUNT     : num 21
##  $ Z_SCORE         : num 0.29

According to the NNI of 1.03 the drinking locations are dispersed throughout the three zip codes. The z-score is 0.29 and so it is not significant at the 5% level.

Interpreting the output provided in Blackboard: NNI of 0.43 and a Z-score of -9.8.

Based on the NNI the places mapped are clustered, they are dispursed and based on the z-score of 9.8 shows that it is significant at the 5% interval level.