R/Vcovmatrix.R

Defines functions Vcovmatrix

Documented in Vcovmatrix

##' Vcovmatrix computes the variance of a univariate data set x and the Covariance Matrix for a univariate data x
##'
##' The input x must be a vector. Vcovmatrix() computes Covariance Matrix for a data set. The (i,j) element of the output matrix is γ(|i-j|) = Cov(X(b),X(b+|i-j|)). More specifically, the dimension of the output matrix is (bmax +1),(bmax+1), and the diagonal element (i,i) is γ(0) which is the variance of the data set. The default value of bmax is 10, which means first row of the output will be [γ(0),γ(1),.....γ(10)], and ith row of the output will be [γ(i-1),γ(i),.....γ(11-i)] where γ(q) = Cov(X(i),X(i+q))
##' @title Covariance Matrix of a Data Set
##' @param x data
##' @param bmax γ(q) = Cov(Xi,Xi+q) , the maximun value of q
##' @return Covariance Matrix M where M(i,j)= γ(|i-j|) = Cov(X(b),X(b+|i-j|))
##' @author Xiulin Xie
##' @export
##' @examples
##' Vcovmatrix(c(1:100),bmax = 9)

Vcovmatrix <- function(x, bmax = 10) {
    cov_vector <- Vcov(x, bmax)
    cov_matrix <- matrix(rep(0, (bmax + 1) * (bmax + 1)), nrow = bmax + 1)
    for (i in 1:(bmax + 1)) {
        for (j in 1:(bmax + 1 - i + 1)) {
            cov_matrix[j, (j + i - 1)] <- cov_vector[i]
            cov_matrix[(j + i - 1), j] <- cov_vector[i]
        }
    }
    return(cov_matrix)
}
XiulinXie/SPCmonitor2 documentation built on Dec. 10, 2019, 12:10 a.m.