如何计算犯罪密度?


無口君
2025-03-11 11:23:20 (5天前)
  1. 总体目标:计算美国城市网格结构中的犯罪密度。每个网格方格应为100平方米。我有一个数据框crime.inc列出了个人犯罪实例latlon; ...

3 条回复
  1. 0# 晴天?霹雳 | 2019-08-31 10-32



    “0.001与纬度/经度坐标相关的约为100m”的假设可能无法保持。距离取决于您所在的世界,但使用您所在地区的示例数据:




    1. library(sf)

    2. adjust latitude by 0.001

      df <- data.frame(lat = c(45.123, 45.124), lon = c(-122.789, -122.789))
      df.sf <- st_as_sf(df, coords = c(“lon”, lat”), crs = 4326)

    3. st_distance(df.sf)
      Units: m
      [,1] [,2]
      [1,] 0.0000 111.1342
      [2,] 111.1342 0.0000

    4. Or, if we adjust the longitude by 0.001:

      df <- data.frame(lat = c(45.123, 45.123), lon = c(-122.789, -122.790))
      df.sf <- st_as_sf(df, coords = c(“lon”, lat”), crs = 4326)

    5. st_distance(df.sf)
      Units: m
      [,1] [,2]
      [1,] 0.00000 78.67796
      [2,] 78.67796 0.00000

    6. </code>


    以下是使用您的问题的替代解决方案

    sf

    包:



    1.   # add a few more points to make it more interesting
    2. df <- data.frame(id = c(1001, 1002, 1003, 1004, 1005),
      lat = c(45.123, 45.123, 45.126, 45.121, 45.130),
      lon = c(-122.456, -122.457, -122.444, -122.442, -122.445))

    3. convert to an sf object and set projection (crs) to 4326 (lon/lat)

      df.sf <- st_as_sf(df, coords = c(“lon”, lat”), crs = 4326)

    4. transform to UTM (Zone 10) for distance

      df.utm <- st_transform(df.sf, “+proj=utm +zone=10 +datum=WGS84 +units=m +no_defs”)

    5. create a 100m grid on these points

      grid.100 <- st_make_grid(x = df.utm, cellsize = c(100, 100))

    6. plot to make sure

      library(ggplot2)
      ggplot() +
      geom_sf(data = df.utm, size = 3) +
      geom_sf(data = grid.100, alpha = 0)

    7. </code>




    1. #将grid转换为sf(不是sfc)并添加id
    2. grid.sflt; - st_sfgrid.100
    3. grid.sf $ idlt; - 1nrowgrid.sf



    1.   # find how many points intersect each grid cell by using lengths() to get the number of points that intersect each grid square
    2. grid.sf$count <- st_intersects(grid.sf, df.utm) %>% lengths()

    3. </code>


    情节检查




    1. ggplot() +
      geom_sf(data = grid.sf, alpha = 0.5, aes(fill = as.factor(count))) +
      geom_sf(data = df.utm, size = 3) +
      scale_fill_discrete(“Number of Points”)

    2. </code>






  2. 1# 晴天 | 2019-08-31 10-32



    对于问题上的数据,lat和lon只有三位小数。因此,您可以简单地使用dplyr按位置分组,而不需要使用GIS包。




    1. library(dplyr)
      densities <- crime.inc %>% group_by(lat,lon) %>%
      summarise(count=n())

    2. </code>


    这样你就会失去身份证。如果你想保留ID




    1. library(dplyr)
      densities <- crime.inc %>% group_by(lat,lon) %>%
      rename(count=n())

    2. </code>

登录 后才能参与评论