From fb1450347caf7151224c53e35bf6fef3329eb64b Mon Sep 17 00:00:00 2001 From: Jessica Burnett Date: Tue, 4 Dec 2018 13:17:46 -0600 Subject: [PATCH 1/2] Add option to omit plots Per open request by @SophieSt submitted June 2018. Added argument create.plots (default = FALSE) logical for printing plots to screen. --- earlywarnings/R/generic_ews.R | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/earlywarnings/R/generic_ews.R b/earlywarnings/R/generic_ews.R index 74692f6..8ce1b1b 100644 --- a/earlywarnings/R/generic_ews.R +++ b/earlywarnings/R/generic_ews.R @@ -48,7 +48,7 @@ # Author: Vasilis Dakos, January 2, 2012 -generic_ews<-function(timeseries,winsize=50,detrending=c("no","gaussian","linear","first-diff"),bandwidth=NULL,logtransform=FALSE,interpolate=FALSE,AR_n=FALSE,powerspectrum=FALSE){ +generic_ews<-function(timeseries,winsize=50,detrending=c("no","gaussian","linear","first-diff"),bandwidth=NULL,logtransform=FALSE,interpolate=FALSE,AR_n=FALSE,powerspectrum=FALSE, create.plots = FALSE){ require(lmtest) require(nortest) @@ -166,6 +166,7 @@ generic_ews<-function(timeseries,winsize=50,detrending=c("no","gaussian","linear # Plotting # Generic Early-Warnings + if(create.plots == T){ dev.new() par(mar=(c(0,2,0,1)+0),oma=c(7,2,3,1),mfrow=c(5,2)) plot(timeindex,Y,type="l",ylab="",xlab="",xaxt="n",las=1,xlim=c(timeindex[1],timeindex[length(timeindex)])) @@ -239,9 +240,10 @@ generic_ews<-function(timeseries,winsize=50,detrending=c("no","gaussian","linear contour(x=(spectfft$freq[2:length(spectfft$freq)]),y=(seq(1,ncol (nSPECT),by=1)),log(nSPECT[2:length(spectfft$freq),]),add=TRUE) mtext("Power spectrum within rolling windows",side=3,line=0.2, outer=TRUE) } + } # End plotting # Output out<-data.frame(timeindex[mw:length(nsmY)],nARR,nSD,nSK,nKURT,nCV,nRETURNRATE,nDENSITYRATIO,nACF) colnames(out)<-c("timeindex","ar1","sd","sk","kurt","cv","returnrate","densratio","acf1") return(out) - } \ No newline at end of file + } From fd53165e560e0a87c98382f42e9436a176960fc2 Mon Sep 17 00:00:00 2001 From: Jessica Burnett Date: Tue, 7 May 2019 15:54:00 -0500 Subject: [PATCH 2/2] Ensure obj `timeseries` is of class "timeseries" Force `timeseries` to ts() class if not already. --- earlywarnings/R/sensitivity_ews.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/earlywarnings/R/sensitivity_ews.R b/earlywarnings/R/sensitivity_ews.R index 501a172..69d4804 100644 --- a/earlywarnings/R/sensitivity_ews.R +++ b/earlywarnings/R/sensitivity_ews.R @@ -49,7 +49,8 @@ sensitivity_ews<-function(timeseries,indicator=c("ar1","sd","acf1","sk","kurt"," require(moments) require(fields) -#timeseries<-ts(timeseries) #strict data-types the input data as tseries object for use in later steps + # Ensure obj timeseries is of class "timeseries" + if(!is.ts(timeseries)) timeseries <- ts(timeseries) #strict data-types the input data as tseries object for use in later steps timeseries<-data.matrix(timeseries) if (dim(timeseries)[2]==1){ Y=timeseries @@ -218,4 +219,4 @@ for (ti in 1:length(tw)){ out<-data.frame(Ktauestind) rownames(out)<-tw return(out) -} \ No newline at end of file +}