Geodetector model and “GD” R package: Q & A


Optimal Parameters-based Geographical Detectors (OPGD) Model in R

## install R package "GD"

To cite GD” R package in publications, please use:
“The OPGD model was performed using ‘GD’ R package (Song et al., 2020).”

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.

Our research team published a series of articles about new methods of spatial stratified heterogeneity theory in top journals: Luo, Song*, et al. (2022)Zhang, Song*, et al. (2022)Song et al. (2021)Luo, Song*, et al., (2021)Song et al. (2020)Song et al. (2018).

If you have a question, please send me an email. I will solve your question as soon as possible.

Q1: What is recommended number of observations for spatial analysis using Geodetector (GD) or OPGD (optimal parameters-based geographical detector) models? How many breaks are recommended for spatial data discretization?

A1: Spatial unit should be defined before modelling because the number of spatial observations is affected by the spatial unit. According to Section 2.1 Optimal spatial data discretization in Song et al. (2021), “there are two applicable strategies based on the number of observations and practical requirements.” For a relatively small data set, the recommended number of breaks is from 3 to 6 (or so). For a relatively large amount of data, the recommended break number is an integer sequence from 3 to 22 (or so), and the discretization method is the quantile break.

Q2: If GD/OPGD models and the “GD” R package work for large datasets? How much time does it cost?

A1: GD works for large datasets. According to the analysis in Section 5 Discussion in Song et al. (2020), “When the sample size reaches 1000, 10 000, 100 000, only 0.05 s, 0.14 s, and 1.55 s are used for simultaneous computation of four parts of geographical detectors by the GD package, respectively.” If you cost too much time and fail to get results, please stop running and check the data and steps of modelling.
If you try to apply GD to a very large dataset, please pay attention to the spatial distribution and density distribution of data, and spatial data discretization during modelling. Practical data is usually very complex in the real world. Please make sure you have an in-depth understanding of data of geographical attributes before spatial modelling.

Q3: GD package runs well for most variables, but it can’t return outcomes for a few variables after a long-time running. What is the issue?

A3: Sometimes because observation data contains “NA”. Please remove NA before computation. If the issue still can’t be solved, please compute spatial data discretization and GD models separately.
If data doesn’t contain NA, but the GD R package still can’t return results, some explanatory variables may contain too many same value data. This will cause all observations in a spatial zone to be identical, the standard deviation is zero, and GD can’t return results. If your case has this issue, you may address it through the following approaches: (1) increasing the size of spatial units to reduce the number of observations; (2) using quantile breaks; and (3) trying to use higher resolution data of this explanatory variable to avoid too many same values of the variable.
Please make sure that there are at least two (a few are recommended) observations with different values within each of the spatial zones derived from spatial discretization.
It is recommended to use “quantile breaks” to manually run spatial discretization to check the basic characteristics of variable data. If spatial data discretization runs well, it should work well for GD.
If the number of observations is higher than 1000, it is recommended to use “quantile breaks” for modelling, because the data is large enough and quantile break is more reliable than equal, natural, standard deviation, and geometrical breaks.
The Geographically Optimal Zones-based Heterogeneity (GOZH) model is recommended if you would like to try the improved GD models (See Q4).

Q4: Are there any advanced GD models for more accurate and effective modelling?

A4: Currently, there are the following advanced GD models:
(1) Song et al. (2020) developed an Optimal Parameters-based Geographical Detector (OPGD) for characterising spatial heterogeneity, identifying geographical factors and interactive impacts of factors, and estimating risks. Software: R package “GD”. The applications include Song et al. (2018), and Luo, Song*, et al., (2021).
(2) Song et al. (2021) developed an Interactive Detector for Spatial Associations (IDSA) for estimating the power of interactive determinants (PID) from a spatial perspective. The IDSA model considers spatial heterogeneity, spatial autocorrelation, and spatial fuzzy overlay of multiple explanatory variables for calculating PID. Software: R package “IDSA”.
(3) Luo, Song*, et al. (2022) developed a Geographically Optimal Zones-based Heterogeneity (GOZH) for identifying individual and interactive determinants of geographical attributes (e.g., global soil moisture) across a large study area. GOZH can identify optimal spatial zones and compute the maximum power of determinant (PD) values using an Ω-index.
(4) Zhang, Song*, et al. (2022) developed a Robust Geographical Detector (RGD) model for the robust estimation of PD values.

A summary and comparison of OPGD, SPADE, and IDSA in terms of data analysis and R software packages can be found in Introduction to Spatial Stratified Heterogeneity Models in R.

Q5: How to export results to data.frame or spreadsheet?

A5: The outcomes of GD/OPGD models are saved as a list of dataframe. There are two options to export results. First, you can print results, and copy to excel. Second, you can use “lapply” and “write.csv” to export the list of dataframe to .csv files.

Q6: Why the sum of q values of two variables are not equal to the q value of the interaction of the two variables?

A6: The primary reason is that the sum of Q values of individual variables is the nonlinear enhanced or weakened relations of variables.

Q7: When I plot the results of “gdm” or spatial discretization functions, it returns multiple figures, but I can only see the last one. How to find previous figures?

A7: Please use RStudio and click “last figure” button to check the last few figures.

Q8: When I plot figures, some texts or elements are overlapped with legends or other elements. How to solve this issue?

A8: Please drag the plotting space in Rstudio to a relatively large area, and then run the plotting codes. The size of plots can be changed.

Q9: In gdm function, everything is good, but it returns an error that continuous variable names are not matched with data.frame. How to solve this issue?

A9: This is because the data set is not a “real data.frame”. Please use the following code to convert it to data.frame.
data <-


Song, Y., Wang, J.F., Ge, Y., et al. An optimal parameters-based geographical detector model enhances geographic characteristics of explanatory variables for spatial heterogeneity analysis: cases with different types of spatial dataGIScience & Remote Sensing, 2020. 57(5): 593-610

Song, Y., Wright, G., Wu, P., Thatcher, D., et al. Segment-Based Spatial Analysis for Assessing Road Infrastructure Performance Using Monitoring Observations and Remote Sensing DataRemote Sensing, 2018. 10(11): 1696.

Song, Y., Wu, P. An interactive detector for spatial associationsInternational Journal of Geographical Information Science, 2021.

Zhang, Z., Song, Y.*, & Wu, P. Robust geographical detectorInternational Journal of Applied Earth Observation and Geoinformation, 2022. 109, 102782.

Luo, P., Song, Y.*, Huang, X., Ma, H., et al. Identifying determinants of spatio-temporal disparities in soil moisture of the Northern Hemisphere using a geographically optimal zones-based heterogeneity modelISPRS Journal of Photogrammetry and Remote Sensing, 2022. 185, 111-128.

Song, Y., Wu, P., Gilmore, D., et al. A Spatial Heterogeneity-Based Segmentation Model for Analyzing Road Deterioration Network Data in Multi-Scale Infrastructure SystemsIEEE Transactions on Intelligent Transportation Systems, 2020

Luo, P., Song, Y.* Wu, P. Spatial disparities in trade-offs: economic and environmental impacts of road infrastructure on continental levelGIScience & Remote Sensing, 2021.