ausgis / GD

Geographical Detectors for Assessing Spatial Factors
https://ausgis.github.io/GD/
8 stars 0 forks source link

Support tibble in GD funs #1

Closed SpatLyu closed 1 month ago

SpatLyu commented 1 month ago

Now, many I/O functions in R return results as tibble, so support for tibble should be considered.

SpatLyu commented 1 month ago

optdisc function is OK in GD 10.5

library(GD)
#> This is GD 10.5.
#>                         
#> To cite GD in publications, please use:
#>                         
#> Song, Y., Wang, J., Ge, Y. & Xu, C. (2020) An optimal parameters-based geographical detector model enhances geographic characteristics of explanatory variables for spatial heterogeneity analysis: Cases with different types of spatial data, GIScience & Remote Sensing, 57(5), 593-610. doi: 10.1080/15481603.2020.1760434.
#> 
data("ndvi_40")
head(ndvi_40)
#>   NDVIchange Climatezone Mining Tempchange Precipitation   GDP Popdensity
#> 1    0.11599         Bwk    low    0.25598        236.54 12.55    1.44957
#> 2    0.01783         Bwk    low    0.27341        213.55  2.69    0.80124
#> 3    0.13817         Bsk    low    0.30247        448.88 20.06   11.49432
#> 4    0.00439         Bwk    low    0.38302        212.76  0.00    0.04620
#> 5    0.00316         Bwk    low    0.35729        205.01  0.00    0.07482
#> 6    0.00838         Bwk    low    0.33750        200.55  0.00    0.54941

discmethod = c("equal","natural","quantile","geometric","sd")
discitv = c(3:8)
optidisc.var = names(ndvi_40)[4:7]
repose.var = names(ndvi_40)[1]
odc.model = paste(repose.var,'~',paste(optidisc.var,collapse = "+"))
odc = optidisc(odc.model,data=ndvi_40,
               discmethod = discmethod,
               discitv = discitv)
plot(odc)
#> Optimal discretization process ...

#> Optimal discretization result ...


odc1 = optidisc(odc.model,
                data = tibble::as_tibble(ndvi_40),
                discmethod = discmethod,
                discitv = discitv)
plot(odc1)
#> Optimal discretization process ...

#> Optimal discretization result ...

Created on 2024-10-18 with reprex v2.1.1

SpatLyu commented 1 month ago
library(GD)
#> This is GD 10.5.
#>                         
#> To cite GD in publications, please use:
#>                         
#> Song, Y., Wang, J., Ge, Y. & Xu, C. (2020) An optimal parameters-based geographical detector model enhances geographic characteristics of explanatory variables for spatial heterogeneity analysis: Cases with different types of spatial data, GIScience & Remote Sensing, 57(5), 593-610. doi: 10.1080/15481603.2020.1760434.
#> 

discmethod = c("equal","natural","quantile")
discitv = c(4:6)
ndvigdm = gdm(NDVIchange ~ Climatezone + Mining + Tempchange + GDP,
              continuous_variable = c("Tempchange", "GDP"),
              data = ndvi_40,
              discmethod = discmethod, discitv = discitv)
