Set up to run Mplus inside Rmarkdown

Biostatistics Psychology/Sociology Mplus R MplusAutomation Tutorial

Mplus as a knitr engine in Rmarkdown
MplusAutomation: a brief guide

Hai Nguyen
May 26, 2021

Goals

Set up Mplus in Rmarkdown

Globally we can change the engine interpreters for one/multiple engine(s), we applied for Mplus:

knitr::opts_chunk$set(engine.path = list(
  mplus = "C:/Program Files/Mplus/Mplus"
))

Next step, we register a Mplus custom language engine:

knitr::knit_engines$set(mplus = function(options) {
    code <- paste(options$code, collapse = "\n")
    fileConn<-file("formplus.inp")
    writeLines(code, fileConn)
    close(fileConn)
    out  <- system2("C:/Program Files/Mplus/Mplus", "formplus.inp")
    fileConnOutput <- file("formplus.out")
    mplusOutput <- readLines(fileConnOutput)
    knitr::engine_output(options, code, mplusOutput)
})

For more language engines, we can refer demos from Yihui Xie’s post.

Further reading, we may read a Rich Jones’ post on rpubs

Gentle introduction to MplusAutomation

The MplusAutomation package for R (Hallquist and Wiley 2018):

Four core routines support these aims:

The MplusAutomation package can be installed within R using the following call:

if (!require(MplusAutomation)) install.packages("MplusAutomation")
library(MplusAutomation)

Run a LPA via MplusAutomation

Generating data

I use Edgar Anderson’s Iris Data with 150 cases (rows) and 5 variables named Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, and Species.

iris data set gives the measurements in cm of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris: Iris setosa, versicolor, and virginica.

?iris
head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

prepareMPlusData() function prepares a data file in the MPlus’ format, namely, a tab-separated .dat file with no column names.

prepareMplusData(iris[, -5], "iris.dat", inpfile=TRUE)

Latent Profile Analysis using Mplus

We can directly run in Rmarkdown or apply runModels to run the .inp files.

Run direct in Rmarkdown

  1. One for which we estimate different means between 2 profiles
TITLE: iris LPA

DATA:
    File is iris.dat
    
VARIABLE: 

    Names are x1, x2, x3, x4;

    Classes = c(2) ;
            
MODEL:
    
    %overall%
    
    x1 x2 x3 x4; 
    
    %c#1%
    
    [x1-x4];
    
    %c#2%
    
    [x1-x4];
    
ANALYSIS: 
    Type is mixture;
            
OUTPUT:
    Tech11;
Mplus VERSION 8.6
MUTHEN & MUTHEN
05/26/2021  10:33 PM

INPUT INSTRUCTIONS

  TITLE: iris LPA

  DATA:
      File is iris.dat

  VARIABLE:

      Names are x1, x2, x3, x4;

      Classes = c(2) ;

  MODEL:

      %overall%

      x1 x2 x3 x4;

      %c#1%

      [x1-x4];

      %c#2%

      [x1-x4];

  ANALYSIS:
      Type is mixture;

  OUTPUT:
      Tech11;



*** WARNING in DATA command
  Statement not terminated by a semicolon:
  File is iris.dat
*** WARNING in MODEL command
  All variables are uncorrelated with all other variables within class.
  Check that this is what is intended.
   2 WARNING(S) FOUND IN THE INPUT INSTRUCTIONS



iris LPA

SUMMARY OF ANALYSIS

Number of groups                                                 1
Number of observations                                         150

Number of dependent variables                                    4
Number of independent variables                                  0
Number of continuous latent variables                            0
Number of categorical latent variables                           1

Observed dependent variables

  Continuous
   X1          X2          X3          X4

Categorical latent variables
   C


Estimator                                                      MLR
Information matrix                                        OBSERVED
Optimization Specifications for the Quasi-Newton Algorithm for
Continuous Outcomes
  Maximum number of iterations                                 100
  Convergence criterion                                  0.100D-05
