Skip to content
Mike Cheung edited this page Sep 26, 2023 · 1 revision

Running multiple-group analysis with wls()

Mike Cheung September 26, 2023

Multiple-group WLS

  • We can convert the wls objects to mxModel to conduct a multiple-group analysis, but the goodness-of-fit indices need to be calculated manually.
library(metaSEM)

## Sample sizes
n1 <- 100
n2 <- 200

## Variable labels
vars <- c("y", "x1", "x2")

## Group 1 data and model
R1 <- matrix(c(1.00, 0.22, 0.24, 
               0.22, 1.00, 0.30, 
               0.24, 0.30, 1.00), ncol=3, nrow=3,
             dimnames=list(vars, vars))
R1
##       y   x1   x2
## y  1.00 0.22 0.24
## x1 0.22 1.00 0.30
## x2 0.24 0.30 1.00
## Calculate the asymptotic sampling covariance matrix
acov1 <- asyCov(R1, n1)

## Model in group 1: b1a is different whereas the others are the same across groups.
model1 <- "y ~ b1a*x1 + b2*x2
           ## Variances of x1 and x2 are 1
           x1 ~~ 1*x1
           x2 ~~ 1*x2
           ## x1 and x2 are correlated
           x1 ~~ r*x2"

RAM1 <- lavaan2RAM(model1, obs.variables=vars)

## Create a mxModel without running it
wls1 <- wls(model="Group1", Cov=R1, aCov=acov1, n=n1, RAM=RAM1, run=FALSE)

## Group 2 data and model
R2 <- matrix(c(1.00, 0.33, 0.41, 
               0.33, 1.00, 0.35, 
               0.41, 0.35, 1.00), ncol=3, nrow=3,
             dimnames=list(vars, vars))
R2
##       y   x1   x2
## y  1.00 0.33 0.41
## x1 0.33 1.00 0.35
## x2 0.41 0.35 1.00
## Calculate the asymptotic sampling covariance matrix
acov2 <- asyCov(R2, n2)

## Model in group 2: b1b is different whereas the others are the same across groups.
model2 <- "y ~ b1b*x1 + b2*x2
           ## Variances of x1 and x2 are 1
           x1 ~~ 1*x1
           x2 ~~ 1*x2
           ## x1 and x2 are correlated
           x1 ~~ r*x2"

RAM2 <- lavaan2RAM(model2, obs.variables=vars)

## Create a mxModel without running it
wls2 <- wls(model="Group2", Cov=R2, aCov=acov2, n=n2, RAM=RAM2, run=FALSE)

## Combine both groups
wls.model <- mxModel(model="combined", wls1, wls2, mxFitFunctionMultigroup(c("Group1", "Group2")))

wls.fit <- mxRun(wls.model)

summary(wls.fit)
## Summary of combined 
##  
## free parameters:
##   name         matrix row col  Estimate  Std.Error A
## 1  b1a Group1.Amatrix   y  x1 0.1195148 0.09583578  
## 2   b2 Group1.Amatrix   y  x2 0.2953879 0.05368413  
## 3    r      Group1.S1  x2  x1 0.3372157 0.05150966  
## 4  b1b Group2.Amatrix   y  x1 0.2308305 0.06357041  
## 
## Model Statistics: 
##                |  Parameters  |  Degrees of Freedom  |  Fit (-2lnL units)
##        Model:              4                     -4              1.639478
##    Saturated:             NA                     NA                    NA
## Independence:             NA                     NA                    NA
## Number of observations/statistics: 0/0
## 
## Information Criteria: 
##       |  df Penalty  |  Parameters Penalty  |  Sample-Size Adjusted
## AIC:             NA                     NA                       NA
## BIC:             NA                     NA                       NA
## CFI: NA 
## TLI: 1   (also known as NNFI) 
## RMSEA:  0  [95% CI (NA, NA)]
## Prob(RMSEA <= 0.05): NA
## To get additional fit indices, see help(mxRefModels)
## timestamp: 2023-09-26 08:50:02 
## Wall clock time: 0.03939486 secs 
## optimizer:  SLSQP 
## OpenMx version number: 2.21.8 
## Need help?  See help(mxSummary)
sessionInfo()
## R version 4.3.1 (2023-06-16)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 22.04.3 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0 
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
## 
## locale:
##  [1] LC_CTYPE=en_SG.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_SG.UTF-8        LC_COLLATE=en_SG.UTF-8    
##  [5] LC_MONETARY=en_SG.UTF-8    LC_MESSAGES=en_SG.UTF-8   
##  [7] LC_PAPER=en_SG.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_SG.UTF-8 LC_IDENTIFICATION=C       
## 
## time zone: Asia/Singapore
## tzcode source: system (glibc)
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] metaSEM_1.3.1 OpenMx_2.21.8
## 
## loaded via a namespace (and not attached):
##  [1] digest_0.6.33      fastmap_1.1.1      Matrix_1.6-1       xfun_0.40         
##  [5] lattice_0.21-8     knitr_1.44         parallel_4.3.1     htmltools_0.5.6   
##  [9] pbivnorm_0.6.0     RcppParallel_5.1.7 rmarkdown_2.25     stats4_4.3.1      
## [13] lifecycle_1.0.3    mvtnorm_1.2-3      cli_3.6.1          lavaan_0.6-16     
## [17] grid_4.3.1         mnormt_2.1.1       compiler_4.3.1     rstudioapi_0.15.0 
## [21] tools_4.3.1        evaluate_0.21      ellipse_0.5.0      Rcpp_1.0.11       
## [25] yaml_2.3.7         quadprog_1.5-8     rlang_1.1.1        MASS_7.3-60
Clone this wiki locally