creates a multi-group model from an OpenMx model.
Usage
mxsem_group_by(
mxModel,
grouping_variables,
parameters = c(".*"),
use_grepl = TRUE
)
Arguments
- mxModel
mxModel with the entire data
- grouping_variables
Variables used to split the data in groups
- parameters
the parameters that should be group specific. By default all parameters are group specific.
- use_grepl
if set to TRUE, grepl is used to check which parameters are group specific. For instance, if parameters = "a" and use_grepl = TRUE, all parameters whose label contains the letter "a" will be group specific. If use_grep = FALSE only the parameter that has the label "a" is group specific.
Details
mxsem_group_by creates a multi-group model by splitting the data found in an mxModel object using dplyr's group_by function. The general idea is as follows:
1. The function extracts the data from mxModel 2. The data is split using the group_by function of dplyr with the variables in grouping_variables 3. a separate model is set up for each group. All parameters that match those specified in the parameters argument are group specific
**Warning**: The multi-group model may differ from **lavaan**! For instance, **lavaan** will automatically set the latent variances for all but the first group free if the loadings are fixed to equality. Such automatic procedures are not yet implemented in **mxsem**.
Examples
# THE FOLLOWING EXAMPLE IS ADAPTED FROM
# https://openmx.ssri.psu.edu/docs/OpenMx/latest/_static/Rdoc/mxModel.html
library(mxsem)
model <- 'spatial =~ visual + cubes + paper
verbal =~ general + paragrap + sentence
math =~ numeric + series + arithmet'
mg_model <- mxsem(model = model,
data = OpenMx::HS.ability.data) |>
# we want separate models for all combinations of grades and schools:
mxsem_group_by(grouping_variables = "school") |>
mxTryHard()
#> Warning: This function is very experimental and may not yet work properly. Use with caution.
#> The following parameters will be the same across groups:
#> The following parameters will be group specific: spatial→cubes, spatial→paper, verbal→paragrap, verbal→sentence, math→series, math→arithmet, visual↔visual, cubes↔cubes, paper↔paper, general↔general, paragrap↔paragrap, sentence↔sentence, numeric↔numeric, series↔series, arithmet↔arithmet, spatial↔spatial, spatial↔verbal, verbal↔verbal, spatial↔math, verbal↔math, math↔math, one→visual, one→cubes, one→paper, one→general, one→paragrap, one→sentence, one→numeric, one→series, one→arithmet
#> Running untitled29 with 60 parameters
#>
#> Beginning initial fit attempt
#> Running untitled29 with 60 parameters
#>
#> Lowest minimum so far: 15915.8767169525
#> Not all eigenvalues of the Hessian are positive: 3213484187.52617, 38024.6042732865, 12689.9573264105, 5730.94837456077, 4937.62470556635, 1953.66903064577, 1890.5373601436, 1141.98745676491, 787.058171585175, 354.297843637968, 310.095509667495, 140.273619058819, 71.9595946638189, 69.7734805818321, 46.2902097160743, 44.997434886997, 42.0029790773461, 24.9088679733441, 23.5209833116692, 22.8590744854324, 20.9226016706779, 18.3026805099979, 16.0852795564987, 16.0208733821851, 15.3106438967275, 9.90809693843778, 8.45359590986983, 5.89154693280105, 5.53494988510465, 4.35907398971035, 4.18484036948047, 3.490775517344, 3.1679804510075, 2.82688715009517, 1.5990018725994, 1.5149590578571, 1.43959661269952, 1.15828394552272, 1.0547057870351, 0.740147892170961, 0.518715751074977, 0.502846713252522, 0.49234870495974, 0.411397339792435, 0.328015262140072, 0.240788228449937, 0.184255124322826, 0.150702541831436, 0.0854468657890009, 0.0836747545807092, 0.041468023841972, 0.0385716855430323, 0.0370223555134969, 0.0339803278877858, 0.0295582367040106, 0.0211168689838238, 0.00785135595685313, 0.00757281285997932, 0.00240851973486545, -1.81305267142488e-06
#>
#> Beginning fit attempt 1 of at maximum 10 extra tries
#> Running untitled29 with 60 parameters
#> Not all eigenvalues of the Hessian are positive: 3213484187.52617, 38024.6042732865, 12689.9573264105, 5730.94837456077, 4937.62470556635, 1953.66903064577, 1890.5373601436, 1141.98745676491, 787.058171585175, 354.297843637968, 310.095509667495, 140.273619058819, 71.9595946638189, 69.7734805818321, 46.2902097160743, 44.997434886997, 42.0029790773461, 24.9088679733441, 23.5209833116692, 22.8590744854324, 20.9226016706779, 18.3026805099979, 16.0852795564987, 16.0208733821851, 15.3106438967275, 9.90809693843778, 8.45359590986983, 5.89154693280105, 5.53494988510465, 4.35907398971035, 4.18484036948047, 3.490775517344, 3.1679804510075, 2.82688715009517, 1.5990018725994, 1.5149590578571, 1.43959661269952, 1.15828394552272, 1.0547057870351, 0.740147892170961, 0.518715751074977, 0.502846713252522, 0.49234870495974, 0.411397339792435, 0.328015262140072, 0.240788228449937, 0.184255124322826, 0.150702541831436, 0.0854468657890009, 0.0836747545807092, 0.041468023841972, 0.0385716855430323, 0.0370223555134969, 0.0339803278877858, 0.0295582367040106, 0.0211168689838238, 0.00785135595685313, 0.00757281285997932, 0.00240851973486545, -1.81305267142488e-06
#>
#> Beginning fit attempt 2 of at maximum 10 extra tries
#> Running untitled29 with 60 parameters
#>
#> Fit attempt worse than current best: 16214.7974710754 vs 15915.8767169525
#>
#> Beginning fit attempt 3 of at maximum 10 extra tries
#> Running untitled29 with 60 parameters
#>
#> Fit attempt generated errors
#>
#> Beginning fit attempt 4 of at maximum 10 extra tries
#> Running untitled29 with 60 parameters
#>
#> Fit attempt generated errors
#>
#> Beginning fit attempt 5 of at maximum 10 extra tries
#> Running untitled29 with 60 parameters
#>
#> Fit attempt worse than current best: 16806.1135808387 vs 15915.8767169525
#>
#> Beginning fit attempt 6 of at maximum 10 extra tries
#> Running untitled29 with 60 parameters
#>
#> Fit attempt generated errors
#>
#> Beginning fit attempt 7 of at maximum 10 extra tries
#> Running untitled29 with 60 parameters
#>
#> Fit attempt worse than current best: 16263.5413115225 vs 15915.8767169525
#>
#> Beginning fit attempt 8 of at maximum 10 extra tries
#> Running untitled29 with 60 parameters
#>
#> Fit attempt generated errors
#>
#> Beginning fit attempt 9 of at maximum 10 extra tries
#> Running untitled29 with 60 parameters
#>
#> Fit attempt worse than current best: 18662.2932117059 vs 15915.8767169525
#>
#> Beginning fit attempt 10 of at maximum 10 extra tries
#> Running untitled29 with 60 parameters
#>
#> Fit attempt worse than current best: 16256.2691679205 vs 15915.8767169525
#>
#> Retry limit reached
#>
#>
#> Retry limit reached; Best fit=15915.877 (started at 11279122) (11 attempt(s): 7 valid, 4 errors)
#> Start values from best fit:
#> 0.42970024624698,0.286020187871656,0.312263928578312,0.44236978394511,2.27692227606046,0.956880866750664,23.8905916910492,15.2680590691191,5.97876328982393,55.1501510703319,3.53185203754856,5.84672550707086,14.0525210559141,39.6628088810004,13.6971925997571,23.5808248341328,25.1605093071122,79.8194252366533,12.1068540387422,18.961368010016,9.13350716275781,29.5792878042905,24.7999952666581,14.3034423053741,44.8482350020105,9.95171613458518,18.848265422415,14.4551613508707,18.7516956421916,25.8275798250657,0.318436450815367,0.222419710437766,0.302365185799373,0.479270964675901,-477.158850746814,-137.413675278134,17.4594141987673,20.7372633772988,6.42377323425363,48.0341463079278,3.66229299796762,6.68701918389048,19.4746526363489,12.3785669987472,13.7553948735031,32.7939308546732,25.3518621804176,89.8931843312095,-0.0660051500984448,-0.0832335213037829,0.000287598875064043,29.6474287770846,23.9359008556727,14.1602548174439,36.6986849232155,8.4679458003026,15.9807683520397,13.8910258508911,17.54486501665,22.7692319425606
# let's summarize the results:
summarize_multi_group_model(mg_model)
#>
#>
#> --------------------------------------------------------------------------------
#> Common Parameters:
#> [1] name matrix row col Estimate Std.Error lbound
#> [8] ubound lboundMet uboundMet
#> <0 rows> (or 0-length row.names)
#> --------------------------------------------------------------------------------
#> group_1:
#> # A tibble: 1 × 1
#> school
#> <fct>
#> 1 Grant-White
#>
#> name matrix row col Estimate
#> 1 spatial→cubes_group_1 untitled29_group_1.A cubes spatial 0.4297002
#> 2 spatial→paper_group_1 untitled29_group_1.A paper spatial 0.2860202
#> 3 verbal→paragrap_group_1 untitled29_group_1.A paragrap verbal 0.3122639
#> 4 verbal→sentence_group_1 untitled29_group_1.A sentence verbal 0.4423698
#> 5 math→series_group_1 untitled29_group_1.A series math 2.2769223
#> 6 math→arithmet_group_1 untitled29_group_1.A arithmet math 0.9568809
#> 7 visual↔visual_group_1 untitled29_group_1.S visual visual 23.8905917
#> 8 cubes↔cubes_group_1 untitled29_group_1.S cubes cubes 15.2680591
#> 9 paper↔paper_group_1 untitled29_group_1.S paper paper 5.9787633
#> 10 general↔general_group_1 untitled29_group_1.S general general 55.1501511
#> 11 paragrap↔paragrap_group_1 untitled29_group_1.S paragrap paragrap 3.5318520
#> 12 sentence↔sentence_group_1 untitled29_group_1.S sentence sentence 5.8467255
#> 13 numeric↔numeric_group_1 untitled29_group_1.S numeric numeric 14.0525211
#> 14 series↔series_group_1 untitled29_group_1.S series series 39.6628089
#> 15 arithmet↔arithmet_group_1 untitled29_group_1.S arithmet arithmet 13.6971926
#> 16 spatial↔spatial_group_1 untitled29_group_1.S spatial spatial 23.5808248
#> 17 spatial↔verbal_group_1 untitled29_group_1.S spatial verbal 25.1605093
#> 18 verbal↔verbal_group_1 untitled29_group_1.S verbal verbal 79.8194252
#> 19 spatial↔math_group_1 untitled29_group_1.S spatial math 12.1068540
#> 20 verbal↔math_group_1 untitled29_group_1.S verbal math 18.9613680
#> 21 math↔math_group_1 untitled29_group_1.S math math 9.1335072
#> 22 one→visual_group_1 untitled29_group_1.M 1 visual 29.5792878
#> 23 one→cubes_group_1 untitled29_group_1.M 1 cubes 24.7999953
#> 24 one→paper_group_1 untitled29_group_1.M 1 paper 14.3034423
#> 25 one→general_group_1 untitled29_group_1.M 1 general 44.8482350
#> 26 one→paragrap_group_1 untitled29_group_1.M 1 paragrap 9.9517161
#> 27 one→sentence_group_1 untitled29_group_1.M 1 sentence 18.8482654
#> 28 one→numeric_group_1 untitled29_group_1.M 1 numeric 14.4551614
#> 29 one→series_group_1 untitled29_group_1.M 1 series 18.7516956
#> 30 one→arithmet_group_1 untitled29_group_1.M 1 arithmet 25.8275798
#> Std.Error lbound ubound lboundMet uboundMet
#> 1 24.9051952 NA NA FALSE FALSE
#> 2 39.6626446 NA NA FALSE FALSE
#> 3 93.4239493 NA NA FALSE FALSE
#> 4 70.1629644 NA NA FALSE FALSE
#> 5 9.3354947 NA NA FALSE FALSE
#> 6 16.7200341 NA NA FALSE FALSE
#> 7 0.5212826 1e-06 NA FALSE FALSE
#> 8 1.0288784 1e-06 NA FALSE FALSE
#> 9 2.5980737 1e-06 NA FALSE FALSE
#> 10 0.2524053 1e-06 NA FALSE FALSE
#> 11 3.4811686 1e-06 NA FALSE FALSE
#> 12 1.9305050 1e-06 NA FALSE FALSE
#> 13 1.0516710 1e-06 NA FALSE FALSE
#> 14 0.3251193 1e-06 NA FALSE FALSE
#> 15 1.0889142 1e-06 NA FALSE FALSE
#> 16 0.6516162 1e-06 NA FALSE FALSE
#> 17 0.6050207 NA NA FALSE FALSE
#> 18 0.2724849 1e-06 NA FALSE FALSE
#> 19 1.8693526 NA NA FALSE FALSE
#> 20 1.2121663 NA NA FALSE FALSE
#> 21 2.2905622 1e-06 NA FALSE FALSE
#> 22 4.2133170 NA NA FALSE FALSE
#> 23 5.9192778 NA NA FALSE FALSE
#> 24 9.4067338 NA NA FALSE FALSE
#> 25 2.9318371 NA NA FALSE FALSE
#> 26 10.8878198 NA NA FALSE FALSE
#> 27 8.1087199 NA NA FALSE FALSE
#> 28 5.9845486 NA NA FALSE FALSE
#> 29 3.3272475 NA NA FALSE FALSE
#> 30 6.0895978 NA NA FALSE FALSE
#> --------------------------------------------------------------------------------
#> group_2:
#> # A tibble: 1 × 1
#> school
#> <fct>
#> 1 Pasteur
#>
#> name matrix row col
#> 31 spatial→cubes_group_2 untitled29_group_2.A cubes spatial
#> 32 spatial→paper_group_2 untitled29_group_2.A paper spatial
#> 33 verbal→paragrap_group_2 untitled29_group_2.A paragrap verbal
#> 34 verbal→sentence_group_2 untitled29_group_2.A sentence verbal
#> 35 math→series_group_2 untitled29_group_2.A series math
#> 36 math→arithmet_group_2 untitled29_group_2.A arithmet math
#> 37 visual↔visual_group_2 untitled29_group_2.S visual visual
#> 38 cubes↔cubes_group_2 untitled29_group_2.S cubes cubes
#> 39 paper↔paper_group_2 untitled29_group_2.S paper paper
#> 40 general↔general_group_2 untitled29_group_2.S general general
#> 41 paragrap↔paragrap_group_2 untitled29_group_2.S paragrap paragrap
#> 42 sentence↔sentence_group_2 untitled29_group_2.S sentence sentence
#> 43 numeric↔numeric_group_2 untitled29_group_2.S numeric numeric
#> 44 series↔series_group_2 untitled29_group_2.S series series
#> 45 arithmet↔arithmet_group_2 untitled29_group_2.S arithmet arithmet
#> 46 spatial↔spatial_group_2 untitled29_group_2.S spatial spatial
#> 47 spatial↔verbal_group_2 untitled29_group_2.S spatial verbal
#> 48 verbal↔verbal_group_2 untitled29_group_2.S verbal verbal
#> 49 spatial↔math_group_2 untitled29_group_2.S spatial math
#> 50 verbal↔math_group_2 untitled29_group_2.S verbal math
#> 51 math↔math_group_2 untitled29_group_2.S math math
#> 52 one→visual_group_2 untitled29_group_2.M 1 visual
#> 53 one→cubes_group_2 untitled29_group_2.M 1 cubes
#> 54 one→paper_group_2 untitled29_group_2.M 1 paper
#> 55 one→general_group_2 untitled29_group_2.M 1 general
#> 56 one→paragrap_group_2 untitled29_group_2.M 1 paragrap
#> 57 one→sentence_group_2 untitled29_group_2.M 1 sentence
#> 58 one→numeric_group_2 untitled29_group_2.M 1 numeric
#> 59 one→series_group_2 untitled29_group_2.M 1 series
#> 60 one→arithmet_group_2 untitled29_group_2.M 1 arithmet
#> Estimate Std.Error lbound ubound lboundMet uboundMet
#> 31 3.184365e-01 2.685566e+01 NA NA FALSE FALSE
#> 32 2.224197e-01 4.786987e+01 NA NA FALSE FALSE
#> 33 3.023652e-01 1.021680e+02 NA NA FALSE FALSE
#> 34 4.792710e-01 7.129340e+01 NA NA FALSE FALSE
#> 35 -4.771589e+02 7.037660e-02 NA NA FALSE FALSE
#> 36 -1.374137e+02 1.036822e-01 NA NA FALSE FALSE
#> 37 1.745941e+01 5.399239e-01 1e-06 NA FALSE FALSE
#> 38 2.073726e+01 8.179357e-01 1e-06 NA FALSE FALSE
#> 39 6.423773e+00 2.577557e+00 1e-06 NA FALSE FALSE
#> 40 4.803415e+01 2.872018e-01 1e-06 NA FALSE FALSE
#> 41 3.662293e+00 3.557058e+00 1e-06 NA FALSE FALSE
#> 42 6.687019e+00 1.687028e+00 1e-06 NA FALSE FALSE
#> 43 1.947465e+01 9.070805e-01 1e-06 NA FALSE FALSE
#> 44 1.237857e+01 4.048493e-01 1e-06 NA FALSE FALSE
#> 45 1.375539e+01 1.215633e+00 1e-06 NA FALSE FALSE
#> 46 3.279393e+01 5.319196e-01 1e-06 NA FALSE FALSE
#> 47 2.535186e+01 4.933868e-01 NA NA FALSE FALSE
#> 48 8.989318e+01 2.222927e-01 1e-06 NA FALSE FALSE
#> 49 -6.600515e-02 3.181335e+02 NA NA FALSE FALSE
#> 50 -8.323352e-02 1.984140e+02 NA NA FALSE FALSE
#> 51 2.875989e-04 8.016813e+04 1e-06 NA TRUE FALSE
#> 52 2.964743e+01 4.366660e+00 NA NA FALSE FALSE
#> 53 2.393590e+01 5.375164e+00 NA NA FALSE FALSE
#> 54 1.416025e+01 9.541825e+00 NA NA FALSE FALSE
#> 55 3.669868e+01 3.185245e+00 NA NA FALSE FALSE
#> 56 8.467946e+00 1.120899e+01 NA NA FALSE FALSE
#> 57 1.598077e+01 7.719842e+00 NA NA FALSE FALSE
#> 58 1.389103e+01 5.660532e+00 NA NA FALSE FALSE
#> 59 1.754487e+01 3.779672e+00 NA NA FALSE FALSE
#> 60 2.276923e+01 6.552698e+00 NA NA FALSE FALSE
#>
#> --------------------------------------------------------------------------------