如何在 R studio 中测试光栅文件的共线性

问题描述 投票:0回答:1

我已将生物气候变量输入到 R studio 中,我希望在其中测试它们的共线性,以便我可以删除高度相关的变量以完成物种分布模型。到目前为止,我已经成功地堆叠了光栅文件,但还没有取得任何进展,并且无法找到有关如何为光栅运行 vif 的明确解释。任何帮助将不胜感激。

我尝试过创建一个列表,但是我无法让它工作,因为当我测试创建的列表的 vif 时,它指出该文件需要位于一个文件中。类型

r-raster multicollinearity
1个回答
0
投票

我认为您可以通过获取大量像素样本、将它们转换为数据帧、创建线性模型并正常使用

vif()
测试共线性来测试堆叠栅格数据集的多重共线性。以下是一个可重现的示例:

##Loading Necessary Packages##
if(!require(terra)){install.packages("terra")}
if(!require(car)){install.packages("car")}

#Setting Random Number Generator Seed For Reproducibility##
set.seed(94)

##Creating a blank raster template##
blank<-rast(ymin=45.5, ymax =45.75, xmin = -122.75, xmax=-122.5, ncols=400, nrows=400, crs="EPSG:4326")

##creating a raster of one predictor variable following a uniform distribution between 0 and 5000 to emulate elevation
x1<-blank
values(x1)<-runif(160000, 0, 5000)

##creating a raster of a second predictor variable ~N(mu=150, sigma=85) to emulate cation exchange capacity
x2<-blank
values(x2)<-rnorm(160000, 150, 85)

##Creating a raster of a third predictor variable heavily collinear with x1 to emulate precipitation
x3<-blank
values(x3)<-17+0.85*values(x1) + rnorm(160000, 0, 50)

##Creating a response variable ~N(mu=100, sigma=255) to emulate species abundance 
y<-blank
values(y)<-rnorm(160000, 1000, 255)

##Combining all rasters into a single multilayer raster
rst<-c(y, x1, x2, x3)
names(rst)<-c("abundance", "elevation", "cec", "precipitation")

##Taking a sample of 10000 pixels without replacement
smp<-spatSample(rst, 10000, replace=FALSE, na.rm=TRUE, as.df=TRUE)

##Creating a linear model of abundance as a function of elevation cation exchange capacity and precipitation
mod<-lm(abundance~., data=smp)

##Testing for multicollinearity using the Variance Inflation Factor
vif(mod)
© www.soinside.com 2019 - 2024. All rights reserved.