Optimization Specifications for the EM Algorithm
  Maximum number of iterations                                 500
  Convergence criteria
    Loglikelihood change                                 0.100D-06
    Relative loglikelihood change                        0.100D-06
    Derivative                                           0.100D-05
Optimization Specifications for the M step of the EM Algorithm for
Categorical Latent variables
  Number of M step iterations                                    1
  M step convergence criterion                           0.100D-05
  Basis for M step termination                           ITERATION
Optimization Specifications for the M step of the EM Algorithm for
Censored, Binary or Ordered Categorical (Ordinal), Unordered
Categorical (Nominal) and Count Outcomes
  Number of M step iterations                                    1
  M step convergence criterion                           0.100D-05
  Basis for M step termination                           ITERATION
  Maximum value for logit thresholds                            15
  Minimum value for logit thresholds                           -15
  Minimum expected cell size for chi-square              0.100D-01
Optimization algorithm                                         EMA
Random Starts Specifications
  Number of initial stage random starts                         20
  Number of final stage optimizations                            4
  Number of initial stage iterations                            10
  Initial stage convergence criterion                    0.100D+01
  Random starts scale                                    0.500D+01
  Random seed for generating random starts                       0

Input data file(s)
  iris.dat
Input data format  FREE



UNIVARIATE SAMPLE STATISTICS

     UNIVARIATE HIGHER-ORDER MOMENT DESCRIPTIVE STATISTICS

         Variable/         Mean/     Skewness/   Minimum/ % with                Percentiles
        Sample Size      Variance    Kurtosis    Maximum  Min/Max      20%/60%    40%/80%    Median

     X1                    5.843       0.312       4.300    0.67%       5.000      5.600      5.800
             150.000       0.681      -0.574       7.900    0.67%       6.100      6.500
     X2                    3.057       0.316       2.000    0.67%       2.700      3.000      3.000
             150.000       0.189       0.181       4.400    0.67%       3.100      3.400
     X3                    3.758      -0.272       1.000    0.67%       1.500      3.900      4.350
             150.000       3.096      -1.396       6.900    0.67%       4.600      5.300
     X4                    1.199      -0.102       0.100    3.33%       0.200      1.100      1.300
             150.000       0.577      -1.336       2.500    2.00%       1.500      1.900

RANDOM STARTS RESULTS RANKED FROM THE BEST TO THE WORST LOGLIKELIHOOD VALUES

Final stage loglikelihood values at local maxima, seeds, and initial stage start numbers:

            -488.915  253358           2
            -488.915  68985            17
            -488.915  76974            16
            -488.915  573096           20



THE BEST LOGLIKELIHOOD VALUE HAS BEEN REPLICATED.  RERUN WITH AT LEAST TWICE THE
RANDOM STARTS TO CHECK THAT THE BEST LOGLIKELIHOOD IS STILL OBTAINED AND REPLICATED.


THE MODEL ESTIMATION TERMINATED NORMALLY



MODEL FIT INFORMATION

Number of Free Parameters                       13

Loglikelihood

          H0 Value                        -488.915
          H0 Scaling Correction Factor      0.9851
            for MLR

Information Criteria

          Akaike (AIC)                    1003.830
          Bayesian (BIC)                  1042.968
          Sample-Size Adjusted BIC        1001.825
            (n* = (n + 2) / 24)



FINAL CLASS COUNTS AND PROPORTIONS FOR THE LATENT CLASSES
BASED ON THE ESTIMATED MODEL

    Latent
   Classes

       1         50.27148          0.33514
       2         99.72852          0.66486


FINAL CLASS COUNTS AND PROPORTIONS FOR THE LATENT CLASSES
BASED ON ESTIMATED POSTERIOR PROBABILITIES

    Latent
   Classes

       1         50.27148          0.33514
       2         99.72852          0.66486


