1

I have a dataset of spatial locations data. I want to do a point pattern analysis using the spatstat package in R using this data. I want the best polygon area for the analysis instead of the rectangle area. The code I have is

original_data = read.csv("/home/hudamoh/PhD_Project_Moh_Huda/Dataset_files/my_coordinates.csv")
plot(original_data$row, original_data$col)

which results in a plot that looks like this original_data

Setting the data for point pattern data

point_pattern_data = ppp(original_data$row, original_data$col, c(0, 77), c(0, 116))
plot(point_pattern_data)
summary(point_pattern_data)

resulting in a plot that looks like this point_pattern

#The observed data has considerably wide white spaces, which I want to remove for a better analysis area. Therefore, I want to make the point pattern a polygon instead of a rectangle. The vertices for the polygon are the pairs of (x,y) below to avoid white space as much as possible.

x = c(3,1,1,0.5,0.5,1,2,2.5,5.5, 16,21,28,26,72,74,76,75,74,63,58,52,47,40)
y = c(116,106,82.5,64,40,35,25,17.5,5,5,5,10,8,116,100,50,30,24,17,10,15,15,8)

I find these vertices above manually by considering the plot below (with the grid lines)

plot(original_data$row,original_data$col)
grid(nx = 40, ny = 25,
 lty = 2,      # Grid line type
 col = "gray", # Grid line color
 lwd = 2)      # Grid line width

plot_grid

So I want to make the point pattern polygon. The code is

my_data_poly = owin(poly = list(x = c(3,1,1,0.5,0.5,1,2,2.5,5.5, 16,21,28,26,72,74,76,75,74,63,58,52,47,40), y = c(116,106,82.5,64,40,35,25,17.5,5,5,5,10,8,116,100,50,30,24,17,10,15,15,8)))
plot(my_data_poly)

but it results in an error. The error is

poly_error

I fix it by

my_data_poly = owin(poly = list(x = c(116,106,82.5,64,40,35,25,17.5,5,5,5,10,8,116,100,50,30,24,17,10,15,15,8), y = c(3,1,1,0.5,0.5,1,2,2.5,5.5, 16,21,28,26,72,74,76,75,74,63,58,52,47,40)))
plot(my_data_poly)

It results in a plot enter image description here

However, this is not what I want. How to get the observed area as a polygon in point pattern data analysis?

MK Huda
  • 605
  • 1
  • 6
  • 16

2 Answers2

1

This should be a reasonable solution to the problem.

require(sp)

poly = Polygon(
cbind(original_data$col,    
original_data$row)
    ))

This will create a polygon from your points. You can use this document to understand the sp package better

sconfluentus
  • 4,693
  • 1
  • 21
  • 40
  • I tried this, and I got the polygon class. However, I cannot use function plot() directly to get the plot. Is it supposed to result in a polygon plot with all points as vertices? I am looking for a polygon with a number of vertices are as I mentioned (23) with vertices coordinates, as I mentioned. I am not familiar with the package but I will check – MK Huda Nov 02 '22 at 00:47
1

We don’t have access to the point data you read in from file, but if you just want to fix the polygonal window that is not a problem.

You need to traverse the vertices of your polygon sequentially and anti-clockwise. The code connects the first point you give to the next etc. Your vertices are:

library(spatstat)
x = c(3,1,1,0.5,0.5,1,2,2.5,5.5, 16,21,28,26,72,74,76,75,74,63,58,52,47,40)
y = c(116,106,82.5,64,40,35,25,17.5,5,5,5,10,8,116,100,50,30,24,17,10,15,15,8)
vert <- ppp(x, y, window = owin(c(0,80),c(0,120)))
plot.ppp(vert, main = "", show.window = FALSE, chars = NA)
text(vert)

Point number 13 is towards the bottom left and 14 in the top right, which gives the funny crossing in the polygon. Moving the order around seems to help:

xnew <- c(x[1:11], x[13:12], x[23:14])
ynew <- c(y[1:11], y[13:12], y[23:14])
p <- owin(poly = cbind(xnew, ynew))
plot(p, main = "")

It is unclear from your provided plot of the data that you really should apply point pattern analysis. The main assumption underlying point process modelling as implemented in spatstat is that the locations of events (points) are random and the process that generated the random locations is of interest. Your points seem to be on a grid and maybe you need another tool for your analysis. Of course spatstat has a lot of functionality for simply handling and summarising data like this so you may still find useful tools in there.

Ege Rubak
  • 4,347
  • 1
  • 10
  • 18
  • Yes, this is partially what I want. The polygon window is exactly what I am looking for. I made a mistake in the order, as you found. After creating this polygon window (instead of a rectangle), I want to include all points (the coordinates are in the data mentioned on the link, i.e. x = original_data$row and y = original_data$col). So, the point pattern will be all these points inside the polygon. I still cannot find a way. Is there a way to include them inside the polygon? – MK Huda Nov 02 '22 at 12:39
  • You are correct, the locations of the events are randomly generated by the Poisson point process, but it will be the following analysis. My data is somehow complicated, so I want to do it step by step since I am new to this analysis and the spatstat as well – MK Huda Nov 02 '22 at 12:46
  • so basically in this code 'ppp(x, y, window = owin(c(0,80),c(0,120)))', my goal is that the x = original_data$row and y = original_data$col, and the window is the polygon – MK Huda Nov 02 '22 at 12:48
  • I found it now actually. `xnew <- c(x[1:11], x[13:12], x[23:14]) ynew <- c(y[1:11], y[13:12], y[23:14]) p <- owin(poly = cbind(xnew, ynew)) point_pattern = ppp(x = original_data$row and y = original_data$col, p) plot(point_pattern, main = "Spatial point pattern")` – MK Huda Nov 02 '22 at 14:01