# Robust Geographical Detector & Robust Interaction Detector(RGD & RID)

### Set up python dependence

1. Install miniconda

2. Open the conda terminal and perform steps 3 to 6

3. Create a new conda env `gdverse`

   > `conda create -n gdverse python=3.9 -y`

4. Activate this env

   > `conda activate gdverse`

5. Install `mamba`

   > `conda install -c conda-forge mamba -y`

6. Set up python packages

   > `mamba install -c conda-forge numpy==1.23.5 joblib pandas ruptures -y`

7. Configure `GDVERSE_PYTHON` environment variable

   - Temporarily setting up `gdverse` python dependence environment using `Sys.setenv()` in the R console or R code like `Sys.setenv(GDVERSE_PYTHON = "path/to/python/environment")`.

   - Or using `usethis::edit_r_environ()` to set long-available `gdverse` python dependencies environment path.

### Load data and package

    library(tidyverse)
    library(gdverse)

    data("ndvi")

    names(ndvi)
    ## [1] "NDVIchange"    "Climatezone"   "Mining"        "Tempchange"    "Precipitation"
    ## [6] "GDP"           "Popdensity"

### Determine discretization use offline change point detection

`Climatezone` and `Mining` is the discrete category variables in the `ndvi` data, we need to discretize others.You can use `robust_disc()` to discretize them based on offline change point detection.When executing `robust_disc()`, note that each `discvar` can use a `discnum` parameter of its own. You can pass in a vector of equal length to `discvar` to use different `discnum` for different `discvar`.

    tictoc::tic()
    new.ndvi = robust_disc(NDVIchange ~ .,
                           data = select(ndvi,-c(Climatezone,Mining)),
                           discnum = 10,cores = 6)
    tictoc::toc()
    ## 20.95 sec elapsed

    new.ndvi
    ## # A tibble: 713 × 4
    ##    Tempchange Precipitation GDP     Popdensity
    ##    <chr>      <chr>         <chr>   <chr>
    ##  1 group1     group5        group7  group6
    ##  2 group1     group5        group7  group4
    ##  3 group1     group9        group8  group10
    ##  4 group1     group5        group1  group3
    ##  5 group1     group3        group1  group3
    ##  6 group1     group3        group1  group4
    ##  7 group1     group5        group7  group6
    ##  8 group1     group5        group8  group10
    ##  9 group1     group7        group10 group10
    ## 10 group1     group7        group9  group10
    ## # ℹ 703 more rows

The `new.ndvi` is the discrete result,we can combine it with `NDVIchange`,`Climatezone` and `Mining` cols in `ndvi` tibble now.

    new.ndvi = bind_cols(select(ndvi,c(NDVIchange,Climatezone,Mining)),new.ndvi)
    new.ndvi
    ## # A tibble: 713 × 7
    ##    NDVIchange Climatezone Mining Tempchange Precipitation GDP     Popdensity
    ##         <dbl> <chr>       <fct>  <chr>      <chr>         <chr>   <chr>
    ##  1    0.116   Bwk         low    group1     group5        group7  group6
    ##  2    0.0178  Bwk         low    group1     group5        group7  group4
    ##  3    0.138   Bsk         low    group1     group9        group8  group10
    ##  4    0.00439 Bwk         low    group1     group5        group1  group3
    ##  5    0.00316 Bwk         low    group1     group3        group1  group3
    ##  6    0.00838 Bwk         low    group1     group3        group1  group4
    ##  7    0.0335  Bwk         low    group1     group5        group7  group6
    ##  8    0.0387  Bwk         low    group1     group5        group8  group10
    ##  9    0.0882  Bsk         low    group1     group7        group10 group10
    ## 10    0.0690  Bsk         low    group1     group7        group9  group10
    ## # ℹ 703 more rows