FINAL CLASS COUNTS AND PROPORTIONS FOR THE LATENT CLASSES
BASED ON THEIR MOST LIKELY LATENT CLASS MEMBERSHIP

Class Counts and Proportions

    Latent
   Classes

       1               50          0.33333
       2              100          0.66667


CLASSIFICATION QUALITY

     Entropy                         0.991


Average Latent Class Probabilities for Most Likely Latent Class Membership (Row)
by Latent Class (Column)

           1        2

    1   1.000    0.000
    2   0.003    0.997


Classification Probabilities for the Most Likely Latent Class Membership (Column)
by Latent Class (Row)

           1        2

    1   0.995    0.005
    2   0.000    1.000


Logits for the Classification Probabilities for the Most Likely Latent Class Membership (Column)
by Latent Class (Row)

              1        2

    1      5.216    0.000
    2    -13.816    0.000


MODEL RESULTS

                                                    Two-Tailed
                    Estimate       S.E.  Est./S.E.    P-Value

Latent Class 1

 Means
    X1                 5.006      0.049    102.032      0.000
    X2                 3.423      0.055     61.909      0.000
    X3                 1.471      0.026     55.788      0.000
    X4                 0.250      0.016     15.938      0.000

 Variances
    X1                 0.328      0.042      7.853      0.000
    X2                 0.121      0.017      7.347      0.000
    X3                 0.459      0.063      7.340      0.000
    X4                 0.123      0.013      9.126      0.000

Latent Class 2

 Means
    X1                 6.265      0.068     92.358      0.000
    X2                 2.873      0.034     85.125      0.000
    X3                 4.911      0.085     57.798      0.000
    X4                 1.678      0.043     38.643      0.000

 Variances
    X1                 0.328      0.042      7.853      0.000
    X2                 0.121      0.017      7.347      0.000
    X3                 0.459      0.063      7.340      0.000
    X4                 0.123      0.013      9.126      0.000

Categorical Latent Variables

 Means
    C#1               -0.685      0.175     -3.924      0.000


QUALITY OF NUMERICAL RESULTS

     Condition Number for the Information Matrix              0.810E-03
       (ratio of smallest to largest eigenvalue)


TECHNICAL 11 OUTPUT

     Random Starts Specifications for the k-1 Class Analysis Model
        Number of initial stage random starts                  20
        Number of final stage optimizations                     4

     VUONG-LO-MENDELL-RUBIN LIKELIHOOD RATIO TEST FOR 1 (H0) VERSUS 2 CLASSES

          H0 Loglikelihood Value                         -741.018
          2 Times the Loglikelihood Difference            504.205
          Difference in the Number of Parameters                5
          Mean                                             18.069
          Standard Deviation                               25.180
          P-Value                                          0.0000

     LO-MENDELL-RUBIN ADJUSTED LRT TEST

          Value                                           484.852
          P-Value                                          0.0000

     Beginning Time:  22:33:15
        Ending Time:  22:33:16
       Elapsed Time:  00:00:01



MUTHEN & MUTHEN
3463 Stoner Ave.
Los Angeles, CA  90066

Tel: (310) 391-9971
Fax: (310) 391-8971
Web: www.StatModel.com
Support: Support@StatModel.com

Copyright (c) 1998-2021 Muthen & Muthen
  1. One for which we estimate different means between the 2 profiles and the model is specified to estimate the correlation (or covariance) for the variables
TITLE: iris LPA

DATA:
    File is iris.dat
    
VARIABLE: 

    Names are x1, x2, x3, x4;

    Classes = c(2) ;
            
MODEL:
    
    %overall%
    
    x1 x2 x3 x4; 
    
    x1 WITH x2-x4;
    x2 WITH x3-x4;
    x3 WITH x4;

    %c#1%
    
    [x1-x4];
    
    %c#2%
    
    [x1-x4];
    
ANALYSIS: 
    Type is mixture;
            
OUTPUT:
    Tech11;
