##' Vcov computes the variance of a univariate data set x and the covariance or correlation between x(i) and x(i+b).
##'
##' The input x must be a vector. Vcov() computes covariance between X(i) and X(i+q),where X(i) and X(i+q) are process observations obtained at times i and i + q where q =0,1,2.....bmax. The length of the output will be bmax+1. The first element in the output γ(0) will just be the variance of the dataset. The default value of bmax is 10, which means the output will be [γ(0),γ(1),.....γ(10)] where γ(q) = Cov(X(i),X(i+q))
##' @title Correlation, Variance and Covariance of a Data Set
##' @param x data
##' @param bmax γ(q) = Cov(Xi,Xi+q), the maximun value of q
##' @return Covariance Vector [γ(0),γ(1),.....γ(bmax)] where γ(q) = Cov(Xi,Xi+q)
##' @author Xiulin Xie
##' @export
##' @examples
##' Vcov(c(1:100),bmax = 9)
Vcov <- function(x, bmax = 10) {
avg <- mean(x)
cov_vector <- rep(0, (bmax + 1))
for (i in 1:(bmax + 1)) {
for (j in 1:(length(x) - i + 1)) {
cov_vector[i] <- cov_vector[i] + (x[j + i - 1] - avg) * (x[j] - avg)
}
cov_vector[i] <- cov_vector[i]/(length(x) - i + 1)
}
return(cov_vector)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.