### Run geodetector

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

    g1 = gd(NDVIchange ~ .,data = new.ndvi, type = 'factor')
    g1
    ## ***          Factor Detector
    ##
    ## |   variable    | Q-statistic | P-value  |
    ## |:-------------:|:-----------:|:--------:|
    ## | Precipitation |  0.8933049  | 3.25e-10 |
    ## |  Climatezone  |  0.8218335  | 7.34e-10 |
    ## |  Tempchange   |  0.3874615  | 7.59e-10 |
    ## |  Popdensity   |  0.2622265  | 3.65e-10 |
    ## |      GDP      |  0.1491878  | 3.63e-10 |
    ## |    Mining     |  0.1411154  | 6.73e-10 |

    g2 = gd(NDVIchange ~ .,data = new.ndvi, type = 'interaction')
    g2
    ## ***        Interaction Detector
    ##
    ## |    Interactive variable     | Interaction  |
    ## |:---------------------------:|:------------:|
    ## |    Climatezone ∩ Mining     | Enhance, bi- |
    ## |  Climatezone ∩ Tempchange   | Enhance, bi- |
    ## | Climatezone ∩ Precipitation | Enhance, bi- |
    ## |      Climatezone ∩ GDP      | Enhance, bi- |
    ## |  Climatezone ∩ Popdensity   | Enhance, bi- |
    ## |     Mining ∩ Tempchange     | Enhance, bi- |
    ## |   Mining ∩ Precipitation    | Enhance, bi- |
    ## |        Mining ∩ GDP         | Enhance, bi- |
    ## |     Mining ∩ Popdensity     | Enhance, bi- |
    ## | Tempchange ∩ Precipitation  | Enhance, bi- |
    ## |      Tempchange ∩ GDP       | Enhance, bi- |
    ## |   Tempchange ∩ Popdensity   | Enhance, bi- |
    ## |     Precipitation ∩ GDP     | Enhance, bi- |
    ## | Precipitation ∩ Popdensity  | Enhance, bi- |
    ## |      GDP ∩ Popdensity       | Enhance, bi- |

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

    ndvi_rgd = rgd(NDVIchange ~ ., data = ndvi,
                   discvar = names(select(ndvi,-c(NDVIchange,Climatezone,Mining))),
                   discnum = 10, cores = 6, type = c('factor','interaction'))
    ndvi_rgd
    ##                  RGD Model
    ## ***          Factor Detector
    ##
    ## |   variable    | Q-statistic | P-value  |
    ## |:-------------:|:-----------:|:--------:|
    ## | Precipitation |  0.8933049  | 3.25e-10 |
    ## |  Climatezone  |  0.8218335  | 7.34e-10 |
    ## |  Tempchange   |  0.3874615  | 7.59e-10 |
    ## |  Popdensity   |  0.2622265  | 3.65e-10 |
    ## |      GDP      |  0.1491878  | 3.63e-10 |
    ## |    Mining     |  0.1411154  | 6.73e-10 |
    ##
    ## ***        Interaction Detector
    ##
    ## |    Interactive variable     | Interaction  |
    ## |:---------------------------:|:------------:|
    ## |    Climatezone ∩ Mining     | Enhance, bi- |
    ## |  Climatezone ∩ Tempchange   | Enhance, bi- |
    ## | Climatezone ∩ Precipitation | Enhance, bi- |
    ## |      Climatezone ∩ GDP      | Enhance, bi- |
    ## |  Climatezone ∩ Popdensity   | Enhance, bi- |
    ## |     Mining ∩ Tempchange     | Enhance, bi- |
    ## |   Mining ∩ Precipitation    | Enhance, bi- |
    ## |        Mining ∩ GDP         | Enhance, bi- |
    ## |     Mining ∩ Popdensity     | Enhance, bi- |
    ## | Tempchange ∩ Precipitation  | Enhance, bi- |
    ## |      Tempchange ∩ GDP       | Enhance, bi- |
    ## |   Tempchange ∩ Popdensity   | Enhance, bi- |
    ## |     Precipitation ∩ GDP     | Enhance, bi- |
    ## | Precipitation ∩ Popdensity  | Enhance, bi- |
    ## |      GDP ∩ Popdensity       | Enhance, bi- |

    plot(ndvi_rgd)

### Run RID

