0

我正在尝试使用cor2covR将相关矩阵转换为协方差矩阵。

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)

但是,我收到此错误消息:

Error in cor2cov(df, sd) : 
  The object 'cor.mat' should be either a symmetric or a triangular matrix

有谁知道我该如何解决这个错误?

谢谢!

4

1 回答 1

0

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
于 2022-02-28T00:37:15.670 回答