I’m looking to use lavaan confirmatory factor analysis (CFA) functionality with 120 indicators (variables) and about 400k respondents. This large sample size is causing memory issues in R. Usually I might just use a smaller batch of people, but I’m trying to replicate existing work. I’ve tried simply reading the correlation/covariance matrix into R and then doing CFA, but I’m having trouble with the getCov function. Every example I see involves manually writing an object that represents the set of correlations. However, that is not possible with a 120-by-120 matrix. I’ve tried generating a correlation matrix within R, and then reading that matrix into getCov, but am still getting a lot of errors. I imagine there is some way to format this, and I’ve tried several approaches (two below), to no avail.
Here is an example where I create a dataset of 120 variables. For the sake of ease, I set sample size to 1000. When I try to generate a correlation matrix and then use the getCov command, I get the following error:
“Error in lav_matrix_lower2full(x, diagonal = diagonal) : p == round(p, 0) is not TRUE”
set.seed(371)
df <- data.frame(matrix(NA, nrow = 1000, ncol = 120))
for (i in 1:120) {
x <- runif(1000, min=1, max=7)
df [, i] <- x
}
dim (df)
#getting correlations... I think getCov needs only one diagonal side#
cor.matrix <- round(cor(df),4)
upper<-cor.matrix
upper[upper.tri(cor.matrix)]<-NA
lower<-as.matrix (upper)
#try 1#
matrix <- getCov (x=lower, names = c('X1', 'X2', 'X3', 'X4', 'X5', 'X6', 'X7', 'X8', 'X9', 'X10', 'X11', 'X12', 'X13', 'X14', 'X15',
'X16', 'X17', 'X18', 'X19', 'X20', 'X21', 'X22', 'X23', 'X24', 'X25', 'X26', 'X27', 'X28',
'X29', 'X30', 'X31', 'X32', 'X33', 'X34', 'X35', 'X36', 'X37', 'X38', 'X39', 'X40', 'X41', 'X42', 'X43', 'X44', 'X45', 'X46',
'X47', 'X48', 'X49', 'X50', 'X51', 'X52', 'X53', 'X54', 'X55', 'X56', 'X57', 'X58', 'X59', 'X60', 'X61', 'X62', 'X63', 'X64',
'X65', 'X66', 'X67', 'X68', 'X69', 'X70', 'X71', 'X72', 'X73', 'X74', 'X75', 'X76', 'X77', 'X78', 'X79', 'X80', 'X81', 'X82',
'X83', 'X84', 'X85', 'X86', 'X87', 'X88', 'X89', 'X90', 'X91', 'X92', 'X93', 'X94', 'X95', 'X96', 'X97', 'X98', 'X99', 'X100',
'X101', 'X102', 'X103', 'X104', 'X105', 'X106', 'X107', 'X108', 'X109', 'X110', 'X111', 'X112', 'X113', 'X114', 'X115', 'X116', 'X117', 'X118',
'X119', 'X120'))
#try 2#
lower <- format_csv (as.data.frame(lower), col_names=FALSE)
matrix <- getCov (x=lower, names = c('X1', 'X2', 'X3', 'X4', 'X5', 'X6', 'X7', 'X8', 'X9', 'X10', 'X11', 'X12', 'X13', 'X14', 'X15',
'X16', 'X17', 'X18', 'X19', 'X20', 'X21', 'X22', 'X23', 'X24', 'X25', 'X26', 'X27', 'X28',
'X29', 'X30', 'X31', 'X32', 'X33', 'X34', 'X35', 'X36', 'X37', 'X38', 'X39', 'X40', 'X41', 'X42', 'X43', 'X44', 'X45', 'X46',
'X47', 'X48', 'X49', 'X50', 'X51', 'X52', 'X53', 'X54', 'X55', 'X56', 'X57', 'X58', 'X59', 'X60', 'X61', 'X62', 'X63', 'X64',
'X65', 'X66', 'X67', 'X68', 'X69', 'X70', 'X71', 'X72', 'X73', 'X74', 'X75', 'X76', 'X77', 'X78', 'X79', 'X80', 'X81', 'X82',
'X83', 'X84', 'X85', 'X86', 'X87', 'X88', 'X89', 'X90', 'X91', 'X92', 'X93', 'X94', 'X95', 'X96', 'X97', 'X98', 'X99', 'X100',
'X101', 'X102', 'X103', 'X104', 'X105', 'X106', 'X107', 'X108', 'X109', 'X110', 'X111', 'X112', 'X113', 'X114', 'X115', 'X116', 'X117', 'X118',
'X119', 'X120'))