alex_bn_lee

导航

【808】R语言实现Hotspot Analysis

参考:Emerging Hot Spot Analysis(前半部分是static hotspot analysis)

参考:dplyr 说明文档


代码:

library (sfdep)
library (dplyr)

# %>% is the same as |>

# guerry is a dataset from sfdep
# mutate: Create, modify, and delete columns (keep all columns)
# include_self: Includes self in neighbor list
# st_contiguity: Identify polygon neighbors
# st_weights: Calculate spatial weights

guerry_nb <- guerry %>%
  mutate (
    nb =  include_self ( st_contiguity (geometry)),
    wt =  st_weights (nb)
  ) 

# transmute: Create, modify, and delete columns (only keep new columns)
# local_gstar_perm: Local G*
# tidyr::unnest: one column -> many detailed columns (gi*, p score, etc.)

donat_gistar <- guerry_nb %>%
  transmute (gi_star =  local_gstar_perm (donations, nb, wt, nsim = 199)) |> 
  tidyr:: unnest (gi_star)

# case_when: A general vectorised if
# create a new column called cluster, which stores different categories of hotspot results

hotspot_results <- donat_gistar %>%
  mutate (cluster =  case_when (
    p_folded_sim > 0.1 ~  "Not Significant" ,
    p_folded_sim <= 0.01 & gi_star < 0 ~  "Cold Spot - 99% Confidence" ,
    p_folded_sim <= 0.01 & gi_star > 0 ~  "Hot Spot - 99% Confidence" ,
    p_folded_sim <= 0.05 & gi_star < 0 ~  "Cold Spot - 95% Confidence" ,
    p_folded_sim <= 0.05 & gi_star > 0 ~  "Hot Spot - 95% Confidence" ,
    p_folded_sim <= 0.1 & gi_star < 0 ~  "Cold Spot - 90% Confidence" ,
    p_folded_sim <= 0.1 & gi_star > 0 ~  "Hot Spot - 90% Confidence"
  )) 

# tm_shape: Specify the shape object
# tm_polygons: Draw polygons
# "cluster" is a column of hotspot_results

tm_shape (hotspot_results, name =  "Hotspot Detection" ) +
  tm_polygons ( "cluster" , title =  "Hotspot Category" ,
              palette =  c ( "Hot Spot - 99% Confidence" =  "#d62a24" ,
                          "Hot Spot - 95% Confidence" =  "#ed7551" ,
                          "Hot Spot - 90% Confidence" =  "#f6b985" ,
                          "Not Significant" =  "#fcfebf" ,
                          "Cold Spot - 90% Confidence" =  "#c0cdbe" ,
                          "Cold Spot - 95% Confidence" =  "#859fba" ,
                          "Cold Spot - 99% Confidence" =  "#4576b5" ),
             
          alpha = 0.7, lwd = 0.8)

效果:

 

posted on 2023-02-28 06:34  McDelfino  阅读(175)  评论(0)    收藏  举报