-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFINAL_submit.Rmd
More file actions
806 lines (611 loc) · 26.6 KB
/
Copy pathFINAL_submit.Rmd
File metadata and controls
806 lines (611 loc) · 26.6 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
---
title: "Spatial Analysis Final"
author: "Ryan Abigan、呂沐田、陳宜安、姚如謙、黃仁輔、劉昱"
date: "`r Sys.Date()`"
output:
html_notebook:
toc: true
toc_depth: 6
toc_float: true
---
```{r include=FALSE}
library(sf)
library(tmap)
library(dplyr)
library(grid)
library(units)
library(ggplot2)
library(readODS)
library(spdep)
```
### Reading Data
```{r message=FALSE}
rm(list = ls())
# The Taipei data provided in class
tpe_vill_sf = st_read("C:/113-2_Spring/Spatial_Analysis/coursefiles/Taipei_Vill/Taipei_Vill.shp", options = "ENCODING=big5")
# Population data
tpe_pop_df = read.csv("C:/113-2_Spring/Spatial_Analysis/final_project/0608_Plotting/taipei_vill_pop.csv", fileEncoding = "big5")
# All schools in Taiwan
schools <- read.csv("C:/113-2_Spring/Spatial_Analysis/final_project/0608_Plotting/school_112_csv.csv",
fileEncoding = "BIG5")
## Select those whose coordinate is not missing
schools_clean <- schools %>%
filter(!is.na(`X.坐標`) & !is.na(`Y.坐標`))
## Turning into sf
schools_sf <- st_as_sf(schools_clean,
coords = c("X.坐標", "Y.坐標"),
crs = 3826) # EPSG:3826 是 TWD97 二度分帶
# Sportfield Data
sportfield_sf = st_read("C:/113-2_Spring/Spatial_Analysis/final_project/0608_Plotting/sportfield_taipei/sportfield_taipei.shp")
# Gaming places data
gaming_sf = st_read("C:/113-2_Spring/Spatial_Analysis/final_project/0608_Plotting/Gaming_center/Gaming_center.shp")
# Hospital data
hospital_sf = st_read("C:/113-2_Spring/Spatial_Analysis/final_project/0608_Plotting/Hospital_taipei/Hospital_taipei.shp")
# fastfood data
fastfood_sf = st_read("C:/113-2_Spring/Spatial_Analysis/final_project/0608_Plotting/Tpe_Fastfood/Tpe_Fastfood.shp")
# park data
park_sf = st_read("C:/113-2_Spring/Spatial_Analysis/final_project/0608_Plotting/Park_centroids/Park_centroids.shp")
```
```{r}
# Selecting population data
tpe_pop_df = tpe_pop_df[tpe_pop_df$性別 == "計" & tpe_pop_df$區域代碼 > 100000000 & tpe_pop_df$年份 == 113 & tpe_pop_df$月份 == 2, ]
tpe_vill_sf$區域代碼 = as.numeric(tpe_vill_sf$TOWN_ID) * 1000 + as.numeric(tpe_vill_sf$VILLAGE_ID)
```
### Calculation
```{r}
# Calculations on population
tpe_pop_df$age_1_6 = rowSums(tpe_pop_df[8:13])
tpe_pop_df$age_7_12 = rowSums(tpe_pop_df[14:19])
tpe_pop_df$adult = rowSums(tpe_pop_df[26:71])
# primary_ratio and secondary ratio
tpe_pop_df$primary_ratio = tpe_pop_df$age_1_6 / tpe_pop_df$adult
tpe_pop_df$secondary_ratio = tpe_pop_df$age_7_12 / tpe_pop_df$adult
tpe_vill_sf = merge(tpe_vill_sf, tpe_pop_df, by = "區域代碼")
```
### Selecting Schools
```{r}
# 選取臺北的國中
schools_sf = schools_sf %>%
filter(學校級別 %in% c("國民中學", "附設國民中學")) %>%
filter(縣市別 == "臺北市")
# 選取「北市公立五虎 + 景美」
five_tigers_sf = schools_sf %>%
filter(學校名稱 %in% c("市立中正國中", "市立龍門國中", "市立敦化國中", "市立介壽國中", "市立金華國中", "市立景美國中"))
# 選取其他建北率高的學校
other_top15_sf = schools_sf %>%
filter(學校名稱 %in% c("國立師大附中附設國中", "私立靜心高中附設國中", "私立延平中學附設國中", "市立大安國中", "市立麗山國中","市立南門國中", "市立天母國中", "市立興雅國中", "市立石牌國中"))
# 建立簡化名稱欄位
five_tigers_sf$school_short <- recode(five_tigers_sf$學校名稱,
"市立中正國中" = "中正",
"市立龍門國中" = "龍門",
"市立敦化國中" = "敦化",
"市立介壽國中" = "介壽",
"市立金華國中" = "金華",
"市立景美國中" = "景美")
```
### LISA of primary ratio (1\~6)
```{r message=FALSE, warning=FALSE}
nb = poly2nb(tpe_vill_sf, queen = FALSE)
listw = nb2listw(nb, style = "W", zero.policy = TRUE)
density = as.vector(tpe_vill_sf$primary_ratio)
lisa = localmoran(density, listw = listw, zero.policy = TRUE)
tpe_vill_sf$local_I_pri = lisa[, 1]
tpe_vill_sf$z_score_pri = lisa[, 4]
tpe_vill_sf$p_value_pri = lisa[, 5]
quadrant = attr(lisa, "quadr")$mean
quadrant = factor(quadrant, levels = c(levels(quadrant), "NoSig"))
signif_level_05 = 0.05
quadrant[lisa[, 5] > signif_level_05] = "NoSig"
tpe_vill_sf$LISA_type_pri = quadrant
lisa_colors <- c(
'High-High' = 'red',
'Low-Low' = 'blue',
'High-Low' = 'pink',
'Low-High' = 'skyblue2',
'NoSig' = 'grey'
)
tm_shape(tpe_vill_sf) +
tm_polygons("LISA_type_pri",
palette = lisa_colors,
title = "LISA Cluster Type (age 1~6 / adult)") +
tm_layout(title = "LISA Map for Age 1~6 / Adult", legend.outside = TRUE)
```
### LISA of secondary ratio (7\~12)
```{r message=FALSE, warning=FALSE}
nb = poly2nb(tpe_vill_sf, queen = FALSE)
listw = nb2listw(nb, style = "W", zero.policy = TRUE)
density = as.vector(tpe_vill_sf$secondary_ratio)
lisa = localmoran(density, listw = listw, zero.policy = TRUE)
tpe_vill_sf$local_I_sec = lisa[, 1]
tpe_vill_sf$z_score_sec = lisa[, 4]
tpe_vill_sf$p_value_sec = lisa[, 5]
quadrant = attr(lisa, "quadr")$mean
quadrant = factor(quadrant, levels = c(levels(quadrant), "NoSig"))
signif_level = 0.05
quadrant[lisa[, 5] > signif_level] = "NoSig"
tpe_vill_sf$LISA_type_sec = quadrant
tm_shape(tpe_vill_sf) +
tm_polygons("LISA_type_sec",
palette = lisa_colors,
title = "LISA Cluster Type (age 7~12 / adult)") +
tm_layout(title = "LISA Map for Age 7~12 / Adult", legend.outside = TRUE)
```
```{r}
density_sec = as.vector(tpe_vill_sf$secondary_ratio)
lisa_sec = localmoran(density_sec, listw = listw, zero.policy = TRUE)
tpe_vill_sf$local_I_sec = lisa_sec[, 1]
tpe_vill_sf$z_score_sec = lisa_sec[, 4]
tpe_vill_sf$p_value_sec = lisa_sec[, 5]
quadrant_sec = attr(lisa_sec, "quadr")$mean
quadrant_sec = factor(quadrant_sec, levels = c(levels(quadrant_sec), "NoSig"))
# 使用 FDR 校正
quadrant_sec[p.adjust(lisa_sec[, 5], method = "fdr") > signif_level] = "NoSig"
tpe_vill_sf$LISA_type_sec = quadrant_sec
# 繪圖(含學校點位)
tm_shape(tpe_vill_sf) +
tm_polygons("LISA_type_sec",
palette = lisa_colors,
title = "LISA Cluster Type (age 7~12 / adult)") +
tm_layout(title = "LISA Map for Age 7~12 / Adult", legend.outside = TRUE)
```
### Displaying the five tigers on the FDRd map
```{r message=FALSE, warning=FALSE}
names(grDevices::windowsFonts()) # Windows 專用
# 手動為每個學校加上偏移值(你可以依照實際需求微調)
five_tigers_sf <- five_tigers_sf %>%
mutate(
xmod = c(0.3, -0.5, 0, 1, -1, 1), # X 偏移
ymod = c(0.8, 0.8, 0.8, 0.8, 0.8, 0.8) # Y 偏移
)
tm_shape(tpe_vill_sf) +
tm_polygons("LISA_type_sec",
palette = lisa_colors,
title = "LISA Cluster Type (age 7~12 / adult)") +
# 其他建北率高的學校
tm_shape(other_top15_sf) +
tm_dots(size = 0.5, col = "gray", shape = 21, border.col = "white") +
# 公立五虎
tm_shape(five_tigers_sf) +
tm_dots(size = 0.5, col = "black", shape = 21, border.col = "white") +
# 主文字
tm_text("school_short",
size = 0.9,
xmod = "xmod",
ymod = "ymod",
col = "black",
fontfamily = "Noto Sans TC",
fontface = "bold") +
tm_layout(title = "LISA Map for Age 7~12 / Adult",
legend.outside = TRUE)
```
## Environment
### Gaming Center
```{r message=FALSE, warning=FALSE}
# Setting sig_level
signif_gaming = 0.01
gaming_sf <- st_transform(gaming_sf, st_crs(tpe_vill_sf))
joined_gaming <- st_join(gaming_sf, tpe_vill_sf, join = st_within)
gaming_counts <- joined_gaming %>%
group_by(TOWN_ID) %>%
summarise(gaming_count = n())
gaming_counts_df <- st_drop_geometry(gaming_counts)
tpe_vill_sf$gaming_count <- gaming_counts_df$gaming_count[match(tpe_vill_sf$TOWN_ID, gaming_counts_df$TOWN_ID)]
tpe_vill_sf$gaming_count[is.na(tpe_vill_sf$gaming_count)] <- 0
tpe_vill_sf$AREA <- set_units(st_area(tpe_vill_sf), "km^2")
tpe_vill_sf$gaming_density <- tpe_vill_sf$gaming_count / tpe_vill_sf$AREA
coords <- st_coordinates(st_centroid(tpe_vill_sf))
nb <- dnearneigh(coords, d1 = 0, d2 = 10000)
dist_list <- nbdists(nb, coords)
inv_dist_list <- lapply(dist_list, function(x) {
y <- 1 / (x + 0.0001)
return(y)
})
listw <- nb2listw(nb, glist = inv_dist_list, style = "W", zero.policy = TRUE)
density <- as.vector(tpe_vill_sf$gaming_density)
global_moran <- moran.test(density, listw = listw, zero.policy = TRUE)
print(global_moran)
lisa <- localmoran(density, listw = listw, zero.policy = TRUE)
tpe_vill_sf$local_I <- lisa[, 1]
tpe_vill_sf$z_score <- lisa[, 4]
tpe_vill_sf$p_value <- lisa[, 5]
quadrant <- attr(lisa, "quadr")$mean
quadrant <- factor(quadrant, levels = c(levels(quadrant), "NoSig"))
tpe_vill_sf$pvalue_FDR <- p.adjust(lisa[, 5], method = "fdr")
tpe_vill_sf$LISA_type_Gaming <- "NoSig"
tpe_vill_sf$LISA_type_Gaming[tpe_vill_sf$pvalue_FDR < signif_gaming & tpe_vill_sf$local_I > 0 & quadrant == "High-High"] <- "High-High"
tpe_vill_sf$LISA_type_Gaming[tpe_vill_sf$pvalue_FDR < signif_gaming & tpe_vill_sf$local_I > 0 & quadrant == "Low-Low"] <- "Low-Low"
tpe_vill_sf$LISA_type_Gaming[tpe_vill_sf$pvalue_FDR < signif_gaming & tpe_vill_sf$local_I < 0 & quadrant == "High-Low"] <- "High-Low"
tpe_vill_sf$LISA_type_Gaming[tpe_vill_sf$pvalue_FDR < signif_gaming & tpe_vill_sf$local_I < 0 & quadrant == "Low-High"] <- "Low-High"
tpe_vill_sf$LISA_type_Gaming <- factor(tpe_vill_sf$LISA_type_Gaming,
levels = c("High-High", "Low-Low", "High-Low", "Low-High", "NoSig"))
lisa_colors <- c(
'High-High' = 'red',
'Low-Low' = 'blue',
'High-Low' = 'pink',
'Low-High' = 'skyblue2',
'NoSig' = 'grey'
)
tm_shape(tpe_vill_sf) +
tm_polygons("LISA_type_Gaming",
palette = lisa_colors,
title = "LISA Cluster Type (Sport Facilities)") +
# 其他建北率高的學校
tm_shape(other_top15_sf) +
tm_dots(size = 0.5, col = "gray", shape = 21, border.col = "white") +
# 五虎
tm_shape(five_tigers_sf) +
tm_dots(size = 0.5, col = "black", shape = 21, border.col = "white") +
# 主文字
tm_text("school_short",
size = 0.9,
xmod = "xmod",
ymod = "ymod",
col = "black",
fontfamily = "Noto Sans TC",
fontface = "bold") +
tm_layout(title = "LISA Map (Inverse Distance) - Gaming Centers",
legend.outside = TRUE)
```
### fastfood
```{r}
# 設定信心水準
signif_fastfood <- 0.05
# 投影一致
fastfood_sf <- st_transform(fastfood_sf, st_crs(tpe_vill_sf))
# 空間連接:找出每個村里有幾個 fast food 點
joined_fastfood <- st_join(fastfood_sf, tpe_vill_sf, join = st_within)
fastfood_counts <- joined_fastfood %>%
group_by(TOWN_ID) %>%
summarise(fastfood_count = n())
fastfood_counts_df <- st_drop_geometry(fastfood_counts)
tpe_vill_sf$fastfood_count <- fastfood_counts_df$fastfood_count[match(tpe_vill_sf$TOWN_ID, fastfood_counts_df$TOWN_ID)]
tpe_vill_sf$fastfood_count[is.na(tpe_vill_sf$fastfood_count)] <- 0
# 面積與密度
tpe_vill_sf$AREA <- set_units(st_area(tpe_vill_sf), "km^2")
tpe_vill_sf$fastfood_density <- tpe_vill_sf$fastfood_count / tpe_vill_sf$AREA
# 鄰近矩陣設定
coords <- st_coordinates(st_centroid(tpe_vill_sf))
nb <- dnearneigh(coords, d1 = 0, d2 = 10000)
dist_list <- nbdists(nb, coords)
inv_dist_list <- lapply(dist_list, function(x) 1 / (x + 0.0001))
listw <- nb2listw(nb, glist = inv_dist_list, style = "W", zero.policy = TRUE)
# 執行 LISA
density <- as.vector(tpe_vill_sf$fastfood_density)
global_moran <- moran.test(density, listw = listw, zero.policy = TRUE)
print(global_moran)
lisa <- localmoran(density, listw = listw, zero.policy = TRUE)
tpe_vill_sf$local_I <- lisa[, 1]
tpe_vill_sf$z_score <- lisa[, 4]
tpe_vill_sf$p_value <- lisa[, 5]
quadrant <- attr(lisa, "quadr")$mean
quadrant <- factor(quadrant, levels = c(levels(quadrant), "NoSig"))
tpe_vill_sf$pvalue_FDR <- p.adjust(lisa[, 5], method = "fdr")
tpe_vill_sf$LISA_type_Fastfood <- "NoSig"
tpe_vill_sf$LISA_type_Fastfood[tpe_vill_sf$pvalue_FDR < signif_fastfood & tpe_vill_sf$local_I > 0 & quadrant == "High-High"] <- "High-High"
tpe_vill_sf$LISA_type_Fastfood[tpe_vill_sf$pvalue_FDR < signif_fastfood & tpe_vill_sf$local_I > 0 & quadrant == "Low-Low"] <- "Low-Low"
tpe_vill_sf$LISA_type_Fastfood[tpe_vill_sf$pvalue_FDR < signif_fastfood & tpe_vill_sf$local_I < 0 & quadrant == "High-Low"] <- "High-Low"
tpe_vill_sf$LISA_type_Fastfood[tpe_vill_sf$pvalue_FDR < signif_fastfood & tpe_vill_sf$local_I < 0 & quadrant == "Low-High"] <- "Low-High"
tpe_vill_sf$LISA_type_Fastfood <- factor(tpe_vill_sf$LISA_type_Fastfood,
levels = c("High-High", "Low-Low", "High-Low", "Low-High", "NoSig"))
# 配色
lisa_colors <- c(
'High-High' = 'red',
'Low-Low' = 'blue',
'High-Low' = 'pink',
'Low-High' = 'skyblue2',
'NoSig' = 'grey'
)
# 畫出地圖
tm_shape(tpe_vill_sf) +
tm_polygons("LISA_type_Fastfood",
palette = lisa_colors,
title = "LISA Cluster Type (Fast Food)") +
# 其他建北率高的學校
tm_shape(other_top15_sf) +
tm_dots(size = 0.5, col = "gray", shape = 21, border.col = "white") +
# 五虎
tm_shape(five_tigers_sf) +
tm_dots(size = 0.5, col = "black", shape = 21, border.col = "white") +
# 標上學校名稱
tm_text("school_short",
size = 0.9,
xmod = "xmod",
ymod = "ymod",
col = "black",
fontfamily = "Noto Sans TC",
fontface = "bold") +
tm_layout(title = "LISA Map (Inverse Distance) - Fast Food",
legend.outside = TRUE)
```
### intersecting
```{r}
# 建立交集標記欄位
tpe_vill_sf$HH_Intersect <- ifelse(
tpe_vill_sf$LISA_type_Fastfood == "High-High" &
tpe_vill_sf$LISA_type_Gaming == "High-High",
"High-High Both",
"Other"
)
# 設定顏色
intersect_colors <- c("High-High Both" = "purple", "Other" = "lightgrey")
# 畫出地圖
tm_shape(tpe_vill_sf) +
tm_polygons("HH_Intersect",
palette = intersect_colors,
title = "High-High Intersection") +
# 其他建北率高的學校
tm_shape(other_top15_sf) +
tm_dots(size = 0.5, col = "gray", shape = 21, border.col = "white") +
# 五虎
tm_shape(five_tigers_sf) +
tm_dots(size = 0.5, col = "black", shape = 21, border.col = "white") +
# 標上學校名稱
tm_text("school_short",
size = 0.9,
xmod = "xmod",
ymod = "ymod",
col = "black",
fontfamily = "Noto Sans TC",
fontface = "bold") +
tm_layout(title = "Intersection of High-High Clusters: Fast Food & Gaming Centers",
legend.outside = TRUE)
```
### sportfield
```{r message=FALSE, warning=FALSE}
sportfield_sf <- st_transform(sportfield_sf, st_crs(tpe_vill_sf))
joined_sportfield <- st_join(sportfield_sf, tpe_vill_sf, join = st_within)
sportfield_counts <- joined_sportfield %>%
group_by(TOWN_ID) %>%
summarise(sportfield_count = n())
sportfield_counts_df <- st_drop_geometry(sportfield_counts)
tpe_vill_sf$sportfield_count <- sportfield_counts_df$sportfield_count[match(tpe_vill_sf$TOWN_ID, sportfield_counts_df$TOWN_ID)]
tpe_vill_sf$sportfield_count[is.na(tpe_vill_sf$sportfield_count)] <- 0
tpe_vill_sf$AREA <- set_units(st_area(tpe_vill_sf), "km^2")
tpe_vill_sf$sportfield_density <- tpe_vill_sf$sportfield_count / tpe_vill_sf$AREA
coords <- st_coordinates(st_centroid(tpe_vill_sf))
# 10km
nb <- dnearneigh(coords, d1 = 0, d2 = 10000)
dist_list <- nbdists(nb, coords)
inv_dist_list <- lapply(dist_list, function(x) {
y <- 1 / (x + 0.0001)
return(y)
})
listw <- nb2listw(nb, glist = inv_dist_list, style = "W", zero.policy = TRUE)
density <- as.vector(tpe_vill_sf$sportfield_density)
global_moran <- moran.test(density, listw = listw, zero.policy = TRUE)
print(global_moran)
lisa <- localmoran(density, listw = listw, zero.policy = TRUE)
tpe_vill_sf$local_I <- lisa[, 1]
tpe_vill_sf$z_score <- lisa[, 4]
tpe_vill_sf$p_value <- lisa[, 5]
quadrant <- attr(lisa, "quadr")$mean
quadrant <- factor(quadrant, levels = c(levels(quadrant), "NoSig"))
tpe_vill_sf$pvalue_FDR <- p.adjust(lisa[, 5], method = "fdr")
# Setting sig_level
signif_sportfield = 0.01
tpe_vill_sf$LISA_type_sportfield <- "NoSig"
tpe_vill_sf$LISA_type_sportfield[tpe_vill_sf$pvalue_FDR < signif_sportfield & tpe_vill_sf$local_I > 0 & quadrant == "High-High"] <- "High-High"
tpe_vill_sf$LISA_type_sportfield[tpe_vill_sf$pvalue_FDR < signif_sportfield & tpe_vill_sf$local_I > 0 & quadrant == "Low-Low"] <- "Low-Low"
tpe_vill_sf$LISA_type_sportfield[tpe_vill_sf$pvalue_FDR < signif_sportfield & tpe_vill_sf$local_I < 0 & quadrant == "High-Low"] <- "High-Low"
tpe_vill_sf$LISA_type_sportfield[tpe_vill_sf$pvalue_FDR < signif_sportfield & tpe_vill_sf$local_I < 0 & quadrant == "Low-High"] <- "Low-High"
tpe_vill_sf$LISA_type_sportfield <- factor(tpe_vill_sf$LISA_type_sportfield,
levels = c("High-High", "Low-Low", "High-Low", "Low-High", "NoSig"))
lisa_colors <- c(
'High-High' = 'red',
'Low-Low' = 'blue',
'High-Low' = 'pink',
'Low-High' = 'skyblue2',
'NoSig' = 'grey'
)
tm_shape(tpe_vill_sf) +
tm_polygons("LISA_type_sportfield",
palette = lisa_colors,
title = "LISA Cluster Type (Sport Facilities)") +
# 其他建北率高的學校
tm_shape(other_top15_sf) +
tm_dots(size = 0.5, col = "gray", shape = 21, border.col = "white") +
# 五虎
tm_shape(five_tigers_sf) +
tm_dots(size = 0.5, col = "black", shape = 21, border.col = "white") +
# 主文字
tm_text("school_short",
size = 0.9,
xmod = "xmod",
ymod = "ymod",
col = "black",
fontfamily = "Noto Sans TC",
fontface = "bold") +
tm_layout(title = "LISA Map (Inverse Distance) - Sport Facilities",
legend.outside = TRUE)
```
### Hospital
```{r message=FALSE, warning=FALSE}
# 設定信心水準
signif_hospital <- 0.01
# 確保 hospital 資料與行政區使用同樣 CRS
hospital_sf <- st_transform(hospital_sf, st_crs(tpe_vill_sf))
# 加入行政區
joined_hospital <- st_join(hospital_sf, tpe_vill_sf, join = st_within)
# 每個行政區的醫療點數量
hospital_counts <- joined_hospital %>%
group_by(TOWN_ID) %>%
summarise(hospital_count = n())
# 移除 geometry
hospital_counts_df <- st_drop_geometry(hospital_counts)
# 對應進原本的行政區圖層
tpe_vill_sf$hospital_count <- hospital_counts_df$hospital_count[match(tpe_vill_sf$TOWN_ID, hospital_counts_df$TOWN_ID)]
tpe_vill_sf$hospital_count[is.na(tpe_vill_sf$hospital_count)] <- 0
# 計算密度(設施數 / 區域面積)
tpe_vill_sf$AREA <- set_units(st_area(tpe_vill_sf), "km^2")
tpe_vill_sf$hospital_density <- tpe_vill_sf$hospital_count / tpe_vill_sf$AREA
# 計算鄰近矩陣
coords <- st_coordinates(st_centroid(tpe_vill_sf))
nb <- dnearneigh(coords, d1 = 0, d2 = 10000)
dist_list <- nbdists(nb, coords)
inv_dist_list <- lapply(dist_list, function(x) {
1 / (x + 0.0001)
})
listw <- nb2listw(nb, glist = inv_dist_list, style = "W", zero.policy = TRUE)
# LISA 分析
density <- as.vector(tpe_vill_sf$hospital_density)
global_moran <- moran.test(density, listw = listw, zero.policy = TRUE)
print(global_moran)
lisa <- localmoran(density, listw = listw, zero.policy = TRUE)
tpe_vill_sf$local_I <- lisa[, 1]
tpe_vill_sf$z_score <- lisa[, 4]
tpe_vill_sf$p_value <- lisa[, 5]
quadrant <- attr(lisa, "quadr")$mean
quadrant <- factor(quadrant, levels = c(levels(quadrant), "NoSig"))
tpe_vill_sf$pvalue_FDR <- p.adjust(lisa[, 5], method = "fdr")
# 指定類型
tpe_vill_sf$LISA_type_Hospital <- "NoSig"
tpe_vill_sf$LISA_type_Hospital[tpe_vill_sf$pvalue_FDR < signif_hospital & tpe_vill_sf$local_I > 0 & quadrant == "High-High"] <- "High-High"
tpe_vill_sf$LISA_type_Hospital[tpe_vill_sf$pvalue_FDR < signif_hospital & tpe_vill_sf$local_I > 0 & quadrant == "Low-Low"] <- "Low-Low"
tpe_vill_sf$LISA_type_Hospital[tpe_vill_sf$pvalue_FDR < signif_hospital & tpe_vill_sf$local_I < 0 & quadrant == "High-Low"] <- "High-Low"
tpe_vill_sf$LISA_type_Hospital[tpe_vill_sf$pvalue_FDR < signif_hospital & tpe_vill_sf$local_I < 0 & quadrant == "Low-High"] <- "Low-High"
tpe_vill_sf$LISA_type_Hospital <- factor(tpe_vill_sf$LISA_type_Hospital,
levels = c("High-High", "Low-Low", "High-Low", "Low-High", "NoSig"))
# 配色
lisa_colors <- c(
'High-High' = 'red',
'Low-Low' = 'blue',
'High-Low' = 'pink',
'Low-High' = 'skyblue2',
'NoSig' = 'grey'
)
# 畫圖
tm_shape(tpe_vill_sf) +
tm_polygons("LISA_type_Hospital",
palette = lisa_colors,
title = "LISA Cluster Type (Hospitals)") +
# 其他建北率高的學校
tm_shape(other_top15_sf) +
tm_dots(size = 0.5, col = "gray", shape = 21, border.col = "white") +
# 五虎
tm_shape(five_tigers_sf) +
tm_dots(size = 0.5, col = "black", shape = 21, border.col = "white") +
# 主文字
tm_text("school_short",
size = 0.9,
xmod = "xmod",
ymod = "ymod",
col = "black",
fontfamily = "Noto Sans TC",
fontface = "bold") +
tm_layout(title = "LISA Map (Inverse Distance) - Hospitals",
legend.outside = TRUE)
```
### Park
```{r message=FALSE, warning=FALSE}
# 1. 設定信心水準
signif_park <- 0.01
# 2. 座標轉換
park_sf <- st_transform(park_sf, st_crs(tpe_vill_sf))
# 3. Spatial join 與計數
joined_park <- st_join(park_sf, tpe_vill_sf, join = st_within)
park_counts <- joined_park %>%
group_by(TOWN_ID) %>%
summarise(park_count = n())
park_counts_df <- st_drop_geometry(park_counts)
# 4. 加入計數與密度欄位
tpe_vill_sf$park_count <- park_counts_df$park_count[match(tpe_vill_sf$TOWN_ID, park_counts_df$TOWN_ID)]
tpe_vill_sf$park_count[is.na(tpe_vill_sf$park_count)] <- 0
tpe_vill_sf$AREA <- set_units(st_area(tpe_vill_sf), "km^2")
tpe_vill_sf$park_density <- tpe_vill_sf$park_count / tpe_vill_sf$AREA
# 5. 計算鄰近矩陣與空間權重
coords <- st_coordinates(st_centroid(tpe_vill_sf))
nb <- dnearneigh(coords, d1 = 0, d2 = 10000)
dist_list <- nbdists(nb, coords)
inv_dist_list <- lapply(dist_list, function(x) {
1 / (x + 0.0001)
})
listw <- nb2listw(nb, glist = inv_dist_list, style = "W", zero.policy = TRUE)
# 6. Global & Local Moran's I
density <- as.vector(tpe_vill_sf$park_density)
global_moran <- moran.test(density, listw = listw, zero.policy = TRUE)
print(global_moran)
lisa <- localmoran(density, listw = listw, zero.policy = TRUE)
# 7. 加入 LISA 指標
tpe_vill_sf$local_I_park <- lisa[, 1]
tpe_vill_sf$z_score_park <- lisa[, 4]
tpe_vill_sf$p_value_park <- lisa[, 5]
quadrant_park <- attr(lisa, "quadr")$mean
quadrant_park <- factor(quadrant_park, levels = c(levels(quadrant_park), "NoSig"))
# FDR 校正
tpe_vill_sf$pvalue_FDR_park <- p.adjust(tpe_vill_sf$p_value_park, method = "fdr")
# 分群
tpe_vill_sf$LISA_type_Park <- "NoSig"
tpe_vill_sf$LISA_type_Park[tpe_vill_sf$pvalue_FDR_park < signif_park & tpe_vill_sf$local_I_park > 0 & quadrant_park == "High-High"] <- "High-High"
tpe_vill_sf$LISA_type_Park[tpe_vill_sf$pvalue_FDR_park < signif_park & tpe_vill_sf$local_I_park > 0 & quadrant_park == "Low-Low"] <- "Low-Low"
tpe_vill_sf$LISA_type_Park[tpe_vill_sf$pvalue_FDR_park < signif_park & tpe_vill_sf$local_I_park < 0 & quadrant_park == "High-Low"] <- "High-Low"
tpe_vill_sf$LISA_type_Park[tpe_vill_sf$pvalue_FDR_park < signif_park & tpe_vill_sf$local_I_park < 0 & quadrant_park == "Low-High"] <- "Low-High"
tpe_vill_sf$LISA_type_Park <- factor(tpe_vill_sf$LISA_type_Park,
levels = c("High-High", "Low-Low", "High-Low", "Low-High", "NoSig"))
# 畫圖
tm_shape(tpe_vill_sf) +
tm_polygons("LISA_type_Park",
palette = lisa_colors,
title = "LISA Cluster Type (Parks)") +
# 其他建北率高的學校
tm_shape(other_top15_sf) +
tm_dots(size = 0.5, col = "gray", shape = 21, border.col = "white") +
# 五虎
tm_shape(five_tigers_sf) +
tm_dots(size = 0.5, col = "black", shape = 21, border.col = "white") +
# 主文字
tm_text("school_short",
size = 0.9,
xmod = "xmod",
ymod = "ymod",
col = "black",
fontfamily = "Noto Sans TC",
fontface = "bold") +
tm_layout(title = "LISA Map (Inverse Distance) - Parks",
legend.outside = TRUE)
```
### intersection
```{r message=FALSE, warning=FALSE}
# 建立 High-High 與 Low-Low 的交集欄位
tpe_vill_sf$LISA_HHH_intersection <- with(tpe_vill_sf,
LISA_type_Park == "High-High" &
LISA_type_sportfield == "High-High" &
LISA_type_Hospital == "High-High"
)
tpe_vill_sf$LISA_LLL_intersection <- with(tpe_vill_sf,
LISA_type_Park == "Low-Low" &
LISA_type_sportfield == "Low-Low" &
LISA_type_Hospital == "Low-Low"
)
# 建立一個新的欄位標示交集類型
tpe_vill_sf$LISA_intersection_type <- "Other"
tpe_vill_sf$LISA_intersection_type[tpe_vill_sf$LISA_HHH_intersection] <- "All High-High"
tpe_vill_sf$LISA_intersection_type[tpe_vill_sf$LISA_LLL_intersection] <- "All Low-Low"
tpe_vill_sf$LISA_intersection_type <- factor(
tpe_vill_sf$LISA_intersection_type,
levels = c("Other", "All High-High", "All Low-Low")
)
# 畫圖
tm_shape(tpe_vill_sf) +
# 畫出交集區域
tm_polygons("LISA_intersection_type",
palette = c("gray", "green", "darkred"),
labels = c("Not all same type", "All High-High", "All Low-Low"),
title = "LISA Intersection Type") +
# 疊上其他建北率高的學校
tm_shape(other_top15_sf) +
tm_dots(size = 0.5, col = "gray", shape = 21, border.col = "white") +
# 疊上五虎
tm_shape(five_tigers_sf) +
tm_dots(size = 0.5, col = "black", shape = 21, border.col = "white") +
# 加上學校文字標籤
tm_text("school_short",
size = 0.9,
xmod = "xmod",
ymod = "ymod",
col = "black",
fontfamily = "Noto Sans TC",
fontface = "bold") +
# 標題與圖例配置
tm_layout(title = "Intersection of LISA Clusters (High-High and Low-Low)",
legend.outside = TRUE)
```