This forum is about the metaSEM package for meta-analysis

Posted on
Picture of user. tbates Joined: 07/31/2009
Forums

Mike Cheung's metaSEM package is introduced here

Post questions to this forum

Replied on Fri, 11/27/2020 - 12:11
No user picture. pascofearon Joined: 05/07/2013

Hi Mike,
Hope you're well. I was just wondering whether there's a simple way to bootstrap the indirect effect in metasem (tssem2 or osmasem).

Thanks,

Best wishes,

Pasco

Replied on Thu, 08/31/2023 - 21:05
No user picture. sharmel Joined: 07/31/2023

Hello All,

In my reproducible example below, you'll see that `Cov_R` (sampling covariances for the R matrix) is positive definite.

However, why when I run the following I get an error message saying?:

metaSEM::wls(R, aCov = Cov_R, RAM = RAM3, n = 9201)
> [1] Error: "Cov" is not positive definite.

Thanks,
Simon
#--- Full Reproducible R code:
R <- structure(c(1, 0.5846, 0.5476, 0.4127, 0.6029, 0.5104, 0.4724,
0.4289, 0.5846, 1, 0.2701, 0.4449, 0.3114, 0.3607, 0.5946, 0.605,
0.5476, 0.2701, 1, 0.4459, 0.5791, 0.4243, 0.4671, 0.5854, 0.4127,
0.4449, 0.4459, 1, 0.4677, 0.5889, 0.6848, 0.5456, 0.6029, 0.3114,
0.5791, 0.4677, 1, 0.466, 0.578, 0.607, 0.5104, 0.3607, 0.4243,
0.5889, 0.466, 1, 0.6488, 0.5677, 0.4724, 0.5946, 0.4671, 0.6848,
0.578, 0.6488, 1, 0.3737, 0.4289, 0.605, 0.5854, 0.5456, 0.607,
0.5677, 0.3737, 1), dim = c(8L, 8L), dimnames = list(c("L2R",
"L2DA", "L2DF", "L2V", "L2G", "L2P", "L2M", "L2L"), c("L2R", "L2DA", "L2DF", "L2V", "L2G", "L2P", "L2M", "L2L")))