Mplus VERSION 8.6
MUTHEN & MUTHEN
05/26/2021  10:33 PM

INPUT INSTRUCTIONS

  TITLE: iris LPA

  DATA:
      File is iris.dat

  VARIABLE:

      Names are x1, x2, x3, x4;

      Classes = c(2) ;

  MODEL:

      %overall%

      x1 x2 x3 x4;

      x1 WITH x2-x4;
      x2 WITH x3-x4;
      x3 WITH x4;

      %c#1%

      [x1-x4];

      %c#2%

      [x1-x4];

  ANALYSIS:
      Type is mixture;

  OUTPUT:
      Tech11;



*** WARNING in DATA command
  Statement not terminated by a semicolon:
  File is iris.dat
   1 WARNING(S) FOUND IN THE INPUT INSTRUCTIONS



iris LPA

SUMMARY OF ANALYSIS

Number of groups                                                 1
Number of observations                                         150

Number of dependent variables                                    4
Number of independent variables                                  0
Number of continuous latent variables                            0
Number of categorical latent variables                           1

Observed dependent variables

  Continuous
   X1          X2          X3          X4

Categorical latent variables
   C


Estimator                                                      MLR
Information matrix                                        OBSERVED
Optimization Specifications for the Quasi-Newton Algorithm for
Continuous Outcomes
  Maximum number of iterations                                 100
  Convergence criterion                                  0.100D-05
Optimization Specifications for the EM Algorithm
  Maximum number of iterations                                 500
  Convergence criteria
    Loglikelihood change                                 0.100D-06
    Relative loglikelihood change                        0.100D-06
    Derivative                                           0.100D-05
Optimization Specifications for the M step of the EM Algorithm for
Categorical Latent variables
  Number of M step iterations                                    1
  M step convergence criterion                           0.100D-05
  Basis for M step termination                           ITERATION
Optimization Specifications for the M step of the EM Algorithm for
Censored, Binary or Ordered Categorical (Ordinal), Unordered
Categorical (Nominal) and Count Outcomes
  Number of M step iterations                                    1
  M step convergence criterion                           0.100D-05
  Basis for M step termination                           ITERATION
  Maximum value for logit thresholds                            15
  Minimum value for logit thresholds                           -15
  Minimum expected cell size for chi-square              0.100D-01
Optimization algorithm                                         EMA
Random Starts Specifications
  Number of initial stage random starts                         20
  Number of final stage optimizations                            4
  Number of initial stage iterations                            10
  Initial stage convergence criterion                    0.100D+01
  Random starts scale                                    0.500D+01
  Random seed for generating random starts                       0

Input data file(s)
  iris.dat
Input data format  FREE



UNIVARIATE SAMPLE STATISTICS

     UNIVARIATE HIGHER-ORDER MOMENT DESCRIPTIVE STATISTICS

         Variable/         Mean/     Skewness/   Minimum/ % with                Percentiles
        Sample Size      Variance    Kurtosis    Maximum  Min/Max      20%/60%    40%/80%    Median

     X1                    5.843       0.312       4.300    0.67%       5.000      5.600      5.800
             150.000       0.681      -0.574       7.900    0.67%       6.100      6.500
     X2                    3.057       0.316       2.000    0.67%       2.700      3.000      3.000
             150.000       0.189       0.181       4.400    0.67%       3.100      3.400
     X3                    3.758      -0.272       1.000    0.67%       1.500      3.900      4.350
             150.000       3.096      -1.396       6.900    0.67%       4.600      5.300
     X4                    1.199      -0.102       0.100    3.33%       0.200      1.100      1.300
             150.000       0.577      -1.336       2.500    2.00%       1.500      1.900

RANDOM STARTS RESULTS RANKED FROM THE BEST TO THE WORST LOGLIKELIHOOD VALUES

Final stage loglikelihood values at local maxima, seeds, and initial stage start numbers:

            -296.448  533738           11
            -296.448  68985            17
            -296.448  unperturbed      0
            -296.448  27071            15



