Skip to content

Instantly share code, notes, and snippets.

@SwampThingPaul
Created October 19, 2022 18:52
Show Gist options
  • Save SwampThingPaul/5568f3156e2f78579e757b878fee3f00 to your computer and use it in GitHub Desktop.
Save SwampThingPaul/5568f3156e2f78579e757b878fee3f00 to your computer and use it in GitHub Desktop.
recurrence interval analysis
## Example Recurrence (or return) interval analysis
## functions to calculate return interval for
## empirical, Gumbel and Log Pearson distributions
library(lmom)
recur.fun=function(x){
#Sorting data by decreasing order
sorted.values<-sort(x,decreasing=T)
p<-ppoints(sorted.values)
#Computing the empirical recurrence time
tr<-1/p
#Estimating the parameters for Gumbel distribution
fit<-samlmu(x)
para<-pelgum(fit)
para
#Estimating the parameters for Log Pearson type 3 distribution
para3<-pelpe3(fit)
para3
rslt=data.frame(dat.val=sorted.values,
emp.rec.tim=tr,
gumbel=1/(1-cdfgum(sorted.values,para)),
LP3=1/(1-cdfpe3(sorted.values,para3)))
return(rslt)
}
## quick example
t=seq(0,10,0.1)
ex.dat=sin(t)
plot(ex.dat)
tmp=recur.fun(ex.dat)
min(subset(tmp,dat.val>=0.5)$emp.rec.tim,na.rm=T)
min(subset(tmp,dat.val>=0.5)$gumbel,na.rm=T)
min(subset(tmp,dat.val>=0.5)$LP3,na.rm=T)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment