point pattern analysis in Spatstat

266 views Asked by At

I am having some trouble setting up my data for some point pattern analysis.

What I want to do: conduct a point pattern analysis on NYC arrest data and see if there exists a spatial dependence between arrests and Covid-19 cases.

What I've done so far: downloaded data in the form of shapefiles

https://data.cityofnewyork.us/City-Government/Borough-Boundaries/tqmj-j8zm (the ZIP code boundaries)

https://www1.nyc.gov/site/nypd/stats/crime-statistics/citywide-crime-stats.page (year to date data for arrests in NYC by zip code)

Code:

library(readxl)
library(rgdal) #Brings Spatial Data in R
library(spatstat) # Spatial Statistics
library(lattice) #Graphing
library(maptools)
library(raster)
library(ggplot2)
library(RColorBrewer)
library(broom)

# Load nyc zip code boundary polygon shapefile 
s <- readOGR("/Users/my_name/Documents/fproject/zip","zip")
nyc <- as(s,"owin")

### OGR data source with driver: ESRI Shapefile 
Source: "/Users/my_name/Documents/project/zip", layer: "zip"
with 263 features

# Load nyc arrests point feature shapefile
> s <- readOGR("/Users/my_name/Documents/project/nycarrests/","geo1")

### OGR data source with driver: ESRI Shapefile 
Source: "/Users/my_name/Documents/project/nycarrests", layer: "geo1"
with 103376 features
It has 19 fields

#Converting the dataset into a point pattern
arrests <- as(s,"ppp”) 

### Error in as.ppp.SpatialPointsDataFrame(from) : 
  Only projected coordinates may be converted to spatstat class objects

This gave me the error above.

I know the error has to do with the coordinates not being in the cartesian coordinates. So my question is:

How can I convert my sp object to have (projected) cartesian coordinates in order to convert it to a point pattern (poisson point process)?

1

There are 1 answers

0
Robert Hijmans On

You are looking for spTransform.

Here is some example data

library(raster)
filename <- system.file("external/lux.shp", package="raster")
p <- shapefile(filename)

Solution

utm <- "+proj=utm +zone=32 +datum=WGS84"
x <- spTransform(p, utm)
x
#class       : SpatialPolygonsDataFrame 
#features    : 12 
#extent      : 266045.9, 322163.8, 5481445, 5563062  (xmin, xmax, ymin, ymax)
#crs         : +proj=utm +zone=32 +datum=WGS84 +units=m +no_defs 
#variables   : 5
#names       : ID_1,     NAME_1, ID_2,   NAME_2, AREA 
#min values  :    1,   Diekirch,    1, Capellen,   76 
#max values  :    3, Luxembourg,   12,    Wiltz,  312