THE BEST LOGLIKELIHOOD VALUE HAS BEEN REPLICATED.  RERUN WITH AT LEAST TWICE THE
RANDOM STARTS TO CHECK THAT THE BEST LOGLIKELIHOOD IS STILL OBTAINED AND REPLICATED.


THE MODEL ESTIMATION TERMINATED NORMALLY



MODEL FIT INFORMATION

Number of Free Parameters                       19

Loglikelihood

          H0 Value                        -296.448
          H0 Scaling Correction Factor      1.0304
            for MLR

Information Criteria

          Akaike (AIC)                     630.895
          Bayesian (BIC)                   688.097
          Sample-Size Adjusted BIC         627.966
            (n* = (n + 2) / 24)



FINAL CLASS COUNTS AND PROPORTIONS FOR THE LATENT CLASSES
BASED ON THE ESTIMATED MODEL

    Latent
   Classes

       1         50.00004          0.33333
       2         99.99996          0.66667


FINAL CLASS COUNTS AND PROPORTIONS FOR THE LATENT CLASSES
BASED ON ESTIMATED POSTERIOR PROBABILITIES

    Latent
   Classes

       1         50.00004          0.33333
       2         99.99996          0.66667


FINAL CLASS COUNTS AND PROPORTIONS FOR THE LATENT CLASSES
BASED ON THEIR MOST LIKELY LATENT CLASS MEMBERSHIP

Class Counts and Proportions

    Latent
   Classes

       1               50          0.33333
       2              100          0.66667


CLASSIFICATION QUALITY

     Entropy                         1.000


Average Latent Class Probabilities for Most Likely Latent Class Membership (Row)
by Latent Class (Column)

           1        2

    1   1.000    0.000
    2   0.000    1.000


Classification Probabilities for the Most Likely Latent Class Membership (Column)
by Latent Class (Row)

           1        2

    1   1.000    0.000
    2   0.000    1.000


Logits for the Classification Probabilities for the Most Likely Latent Class Membership (Column)
by Latent Class (Row)

              1        2

    1     11.903    0.000
    2    -12.706    0.000


MODEL RESULTS

                                                    Two-Tailed
                    Estimate       S.E.  Est./S.E.    P-Value

Latent Class 1

 X1       WITH
    X2                 0.113      0.019      5.805      0.000
    X3                 0.305      0.050      6.104      0.000
    X4                 0.114      0.019      6.112      0.000

 X2       WITH
    X3                 0.098      0.022      4.359      0.000
    X4                 0.056      0.010      5.330      0.000

 X3       WITH
    X4                 0.193      0.024      8.175      0.000

 Means
    X1                 5.006      0.049    101.442      0.000
    X2                 3.428      0.053     64.589      0.000
    X3                 1.462      0.024     60.137      0.000
    X4                 0.246      0.015     16.674      0.000

 Variances
    X1                 0.331      0.042      7.870      0.000
    X2                 0.120      0.016      7.574      0.000
    X3                 0.460      0.063      7.333      0.000
    X4                 0.123      0.013      9.137      0.000

Latent Class 2

 X1       WITH
    X2                 0.113      0.019      5.805      0.000
    X3                 0.305      0.050      6.104      0.000
    X4                 0.114      0.019      6.112      0.000

 X2       WITH
    X3                 0.098      0.022      4.359      0.000
    X4                 0.056      0.010      5.330      0.000

 X3       WITH
    X4                 0.193      0.024      8.175      0.000

 Means
    X1                 6.262      0.066     94.947      0.000
    X2                 2.872      0.033     86.743      0.000
    X3                 4.906      0.082     59.719      0.000
    X4                 1.676      0.042     39.652      0.000

 Variances
    X1                 0.331      0.042      7.870      0.000
    X2                 0.120      0.016      7.574      0.000
    X3                 0.460      0.063      7.333      0.000
    X4                 0.123      0.013      9.137      0.000

