---
title: "Optimal Parameters-based Geographical Detector(OPGD)"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{opgd}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---



### Load data and package


``` r
library(tidyverse)
library(gdverse)

data("ndvi")
head(ndvi)
## # A tibble: 6 × 7
##   NDVIchange Climatezone Mining Tempchange Precipitation   GDP Popdensity
##        <dbl> <chr>       <fct>       <dbl>         <dbl> <dbl>      <dbl>
## 1    0.116   Bwk         low         0.256          237. 12.6      1.45  
## 2    0.0178  Bwk         low         0.273          214.  2.69     0.801 
## 3    0.138   Bsk         low         0.302          449. 20.1     11.5   
## 4    0.00439 Bwk         low         0.383          213.  0        0.0462
## 5    0.00316 Bwk         low         0.357          205.  0        0.0748
## 6    0.00838 Bwk         low         0.338          201.  0        0.549
```

### Determine optimal discretization parameters

`Climatezone` and `Mining` is the discrete category variables in the `ndvi` data, we need to discretize others. We can use `gd_bestunidisc()` to discretize them based on geodetector q-statistic.


``` r
tictoc::tic()
g = gd_bestunidisc(NDVIchange ~ .,
                   data = select(ndvi,-c(Climatezone,Mining)),
                   discnum = 3:15, cores = 6)
tictoc::toc()
## 2.95 sec elapsed
```


``` r
g
## $x
## [1] "GDP"           "Popdensity"    "Precipitation" "Tempchange"   
## 
## $k
## [1] 13 15 14 14
## 
## $method
## [1] "quantile" "quantile" "fisher"   "equal"   
## 
## $disv
## # A tibble: 713 × 4
##      GDP Popdensity Precipitation Tempchange
##    <int>      <int>         <int>      <int>
##  1     7          6             5          3
##  2     5          4             4          3
##  3     7         11            10          3
##  4     3          1             4          4
##  5     3          2             4          3
##  6     3          4             4          3
##  7     7          6             4          3
##  8     8          9             5          3
##  9    11         12             7          3
## 10     8         10             8          3
## # ℹ 703 more rows
new.ndvi = g$disv
new.ndvi
## # A tibble: 713 × 4
##      GDP Popdensity Precipitation Tempchange
##    <int>      <int>         <int>      <int>
##  1     7          6             5          3
##  2     5          4             4          3
##  3     7         11            10          3
##  4     3          1             4          4
##  5     3          2             4          3
##  6     3          4             4          3
##  7     7          6             4          3
##  8     8          9             5          3
##  9    11         12             7          3
## 10     8         10             8          3
## # ℹ 703 more rows
```

The `new.ndvi` is the discreted result of the optimal discretization parameter based on the Q statistic of the geographical detector (factor detector), we can combine it with other columns in `ndvi` tibble now.


``` r
new.ndvi = bind_cols(select(ndvi,NDVIchange,Climatezone,Mining),new.ndvi)
new.ndvi
## # A tibble: 713 × 7
##    NDVIchange Climatezone Mining   GDP Popdensity Precipitation Tempchange
##         <dbl> <chr>       <fct>  <int>      <int>         <int>      <int>
##  1    0.116   Bwk         low        7          6             5          3
##  2    0.0178  Bwk         low        5          4             4          3
##  3    0.138   Bsk         low        7         11            10          3
##  4    0.00439 Bwk         low        3          1             4          4
##  5    0.00316 Bwk         low        3          2             4          3
##  6    0.00838 Bwk         low        3          4             4          3
##  7    0.0335  Bwk         low        7          6             4          3
##  8    0.0387  Bwk         low        8          9             5          3
##  9    0.0882  Bsk         low       11         12             7          3
## 10    0.0690  Bsk         low        8         10             8          3
## # ℹ 703 more rows
```

### Run geodetector

Then, we can run geodetector model by `gd()` function.