Cov_R <- structure(c(0.0149, 0.0087, 0.0092, 0.0086, 0.0088, 0.0089, 0.0089,
0.0094, 0.009, 0.0094, 0.0093, 0.0088, 0.0087, 0.0087, 0.0088,
0.0096, 0.0085, 0.0089, 0.0093, 0.0085, 0.009, 0.0087, 0.0087,
0.0086, 0.0085, 0.009, 0.0087, 0.0087, 0.0087, 0.018, 0.0086,
0.0087, 0.0087, 0.0089, 0.0087, 0.0087, 0.0087, 0.0087, 0.0087,
0.0088, 0.0092, 0.0095, 0.0087, 0.0087, 0.0085, 0.0088, 0.0087,
0.0088, 0.0087, 0.0087, 0.0087, 0.0086, 0.0088, 0.0087, 0.0087,
0.0087, 0.0092, 0.0086, 0.0143, 0.0087, 0.0086, 0.0088, 0.0088,
0.0093, 0.0088, 0.009, 0.009, 0.0088, 0.0087, 0.0086, 0.0092,
0.0095, 0.0084, 0.0089, 0.0091, 0.0085, 0.0092, 0.0087, 0.0086,
0.0086, 0.0085, 0.009, 0.0087, 0.0085, 0.0086, 0.0087, 0.0087,
0.0132, 0.0087, 0.0087, 0.0086, 0.0086, 0.0088, 0.0087, 0.0086,
0.0085, 0.0089, 0.0087, 0.0089, 0.0086, 0.0088, 0.0087, 0.0086,
0.0084, 0.0086, 0.0089, 0.0086, 0.0084, 0.0084, 0.0086, 0.0088,
0.0087, 0.0088, 0.0087, 0.0086, 0.0087, 0.0122, 0.0088, 0.0086,
0.0087, 0.0087, 0.009, 0.0087, 0.0085, 0.0087, 0.0089, 0.0086,
0.009, 0.0087, 0.0088, 0.0087, 0.0084, 0.0086, 0.0087, 0.0089,
0.0084, 0.0084, 0.0085, 0.0086, 0.0089, 0.0089, 0.0089, 0.0088,
0.0087, 0.0088, 0.0108, 0.0087, 0.0088, 0.0087, 0.0088, 0.0088,
0.0086, 0.0088, 0.0088, 0.0088, 0.0087, 0.0086, 0.0088, 0.0088,
0.0085, 0.0087, 0.0087, 0.0087, 0.0085, 0.0085, 0.0087, 0.0087,
0.0088, 0.0089, 0.0087, 0.0088, 0.0086, 0.0086, 0.0087, 0.0243,
0.0091, 0.0093, 0.0088, 0.0093, 0.0092, 0.0092, 0.0087, 0.0092,
0.0088, 0.0084, 0.0087, 0.0095, 0.009, 0.009, 0.0087, 0.0086,
0.0088, 0.0089, 0.0091, 0.0088, 0.0085, 0.0094, 0.0087, 0.0093,
0.0086, 0.0087, 0.0088, 0.0091, 0.0181, 0.0091, 0.0092, 0.0093,
0.009, 0.009, 0.0087, 0.0091, 0.0099, 0.0084, 0.0089, 0.0092,
0.0086, 0.0092, 0.0087, 0.0087, 0.0087, 0.0086, 0.0092, 0.0087,
0.0086, 0.009, 0.0087, 0.0088, 0.0088, 0.0087, 0.0087, 0.0093,
0.0091, 0.0238, 0.0092, 0.0094, 0.009, 0.0092, 0.0087, 0.0093,
0.0089, 0.0087, 0.0088, 0.0092, 0.0087, 0.0089, 0.0091, 0.0087,
0.0087, 0.0087, 0.009, 0.0091, 0.0087, 0.0094, 0.0087, 0.009,
0.0087, 0.009, 0.0088, 0.0088, 0.0092, 0.0092, 0.0237, 0.0092,
0.0087, 0.0087, 0.0087, 0.0088, 0.0105, 0.0087, 0.0089, 0.0092,
0.0085, 0.0088, 0.0087, 0.0089, 0.0086, 0.0085, 0.0088, 0.0087,
0.0089, 0.0093, 0.0087, 0.009, 0.0086, 0.0087, 0.0088, 0.0093,
0.0093, 0.0094, 0.0092, 0.014, 0.0089, 0.0089, 0.0087, 0.009,
0.009, 0.0085, 0.0088, 0.0093, 0.0087, 0.009, 0.0088, 0.0087,
0.0087, 0.0086, 0.009, 0.0088, 0.0087, 0.0088, 0.0088, 0.0088,
0.0085, 0.0085, 0.0086, 0.0092, 0.009, 0.009, 0.0087, 0.0089,
0.0163, 0.009, 0.0087, 0.0091, 0.0087, 0.0083, 0.0087, 0.0089,
0.0092, 0.0094, 0.0087, 0.0085, 0.0088, 0.0091, 0.0096, 0.0086,
0.0084, 0.0087, 0.0092, 0.0087, 0.0089, 0.0087, 0.0088, 0.0092,
0.009, 0.0092, 0.0087, 0.0089, 0.009, 0.0237, 0.0092, 0.0092,
0.0087, 0.0086, 0.0088, 0.0088, 0.0088, 0.0088, 0.009, 0.0086,
0.0086, 0.0088, 0.0089, 0.009, 0.0087, 0.0087, 0.0095, 0.0086,
0.0087, 0.0089, 0.0088, 0.0087, 0.0087, 0.0087, 0.0087, 0.0087,
0.0087, 0.0092, 0.0309, 0.0087, 0.0087, 0.0087, 0.0088, 0.0087,
0.0087, 0.0086, 0.0087, 0.0089, 0.0086, 0.0087, 0.0087, 0.0087,
0.009, 0.0088, 0.0087, 0.0092, 0.0089, 0.0086, 0.0088, 0.0092,
0.0091, 0.0093, 0.0088, 0.009, 0.0091, 0.0092, 0.0087, 0.0236,
0.0088, 0.0085, 0.0088, 0.0089, 0.0087, 0.0091, 0.009, 0.0086,
0.0087, 0.0087, 0.0092, 0.0091, 0.0086, 0.0096, 0.0087, 0.0095,
0.0086, 0.009, 0.0087, 0.0088, 0.0099, 0.0089, 0.0105, 0.009,
0.0087, 0.0087, 0.0087, 0.0088, 0.0532, 0.0085, 0.009, 0.0093,
0.0085, 0.0091, 0.0086, 0.009, 0.0085, 0.0085, 0.0088, 0.0086,
0.0086, 0.0085, 0.0085, 0.0084, 0.0088, 0.0087, 0.0086, 0.0084,
0.0084, 0.0087, 0.0087, 0.0085, 0.0083, 0.0086, 0.0087, 0.0085,
0.0085, 0.0161, 0.0086, 0.0085, 0.0082, 0.0084, 0.0087, 0.0087,
0.0083, 0.0082, 0.0084, 0.0087, 0.0087, 0.0089, 0.0088, 0.0089,
0.0087, 0.0088, 0.0088, 0.0087, 0.0089, 0.0088, 0.0089, 0.0088,
0.0087, 0.0088, 0.0088, 0.0088, 0.009, 0.0086, 0.0104, 0.0088,
0.0085, 0.0088, 0.0087, 0.0087, 0.0085, 0.0085, 0.0088, 0.0087,
0.0087, 0.0093, 0.0087, 0.0091, 0.0086, 0.0087, 0.0088, 0.0095,
0.0092, 0.0092, 0.0092, 0.0093, 0.0089, 0.0088, 0.0087, 0.0089,
0.0093, 0.0085, 0.0088, 0.0122, 0.0086, 0.0089, 0.0087, 0.0087,
0.0087, 0.0086, 0.0089, 0.0087, 0.0086, 0.0085, 0.0088, 0.0085,
0.0084, 0.0084, 0.0085, 0.009, 0.0086, 0.0087, 0.0085, 0.0087,
0.0092, 0.0088, 0.0087, 0.0087, 0.0085, 0.0082, 0.0085, 0.0086,
0.011, 0.0088, 0.0085, 0.0084, 0.0086, 0.009, 0.0089, 0.0084,
0.0083, 0.009, 0.0087, 0.0092, 0.0086, 0.0086, 0.0087, 0.009,
0.0092, 0.0089, 0.0088, 0.009, 0.0094, 0.0088, 0.0086, 0.0091,
0.0091, 0.0084, 0.0088, 0.0089, 0.0088, 0.0118, 0.0087, 0.0086,
0.0087, 0.0087, 0.0093, 0.0087, 0.0085, 0.0087, 0.0087, 0.0087,
0.0089, 0.0087, 0.0087, 0.0087, 0.0087, 0.0091, 0.0087, 0.0088,
0.0087, 0.009, 0.0087, 0.009, 0.0086, 0.0087, 0.0087, 0.0087,
0.0085, 0.0087, 0.0124, 0.0086, 0.0085, 0.0085, 0.0087, 0.0089,
0.0086, 0.0087, 0.0087, 0.0086, 0.0086, 0.0089, 0.0087, 0.0086,
0.0087, 0.0087, 0.0089, 0.0087, 0.0085, 0.0086, 0.0089, 0.0086,
0.009, 0.0087, 0.0087, 0.0087, 0.0084, 0.0086, 0.0086, 0.0116,
0.0085, 0.0084, 0.0086, 0.0086, 0.0088, 0.0086, 0.0086, 0.0086,
0.0084, 0.0084, 0.0085, 0.0088, 0.0087, 0.0087, 0.0086, 0.0087,
0.0088, 0.0086, 0.0086, 0.0087, 0.0085, 0.0083, 0.0085, 0.0087,
0.0086, 0.0087, 0.0085, 0.0085, 0.0095, 0.0086, 0.0088, 0.0085,
0.0084, 0.0085, 0.0088, 0.0085, 0.0084, 0.0084, 0.0085, 0.0089,
0.0086, 0.0087, 0.0085, 0.0086, 0.0091, 0.0088, 0.0087, 0.0087,
0.0085, 0.0082, 0.0085, 0.0086, 0.009, 0.0087, 0.0085, 0.0084,
0.0086, 0.011, 0.0089, 0.0084, 0.0083, 0.009, 0.0087, 0.009,
0.0086, 0.0085, 0.0087, 0.0091, 0.0092, 0.009, 0.0088, 0.009,
0.0096, 0.0089, 0.0087, 0.0092, 0.0088, 0.0084, 0.0088, 0.0089,
0.0089, 0.0093, 0.0087, 0.0086, 0.0088, 0.0089, 0.0127, 0.0087,
0.0085, 0.0087, 0.0087, 0.0087, 0.0088, 0.0086, 0.0087, 0.0088,
0.0087, 0.0091, 0.0087, 0.0088, 0.0086, 0.009, 0.0087, 0.0091,
0.0086, 0.0087, 0.0087, 0.0087, 0.0084, 0.0087, 0.0089, 0.0086,
0.0085, 0.0084, 0.0087, 0.013, 0.0086, 0.0087, 0.0087, 0.0085,
0.0087, 0.0089, 0.0088, 0.0085, 0.0086, 0.0087, 0.0089, 0.0087,
0.0084, 0.0087, 0.009, 0.0086, 0.0086, 0.0087, 0.0087, 0.0086,
0.0083, 0.0085, 0.0086, 0.0088, 0.0084, 0.0083, 0.0085, 0.0086,
0.0131), dim = c(28L, 28L))

