I am trying to convert a correlation matrix to a covariance matrix using cor2cov
in R.
library(MBESS)
eff_1971 <- c(NA, .56, .25, .25, .22, -.47, -.01, -.06)
eff_1972 <- c(NA, NA, .23, .23, .25, .47, -.01, .03)
annual_earnings_1970 <- c(NA, NA, NA, .88, .83, -.02, -.28, -.14)
annual_earnings_1971 <- c(NA, NA, NA, NA, .88, -.02, .21, -.29)
annual_earnings_1972 <- c(NA, NA, NA, NA, NA, .03, .06, .21)
change_eff_1971_1972 <- c(NA, NA, NA, NA, NA, NA, 0.0, .1)
change_ann_earn_1970_1971 <- c(NA, NA, NA, NA, NA, NA, NA, -.29)
change_ann_earn_1971_1972 <- c(NA, NA, NA, NA, NA, NA, NA, NA)
df <- data.frame(eff_1971,
eff_1972,
annual_earnings_1970,
annual_earnings_1971,
annual_earnings_1972,
change_eff_1971_1972,
change_ann_earn_1970_1971,
change_ann_earn_1971_1972)
df <- as.matrix(df)
sd <- c(.82, .82, .52, .51, .50, .77, .25, .25)
cor2cov(df, sd)
However, I get this error message:
Error in cor2cov(df, sd) :
The object 'cor.mat' should be either a symmetric or a triangular matrix
Does anyone know how I can fix this error?
Thank you!
You can make df triangular by setting the diag to 1 and the upper values to 0
diag(df) <- 1
df[is.na(df)] <- 0