Calculate the correlation, covariance, or chi-squared, Cramer's V, or Kruskal-Wallis's H between two or more rasters. Note that cells that are NA for any raster are ignored across all rasters.
Usage
# S4 method for class 'GRaster'
layerCor(
x,
fun = "cor",
cor = "Pearson",
correct = TRUE,
simulate = FALSE,
nSim = 2000,
integerAsNumeric = TRUE,
na.rm = TRUE,
verbose = FALSE
)Arguments
- x
A
GRasterwith two or more layers. Partial matching is used and capitalization ignored.- fun
Character: Name of the statistic(s) to calculate:
"cor"(default): Pearson sample correlation (i.e., the denominator isn - 1). Appropriate for numeric-numeric raster comparisons."cov": Covariance."chisq": Chi-squared test and Cramer's V. Default for integer-integer, factor-factor, or integer-factor raster comparisons."kw": Kruskal-Wallis H statistic. Appropriate for integer-numeric or factor-numeric raster comparisons."auto": Automatically select the appropriate statistic based on the data types of the rasters and calculates the (approximate) effect size and (where necessary) rescale to the range from 0 to 1. If any two rasters are numeric, then the absolute value of the Pearson or Spearman correlation will be calculated (depending on argumentcor). If any two rasters are integer or factor, then Cramer's V will be returned. If any two are numeric and factor/integer, then the square root of the proportion of variance explained by the Kruskal-Wallis test will be returned. This issqrt(H / (N - 1), whereHis the KW test statistic andNthe number of non-NAcells.
- cor
Either 'pearson' (default) or 'spearman'. Only used if
fun = "cor"orfun = "auto"and any two rasters are numeric. Indicates the type of correlation statistic to calculate. Capitalization is ignored and partial matching is used.- correct
Logical (only used if
fun = "chisq"): IfTRUE(default), then apply continuity correction when computing the test statistic for 2 by 2 tables: one half is subtracted from all |O - E| differences; however, the correction will not be bigger than the differences themselves. No correction is performed ifsimulate = TRUE.- simulate
Logical (only used if
fun = "chisq"): IfTRUE, then the p-value will be estimated by Monte Carlo simulation, usingstats::chisq.test()]. This is recommended when there are many categories, because the chi-squared distribution is not a good approximation of the distribution of the test statistic in this case. The default isFALSEbecause simulating p-values can be time-consuming.- nSim
Numeric or integer (only used if
fun = "chisq"): Number of replicates for Monte Carlo simulation whensimulateisTRUE. The default is 2000.- integerAsNumeric
Logical: If
TRUE(default), then treat integer rasters as numeric. This is useful for rasters that are stored as integers but are actually continuous variables. IfFALSE, then treat integer rasters as categorical variables. Only applicable for Kruskal-Wallis test or whencor = 'auto'.- na.rm
Logical: If
TRUE(default), then remove cells withNAvalues in any raster. IfFALSE, then pairwise comparisons of rasters will use all pairs of non-NAcells, even if they areNAin otherGRasters.- verbose
Logical: If
TRUE, then display progress. Default isFALSE. Ignored for some values offun.
Value
The output depends on the selected statistic:
"cor": A correlationmatrix. An attribute "n" gives the number of cells used in each pairwise correlation."cov": A covariancematrix."chisq": Alistwith five or six elements:chisq: Amatrixof chi-squared values for each pairwise comparison;df: Amatrixof degrees of freedom for each pairwise comparison (only included ifsimulate = FALSE);p.value: Amatrixof p-values for each pairwise comparison. Note that most rasters have so many cells that even very small differences create very small p-values, so do not get too excited;nCats1: Amatrixwith the number of categories/integer values for each raster in each pairwise comparison;nCats2: Amatrixwith the number of categories/integer values for each raster in each pairwise comparison;nCells: Amatrixwith the number of cells used in each pairwise comparison.
"kw": A matrix with H values for each comparison."auto": Alistwith these elements:stat: A character vector with the name of the statistic calculated for each pairwise comparison.relative.effect.size: A numeric matrix with the relative effect size of each comparison. Values are in the range of 0 to 1.
Examples
if (grassStarted()) {
# Setup
library(terra)
# Example data
madChelsa <- fastData("madChelsa")
# Convert a SpatRaster to a GRaster:
chelsa <- fast(madChelsa)
# Correlation
layerCor(chelsa, "cor", na.rm = FALSE) # Pearson correlation
layerCor(chelsa, "cor", cor = "spearman", na.rm = FALSE) # Spearman correlation
# Covariance
layerCor(chelsa, "cov", na.rm = FALSE)
# To illustrate categorical tests, force two layers to be of type integer
chelsa[[1:2]] <- as.int(chelsa[[1:2]])
# Chi-^2 and Cramer's V (integer vs integer)
layerCor(chelsa[[1:2]], "chisq", na.rm = FALSE)
# Kruskal-Wallis test (integer vs continuous)
integerCont <- c(chelsa[[1]], chelsa[[3]])
layerCor(integerCont, "kw", na.rm = FALSE)
# automatic by data type
layerCor(chelsa, "auto", cor = "spear", na.rm = FALSE,
verbose = TRUE, integerAsNumeric = FALSE)
}
