| layerCor,GRaster-method | R Documentation |
Calculate the correlation, covariance, or chi-squared, Cramer's V, or Kruskal-Wallis's H between two or more rasters.
## S4 method for signature 'GRaster'
layerCor(
x,
fun = "cor",
cor = "Pearson",
correct = TRUE,
simulate = FALSE,
nSim = 2000,
integerAsNumeric = TRUE,
na.rm = TRUE,
verbose = FALSE
)
x |
A |
fun |
Character: Name of the statistic(s) to calculate:
|
cor |
Either 'pearson' (default) or 'spearman'. Only used if |
correct |
Logical (only used if |
simulate |
Logical (only used if |
nSim |
Numeric or integer (only used if |
integerAsNumeric |
Logical: If |
na.rm |
Logical: If |
verbose |
Logical: If |
The output depends on the selected statistic:
"cor": A correlation matrix. An attribute "n" gives the number of cells used in each pairwise correlation.
"cov": A covariance matrix.
"chisq": A list with five or six elements:
chisq: A matrix of chi-squared values for each pairwise comparison;
df: A matrix of degrees of freedom for each pairwise comparison (only included if simulate = FALSE);
p.value: A matrix of 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: A matrix with the number of categories/integer values for each raster in each pairwise comparison;
nCats2: A matrix with the number of categories/integer values for each raster in each pairwise comparison;
nCells: A matrix with the number of cells used in each pairwise comparison.
"kw": A matrix with H values for each comparison.
"auto": A list with 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.
terra::layerCor(), stats::cor(), stats::cov()
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)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.