`RID` allows you to obtain the interactions of all possible combinations of variables. By default, `Spatial Fuzzy And Join` are used to generate a new spatial partition of the interaction variables.

    ndvi_rid = rid(NDVIchange ~ ., data = ndvi,
                   discvar = names(select(ndvi,-c(NDVIchange,Climatezone,Mining))),
                   discnum = 10, cores = 6)
    ndvi_rid
    ## ***          Robust Interaction Detector
    ##
    ## |                               varibale                               |    PD     |
    ## |:--------------------------------------------------------------------:|:---------:|
    ## |                     Climatezone ∩ Precipitation                      | 0.8983194 |
    ## |                            Precipitation                             | 0.8933049 |
    ## |                  Climatezone ∩ Precipitation ∩ GDP                   | 0.8470890 |
    ## |                 Climatezone ∩ Mining ∩ Precipitation                 | 0.8448562 |
    ## |               Climatezone ∩ Precipitation ∩ Popdensity               | 0.8426167 |
    ## |            Climatezone ∩ Precipitation ∩ GDP ∩ Popdensity            | 0.8351028 |
    ## |               Climatezone ∩ Tempchange ∩ Precipitation               | 0.8343010 |
    ## |              Climatezone ∩ Mining ∩ Precipitation ∩ GDP              | 0.8302478 |
    ## |                         Precipitation ∩ GDP                          | 0.8291002 |
    ## |          Climatezone ∩ Mining ∩ Precipitation ∩ Popdensity           | 0.8266936 |
    ## |                      Tempchange ∩ Precipitation                      | 0.8262867 |
    ## |                             Climatezone                              | 0.8218335 |
    ## |       Climatezone ∩ Mining ∩ Precipitation ∩ GDP ∩ Popdensity        | 0.8206854 |
    ## |            Climatezone ∩ Tempchange ∩ Precipitation ∩ GDP            | 0.8033886 |
    ## |                   Precipitation ∩ GDP ∩ Popdensity                   | 0.7984676 |
    ## |        Climatezone ∩ Tempchange ∩ Precipitation ∩ Popdensity         | 0.7941671 |
    ## |                         Climatezone ∩ Mining                         | 0.7930330 |
    ## |                      Precipitation ∩ Popdensity                      | 0.7928397 |
    ## |          Climatezone ∩ Mining ∩ Tempchange ∩ Precipitation           | 0.7926405 |
    ## |     Climatezone ∩ Tempchange ∩ Precipitation ∩ GDP ∩ Popdensity      | 0.7914425 |
    ## |                   Tempchange ∩ Precipitation ∩ GDP                   | 0.7898127 |
    ## |       Climatezone ∩ Mining ∩ Tempchange ∩ Precipitation ∩ GDP        | 0.7826698 |
    ## |                     Mining ∩ Precipitation ∩ GDP                     | 0.7780201 |
    ## |    Climatezone ∩ Mining ∩ Tempchange ∩ Precipitation ∩ Popdensity    | 0.7767463 |
    ## | Climatezone ∩ Mining ∩ Tempchange ∩ Precipitation ∩ GDP ∩ Popdensity | 0.7748192 |
    ## |                        Mining ∩ Precipitation                        | 0.7742147 |
    ## |                       Climatezone ∩ Tempchange                       | 0.7593504 |
    ## |            Tempchange ∩ Precipitation ∩ GDP ∩ Popdensity             | 0.7592357 |
    ## |               Tempchange ∩ Precipitation ∩ Popdensity                | 0.7564501 |
    ## |                  Climatezone ∩ Mining ∩ Popdensity                   | 0.7554556 |
    ## |                      Climatezone ∩ Mining ∩ GDP                      | 0.7546429 |
    ## |              Mining ∩ Precipitation ∩ GDP ∩ Popdensity               | 0.7529482 |
    ## |                  Climatezone ∩ Mining ∩ Tempchange                   | 0.7485414 |
    ## |                       Climatezone ∩ Popdensity                       | 0.7478818 |
    ## |                          Climatezone ∩ GDP                           | 0.7442993 |
    ## |                 Mining ∩ Precipitation ∩ Popdensity                  | 0.7428155 |
    ## |              Mining ∩ Tempchange ∩ Precipitation ∩ GDP               | 0.7390511 |
    ## |                    Climatezone ∩ Tempchange ∩ GDP                    | 0.7373548 |
    ## |                 Mining ∩ Tempchange ∩ Precipitation                  | 0.7348493 |
    ## |               Climatezone ∩ Mining ∩ GDP ∩ Popdensity                | 0.7337329 |
    ## |               Climatezone ∩ Mining ∩ Tempchange ∩ GDP                | 0.7316833 |
    ## |                    Climatezone ∩ GDP ∩ Popdensity                    | 0.7217405 |
    ## |                Climatezone ∩ Tempchange ∩ Popdensity                 | 0.7203489 |
    ## |            Climatezone ∩ Mining ∩ Tempchange ∩ Popdensity            | 0.7190660 |
    ## |        Mining ∩ Tempchange ∩ Precipitation ∩ GDP ∩ Popdensity        | 0.7124629 |
    ## |         Climatezone ∩ Mining ∩ Tempchange ∩ GDP ∩ Popdensity         | 0.7106643 |
    ## |             Climatezone ∩ Tempchange ∩ GDP ∩ Popdensity              | 0.7100158 |
    ## |           Mining ∩ Tempchange ∩ Precipitation ∩ Popdensity           | 0.7016658 |
    ## |                Mining ∩ Tempchange ∩ GDP ∩ Popdensity                | 0.4132554 |
    ## |                      Mining ∩ Tempchange ∩ GDP                       | 0.4057062 |
    ## |                         Mining ∩ Tempchange                          | 0.4032055 |
    ## |                   Mining ∩ Tempchange ∩ Popdensity                   | 0.4026808 |
    ## |                    Tempchange ∩ GDP ∩ Popdensity                     | 0.3996984 |
    ## |                       Tempchange ∩ Popdensity                        | 0.3990126 |
    ## |                           Tempchange ∩ GDP                           | 0.3914879 |
    ## |                              Tempchange                              | 0.3874615 |
    ## |                      Mining ∩ GDP ∩ Popdensity                       | 0.3293260 |
    ## |                         Mining ∩ Popdensity                          | 0.3188582 |
    ## |                           GDP ∩ Popdensity                           | 0.2773077 |
    ## |                              Popdensity                              | 0.2622265 |
    ## |                             Mining ∩ GDP                             | 0.2157334 |
    ## |                                 GDP                                  | 0.1491878 |
    ## |                                Mining                                | 0.1411154 |