library(metaSEM)

# Positive definite checks OK:
is.pd(Cov_R, cor.analysis=FALSE)
> [1] TRUE

ModelC <-
'Comprehension =~ L2L + L2V + L2G
Decoding =~ L2DA + L2DF
Meta =~ L2P + L2M
L2R ~ Comprehension + Decoding
Comprehension ~~ 0 * Meta
Decoding ~ 0 * Meta
Comprehension ~~ Decoding'

RAM3 <- lavaan2RAM(ModelC, obs.variables = varnames,
A.notation = "on", S.notation = "with")

wls(R, aCov = Cov_R, RAM = RAM3, n = 9201)
[1] > "Cov" is not positive definite.

Replied on Thu, 08/31/2023 - 21:35
Picture of user. Mike Cheung Joined: 10/08/2009

Dear Simon,

The problem is due to the R, not Cov_R.

> is.pd(R)
[1] FALSE

By the way, please use a new subject next time, as unrelated follow-ups may confuse readers.

Mike

Replied on Thu, 08/31/2023 - 22:34
No user picture. sharmel Joined: 07/31/2023

In reply to by Mike Cheung

Dear Mike,

Many thanks for your response. Do you have any suggestion to resolve the problem?

I can think of using nearest PD of R like: is.pd(Matrix::as.matrix(nearPD(R, corr = TRUE)$mat))

