나는이 벡터 (시계열) 계산 코드를 재현하기 위해 노력하고있어 : 래스터 연산 코드로r -이 시계열 계산을 래스터 계산으로 변환하는 방법?
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()
을 계산하십시오.
rstudio 태그는 관련이 없으므로 삭제되었습니다 (일반 R). 래스터 태그가 추가되었습니다. 우리가 모두 실행할 수있는 재현 가능한 예를 만들 수 있습니까? – Spacedman
재생 해 주셔서 감사합니다. 나는 예제를 추가했다. –