空間分析  實習十

助教 杜承軒 2021.05.30

繪製台灣鄉鎮高齡人口比例的主題地圖


*資料前處理

library(sf);library(tmap);library(spdep)
setwd("D:/1092SA/Data")
TW=st_read("Popn_TWN2.shp",options="ENCODING=BIG5")
old=TW$old=TW$A65UP_CNT/(TW$A0A14_CNT+TW$A15A64_CNT+TW$A65UP_CNT)
  1. 原始數值
qtm(TW,'old',fill.title="高齡人口比例")

  1. LISA map
TW.nb = poly2nb(TW) 
TW.nb.w = nb2listw(TW.nb,zero.policy=T)
LISA = localmoran(old, TW.nb.w,zero.policy = T,alternative = "two.sided")
z=LISA[,4]
p=LISA[,5]
diff=old-mean(old)
col=c()
col[z>0 & diff>0]="red"
col[z>0 & diff<0]="blue"
col[z<0 & diff>0]="pink"
col[z<0 & diff<0]="lightblue"
col[p>0.05]="grey90"
TW$colI=col
qtm(TW,'colI')+tm_add_legend("fill",labels=c("HH","LL","HL","LH","NS"),col=c("red","blue","pink","lightblue","grey90"))

  1. Standardized Gi* values
TW.nb.in = include.self(TW.nb)
TW.nb.w.in = nb2listw(TW.nb.in)
TW$Gi = localG(old,TW.nb.w.in)
TW$colG="grey90"
TW$colG[TW$Gi>=qnorm(.95)]="red"
qtm(TW,'colG')+tm_add_legend("fill",labels=c("cluster","no-cluster"),col=c("red","grey90"))

  1. 比較LISA進行FDR校正前後的HH熱區分布
LISA. = localmoran(old, TW.nb.w,zero.policy = T)
p=LISA.[,5]
TW$colHH="grey90"
TW$colHH[p<0.05 & diff>0]="red"

p.adj=p.adjust(p,"fdr")
TW$colHHfdr="grey90"
TW$colHHfdr[p.adj<0.05 & diff>0]="red"

HH=tm_shape(TW,xlim=c(146500,351000),ylim=c(2400000,2850000))+tm_polygons(col='colHH')
HH.fdr=tm_shape(TW,xlim=c(146500,351000),ylim=c(2400000,2850000))+tm_polygons(col='colHHfdr')
tmap_arrange(HH,HH.fdr)

  1. 比較Gi*進行Bonferroni校正前後的熱區分布
TW$colG.="grey90"
TW$colG.[TW$Gi>=qnorm(1-.05/nrow(TW))]="red"
qtm(TW,'colG.')