daniel1noble / metaAidR

Functions for aiding meta-analyses in Ecology, Evolution and beyond
7 stars 6 forks source link

Covariance matrix #8

Open daniel1noble opened 7 years ago

daniel1noble commented 7 years ago

Bring this new dependency function BACK into the covariance matrix function

VmCovMat <- function(data, es_var, depend, cor = 0.5){
        data$dep<-paste(data[,"study"], data[,depend], sep="_")

        tmp <- reshape::expand.grid.df(data.frame(sd1 = sqrt(data[, es_var]), stdy1 = data[,"dep"]), data.frame(sd2 = sqrt(data[, es_var]), stdy2 = data[,"dep"]))

        tmp$cor <- ifelse((tmp$stdy1 == tmp$stdy2) & (tmp$sd1 != tmp$sd2), cor, 0)
        tmp$cov <- tmp$cor * tmp$sd1 * tmp$sd2
        corMat <- matrix(tmp$cov , nrow = nrow(data), ncol = nrow(data))
        diag(corMat) <- data[,es_var]

    return(corMat)
}