Mplus version 8 was used for these examples. The examples on this page use a dataset (path.dat). All files used in this portion of the seminar can be downloaded here.
In the course of an analysis, you may wish to save information from a given model. For example, you may want to use the output as the basis for a simulation in Mplus or to perform certain types of model diagnostics. The savedata command allows the user to save information from a model in a text file. This information can then be used by Mplus or read into another statistical package. Unlike the output files, which are formatted for human readers, the files created by savedata are intended for Mplus or other programs to read; thus the results are often saved in a plain text format, and values are often in scientific notation. This page shows only a few of the options available with the savedata command. Please see the Mplus manual for a full listing of available options for the savedata command. In some cases, more model-specific information can be saved. For example, one can request factor scores be saved after a confirmatory factor analysis.
1.0 Saving the data used in estimation
The file option of the savedata command allows you to save the variables used in the analysis to a text file. All variables used in the analysis, including variables that are transformations of other variables, are saved. Categorical variables that have been recoded and weight variables that have been rescaled by Mplus are saved in their new form. Additional variables can be saved using the auxiliary option of the variable command. The name of the new file follows the file is option. In this example the file name is newdata with the extension .dat. If no extension is given, the file is produced without one. The input model below is a relatively simple path model, but the savedata command is available for a variety of models. No changes to the model, other than the addition of the savedata command and file option, are necessary.
title: Saving data used in estimation data: file is path.dat; variable: names are hs gre col grad; model: gre on hs col; grad on hs col gre; hs with col; savedata: file is newdata.dat;
Below is a portion of the output generated by the above input file. The omitted output is exactly the same as the output from an otherwise identical input file that did not include the savedata. In other words, the savedata command does not change the model. The savedata command does result in some additional output at the very bottom of the output file, as shown below. Among other information, the additional output gives the order of variables in the new dataset, and the format in which they are saved.
<output omitted> SAVEDATA INFORMATION Save file newdata.dat Order and format of variables GRE F10.3 GRAD F10.3 HS F10.3 COL F10.3 Save file format 4F10.3 Save file record length 10000 DIAGRAM INFORMATION Use View Diagram under the Diagram menu in the Mplus Editor to view the diagram. If running Mplus from the Mplus Diagrammer, the diagram opens automatically. Diagram output c:\temp\01-saving.dgm
The file produced by the file option of the savedata command contains one line for each case used to estimate our model. The first few lines of the file newdata.dat are shown below.
52.000 57.000 57.000 41.000 59.000 61.000 68.000 53.000 33.000 31.000 44.000 54.000 44.000 56.000 63.000 47.000 52.000 61.000 47.000 57.000 <output omitted>
2.0 Adding Measures of Influence to Saved Data
The log-likelihood distance measure of influence, and/or Cook’s D can be requested in conjunction with the file option of the savedata command. Including save = influence; or save = cooks; adds the log-likelihood (influence) and/or Cook’s D (cooks) measure of influence for each case to the file containing the data used in estimation (i.e., the file specified by the file is option). Below we have used save = influence cooks; to request both measures.
title: Save data + ll distance + Cook's D data: file is path.dat; variable: names are hs gre col grad; model: gre on hs col; grad on hs col gre; hs with col; savedata: file is influence.dat; save is influence cooks;
Below is a portion of the output from the above input file. The output is similar to that from savedata with only the file option, except that two additional variables, outinfl and outcook are included in the saved dataset.
SAVEDATA INFORMATION Save file influence.dat Order and format of variables GRE F10.3 GRAD F10.3 HS F10.3 COL F10.3 OUTINFL F10.3 OUTCOOK F10.3 Save file format 6F10.3 Save file record length 10000 DIAGRAM INFORMATION Use View Diagram under the Diagram menu in the Mplus Editor to view the diagram. If running Mplus from the Mplus Diagrammer, the diagram opens automatically. Diagram output c:\temp\02-saving.dgm
As with the previous example, the file influence.dat contains one line for each case used to estimate the model. The first few lines of the file influence.dat are shown below. Note that the file now contains six variables (each in its own column): the four observed variables, plus two variables containing the value of the influence statistics for each case.
52.000 57.000 57.000 41.000 0.075 0.074 59.000 61.000 68.000 53.000 0.054 0.054 33.000 31.000 44.000 54.000 0.276 0.270 44.000 56.000 63.000 47.000 0.114 0.113 52.000 61.000 47.000 57.000 0.036 0.036
3.0 Saving sample statistics (Correlation and Covariance Matrices)
The sample option of the savedata command saves a sample correlation or covariance matrix in a text file. By default a covariance matrix is produced if all of the variables are continuous, and a correlation matrix is produced if the variables are categorical or a mix of categorical and continuous. The sample option both requests the additional output and specifies the name of the file, in this example, sampledata.dat. The input file below includes the savedata command with the sample option.
title: Saving correlation and covariance matrices data: file is path.dat; variable: names are hs gre col grad; model: gre on hs col; grad on hs col gre; hs with col; savedata: sample is sampledata.dat;
The output associated with the sample option of the savedata command is shown below.
SAVEDATA INFORMATION Sample/H1/Pooled-Within Matrix Save file sampledata.dat Save type COVARIANCE Save format Free DIAGRAM INFORMATION Use View Diagram under the Diagram menu in the Mplus Editor to view the diagram. If running Mplus from the Mplus Diagrammer, the diagram opens automatically. Diagram output c:\temp\03-saving.dgm
The entire contents of the file sampledata.dat is shown below. The file contains two lines, each with values that appear in five columns, for a total of ten values, which happens to be the number of unique covariances/correlations in a matrix with four variables (recall that the number of unique values in a covariance matrix is n*(n+1)/2, where n is the number of variables). Note that the values are given in scientific notation.
0.89394375E+02 0.61236125E+02 0.11468097E+03 0.57706750E+02 0.68066850E+02 0.10459710E+03 0.54555125E+02 0.54488775E+02 0.63296650E+02 0.87328975E+02