ndvigdm
#> Explanatory variables include 2 continuous variables.
#> 
#> optimal discretization result of Tempchange
#> method             :  quantile
#> number of intervals:  6
#> intervals:
#>  -0.39277 0.3858 0.8397533 1.23631 1.54196 1.96675 3.22051
#> numbers of data within intervals:
#>  119 119 119 118 119 119
#> 
#> optimal discretization result of GDP
#> method             :  quantile
#> number of intervals:  4
#> intervals:
#>  0 0.25 14.57 176.58 16589.09
#> numbers of data within intervals:
#>  179 178 178 178
#> 
#> Geographical detectors results:
#> 
#> Factor detector:
#>      variable         qv          sig
#> 1 Climatezone 0.82183348 7.340526e-10
#> 2      Mining 0.14111542 6.734163e-10
#> 3  Tempchange 0.31797346 5.104492e-10
#> 4         GDP 0.09596755 4.022449e-11
#> 
#> Risk detector:
#> Climatezone
#>   itv    meanrisk
#> 1 Bsk 0.143572961
#> 2 Bwk 0.004536505
#> 3 Dwa 0.321735000
#> 4 Dwb 0.343155655
#> 5 Dwc 0.444868361
#> 
#> Mining
#>         itv   meanrisk
#> 1  very low 0.21008297
#> 2       low 0.03294513
#> 3    medium 0.30733460
#> 4      high 0.26695286
#> 5 very high 0.19176875
#> 
#> Tempchange
#>              itv   meanrisk
#> 1 [-0.393,0.386] 0.03611076
#> 2   (0.386,0.84] 0.11407521
#> 3    (0.84,1.24] 0.30719824
#> 4    (1.24,1.54] 0.31380373
#> 5    (1.54,1.97] 0.26461966
#> 6    (1.97,3.22] 0.18935924
#> 
#> GDP
#>              itv  meanrisk
#> 1       [0,0.25] 0.1385164
#> 2    (0.25,14.6] 0.1615299
#> 3     (14.6,177] 0.2400161
#> 4 (177,1.66e+04] 0.2764687
#> 
#> Climatezone
#>   interval  Bsk  Bwk  Dwa  Dwb  Dwc
#> 1      Bsk <NA> <NA> <NA> <NA> <NA>
#> 2      Bwk    Y <NA> <NA> <NA> <NA>
#> 3      Dwa    Y    Y <NA> <NA> <NA>
#> 4      Dwb    Y    Y    N <NA> <NA>
#> 5      Dwc    Y    Y    Y    Y <NA>
#> 
#> Mining
#>    interval very low  low medium high very high
#> 1  very low     <NA> <NA>   <NA> <NA>      <NA>
#> 2       low        Y <NA>   <NA> <NA>      <NA>
#> 3    medium        Y    Y   <NA> <NA>      <NA>
#> 4      high        Y    Y      N <NA>      <NA>
#> 5 very high        N    Y      Y    Y      <NA>
#> 
#> Tempchange
#>         interval [-0.393,0.386] (0.386,0.84] (0.84,1.24] (1.24,1.54]
#> 1 [-0.393,0.386]           <NA>         <NA>        <NA>        <NA>
#> 2   (0.386,0.84]              Y         <NA>        <NA>        <NA>
#> 3    (0.84,1.24]              Y            Y        <NA>        <NA>
#> 4    (1.24,1.54]              Y            Y           N        <NA>
#> 5    (1.54,1.97]              Y            Y           Y           Y
#> 6    (1.97,3.22]              Y            Y           Y           Y
#>   (1.54,1.97] (1.97,3.22]
#> 1        <NA>        <NA>
#> 2        <NA>        <NA>
#> 3        <NA>        <NA>
#> 4        <NA>        <NA>
#> 5        <NA>        <NA>
#> 6           Y        <NA>
#> 
#> GDP
#>         interval [0,0.25] (0.25,14.6] (14.6,177] (177,1.66e+04]
#> 1       [0,0.25]     <NA>        <NA>       <NA>           <NA>
#> 2    (0.25,14.6]        N        <NA>       <NA>           <NA>
#> 3     (14.6,177]        Y           Y       <NA>           <NA>
#> 4 (177,1.66e+04]        Y           Y          Y           <NA>
#> 
#> Interaction detector:
#>      variable Climatezone Mining Tempchange GDP
#> 1 Climatezone          NA     NA         NA  NA
#> 2      Mining      0.8345     NA         NA  NA
#> 3  Tempchange      0.8581 0.4139         NA  NA
#> 4         GDP      0.8481 0.2389     0.3754  NA
#> 
#> Ecological detector:
#>      variable Climatezone Mining Tempchange  GDP
#> 1 Climatezone        <NA>   <NA>       <NA> <NA>
#> 2      Mining           Y   <NA>       <NA> <NA>
#> 3  Tempchange           Y      Y       <NA> <NA>
#> 4         GDP           Y      Y          Y <NA>
plot(ndvigdm)
#> Optimal discretization process ...

#> Optimal discretization result ...

#> plot factor detectors ...

#> plot risk mean values ...

#> plot risk detectors ...

#> plot interaction detectors ...

#> plot ecological detectors ...


ndvigdm1 = gdm(NDVIchange ~ Climatezone + Mining + Tempchange + GDP,
              continuous_variable = c("Tempchange", "GDP"),
              data = tibble::as_tibble(ndvi_40),
              discmethod = discmethod, discitv = discitv)
