cov4gappy: Covariance matrix calculation for gappy data

View source: R/cov4gappy.R

cov4gappyR Documentation

Covariance matrix calculation for gappy data

Description

This function calculates a covoriance matrix for data that contain missing values ('gappy data').

Usage

cov4gappy(F1, F2 = NULL)

Arguments

F1

A data field.

F2

An optional 2nd data field.

Details

This function gives comparable results to cov(F1, y=F2, use="pairwise.complete.obs") whereby each covariance value is divided by n number of shared values (as opposed to n-1 in the case of cov(). Futhermore, the function will return a 0 (zero) in cases where no shared values exist between columns; the advantage being that a covariance matrix will still be calculated in cases of very gappy data, or when spatial locations have accidentally been included without observations (i.e. land in fields of aquatic-related parameters).

Value

A matrix with covariances between columns of F1. If both F1 and F2 are provided, then the covariances between columns of F1 and the columns of F2 are returned.

Examples

# Create synthetic data 
set.seed(1)
mat <- matrix(rnorm(500, sd=10), nrow=50, ncol=10)
matg <- mat
matg[sample(length(mat), 0.5*length(mat))] <- NaN # Makes 50% missing values
matg # gappy matrix

# Calculate covariance matrix and compare to 'cov' function output
c1 <- cov4gappy(matg)
c2 <- cov(matg, use="pairwise.complete.obs")
plot(c1,c2, main="covariance comparison", xlab="cov4gappy", ylab="cov")
abline(0,1,col=8)


marchtaylor/sinkr documentation built on July 4, 2022, 5:48 p.m.