Categorical Latent Variables

 Means
    C#1               -0.693      0.173     -4.002      0.000


QUALITY OF NUMERICAL RESULTS

     Condition Number for the Information Matrix              0.149E-04
       (ratio of smallest to largest eigenvalue)


TECHNICAL 11 OUTPUT

     Random Starts Specifications for the k-1 Class Analysis Model
        Number of initial stage random starts                  20
        Number of final stage optimizations                     4

     VUONG-LO-MENDELL-RUBIN LIKELIHOOD RATIO TEST FOR 1 (H0) VERSUS 2 CLASSES

          H0 Loglikelihood Value                         -379.915
          2 Times the Loglikelihood Difference            166.934
          Difference in the Number of Parameters                5
          Mean                                             11.054
          Standard Deviation                                9.531
          P-Value                                          0.0000

     LO-MENDELL-RUBIN ADJUSTED LRT TEST

          Value                                           160.527
          P-Value                                          0.0000

     Beginning Time:  22:33:16
        Ending Time:  22:33:17
       Elapsed Time:  00:00:01



MUTHEN & MUTHEN
3463 Stoner Ave.
Los Angeles, CA  90066

Tel: (310) 391-9971
Fax: (310) 391-8971
Web: www.StatModel.com
Support: Support@StatModel.com

Copyright (c) 1998-2021 Muthen & Muthen
  1. One for which we estimate different means and the model is specified to different covariances (and variable variances) between the 2 profiles
TITLE: iris LPA

DATA:
    File is iris.dat
    
VARIABLE: 

    Names are x1, x2, x3, x4;

    Classes = c(2) ;
            
MODEL:

    %c#1%
    
    x1 x2 x3 x4; 
    
    x1 WITH x2-x4;
    x2 WITH x3-x4;
    x3 WITH x4;
    
    [x1-x4];
    
    %c#2%
    
    x1 x2 x3 x4; 
    
    x1 WITH x2-x4;
    x2 WITH x3-x4;
    x3 WITH x4;
    
    [x1-x4];
    
ANALYSIS: 
    Type is mixture;
            
OUTPUT:
    Tech11;

The disadvantage is that Rmarkdown shows a lengthy output, making “eye strain” to track the necessary information.

Using runModels

Save each chunk of the following models (either in a .txt file or in MPlus style using a .inp file type), then run with runModels

# Model 1
runModels("2-iris-LPA_means.inp")

Running model: 2-iris-LPA_means.inp 
System command: C:\WINDOWS\system32\cmd.exe /c cd "C:\Users\minhh\Box Sync\Dissertation\hai-mn.github.io\_posts\2021-05-26-Mplus-as-a-knitr-engine" && "Mplus" "2-iris-LPA_means.inp" 
# Model 2
runModels("2-iris-LPA_means_correlated.inp")

Running model: 2-iris-LPA_means_correlated.inp 
System command: C:\WINDOWS\system32\cmd.exe /c cd "C:\Users\minhh\Box Sync\Dissertation\hai-mn.github.io\_posts\2021-05-26-Mplus-as-a-knitr-engine" && "Mplus" "2-iris-LPA_means_correlated.inp" 
# Model 3
runModels("2-iris-LPA_means_correlated_free_variances.inp")

Running model: 2-iris-LPA_means_correlated_free_variances.inp 
System command: C:\WINDOWS\system32\cmd.exe /c cd "C:\Users\minhh\Box Sync\Dissertation\hai-mn.github.io\_posts\2021-05-26-Mplus-as-a-knitr-engine" && "Mplus" "2-iris-LPA_means_correlated_free_variances.inp" 
m1 <- readModels("2-iris-LPA_means.out")
Reading model:  2-iris-LPA_means.out 
m2 <- readModels("2-iris-LPA_means_correlated.out")
Reading model:  2-iris-LPA_means_correlated.out 
m3 <- readModels("2-iris-LPA_means_correlated_free_variances.out")
Reading model:  2-iris-LPA_means_correlated_free_variances.out 