ndvigdm1
#> Explanatory variables include 2 continuous variables.
#> 
#> optimal discretization result of Tempchange
#> method             :  quantile
#> number of intervals:  6
#> intervals:
#>  -0.39277 0.3858 0.8397533 1.23631 1.54196 1.96675 3.22051
#> numbers of data within intervals:
#>  119 119 119 118 119 119
#> 
#> optimal discretization result of GDP
#> method             :  quantile
#> number of intervals:  4
#> intervals:
#>  0 0.25 14.57 176.58 16589.09
#> numbers of data within intervals:
#>  179 178 178 178
#> 
#> Geographical detectors results:
#> 
#> Factor detector:
#>      variable         qv          sig
#> 1 Climatezone 0.82183348 7.340526e-10
#> 2      Mining 0.14111542 6.734163e-10
#> 3  Tempchange 0.31797346 5.104492e-10
#> 4         GDP 0.09596755 4.022449e-11
#> 
#> Risk detector:
#> Climatezone
#>   itv    meanrisk
#> 1 Bsk 0.143572961
#> 2 Bwk 0.004536505
#> 3 Dwa 0.321735000
#> 4 Dwb 0.343155655
#> 5 Dwc 0.444868361
#> 
#> Mining
#>         itv   meanrisk
#> 1  very low 0.21008297
#> 2       low 0.03294513
#> 3    medium 0.30733460
#> 4      high 0.26695286
#> 5 very high 0.19176875
#> 
#> Tempchange
#>              itv   meanrisk
#> 1 [-0.393,0.386] 0.03611076
#> 2   (0.386,0.84] 0.11407521
#> 3    (0.84,1.24] 0.30719824
#> 4    (1.24,1.54] 0.31380373
#> 5    (1.54,1.97] 0.26461966
#> 6    (1.97,3.22] 0.18935924
#> 
#> GDP
#>              itv  meanrisk
#> 1       [0,0.25] 0.1385164
#> 2    (0.25,14.6] 0.1615299
#> 3     (14.6,177] 0.2400161
#> 4 (177,1.66e+04] 0.2764687
#> 
#> Climatezone
#>   interval  Bsk  Bwk  Dwa  Dwb  Dwc
#> 1      Bsk <NA> <NA> <NA> <NA> <NA>
#> 2      Bwk    Y <NA> <NA> <NA> <NA>
#> 3      Dwa    Y    Y <NA> <NA> <NA>
#> 4      Dwb    Y    Y    N <NA> <NA>
#> 5      Dwc    Y    Y    Y    Y <NA>
#> 
#> Mining
#>    interval very low  low medium high very high
#> 1  very low     <NA> <NA>   <NA> <NA>      <NA>
#> 2       low        Y <NA>   <NA> <NA>      <NA>
#> 3    medium        Y    Y   <NA> <NA>      <NA>
#> 4      high        Y    Y      N <NA>      <NA>
#> 5 very high        N    Y      Y    Y      <NA>
#> 
#> Tempchange
#>         interval [-0.393,0.386] (0.386,0.84] (0.84,1.24] (1.24,1.54]
#> 1 [-0.393,0.386]           <NA>         <NA>        <NA>        <NA>
#> 2   (0.386,0.84]              Y         <NA>        <NA>        <NA>
#> 3    (0.84,1.24]              Y            Y        <NA>        <NA>
#> 4    (1.24,1.54]              Y            Y           N        <NA>
#> 5    (1.54,1.97]              Y            Y           Y           Y
#> 6    (1.97,3.22]              Y            Y           Y           Y
#>   (1.54,1.97] (1.97,3.22]
#> 1        <NA>        <NA>
#> 2        <NA>        <NA>
#> 3        <NA>        <NA>
#> 4        <NA>        <NA>
#> 5        <NA>        <NA>
#> 6           Y        <NA>
#> 
#> GDP
#>         interval [0,0.25] (0.25,14.6] (14.6,177] (177,1.66e+04]
#> 1       [0,0.25]     <NA>        <NA>       <NA>           <NA>
#> 2    (0.25,14.6]        N        <NA>       <NA>           <NA>
#> 3     (14.6,177]        Y           Y       <NA>           <NA>
#> 4 (177,1.66e+04]        Y           Y          Y           <NA>
#> 
#> Interaction detector:
#>      variable Climatezone Mining Tempchange GDP
#> 1 Climatezone          NA     NA         NA  NA
#> 2      Mining      0.8345     NA         NA  NA
#> 3  Tempchange      0.8581 0.4139         NA  NA
#> 4         GDP      0.8481 0.2389     0.3754  NA
#> 
#> Ecological detector:
#>      variable Climatezone Mining Tempchange  GDP
#> 1 Climatezone        <NA>   <NA>       <NA> <NA>
#> 2      Mining           Y   <NA>       <NA> <NA>
#> 3  Tempchange           Y      Y       <NA> <NA>
#> 4         GDP           Y      Y          Y <NA>
plot(ndvigdm1)
#> Optimal discretization process ...

#> Optimal discretization result ...

#> plot factor detectors ...

#> plot risk mean values ...

#> plot risk detectors ...

#> plot interaction detectors ...

#> plot ecological detectors ...

Created on 2024-10-18 with reprex v2.1.1

SpatLyu commented 1 month ago

Now, many I/O functions in R return results as tibble, so support for tibble should be considered.

This has been achieved by Prof. Yongze Song from GD 10.3.