# How do I study the association between categorical rasterised environmental variables in R?

I would like to study the association between categorical rasterised environmental variables in R. Is there any way to do it in R?

• This question is pretty broad -- can you expand on your question and provide any more specifics on what you're trying to accomplish? What steps have you taken thus far to identify resources in R to do what you're trying to do? What are the major gaps in the search for resources you've undertaken so far?
– dlid
Oct 12 '20 at 20:34
• I have a set of environmental variables in raster format where I want to study the dependencies between them. To do so I was thinking of using association tests such as chi-squared test. I want to know if there are any libraries in R dedicated to that. Oct 12 '20 at 21:45

The raster package in R is devoted to handle with raster data: https://cran.r-project.org/web/packages/raster/raster.pdf

I'm not quite sure about what do you want to do with your data. Since I assume your rasters contain quantitative data, you can use the function 'layerStats' to get the correlation between two rasters. However, to use it, you must join the raster in a multiband raster using 'stack'. Here is an example written by me:

library(raster)

#standardize the rasters with same extent and crs
cultural <- raster(vals=values(cultural),ext=extent(provision),
crs=crs(provision),
nrows=dim(provision)[1],ncols=dim(provision)[2])
regulating <- raster(vals=values(regulating),ext=extent(provision),
crs=crs(provision),
nrows=dim(provision)[1],ncols=dim(provision)[2])

#do the correlation matrix among the three rasters
SE <- stack(provision, regulating, cultural)
cor.SE <- layerStats(SE, 'pearson', na.rm = TRUE)