Compare the model fit

Now, we inspect the fit statistics and other summary information for the three models:

m1$summaries$BIC
[1] 1042.968
m2$summaries$BIC
[1] 688.097
m3$summaries$BIC
[1] 574.018

And examine parameters:

m1$parameters[[1]][-nrow(m1$parameters[[1]]), ]
   paramHeader param   est    se  est_se pval LatentClass
1        Means    X1 5.006 0.049 102.032    0           1
2        Means    X2 3.423 0.055  61.909    0           1
3        Means    X3 1.471 0.026  55.788    0           1
4        Means    X4 0.250 0.016  15.938    0           1
5    Variances    X1 0.328 0.042   7.853    0           1
6    Variances    X2 0.121 0.017   7.347    0           1
7    Variances    X3 0.459 0.063   7.340    0           1
8    Variances    X4 0.123 0.013   9.126    0           1
9        Means    X1 6.265 0.068  92.358    0           2
10       Means    X2 2.873 0.034  85.125    0           2
11       Means    X3 4.911 0.085  57.798    0           2
12       Means    X4 1.678 0.043  38.643    0           2
13   Variances    X1 0.328 0.042   7.853    0           2
14   Variances    X2 0.121 0.017   7.347    0           2
15   Variances    X3 0.459 0.063   7.340    0           2
16   Variances    X4 0.123 0.013   9.126    0           2
m2$parameters[[1]][-nrow(m2$parameters[[1]]), ]
   paramHeader param   est    se  est_se pval LatentClass
1      X1.WITH    X2 0.113 0.019   5.805    0           1
2      X1.WITH    X3 0.305 0.050   6.104    0           1
3      X1.WITH    X4 0.114 0.019   6.112    0           1
4      X2.WITH    X3 0.098 0.022   4.359    0           1
5      X2.WITH    X4 0.056 0.010   5.330    0           1
6      X3.WITH    X4 0.193 0.024   8.175    0           1
7        Means    X1 5.006 0.049 101.442    0           1
8        Means    X2 3.428 0.053  64.589    0           1
9        Means    X3 1.462 0.024  60.137    0           1
10       Means    X4 0.246 0.015  16.674    0           1
11   Variances    X1 0.331 0.042   7.870    0           1
12   Variances    X2 0.120 0.016   7.574    0           1
13   Variances    X3 0.460 0.063   7.333    0           1
14   Variances    X4 0.123 0.013   9.137    0           1
15     X1.WITH    X2 0.113 0.019   5.805    0           2
16     X1.WITH    X3 0.305 0.050   6.104    0           2
17     X1.WITH    X4 0.114 0.019   6.112    0           2
18     X2.WITH    X3 0.098 0.022   4.359    0           2
19     X2.WITH    X4 0.056 0.010   5.330    0           2
20     X3.WITH    X4 0.193 0.024   8.175    0           2
21       Means    X1 6.262 0.066  94.947    0           2
22       Means    X2 2.872 0.033  86.743    0           2
23       Means    X3 4.906 0.082  59.719    0           2
24       Means    X4 1.676 0.042  39.652    0           2
25   Variances    X1 0.331 0.042   7.870    0           2
26   Variances    X2 0.120 0.016   7.574    0           2
27   Variances    X3 0.460 0.063   7.333    0           2
28   Variances    X4 0.123 0.013   9.137    0           2
m3$parameters[[1]][-nrow(m3$parameters[[1]]), ]
   paramHeader param   est    se  est_se  pval LatentClass
