mplusModeler Example

Rich Jones with all credit to Joshua Wiley

Tuesday, December 02, 2014

mplusModeler

mplusModeler is part of the MplusAutomation package.

It can be used to call Mplus interactively.

I will show you how.

This code was prepared by Josh Wiley, and shared without permission but I think he would d be cool with it.

Note: must needs R 3.1.2+ and MplusAutomation 0.6-3

set the working directory

setwd("c:/work/syntax")

There are those in the R community who think this is a bad idea, setting the working directory.

I think they are wrong. Just do all your setwd’ing at the top of the file and make all folder referencing that follows using relative paths. It’ll be OK.

Load some packages

library(MplusAutomation)
library(texreg)
library(datasets)

Clean up

In case you’ve run this model before and want to clean up any leftover output and input and dat files. Note that mplusModeler will overwrite by default. But belts and suspenders.

foo <- file.remove("mtcars.inp")
foo <- file.remove("mtcars.out")
foo <- file.remove("mtcars.dat")

Call Mplus with mplusModeler

res <- mplusModeler(
  mplusObject(
    MODEL = "mpg ON wt hp; wt WITH hp;", rdata = mtcars),
  "mtcars.dat", run = 1L)
## No R variables to use specified. 
## Selected automatically as any variable name that occurs in the MODEL or DEFINE section.
## Wrote model to: mtcars.inp
## Wrote data to: mtcars.dat
## 
## Running model: mtcars.inp 
## System command: C:\Windows\system32\cmd.exe /c cd "C:\WORK\Syntax" && "Mplus" "mtcars.inp" 
## Reading model:  mtcars.out

See whats here now

pwd <- getwd()
list.files(pwd, "mtcars.*")
## [1] "mtcars.dat" "mtcars.inp" "mtcars.out"

look at the results (1 of 2)

summary(res)
## Estimated using ML 
## Number of obs: 32, number of (free) parameters: 9 
## 
## Model: Chi2(df = 0) = 0, p = 0 
## Baseline model: Chi2(df = 2) = 56.103, p = 0 
## 
## Fit Indices: 
## 
## CFI = 1, TLI = 1, SRMR = 0 
## RMSEA = 0, 90% CI [0, 0], p < .05 = 0 
## AIC = 597.217, BIC = 610.408

look at the results (2 of 2)

screenreg(res, single.row=TRUE)
## 
## =======================================
##                   Model 1              
## ---------------------------------------
##  MPG<-WT            -3.88    (0.60) ***
##  MPG<-HP            -0.03    (0.01) ***
##  WT<->HP            42.81   (13.76) ** 
##  HP<-Means         146.69   (11.93) ***
##  WT<-Means           3.22    (0.17) ***
##  MPG<-Intercepts    37.23    (1.52) ***
##  HP<->HP          4553.97 (1138.49) ***
##  WT<->WT             0.93    (0.23) ***
##  MPG<->MPG           6.09    (1.52) ***
## =======================================
## *** p < 0.001, ** p < 0.01, * p < 0.05

Examine the ouput in detail

