首页 > 解决方案 > 使用 st_buffer() 和 st_sample() 模拟许多变量会导致 R 内存崩溃

问题描述

在我的示例中,我想使用 st_buffer() 和 st_sample() 创建一个循环过程但没有成功:

library("dplyr")
library("sf")

# Create the bounding box of the area and convert to polygon
bb <- data.frame(lat = c(3541259,3541259,2125022,2125022), long=c(7301161,5839616,5839616,7301161))
bb_sf <- bb %>% st_as_sf(coords=c('long','lat'),
                crs = "+proj=laea +x_0=4600000 +y_0=4600000 +lon_0=29.9 +lat_0=13.4 +datum=WGS84 +units=m") 
pol <- bb_sf %>%
  summarise(geometry = st_combine(bb_sf)) %>%
  st_cast("POLYGON") 

# Create 5 father coordinates 
father_sim <- st_sample(
  x = pol,
  type = "random",
  size = 5 
)


# Create a 10000m buffer around each father and random 20 sons points inside the buffer created
sons_buffer <- st_buffer(father_sim, dist = 10000)
sons_sim <- st_sample(
  x = sons_buffer,
  type = "random",
  size = 20
)

# Vizualize all
plot(pol) # Border
plot(st_geometry(father_sim),col="blue",add=T) # Fathers
plot(st_geometry(sons_sim),col="red",add=T)  # Sons 



# Calculate mean distance of all sons
all_distances <- st_distance(sons_sim)
sum(all_distances)
# 221648260 [m]

花一些时间,但没关系,工作!

但是,如果我尝试让模拟过程也改变父子数量和缓冲区大小:

RES <- NULL

fathers_n <- c(5,10,15,20,25,30)
buffer_dist<- c(500,600,700,800,900,1000)
sons_n <- c(20,30,40,50,60)


for(i in 1:99){
    for(f in 1:length(fathers_n)){
        for(b in 1:length(buffer_dist)){
            for(s in 1:length(sons_n)){

# Create father coordinates 
father_sim <- st_sample(
  x = pol,
  type = "random",
  size = fathers_n[f]
)


# Create a buffer around each father and random sons points inside the buffer created
sons_buffer <- st_buffer(father.sim, dist = buffer_dist[b])
sons_sim <- st_sample(
  x = sons_buffer,
  type = "random",
  size = sons_n[s]
)

# Vizualize all
plot(pol) # Border
plot(st_geometry(father_sim),col="blue",add=T) # Fathers
plot(st_geometry(sons_sim),col="blue",add=T)  # Sons 

# Calculate mean distance of all sons
all_distances <- st_distance(sons_sim)
mean_dist <- sum(all_distances)

#Keep results 
RES <- rbind(RES,c(i,fathers_n[f],buffer_dist[b],sons_n[s],mean_dist))
write.csv(RES,"simulation_results.csv", row.names=FALSE)
}}}}
#

但是,尽管我的电脑有 32GB 的 RAM,但在 9 次模拟之后,R 总是因内存限制而崩溃。有什么办法可以解决吗?回收内存之类的?

我的会话信息:

# R version 4.0.3 (2020-10-10)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 10 x64 (build 19042)

# Matrix products: default

# locale:
# [1] LC_COLLATE=Portuguese_Brazil.1252  LC_CTYPE=Portuguese_Brazil.1252    LC_MONETARY=Portuguese_Brazil.1252
# [4] LC_NUMERIC=C                       LC_TIME=Portuguese_Brazil.1252    

# attached base packages:
# [1] stats     graphics  grDevices utils     datasets  methods   base     

# other attached packages:
#  [1] cluster_2.1.2    car_3.0-11       carData_3.0-4    MASS_7.3-54      lubridate_1.7.10 sfheaders_0.4.0 
#  [7] raster_3.4-13    automap_1.0-14   sp_1.4-5         gstat_2.0-7      sf_1.0-2         dplyr_1.0.7     
# [13] ggplot2_3.3.5   

# loaded via a namespace (and not attached):
#  [1] Rcpp_1.0.7         lattice_0.20-41    FNN_1.1.3          class_7.3-19       zoo_1.8-9          digest_0.6.27     
#  [7] assertthat_0.2.1   utf8_1.2.2         R6_2.5.1           cellranger_1.1.0   plyr_1.8.6         e1071_1.7-8       
# [13] pillar_1.6.2       rlang_0.4.11       curl_4.3.2         readxl_1.3.1       rstudioapi_0.13    data.table_1.14.0 
# [19] labeling_0.4.2     foreign_0.8-81     munsell_0.5.0      proxy_0.4-26       compiler_4.0.3     pkgconfig_2.0.3   
# [25] tidyselect_1.1.1   tibble_3.1.3       intervals_0.15.2   rio_0.5.27         codetools_0.2-18   reshape_0.8.8     
# [31] fansi_0.5.0        spacetime_1.2-5    crayon_1.4.1       withr_2.4.2        grid_4.0.3         gtable_0.3.0      
# [37] lifecycle_1.0.0    DBI_1.1.1          magrittr_2.0.1     units_0.7-2        scales_1.1.1       KernSmooth_2.23-20
# [43] zip_2.2.0          cli_3.0.1          stringi_1.7.3      farver_2.1.0       ellipsis_0.3.2     xts_0.12.1        
# [49] generics_0.1.0     vctrs_0.3.8        openxlsx_4.2.4     tools_4.0.3        forcats_0.5.1      glue_1.4.2        
# [55] purrr_0.3.4        hms_1.1.0          abind_1.4-5        colorspace_2.0-2   classInt_0.4-3     pbdZMQ_0.3-5      
# [61] haven_2.4.3 

标签: rsfspspatstat

解决方案


推荐阅读