Dear lavaan project team,
I am wondering how to compute a weighted covariance/correlation matrix using the lavcor. In the long run, I want to combine the process with missing values, but I already fail to achieve it with a full data case. The lavcor function does not have an argument for weights, but the help mentions that any arguments from lavaan can be used (in this case sampling.weights). For a minimal example of my code (with really excessive weights):
M <- matrix(c(1, 0.7, 0.7, 0.5,
0.7, 1, 0.95, 0.3,
0.7, 0.95, 1, 0.3,
0.5, 0.3, 0.3, 1),
nrow=4, ncol=4)
set.seed(1901)
data_small <- as.data.frame(MASS::mvrnorm(n = 30, mu = rep(0,4), Sigma = M))
weights <- c(rep(0.3, 15), rep(100000,15))
lavaan::lavCor(data_small, estimator = "ML", output = "cov", sampling.weights = weights)
lavaan::lavCor(data_small, estimator = "ML", output = "cov")
Looking at the help from the lavaan function, sampling.weights should be a variable from the dataset. Therfor, I included the weights as a variable, which would probabaly not be an option having missing values using an EM-algorithm, but it does not change anything.
data_small$weights <- weights
lavaan::lavCor(data_small, estimator = "ML", output = "cov", sampling.weights = "weights")
lavaan::lavCor(data_small[,1:4], estimator = "ML", output = "cov")
I would be glad about any pointers for corrections in the code.All the best and many thanks,
Kai