在科研中,我们经常会碰到如何去量化多个因子对某个要素的贡献程度,其贡献程度可以用相对重要性来进行量化,本文提供一套如何在R中进行相对重要性分析的代码,在本人发表的论文中<Yin et al. 2020. Irrigation water consumption of irrigated cropland and its dominant factor in China from 1982 to 2015. Advance in water resources. 143. doi: 10.1016/j.advwatres.2020.103661>, 也是用该方法量化了气候变化和人类活动对总灌溉耗水变化的影响。
基于面板数据
用面板上的数据进行相对重要性分析,下面的代码是用R中自带的例子进行说明,使用者需要先安装relaimpo包,相对重要性的原理是用多元回归的方法,得到的结果是每个要素的R2, 即每个要素能够解释Y的程度,但需要结合总R2来进行解释,总R2过低(低于0.5)则没有意义,说明选择的要素不合理。
@ author yinlichang3064@163.com
library("relaimpo")
data(swiss)
bootswiss <- boot.relimp(swiss, b = 100,
type = c("lmg", "genizi","first", "car"),
rank = TRUE, diff = TRUE, rela = TRUE)
booteval.relimp(bootswiss)
plot(booteval.relimp(bootswiss))
上述代码可以直接把结果绘制出来,该图已经达到了论文的出版要求,使用者可以直接导出放在论文中。
基于栅格数据
如果想将该方法用在长时间序列的栅格数据上来的化,那么首先需要将每个要素和Y转化成.csv格式,转化的时候注意保留索引位置,为了之后将csv的数据重新返回到对应的栅格数据中,提供的R代码如下,这里是计算影响WUE的要素的重要程度。
library("relaimpo")
albedo=read.csv('C:/Users/lenovo/Documents/WeChat Files/ylc107919293/FileStorage/File/2020-06/climate/albedo.csv',header = FALSE)
lai=read.csv('C:/Users/lenovo/Documents/WeChat Files/ylc107919293/FileStorage/File/2020-06/climate/lai.csv',header = FALSE)
pre=read.csv('C:/Users/lenovo/Documents/WeChat Files/ylc107919293/FileStorage/File/2020-06/climate/pre.csv',header = FALSE)
rad=read.csv('C:/Users/lenovo/Documents/WeChat Files/ylc107919293/FileStorage/File/2020-06/climate/rad.csv',header = FALSE)
temp=read.csv('C:/Users/lenovo/Documents/WeChat Files/ylc107919293/FileStorage/File/2020-06/climate/temp.csv',header = FALSE)
vpd=read.csv('C:/Users/lenovo/Documents/WeChat Files/ylc107919293/FileStorage/File/2020-06/climate/vpd.csv',header = FALSE)
wind=read.csv('C:/Users/lenovo/Documents/WeChat Files/ylc107919293/FileStorage/File/2020-06/climate/wind.csv',header = FALSE)
wue=read.csv('C:/Users/lenovo/Documents/WeChat Files/ylc107919293/FileStorage/File/2020-06/climate/wue.csv',header = FALSE)
resultz=array(data=0,dim=c(20762,9))
for (i in 1:20762){
d1=as.numeric(albedo[i,])
d2=as.numeric(lai[i,])
d3=as.numeric(pre[i,])
d4=as.numeric(rad[i,])
d5=as.numeric(temp[i,])
d6=as.numeric(vpd[i,])
d7=as.numeric(wind[i,])
d8=as.numeric(wue[i,])
bootswiss <- boot.relimp(d8~d1+d2+d3+d4+d5+d6+d7, b = 100,
type = c("lmg"),
rank = TRUE, diff = TRUE, rela = TRUE)
a1=booteval.relimp(bootswiss)
a2=a1@lmg
a3=a1@R2
vv=c(a2,a3)
resultz[i,]=vv
}
write.csv(resultz,'C:/Users/lenovo/Documents/WeChat Files/ylc107919293/FileStorage/File/2020-06/climate/相对重要性.csv')
在用上述代码的时候请引用以下文献:
Yin et al. 2020. Irrigation water consumption of irrigated cropland and its dominant factor in China from 1982 to 2015. Advance in water resources. 143. doi: 10.1016/j.advwatres.2020.103661
如有更多需求,请查看个人介绍
网友评论