Skip to content

Commit

Permalink
modify gd vignette
Browse files Browse the repository at this point in the history
  • Loading branch information
SpatLyu committed Jun 6, 2024
1 parent 57815f5 commit f460732
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 67 deletions.
132 changes: 67 additions & 65 deletions vignettes/Geographic-detector.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ library(cowplot)

f1 = ggplot(data = disease) +
geom_sf(aes(fill = incidence),lwd = .1,color = 'grey') +
viridis::scale_fill_viridis(option="mako", direction = -1) +
viridis::scale_fill_viridis(option="mako", direction = -1) +
theme_bw() +
theme(
axis.text = element_blank(),
Expand All @@ -73,9 +73,9 @@ f1 = ggplot(data = disease) +
)
f2 = ggplot(data = watershed) +
geom_sf(aes(fill = watershed),lwd = .1,color = 'grey') +
tidyterra::scale_fill_whitebox_c() +
tidyterra::scale_fill_whitebox_c() +
coord_sf(crs = NULL) +
theme_bw() +
theme_bw() +
theme(
axis.text = element_blank(),
axis.ticks = element_blank(),
Expand All @@ -87,7 +87,7 @@ f2 = ggplot(data = watershed) +
)
f3 = ggplot(data = elevation) +
geom_sf(aes(fill = elevation),lwd = .1,color = 'grey') +
tidyterra::scale_fill_hypso_c() +
tidyterra::scale_fill_hypso_c() +
theme_bw() +
theme(
axis.text = element_blank(),
Expand All @@ -100,7 +100,7 @@ f3 = ggplot(data = elevation) +
)
f4 = ggplot(data = soiltype) +
geom_sf(aes(fill = soiltype),lwd = .1,color = 'grey') +
tidyterra::scale_fill_wiki_c() +
tidyterra::scale_fill_wiki_c() +
theme_bw() +
theme(
axis.text = element_blank(),
Expand All @@ -111,7 +111,7 @@ f4 = ggplot(data = soiltype) +
legend.position.inside = c(.1,.25),
legend.background = element_rect(fill = 'transparent',color = NA)
)

