Skip to content

Instantly share code, notes, and snippets.

@BlasBenito
Last active May 19, 2021 14:01
Show Gist options
  • Save BlasBenito/06737ebf130bc2b7816c7844c9cb73f5 to your computer and use it in GitHub Desktop.
Save BlasBenito/06737ebf130bc2b7816c7844c9cb73f5 to your computer and use it in GitHub Desktop.
Shows how spatialRF::rf_interactions() works to find variable combinations (via multiplication and PCA) that improve model transferability.
#installing the development version of the package (v.1.1.1)
remotes::install_github(
repo = "blasbenito/spatialRF",
ref = "development",
force = TRUE,
quiet = TRUE
)
library(spatialRF)
#loading the example data
data(plant_richness_df)
#data required to execute the function
xy <- plant_richness_df[, c("x", "y")]
dependent.variable.name <- "richness_species_vascular"
predictor.variable.names <- colnames(plant_richness_df)[5:21]
#finding useful variable combinations
combinations <- rf_interactions(
data = plant_richness_df,
dependent.variable.name = dependent.variable.name,
predictor.variable.names = predictor.variable.names,
xy = xy,
importance.threshold = 0.50, #selects predictors with importance above quantile 0.5
cor.threshold = 0.60, #Pearson correlation threshold to remove redundant combinations
repetitions = 100, #number of independent spatial folds to perform spatial cross-validation
training.fraction = 0.75, #fraction of records to train and evaluate models via spatial cross-validation
seed = 1, #for reproducibility, results might change with different random seeds
verbose = TRUE
)
#data frame with all the screened variable combinations
combinations$screening
#data frame with the selected variable combinations
combinations$selected
#plotting the whole thing
patchwork::wrap_plots(combinations$plot)
#fitting a model with all the variable combinations
m <- rf(
data = combinations$data,
dependent.variable.name = combinations$dependent.variable.name,
predictor.variable.names = combinations$predictor.variable.names
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment