MPlus, Python and R: One Development Enviornment for All

I often have to work in different environments to perform analyses (e.g. MPlus, Python and R). In some cases data manipulation and cleaning is better done in certain environments (R > MPlus). Plotting and stats packages are better in other environments (e.g. R > MPlus > Python). However, I wanted to create a way to manage workflow across environments within a single IDE (i.e. Rstudio). Naive R cannot do this but with a few packages in R, you can use RStudio for all your data management needs. Probably the only downside is that someone else who is not familiar with this type of workflow could easily be confused. The nice thing is that provided that you do not change versions of software you should be able to have a reliable record of your steps in data analysis.

For each install the following:

MPlus: MPlusAutomation

Python: PythonInR

**Note for both you must have Python and MPlus installed on your system. Python works for both versions (2+ and 3+). MPlus has only been tested on version 7.2, however I assume it works with versions 7+.

I have created Markdown files to show sample workflow.

MPlus

For MPlus I had to create a number of custom functions in order to streamline and automate creation of the .inp files. These functions automatically, create an MPlus header that is based on the data (R data.frame) and the variables of interest. Then you go onto write your MPlus syntax beginning in the middle of the VARIABLE: section. You can add or remove MPlus sections to the code thereafter. When run, the code generates a custom folder for the .inp, .out, .dat files. It also saves the output in standard text format and does some conversions to for knitr markdown presentation. It also saves all relevant output in an R list() object for post processing and plotting in R. MPlusAutomation has a number of built in features including plotting that are not highlighted in my examples.

MPlus_Example

MPlus_Example.Rmd

Python

This implementations is fairly straight forward. With different systems you need to make some adjustments to the code. Unfortunately, with windows systems you have to point the script at where python is installed on that particular computer using the pyConnect() function. This could be annoying if you are working across computers with different file architectures.

Python_Example

Python_Example.Rmd

Python Code for the above example

# -*- coding: utf-8 -*-
"""
Created on Wed Mar 9 09:55:46 2016

@author: Subhash Jaini
"""


import pandas as pd
import matplotlib.pyplot as plt

d = {'First' : [1., 2., 3.],'Second' : [1,2,3]}

Test_string = "Hello world"

BB = pd.DataFrame(d)
print(BB)


plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.savefig('tessstttyyy.png', dpi=100)
#plt.show()

You can also plot objects within Python but to import them into .Rmd you need to save them as files in python then import. Other objects (e.g. data.frame) can be passed from Python to R.

Enjoy,

Eric