Replied on Fri, 09/01/2023 - 04:05
Picture of user. Mike Cheung Joined: 10/08/2009

There is an issue with your data. I recommend investigating the cause, such as whether it is due to using pairwise deletion.

While adjusting the data with near PD may help, the more critical question is whether the results obtained from this modified data can be considered trustworthy.

Replied on Fri, 09/01/2023 - 13:46
No user picture. sharmel Joined: 07/31/2023

In reply to by Mike Cheung

Thanks, Mike. Do you mean investigating whether the primary studies had used pairwise deletion when calculating their correlations OR investigating whether I, as the meta-analyst, have used some kind of deletion, such as na.omit(data), when preparing my data for meta-analysis??

In my case, several of the primary studies have NOT reported on correlations between some pairs of variables. Therefore, the data prepared for meta-analysis contains many NAs for those pairs of variables which I deleted using listwise deletion.

Replied on Fri, 09/01/2023 - 22:09
No user picture. sharmel Joined: 07/31/2023

Thank you, Mike. As I mentioned, several of the primary studies have NOT reported on correlations between some pairs of variables. Therefore, the data prepared for meta-analysis contains many NAs for those pairs of variables which I deleted using listwise deletion i.e., na.omit(data).

Are you suggesting that I don't remove the rows where r values are missing?

