r - 如何将此时间序列计算转换为栅格计算?

时间:2017-11-26 18:45:54

标签: r raster

我试图重现此向量(时间序列)计算代码:

gamma.parameters<- fitdistr(may_baseline_3months[may_baseline_3months>0],"gamma")

进入栅格计算代码。

此代码最初的作用是尝试通过最大似然估计将伽玛分布拟合到向量(时间序列)may_baseline_3months

我想做的是计算相同的东西但是使用栅格堆栈。

我尝试使用calc()函数执行此操作:

f1<-function(x) { library(MASS) return(fitdistr(x,"gamma")) } gamma.parameters<- calc(x = may_baseline_3months,fun = f1)

Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) : cannot use this function

但它没有用。 注意:我的栅格堆栈只有4层。

修改

您可以在此处下载示例数据spi

fitdistr是我主要目标的一部分。我试图计算标准降水指数。我已经用30年的月降水量的时间序列做了。

这是一个时间序列的代码,直到我库存的行:

data<-read.csv("guatemala_spi.csv",header = T,sep=";")

dates<-data[,1]
rain_1month<-data[,2]
rain_3months<-0
#Setting the first 2 elements to NA because I'm going to calcule the accumulating the rainfall for 3 month
for (i in c(1:2)) {
  rain_3months[i]<-NA
}
#Accumulating the rainfall for the rest of the data
number_of_months<-length(rain_1month)
for (j in c(3:number_of_months)) 
{
 rain_3months[j]<-0.0
 for (i in c(0:2)) 
{
 rain_3months[j] = rain_3months[j] + rain_1month[j-i]
}
}
#Extracting a time-series for the month of interest (May)
may_rain_3months<-rain_3months[substr(dates,5,6)==”05”]
dates_may<-dates[substr(dates,5,6)==”05”]
number_of_years<-length(dates_may) 

#Fitting the gama distribution by maximum likelihood estimation 
start_year<-1971
end_year<-2010
start_index<-which(substr(dates_may,1,4)==start_year)
end_index<-which(substr(dates_may,1,4)==end_year)
may_baseline_3months<-may_rain_3months[start_index:end_index] 

library(MASS)
gamma.parameters<-fitdistr(may_baseline_3months[may_baseline_3months>0],"gamma")

最后一行是我在计算栅格堆栈时遇到问题的那一行。

这是我到目前为止光栅形式的内容:

多层栅格示例here(2001至2004年每月降水量,总共48层)

#Initiating a dates vector
dates<-c("200101","200102","200103","200104","200105","200106","200107","200108","200109","200110","200111","200112",
    "200201","200202","200203","200204","200205","200206","200207","200208","200209","200210","200211","200212",
    "200301","200302","200303","200304","200305","200306","200307","200308","200309","200310","200311","200312",
    "200401","200402","200403","200404","200405","200406","200407","200408","200409","200410","200411","200412")
#Initiating a NA raster
rain_3months_1layer<-raster(nrow=1600, ncol=1673,extent(-118.4539, -34.80395, -50, 30),res=c(0.05,0.05))
values(rain_3months_1layer)<-NA

#Creating a raster stack NA of 48 layers
rain_3months<-stack(mget(rep( "rain_3months_1layer" , 48 )))

#Reading the data
rain_1month <- stack("chirps_rain_1month.tif")

#Accumulating the rainfall
number_of_months<-nlayers(rain_1month)
for (j in c(3:number_of_months)) 
{
 rain_3months[[j]]<-0.0
 for (i in c(0:2)) 
 {
 rain_3months[[j]] = rain_3months[[j]] + rain_1month[[j-i]]
 }
}

#Extracting the raster for the month of interest (May)
may_rain_3months<-stack(rain_3months[[which(substr(dates,5,6)=="05", arr.ind = T)]])
dates_may<-dates[substr(dates,5,6)=="05"]
number_of_years<-length(dates_may)

#Fitting the gama distribution by maximum likelihood estimation 
start_year<-2001
end_year<-2004
start_index<-which(substr(dates_may,1,4)==start_year)
end_index<-which(substr(dates_may,1,4)==end_year)
may_baseline_3months<-stack(may_rain_3months[[start_index:end_index]])

library(MASS)

f1<-function(x)
{
library(MASS)
return(fitdistr(x,"gamma"))
}
gamma.parameters<-  calc(x = may_baseline_3months,fun = f1)

我无法calc()计算fitdistr()到栅格堆栈。

0 个答案:

没有答案