#' Sample Covariance
#'
#' @param x a matrix
#'
#' @return xcov the sample covariance
#' @export
#'
#' @examples samp_cov(matrix(1:9,nrow=3))
samp_cov <- function(x) {
n = nrow(x)
p = ncol(x)
xcov <- matrix(0, nr=p, nc=p)
for(i in 1:p) {
xibar = samp_mean(x[,i])
for(k in 1:p) {
xkbar = samp_mean(x[,k])
for(j in 1:n) {
xcov[i,k] <- xcov[i,k] + ((x[j,i]-xibar)*(x[j,k]-xkbar))
}
xcov[i,k] <- xcov[i,k] / n
}
}
return(xcov)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.