Replied on Sat, 09/02/2023 - 02:08
Picture of user. Mike Cheung Joined: 10/08/2009

There is no need to remove studies with NA. The tssem1() function uses FIML in OpneMx to handle missing data.

Please refer to the relevant literature and examples.

Replied on Wed, 10/09/2024 - 12:59
No user picture. JuanJMV Joined: 07/20/2016

Hi,

 

I am new with MetaSEM. I am trying to fit a simple meta-analysis (I just want to analyse 2 correlations; see attached file).

 

Here you can find the code that I am using with some simulated data but if there are no regressions I cannot make it work. It should be an easy model but I am stuck. I just want two covariances and I have fixed all the variances to one. 

Correlations between S and D, S and I and D and I are stored here:

rSD <- c(0.85, 0.8, 0.90, 0.78, 0.70,NA,NA,NA,NA,NA)
rSI <- c(NA,NA,NA,NA,NA,0.85,NA,NA,NA,NA)
rDI <- c(NA,NA,NA,NA,NA,0.60, 0.50, 0.55, 0.60, 0.55)

 

rSI could be al NAs since I am not interested in that correlation. 

Any help and guidance would be really appreciated.

 

library(metaSEM)
source("http://www.suzannejak.nl/MASEM_functions.R")

rSD <- c(0.85, 0.8, 0.90, 0.78, 0.70,NA,NA,NA,NA,NA)
rSI <- c(NA,NA,NA,NA,NA,0.85,NA,NA,NA,NA)
rDI <- c(NA,NA,NA,NA,NA,0.60, 0.50, 0.55, 0.60, 0.55)

 


N <- c(10000,40000, 30000, 10000, 10000,20000,10000, 50000, 10000, 10000  )

data <- as.data.frame(cbind(rSD,rSI,rSI, N))


nvar <- 3
varnames <- c("S","D", "I")
labels <- list(varnames,varnames)

cormatrices <- readstack(data[,c(2,1,3)], no.var = nvar, var.names = varnames, diag = FALSE)

n <-  data$N

pattern.na(cormatrices, show.na=F)
pattern.n(cormatrices, n=n)


my.df <- Cor2DataFrame(cormatrices, n, acov = "weighted")


## Specify model using lavaan syntax
model <-
 '

# Covariances
D ~~ I
D ~~ S
# Variances

D ~~ 1*D
S ~~ 1*S
I ~~ 1*I
'

RAM1 <- lavaan2RAM(model, obs.variables=varnames)
RAM1

 


## Create the model implied correlation structure with implicit diagonal constraints
M0 <- create.vechsR(A0=RAM1$A, S0=RAM1$S)

## Create the heterogeneity variance-covariance matrix
T0 <- create.Tau2(RAM=RAM1, RE.type="Diag", Transform="expLog", RE.startvalues=0.05)

## Fit the model
mx.fit0 <- osmasem(model.name="No moderator", Mmatrix=M0, Tmatrix=T0, data=my.df)


## View the results
summary(mx.fit0, fitIndices = TRUE)
VarCorr(mx.fit0)

File attachments