# A Geographically Weighted Random Forest Approach to Predict Corn Yield in the US Corn Belt
## 2 Materials and Methods
### Methodology
#### 2.4.6 Experimental Design  
After data preparation, we divided the data into training and testing datasets. The training dataset is used to train the model, while the testing dataset is used in model validation. Independent variables in this study are standardized using the standard scalar. The standard scalar rescales the distribution of the data so that the mean value becomes 0 and the standard deviation becomes 1. Feature scaling is an important step in machine learning as it reduces the undue influence of individual variables on the model [82]. Different independent variables have different scales, and sometimes large values influence models more than other values. Therefore, it is necessary to scale the independent variables before model construction to normalize their influence on models. For selecting the best hyperparameters for different machine learning models, a grid search with 10-fold cross validation was used to select the best parameters [38]. Mean squared error (MSE) was used as the criterion to select the best model. The models with the lowest MSE will be used for final predictions and evaluations. The experimental design is illustrated in Figure 2. The models trained in this study include MLR, PLSR, SVR, DTR, RFR, and GWRFR. We will compare the results of GWRFR with those of the other five machine learning algorithms.  
We use (R\({}^{2}\)) and root mean squared error (RMSE) to assess model performance. The equations of R\({}^{2}\) and RMSE are as follows:  
\[R^{2}=\frac{\sum_{i=1}^{n}(y_{i}-\bar{y})^{2}}{\left(y_{i}-\overline{y}\right) ^{2}} \tag{4}\]  
Figure 2: Experimental design.  
\[RMSE=\sqrt{\frac{\sum_{i=1}^{n}(y_{i}-\hat{y})^{2}}{n}} \tag{5}\]  
One way to measure to what extent a model can capture spatial heterogeneity is to derive the spatial autocorrelation of the residuals [83]. [PERSON]'s I statistics can be used to study spatial patterns [84]. [PERSON]'s I value usually ranges from \(-1\) to \(1\). A positive value represents positive autocorrelation, a negative value denotes negative autocorrelation, and a value close to zero indicates spatial randomness [84]. The presence of spatial autocorrelation in residuals suggests that the model is not able to consider the spatial effects [85]. Highly clustered values over space can bias predictions and result in large prediction errors [86]. We calculated spatial autocorrelation for the residuals of different models. The [PERSON]'s I values of errors produced by non-spatial models such as MLR, PLSR, SVR, DTR, and RFR are compared with that of GWRFR to assess if GWRFR can better capture the spatial variability of corn yield in the US Corn Belt. For this study, if the Moran's I values are closer to zero, the errors of the model are not clustered, which means the model can incorporate the spatial relationships [11].  
Data preprocessing was performed using Python and the Jupyter Notebook [87] in Anaconda Version 3. Anaconda is a software distribution system that provides a package management system for Python and R. NumPy and GeoPandas libraries in Python were utilized for data preprocessing. The samples with incomplete information were eliminated using Pandas. To implement GWRFR, the SpatialML package in R was used, as this package was developed for spatially weighted machine learning algorithms [51]. Python and Scikit-learn [88] were used to implement the other five machine learning models. Scikit-learn provides a wide range of functionalities such as data standardization, model selection, data visualization, and model implementation. Finally, to derive the R\({}^{2}\) of machine learning models, the r2 score function in Scikit-learn was used in this research. To calculate the RMSE of the models, a new Python function was defined and used. We used the Matplotlib [89] and Seaborn [90] packages in Python to visualize the results of machine learning models. Finally, the Global Moran's I tool in ArcGIS Pro 2.8.0 was used to examine spatial autocorrelation in residuals.