Learn R Programming

metRology (version 0.9-28-1)

buildCor: Functions to build correlation and covariance matrices.

Description

Functions to build and update correlation and covariance matrices using a compact specification of off-diagonal terms.

Usage

buildCor(n, cors)

updateCor(cor, cors, cor.names)

buildCov(s, covs, vars = s^2, cors, cov.names)

updateCov(cov, covs, cors, cov.names)

Arguments

n

scalar: number of rows/colums required in correlation matrix.

cors, covs

3-column matrix or data frame specification of individual correlation or covariance terms. Can also be a vector of length 3. See Details.

s, vars

vector of standard deviations or variances respectively. One of s or vars must be present.

cor.names, cov.names

vectors of names for the rows and columns of the returned matrix. cov.names defaults to names(s) if s is named.

cor, cov

correlation or covariance matrix requiring amendment.

Value

A square symmetric correlation or covariance matrix.

Details

For buildCor, the size of the returned correlation matrix is set using n; an n by n correlation matrix is returned. For buildCov the size is set to length(s) by length(s).

Each row of cors specifies a correlation term \(r_{ij}\) in the form \((i, j, r_{ij})\). That is, the first two columns give the row and column in the desired correlation matrix, and the third gives the relevant correlation coefficient. On constructing or updating the correlation matrix, \(r_{ij}\) is set equal to \(r_{ji}\), so it is only necessary to specify one of \(r_{ij}\) or \(r_{ji}\).

covs specifies covariance terms in the same way except that the third column of covs must be a covariance.

If either cors or covs is a vector of length 3, it is coerced to a matrix of three columns.

If cor.names or cov.names are present, the matrix returned has dimnames set to the names supplied.

All four functions test for positive definite return values and generate a warning if not positive definite.

References

None.

See Also

None.

Examples

Run this code
# NOT RUN {
    
    #Duplicate correlation for example for uncert()
    buildCor(4, cors=c(3, 4, 0.5))
               
    
    #Multiple correlations
    r<-buildCor(3, cors=rbind( c(1,2,0.5), c(2,3,0.25) ) )
    r
    
    updateCor(r, cors=c(1,3,0.13)) #perhaps more realistic
    
    buildCov(1:3, cors=rbind( c(1,2,0.5), c(2,3,0.25),c(1,3,0.13) ) )
    
# }

Run the code above in your browser using DataLab