plot_grid(f1,f2,f3,f4, nrow = 2,label_fontfamily = 'serif',
labels = paste0('(',letters[1:4],')'),
label_fontface = 'plain',label_size = 10,
Expand All @@ -125,18 +125,18 @@ Attribute spatial join


``` r
NTDs = disease %>%
st_centroid() %>%
st_join(watershed[,"watershed"]) %>%
st_join(elevation[,"elevation"]) %>%
st_join(soiltype[,"soiltype"])
NTDs = disease %>%
st_centroid() %>%
st_join(watershed[,"watershed"]) %>%
st_join(elevation[,"elevation"]) %>%
st_join(soiltype[,"soiltype"])
```

Check whether has `NA` in `NTDs`:


``` r
NTDs %>%
NTDs %>%
dplyr::filter(if_any(everything(),~is.na(.x)))
## Simple feature collection with 4 features and 5 fields
## Geometry type: POINT
Expand All @@ -154,7 +154,7 @@ NTDs %>%


``` r
NTDs %>%
NTDs %>%
dplyr::filter(if_all(everything(),~!is.na(.x))) -> NTDs
```

Expand Down Expand Up @@ -347,7 +347,7 @@ ed99
## |elevation |NA |No |
```

## optimal parameters geographic detector
## optimal parameters geographic detector(OPGD)


``` r
Expand All @@ -368,25 +368,26 @@ fvc

``` r
names(fvc)
## [1] "fvc" "premax" "premin" "presum" "tmpmax" "tmpmin" "tmpavg" "pop" "ntl" "lulc"
## [11] "elev" "slope" "aspect"
## [1] "fvc" "premax" "premin" "presum" "tmpmax" "tmpmin" "tmpavg" "pop"
## [9] "ntl" "lulc" "elev" "slope" "aspect"
```

### Convert data from `SpatRaster` to `tiibble`
### Convert data from `SpatRaster` to `tibble`


``` r
fvc = as_tibble(terra::as.data.frame(fvc,na.rm = T))
head(fvc)
## # A tibble: 6 × 13
## fvc premax premin presum tmpmax tmpmin tmpavg pop ntl lulc elev slope aspect
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.198 163. 7.95 3956. 20.8 -7.53 8.05 1.90 6.60 10 1758. 2.65 176.
## 2 0.193 161. 6.80 3892. 20.7 -7.55 8.02 1.20 4.91 10 1754. 3.45 170.
## 3 0.192 160. 5.24 3842. 20.9 -7.48 8.15 0.547 3.75 10 1722. 3.96 139.
## 4 0.189 159. 5 3808. 21.1 -7.39 8.35 0.542 3.99 10 1672. 2.90 111.
## 5 0.208 164. 9.98 4051. 20.6 -7.59 7.97 10.4 7.10 10 1780. 1.94 99.5
## 6 0.196 163. 8.15 3973. 20.7 -7.53 8.03 9.31 6.56 10 1755. 3.01 99.6
## fvc premax premin presum tmpmax tmpmin tmpavg pop ntl lulc elev
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.198 163. 7.95 3956. 20.8 -7.53 8.05 1.90 6.60 10 1758.
## 2 0.193 161. 6.80 3892. 20.7 -7.55 8.02 1.20 4.91 10 1754.
## 3 0.192 160. 5.24 3842. 20.9 -7.48 8.15 0.547 3.75 10 1722.
## 4 0.189 159. 5 3808. 21.1 -7.39 8.35 0.542 3.99 10 1672.
## 5 0.208 164. 9.98 4051. 20.6 -7.59 7.97 10.4 7.10 10 1780.
## 6 0.196 163. 8.15 3973. 20.7 -7.53 8.03 9.31 6.56 10 1755.
## # ℹ 2 more variables: slope <dbl>, aspect <dbl>
```

### Determine optimal discretization parameters
Expand All @@ -399,37 +400,37 @@ We can use `gd_bestunidisc` to discretize them based on geodetector q-statistic.
tictoc::tic()
g = gd_bestunidisc(fvc ~ .,data = select(fvc,-lulc),discnum = 2:15,cores = 6)
tictoc::toc()
## 18.41 sec elapsed
## 18.81 sec elapsed
```


``` r
g
## $x
## [1] "aspect" "elev" "ntl" "pop" "premax" "premin" "presum" "slope" "tmpavg" "tmpmax"
## [11] "tmpmin"
## [1] "aspect" "elev" "ntl" "pop" "premax" "premin" "presum" "slope"
## [9] "tmpavg" "tmpmax" "tmpmin"
##
## $k
## [1] 15 15 14 15 15 15 15 15 15 15 15
## [1] 15 15 15 15 15 12 15 12 15 15 15
##
## $method
## [1] "equal" "quantile" "fisher" "quantile" "fisher" "fisher" "fisher" "fisher"
## [9] "quantile" "quantile" "fisher"
## [1] "equal" "quantile" "fisher" "quantile" "fisher" "fisher"
## [7] "fisher" "fisher" "quantile" "quantile" "fisher"
##
## $disv
## # A tibble: 136,243 × 11
## aspect elev ntl pop premax premin presum slope tmpavg tmpmax tmpmin
## <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
## 1 8 12 3 1 7 1 1 4 4 8 2
## 2 8 12 2 1 7 1 1 6 4 8 2
## 3 6 12 2 1 6 1 1 7 4 9 2
## 4 5 12 2 1 6 1 1 5 5 10 2
## 2 8 12 2 1 7 1 1 5 4 8 2
## 3 6 12 2 1 6 1 1 5 4 9 2
## 4 5 12 2 1 6 1 1 4 5 10 2
## 5 5 13 3 4 7 2 1 3 4 8 2
## 6 5 12 3 3 7 1 1 5 4 8 2
## 7 6 12 2 1 7 1 1 7 4 9 2
## 8 7 12 2 1 6 1 1 6 5 10 2
## 9 8 12 2 1 6 1 1 4 5 10 2
## 10 7 12 3 1 6 1 1 5 5 10 2
## 6 5 12 3 3 7 2 1 4 4 8 2
## 7 6 12 2 1 7 1 1 6 4 9 3
## 8 7 12 2 1 6 1 1 5 5 10 3
## 9 8 12 2 1 6 1 1 3 5 10 3
## 10 7 12 2 1 6 1 1 4 5 10 3
## # ℹ 136,233 more rows
```

Expand All @@ -440,15 +441,15 @@ new.fvc
## aspect elev ntl pop premax premin presum slope tmpavg tmpmax tmpmin
## <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
## 1 8 12 3 1 7 1 1 4 4 8 2
## 2 8 12 2 1 7 1 1 6 4 8 2
## 3 6 12 2 1 6 1 1 7 4 9 2
## 4 5 12 2 1 6 1 1 5 5 10 2
## 2 8 12 2 1 7 1 1 5 4 8 2
## 3 6 12 2 1 6 1 1 5 4 9 2
## 4 5 12 2 1 6 1 1 4 5 10 2
## 5 5 13 3 4 7 2 1 3 4 8 2
## 6 5 12 3 3 7 1 1 5 4 8 2
## 7 6 12 2 1 7 1 1 7 4 9 2
## 8 7 12 2 1 6 1 1 6 5 10 2
## 9 8 12 2 1 6 1 1 4 5 10 2
## 10 7 12 3 1 6 1 1 5 5 10 2
## 6 5 12 3 3 7 2 1 4 4 8 2
## 7 6 12 2 1 7 1 1 6 4 9 3
## 8 7 12 2 1 6 1 1 5 5 10 3
## 9 8 12 2 1 6 1 1 3 5 10 3
## 10 7 12 2 1 6 1 1 4 5 10 3
## # ℹ 136,233 more rows
```

Expand All @@ -459,19 +460,20 @@ The `new.fvc` is the discrete result of the optimal discretization parameter ba
new.fvc = bind_cols(select(fvc,fvc,lulc),new.fvc)
new.fvc
## # A tibble: 136,243 × 13
## fvc lulc aspect elev ntl pop premax premin presum slope tmpavg tmpmax tmpmin
## <dbl> <dbl> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
## 1 0.198 10 8 12 3 1 7 1 1 4 4 8 2
## 2 0.193 10 8 12 2 1 7 1 1 6 4 8 2
## 3 0.192 10 6 12 2 1 6 1 1 7 4 9 2
## 4 0.189 10 5 12 2 1 6 1 1 5 5 10 2
## 5 0.208 10 5 13 3 4 7 2 1 3 4 8 2
## 6 0.196 10 5 12 3 3 7 1 1 5 4 8 2
## 7 0.191 10 6 12 2 1 7 1 1 7 4 9 2
## 8 0.185 10 7 12 2 1 6 1 1 6 5 10 2
## 9 0.174 10 8 12 2 1 6 1 1 4 5 10 2
## 10 0.166 10 7 12 3 1 6 1 1 5 5 10 2
## fvc lulc aspect elev ntl pop premax premin presum slope tmpavg
## <dbl> <dbl> <int> <int> <int> <int> <int> <int> <int> <int> <int>
## 1 0.198 10 8 12 3 1 7 1 1 4 4
## 2 0.193 10 8 12 2 1 7 1 1 5 4
## 3 0.192 10 6 12 2 1 6 1 1 5 4
## 4 0.189 10 5 12 2 1 6 1 1 4 5
## 5 0.208 10 5 13 3 4 7 2 1 3 4
## 6 0.196 10 5 12 3 3 7 2 1 4 4
## 7 0.191 10 6 12 2 1 7 1 1 6 4
## 8 0.185 10 7 12 2 1 6 1 1 5 5
## 9 0.174 10 8 12 2 1 6 1 1 3 5
## 10 0.166 10 7 12 2 1 6 1 1 4 5
## # ℹ 136,233 more rows
## # ℹ 2 more variables: tmpmax <int>, tmpmin <int>
```

### Run geodetector
Expand All @@ -489,27 +491,27 @@ ssh.test(fvc ~ .,data = new.fvc,type = 'factor')
------------------------------------
variable Q-statistic P-value
---------- ------------- -----------
presum 0.6417 4.477e-10
presum 0.6406 5.831e-10

lulc 0.5533 9.106e-10

premin 0.443 6.081e-10
premin 0.4361 5.258e-10

tmpmin 0.4054 3.477e-10
tmpmin 0.4048 6.012e-10

tmpmax 0.2284 5.111e-10

elev 0.209 1.5e-10

tmpavg 0.197 6.833e-10

slope 0.1934 5.098e-10
slope 0.193 8.314e-10

pop 0.1856 3.221e-10

premax 0.135 2.861e-10
premax 0.1326 6.272e-10

ntl 0.02152 5.086e-10
ntl 0.02147 7.455e-10

aspect 0.00741 5.448e-10
------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions vignettes/Geographic-detector.Rmd.orig
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ ed99 = ssh.test(incidence ~ watershed + elevation + soiltype,
ed99
```

## optimal parameters geographic detector
## optimal parameters geographic detector(OPGD)

```{r}
library(terra)
Expand All @@ -197,7 +197,7 @@ fvc
names(fvc)
```

### Convert data from `SpatRaster` to `tiibble`
### Convert data from `SpatRaster` to `tibble`

```{r}
fvc = as_tibble(terra::as.data.frame(fvc,na.rm = T))
Expand Down

0 comments on commit f460732

Please sign in to comment.