# R/cor.lag.R In YRmisc: Y&R Miscellaneous R Functions

#### Documented in cor.lag

```###############################################################################################
###############################################################################################

#' @name cor.lag
#' @aliases cor.lag
#' @description Calculating correlation of two vectors with lag and lead periods. The correlations are
#' used to determine the lag or lead effect between two variables. The correlation function uses "na.or.complete"
#' method and calculate the Pearson's correlation.
#' @param x :the moving  vector
#' @param y :the fixed vector
#' @param lag :number of lag periods
#' @examples cor.lag(mtcars[,1],mtcars[,2],3,3)

# output dataframe
a <- data.frame(matrix(nrow = 1, ncol = lag+lead+1))

for(i in 1:lag){

b <- na.omit(cbind(cv.lag(x,i),y))
a[i]<-cor(b[,1],b[,2],use = "na.or.complete")

}

a[lag+1] <- cor(x,y,use = "na.or.complete")

a[i+lag+1] <- cor(b[,1],b[,2],use = "na.or.complete")

}

return(a)

}
```

## Try the YRmisc package in your browser

Any scripts or data that you put into this service are public.

YRmisc documentation built on March 25, 2020, 5:13 p.m.