Skip to contents

Utility that prints the name of correlated variables and the relative correlation coefficient value.

Usage

corVar(
  bg,
  method = "spearman",
  cor_th = NULL,
  order = TRUE,
  remove_diagonal = TRUE
)

Arguments

bg

SWD object with the locations used to compute the correlation between environmental variables.

method

character. The method used to compute the correlation matrix.

cor_th

numeric. If provided it prints only the variables whose correlation coefficient is higher or lower than the given threshold.

order

logical. If TRUE the variable are ordered from the most to the less highly correlated.

remove_diagonal

logical. If TRUE the values in the diagonal are removed.

Value

A data.frame with the variables and their correlation.

Author

Sergio Vignali

Examples

# Acquire environmental variables
files <- list.files(path = file.path(system.file(package = "dismo"), "ex"),
                    pattern = "grd",
                    full.names = TRUE)

predictors <- terra::rast(files)

# Prepare background locations
bg_coords <- terra::spatSample(predictors,
                               size = 10000,
                               method = "random",
                               na.rm = TRUE,
                               xy = TRUE,
                               values = FALSE)
#> Warning: [spatSample] fewer cells returned than requested

# Create SWD object
bg <- prepareSWD(species = "Virtual species",
                 a = bg_coords,
                 env = predictors,
                 categorical = "biome")
#>  Extracting predictor information for absence/background locations
#>  Extracting predictor information for absence/background locations [67ms]
#> 

# Get the correlation among all the environmental variables
corVar(bg,
       method = "spearman")
#>     Var1  Var2       value
#> 1   bio1  bio6  0.95135409
#> 2  bio12 bio16  0.94475589
#> 3   bio6  bio7 -0.87344980
#> 4   bio1  bio8  0.84596493
#> 5  bio16  bio6  0.74712692
#> 6   bio6  bio8  0.72867234
#> 7   bio1  bio7 -0.71191347
#> 8  bio16  bio7 -0.70275684
#> 9   bio1 bio16  0.70235845
#> 10 bio12 bio17  0.69149337
#> 11 bio12  bio6  0.68648556
#> 12 bio12  bio7 -0.67400573
#> 13  bio5  bio8  0.64898352
#> 14  bio1 bio12  0.62722952
#> 15  bio1  bio5  0.53417673
#> 16 bio16 bio17  0.46633132
#> 17 bio16  bio8  0.45811313
#> 18  bio7  bio8 -0.44179392
#> 19 bio12  bio8  0.38856291
#> 20 bio17  bio7 -0.34811898
#> 21  bio5  bio6  0.30086090
#> 22 bio17  bio6  0.26811250
#> 23  bio1 bio17  0.18529719
#> 24 bio16  bio5  0.15713642
#> 25 bio17  bio5 -0.13452379
#> 26  bio5  bio7  0.10995326
#> 27 bio12  bio5  0.09855589
#> 28 bio17  bio8  0.09219238

# Get the environmental variables that have a correlation greater or equal to
# the given threshold
corVar(bg,
       method = "pearson",
       cor_th = 0.8)
#>    Var1  Var2      value
#> 1 bio12 bio16  0.9314559
#> 2  bio1  bio6  0.9247660
#> 3  bio6  bio7 -0.8576052
#> 4  bio1  bio8  0.8552133