问题描述
##希望用R语言做偏相关分析 图片。例如,当我有降水(2000-2017)和温度数据(2000-2017)和植被净初级生产力(2000-2017)的栅格数据时,我想在降水为持续的。我将npp栅格数据(2000-2017)、降水栅格数据(2000-2017)、温度栅格数据(2000-2017)组合成一个栅格堆栈,共54个波段,1-18个波段代表时间序列NPP,19-36 波段代表温度时间序列,37-54 代表降水时间序列。进行偏相关分析,降水一定时,NPP与温度的偏相关。
##combined raster bb :NPP(2000-2017);Precipitation(2000-
2017);temperature2000-2017
bb<-stack("G:\\NPP_Partial_relation\\npp_tem_pre")
plot(bb)
#partial correlation function
stack.correlation <- function(stack1){
##output template
cor.map <- raster(stack1)
# the number of bands
rnlayers=nlayers(stack1)
# the function takes a vector,partitions it in one third,then partial correlates
# returning the partial correlation coefficient.
stack.sequence.cor <- function(myvec,na.rm=T){
#print(myvec)
##delete infinite value
if (all(is.infinite(myvec))) {
NA_real_
} else {
####extract the time series of NPP data (1-18 layer means 2000-2017)
myvecT1<-myvec[1:(length(myvec)/3)]
####extract the time series of temperature data (19-36 layer means 2000-2017)
myvecT2<-myvec[((length(myvec)/3)+1):((length(myvec)/3)*2)]
##extract the time series of precipitation data (37-54 layer means 2000-2017)
myvecT3<-myvec[(((length(myvec)/3)*2)+1):length(myvec)]
###dataframe
tt<-data.frame(myvecT1,myvecT2,myvecT3)
#print(tt)
##Partial correlation function:pcor.test()
ttt<-pcor.test(tt[,1],tt[,2],3])
tttt<-ttt$estimate
return(tttt)
}
}
# apply the function above to each cell and write the partial correlation
# partial coefficient to the output template.
cor.map <- stackApply(stack1,indices = rep(1,rnlayers),fun = stack.sequence.cor)
#print(cor.map)
return(cor.map)
}
cor_r=stack.correlation(bb)
#Error in (function (classes,fdef,mtable) : unable to find an inherited method for function ‘writeValues’ for signature ‘"RasterLayer","NULL"’
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)