``` r
gd(NDVIchange ~ .,data = new.ndvi,type = 'factor')
## ***          Factor Detector            
## 
## |   variable    | Q-statistic | P-value  |
## |:-------------:|:-----------:|:--------:|
## | Precipitation |  0.8811989  | 9.91e-10 |
## |  Climatezone  |  0.8218335  | 7.34e-10 |
## |  Tempchange   |  0.3478433  | 2.41e-10 |
## |  Popdensity   |  0.2100134  | 1.61e-10 |
## |    Mining     |  0.1411154  | 6.73e-10 |
## |      GDP      |  0.1069398  | 4.54e-10 |
gd(NDVIchange ~ .,data = new.ndvi,type = 'interaction')
## ***        Interaction Detector         
## 
## |    Interactive variable     |    Interaction     |
## |:---------------------------:|:------------------:|
## |    Climatezone ∩ Mining     |    Enhance, bi-    |
## |      Climatezone ∩ GDP      |    Enhance, bi-    |
## |  Climatezone ∩ Popdensity   |    Enhance, bi-    |
## | Climatezone ∩ Precipitation |    Enhance, bi-    |
## |  Climatezone ∩ Tempchange   |    Enhance, bi-    |
## |        Mining ∩ GDP         | Enhance, nonlinear |
## |     Mining ∩ Popdensity     | Enhance, nonlinear |
## |   Mining ∩ Precipitation    |    Enhance, bi-    |
## |     Mining ∩ Tempchange     |    Enhance, bi-    |
## |      GDP ∩ Popdensity       |    Enhance, bi-    |
## |     GDP ∩ Precipitation     |    Enhance, bi-    |
## |      GDP ∩ Tempchange       | Enhance, nonlinear |
## | Popdensity ∩ Precipitation  |    Enhance, bi-    |
## |   Popdensity ∩ Tempchange   |    Enhance, bi-    |
## | Precipitation ∩ Tempchange  |    Enhance, bi-    |
```

### You can also use `opgd()` in one time to get result above.


``` r
ndvi_opgd = opgd(NDVIchange ~ ., data = ndvi, discnum = 3:15,
                 discvar = names(select(ndvi,-c(NDVIchange,Climatezone,Mining))),
                 cores = 6, type = c('factor','interaction'))
ndvi_opgd
##                 OPGD Model                  
## ***          Factor Detector            
## 
## |   variable    | Q-statistic | P-value  |
## |:-------------:|:-----------:|:--------:|
## | Precipitation |  0.8811989  | 9.91e-10 |
## |  Climatezone  |  0.8218335  | 7.34e-10 |
## |  Tempchange   |  0.3478433  | 2.41e-10 |
## |  Popdensity   |  0.2100134  | 1.61e-10 |
## |    Mining     |  0.1411154  | 6.73e-10 |
## |      GDP      |  0.1069398  | 4.54e-10 |
## 
## ***        Interaction Detector         
## 
## |    Interactive variable     |    Interaction     |
## |:---------------------------:|:------------------:|
## |    Climatezone ∩ Mining     |    Enhance, bi-    |
## |      Climatezone ∩ GDP      |    Enhance, bi-    |
## |  Climatezone ∩ Popdensity   |    Enhance, bi-    |
## | Climatezone ∩ Precipitation |    Enhance, bi-    |
## |  Climatezone ∩ Tempchange   |    Enhance, bi-    |
## |        Mining ∩ GDP         | Enhance, nonlinear |
## |     Mining ∩ Popdensity     | Enhance, nonlinear |
## |   Mining ∩ Precipitation    |    Enhance, bi-    |
## |     Mining ∩ Tempchange     |    Enhance, bi-    |
## |      GDP ∩ Popdensity       |    Enhance, bi-    |
## |     GDP ∩ Precipitation     |    Enhance, bi-    |
## |      GDP ∩ Tempchange       | Enhance, nonlinear |
## | Popdensity ∩ Precipitation  |    Enhance, bi-    |
## |   Popdensity ∩ Tempchange   |    Enhance, bi-    |
## | Precipitation ∩ Tempchange  |    Enhance, bi-    |
```

You can use `plot()` to visualize the result.


``` r
plot(ndvi_opgd)
```

![](../man/figures/opgd/opgd_plot-1.png)
