Skip to content

Commit

Permalink
More robust firstzero_ac
Browse files Browse the repository at this point in the history
  • Loading branch information
robjhyndman committed Jan 29, 2019
1 parent a27827c commit 739947a
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions R/compengine.R
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,16 @@ embed2_incircle <- function(y, boundary = NULL, acfv = stats::acf(y, length(y) -
#' @export
firstzero_ac <- function(y, acfv = stats::acf(y, N - 1, plot = FALSE, na.action = na.pass)) {
N <- length(y)
corrs <- acfv$acf[-1]
# If y doesnt change then then corrs will be a list of NaN
if (is.nan(corrs)) {
print(acfv)
tau <- which(acfv$acf[-1] < 0)
if(length(tau)==0L) # Nothing to see here
return(0)
}
for (tau in 1:(N - 1)) {
if (corrs[tau] < 0) return(tau) # we know it starts 1, so first negative will be the zero-crossing
}
return(N) # If haven't left yet, set output to sample size
else if(all(is.na(tau))) # All missing
return(0)
else if(!any(tau)) # No negatives, so set output to sample size
return(N)
else # Return lag of first negative
return(tau[1])
}

# ac_9
Expand Down

0 comments on commit 739947a

Please sign in to comment.