cat( readLines( "mtcars.out" ) , sep = "\n" )
## Mplus VERSION 7.11
## MUTHEN & MUTHEN
## 12/02/2014   2:56 PM
## 
## INPUT INSTRUCTIONS
## 
##   DATA:
##   FILE = "mtcars.dat";
## 
##   VARIABLE:
##   NAMES = mpg hp wt;
##    MISSING=.;
## 
##   MODEL:
##   mpg ON wt hp; wt WITH hp;
## 
## 
## 
## INPUT READING TERMINATED NORMALLY
## 
## 
## 
## 
## SUMMARY OF ANALYSIS
## 
## Number of groups                                                 1
## Number of observations                                          32
## 
## Number of dependent variables                                    1
## Number of independent variables                                  2
## Number of continuous latent variables                            0
## 
## Observed dependent variables
## 
##   Continuous
##    MPG
## 
## Observed independent variables
##    HP          WT
## 
## 
## Estimator                                                       ML
## Information matrix                                        OBSERVED
## Maximum number of iterations                                  1000
## Convergence criterion                                    0.500D-04
## Maximum number of steepest descent iterations                   20
## Maximum number of iterations for H1                           2000
## Convergence criterion for H1                             0.100D-03
## 
## Input data file(s)
##   mtcars.dat
## 
## Input data format  FREE
## 
## 
## SUMMARY OF DATA
## 
##      Number of missing data patterns             1
## 
## 
## COVARIANCE COVERAGE OF DATA
## 
## Minimum covariance coverage value   0.100
## 
## 
##      PROPORTION OF DATA PRESENT
## 
## 
##            Covariance Coverage
##               MPG           HP            WT
##               ________      ________      ________
##  MPG            1.000
##  HP             1.000         1.000
##  WT             1.000         1.000         1.000
## 
## 
## 
## THE MODEL ESTIMATION TERMINATED NORMALLY
## 
## 
## 
## MODEL FIT INFORMATION
## 
## Number of Free Parameters                        9
## 
## Loglikelihood
## 
##           H0 Value                        -289.608
##           H1 Value                        -289.608
## 
## Information Criteria
## 
##           Akaike (AIC)                     597.217
##           Bayesian (BIC)                   610.408
##           Sample-Size Adjusted BIC         582.351
##             (n* = (n + 2) / 24)
## 
## Chi-Square Test of Model Fit
## 
##           Value                              0.000
##           Degrees of Freedom                     0
##           P-Value                           0.0000
## 
## RMSEA (Root Mean Square Error Of Approximation)
## 
##           Estimate                           0.000
##           90 Percent C.I.                    0.000  0.000
##           Probability RMSEA <= .05           0.000
## 
## CFI/TLI
## 
##           CFI                                1.000
##           TLI                                1.000
## 
## Chi-Square Test of Model Fit for the Baseline Model
## 
##           Value                             56.103
##           Degrees of Freedom                     2
##           P-Value                           0.0000
## 
## SRMR (Standardized Root Mean Square Residual)
## 
##           Value                              0.000
## 
## 
## 
## MODEL RESULTS
## 
##                                                     Two-Tailed
##                     Estimate       S.E.  Est./S.E.    P-Value
## 
##  MPG      ON
##     WT                -3.878      0.602     -6.438      0.000
##     HP                -0.032      0.009     -3.696      0.000
## 
##  WT       WITH
##     HP                42.812     13.757      3.112      0.002
## 
##  Means
##     HP               146.688     11.929     12.296      0.000
##     WT                 3.217      0.170     18.898      0.000
## 
##  Intercepts
##     MPG               37.227      1.522     24.459      0.000
## 
##  Variances
##     HP              4553.965   1138.491      4.000      0.000
##     WT                 0.927      0.232      4.000      0.000
## 
##  Residual Variances
##     MPG                6.095      1.524      4.000      0.000
## 
## 
## QUALITY OF NUMERICAL RESULTS
## 
##      Condition Number for the Information Matrix              0.116E-04
##        (ratio of smallest to largest eigenvalue)
## 
## 
##      Beginning Time:  14:56:46
##         Ending Time:  14:56:46
##        Elapsed Time:  00:00:00
## 
## 
## 
## 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-2013 Muthen & Muthen

Session information

sessionInfo()
## R version 3.1.2 (2014-10-31)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## 
## locale:
## [1] LC_COLLATE=English_United States.1252 
## [2] LC_CTYPE=English_United States.1252   
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] texreg_1.34           MplusAutomation_0.6-3
## 
## loaded via a namespace (and not attached):
##  [1] boot_1.3-13      coda_0.16-1      digest_0.6.4     evaluate_0.5.5  
##  [5] formatR_1.0      grid_3.1.2       gsubfn_0.6-6     htmltools_0.2.6 
##  [9] knitr_1.7        lattice_0.20-29  plyr_1.8.1       proto_0.3-10    
## [13] Rcpp_0.11.3      rmarkdown_0.3.10 stringr_0.6.2    tcltk_3.1.2     
## [17] tools_3.1.2      xtable_1.7-4     yaml_2.1.13