0

I am building a shiny app to perform spatial analysis using the spatstat package. I do not have any issue exciting the code. But rendering the map, points and contours are taking quite a long time to load on the map. The data frame has almost 87,000 data points to render.

My code to load the shapefile and create ppp data format is as below:

library(sf)
library(spatstat)
library(sp)
library(rgdal)
library(maptools)
library(ggmap)
library(RColorBrewer)

s.sf <- readShapeSpatial("india.shp")
s.owin <- as(s.sf, "owin")

breakpoints <- c(-50,-40,-30,-20,-10,0,100,200,300,400,500,600,800,1000,1200)
colors<- c("#08519c","#3182bd","#9ecae1","#c6dbef","#f7f7f7","#fff5f0","#fee0d2","#fcbba1","#fc9272","#fb6a4a","#ef3b2c","#cb181d","#a50f15","#67000d")
    

# The data here has 87,000 rows post filtering
df <- read.csv("data_illness.csv")
df <- df[df$condition == 1,]

# taking look long to create ppp data format
data.ppp <- ppp(x = df$Longitude, y = df$Latitude, window = s.owin, marks = df$condition) 
plot(density(data.ppp, sigma = 1), col=colors, breaks = breakpoints, pch=".")
contour(density(data.ppp, 1), axes = F, add=TRUE, pch=".")
plot(s.owin, add=TRUE, las=1, pch=".")

I need an alternative/efficient way to run the code so the loading of the map is quick on the shiny app.

I am stuck with this and really need help here!

2 Answers2

1

It appears that your own data may be in geographic coordinates (lon,lat), which may give a bit of distortion to your results. In spatstat everything is assumed planar, so you should really project your polygon and points before converting to spatstat format.

Since we don’t have access to your data I make some fake data below to give a few suggestions for improvements.

library(spatstat)
W1 <- Window(chorley)
set.seed(42)
X1 <- runifpoint(87000, win = W1)

Now X1 is a ppp with 87000 points in a polygonal window. The command density.ppp estimates the intensity of points by kernel density estimation. You should only excecute this command once and save the results for plotting:

inten1 <- density(X1, sigma = 1)
plot(inten1)
contour(inten1, add = TRUE)

If the conversion to ppp needs to be faster you can add check = FALSE if you already know that all points are inside the window of observation, so the code doesn’t need to check this for all 87000 points. Compare these:

df1 <- coords(X1)

t0 <- proc.time()
X1 <- ppp(df1$x, df1$y, window = W1)
proc.time()-t0
#>    user  system elapsed 
#>   0.096   0.000   0.096

t0 <- proc.time()
X1 <- ppp(df1$x, df1$y, window = W1, check = FALSE)
proc.time()-t0
#>    user  system elapsed 
#>   0.002   0.000   0.003

I don’t see anywhere in your code where you plot the actual points. Could you make your example reproducible and show the results and explain clearly which part you need to be faster.

Ege Rubak
  • 4,347
  • 1
  • 10
  • 18
0

Try data.table::fread("data_illness.csv") instead of read.csv("data_illness.csv") as it is often faster to read larger files.

Also, pre-filter your csv to only contain the columns you use (e.g. condition, Longitude, Latitude)

Do you need to load all 87,000 data points? Can you summarise them first, for example, averaging specific coordinate squares?

psychonomics
  • 714
  • 4
  • 12
  • 26