使用 Spatstat 从数据帧渲染点图案在 R/Shiny 中执行时间太长

发布于 2025-01-13 16:40:21 字数 1103 浏览 5 评论 0原文

我正在构建一个闪亮的应用程序来使用 spatstat 包执行空间分析。我没有任何令人兴奋的代码问题。但是渲染地图、点和轮廓需要相当长的时间才能加载到地图上。该数据框有近 87,000 个数据点需要渲染。

我加载 shapefile 并创建 ppp 数据格式的代码如下:

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 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

GRAY°灰色天空 2025-01-20 16:40:21

看来您自己的数据可能位于地理坐标(经度,纬度)中,
这可能会给你的结果带来一些失真。在 spatstat 中一切
假设是平面的,所以你应该真正投影你的多边形和点
在转换为 spatstat 格式之前。

由于我们无法访问您的数据,我在下面制作了一些虚假数据以供参考
一些改进建议。

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

现在,X1 是一个在多边形窗口中包含 87000 个点的 ppp。命令
密度.ppp通过核密度估计来估计点的强度。
您应该只执行此命令一次并保存结果以进行绘图:

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

如果转换为 ppp如果需要更快,您可以添加 check = FALSE
你已经知道所有点都在观察窗口内,所以
代码不需要检查所有 87000 点。比较这些:

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

我在代码中没有看到绘制实际点的任何地方。可以
您使示例可重现并显示结果并清楚地解释
哪一部分你需要更快。

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.

a√萤火虫的光℡ 2025-01-20 16:40:21

尝试使用 data.table::fread("data_illness.csv") 而不是 read.csv("data_illness.csv"),因为读取较大的文件通常速度更快。

另外,预先过滤您的 csv,使其仅包含您使用的列(例如条件、经度、纬度)

您需要加载所有 87,000 个数据点吗?你能先总结一下它们吗,例如,对特定的坐标平方求平均?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文