【808】R语言实现Hotspot Analysis
参考:Emerging Hot Spot Analysis(前半部分是static hotspot analysis)
参考:dplyr 说明文档
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 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) |
效果:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2019-02-28 【376】COMP 9021 相关笔记(二)