1      X1.WITH    X2 0.097 0.022   4.469 0.000           1
2      X1.WITH    X3 0.016 0.010   1.655 0.098           1
3      X1.WITH    X4 0.010 0.004   2.486 0.013           1
4      X2.WITH    X3 0.011 0.008   1.418 0.156           1
5      X2.WITH    X4 0.009 0.005   1.763 0.078           1
6      X3.WITH    X4 0.006 0.003   2.316 0.021           1
7        Means    X1 5.006 0.049 101.439 0.000           1
8        Means    X2 3.428 0.053  64.591 0.000           1
9        Means    X3 1.462 0.024  60.132 0.000           1
10       Means    X4 0.246 0.015  16.673 0.000           1
11   Variances    X1 0.122 0.022   5.498 0.000           1
12   Variances    X2 0.141 0.033   4.267 0.000           1
13   Variances    X3 0.030 0.007   4.222 0.000           1
14   Variances    X4 0.011 0.003   3.816 0.000           1
15     X1.WITH    X2 0.121 0.027   4.467 0.000           2
16     X1.WITH    X3 0.449 0.070   6.377 0.000           2
17     X1.WITH    X4 0.166 0.026   6.282 0.000           2
18     X2.WITH    X3 0.141 0.033   4.330 0.000           2
19     X2.WITH    X4 0.079 0.015   5.295 0.000           2
20     X3.WITH    X4 0.286 0.031   9.107 0.000           2
21       Means    X1 6.262 0.066  94.948 0.000           2
22       Means    X2 2.872 0.033  86.743 0.000           2
23       Means    X3 4.906 0.082  59.719 0.000           2
24       Means    X4 1.676 0.042  39.652 0.000           2
25   Variances    X1 0.435 0.059   7.332 0.000           2
26   Variances    X2 0.110 0.017   6.442 0.000           2
27   Variances    X3 0.675 0.086   7.822 0.000           2
28   Variances    X4 0.179 0.018  10.148 0.000           2

One last thing, I want to mention about the createModels() which can creates a set of models using a template. We can save file as mplus_iris_lpa_template.txt

[[init]]
iterators = classes;
classes = 1:9;
filename = "[[classes]]-iris-LPA.inp";
outputDirectory = the_dir;
[[/init]]

TITLE: iris LPA

DATA:
    File is iris.dat
    
VARIABLE: 

    Names are x1 x2 x3 x4;

    Classes = c([[classes]]) ;

MODEL:
    
    %overall%
    
    x1 x2 x3 x4; 
    
    [x1-x4];

            
ANALYSIS: 
    Type is mixture;
            
OUTPUT:
    Tech11;

Here is an example that would create models with different numbers of profiles, from 1 to 9.

#Set the folder containing mplus.txt as working
createModels("mplus_iris_lpa_template.txt")

#Set the folder containing input files as working directory
runModels()

#Set the folder containing output files as working directory
models_list <- readModels()
outputs<-extractModelParameters()

Further reading, we can look at a Josh Rosenberg’s post and the Mplus website

More information about the MplusAutomation package in GitHub.

Hallquist, M. N., and J. F. Wiley. 2018. “MplusAutomation: An r Package for Facilitating Large-Scale Latent Variable Analyses in Mplus.” Journal Article. Struct Equ Modeling 25 (4): 621–38. https://doi.org/10.1080/10705511.2017.1402334.

References

Corrections

If you see mistakes or want to suggest changes, please create an issue on the source repository.

Reuse

Text and figures are licensed under Creative Commons Attribution CC BY 4.0. Source code is available at https://github.com/hai-mn/hai-mn.github.io, unless otherwise noted. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".

Citation

For attribution, please cite this work as

Nguyen (2021, May 26). HaiBiostat: Set up to run Mplus inside Rmarkdown. Retrieved from https://hai-mn.github.io/posts/2021-05-26-Mplus-as-a-knitr-engine/

BibTeX citation

@misc{nguyen2021set,
  author = {Nguyen, Hai},
  title = {HaiBiostat: Set up to run Mplus inside Rmarkdown},
  url = {https://hai-mn.github.io/posts/2021-05-26-Mplus-as-a-knitr-engine/},
  year = {2021}
}