R/auto.corr.R

Defines functions auto.corr

Documented in auto.corr

#' @title Calculating autocorrelation
#'
#' @description Estimates the autocorrelation in the data.
#' This function is used by other functions and will generally not be used directly by users.
#'
#' @param x vector of sample means
#'
#' @param model the model being evaluated
#'
#' @details This function calculates the autocorrelation in a vector of sample means
#' defined as the correlation of the first n-1 observations with the last n-1. The
#' autocorrelation is calculated directly on the sample means if the evaluated model is stasis.
#' If a different model is evaluated (random walk or directional trend), the data is
#' detrended prior to the calculation of autocorrelation.
#' @export
#'
#' @return the autocorrelation
#'
#'


auto.corr <- function(x, model, tt=NULL, int=NULL, mstep=NULL){
  if (model=="RW" | model=="accel_decel")
    {
    x<-x-x[1]
    x<-diff(x,1)
    x<-c(0,x)
  }
  
  if (model=="trend")
    {
    x<-x-(int+mstep*tt)
  }
 
  
  return(cor(x[1:length(x)-1],x[2:length(x)]))
}
klvoje/adePEM documentation built on Feb. 24, 2023, 1:28 p.m.