I want to output a path diagram of distributed covariance structure analysis (SEM) by linking Python and R.

Motivation

--I can't find a module to do sem with python! --I want to automatically output a beautiful sem diagram --I want to use R on python ――It is R (I feel) that the itchy part of the analysis can be reached. -According to R vs Python: Compare Data Analysis --Python is simpler for tasks other than statistics --R generally supports more statistics

** Theory: The strongest theory if supplemented with python and R **

--R and python can be linked by using "pyper" which is a python module.

Pyper settings on python

--Installing pyper pip install pyper

--Import pyper and create pyper indent --It is necessary to specify the path of R with "RCMD =" (with //) -By setting ʻuse_numpy ='True', use_pandas ='True'`, it is possible to read numpy, pandas type data into R.

python


import pyper 
import os
import numpy as np
import pandas as pd
#Create pyper indent
r = pyper.R(RCMD="C:\\Program Files\\R\\R-3.2.5\\bin\\x64\\R", use_numpy='True', use_pandas='True')

Creating sem program files in R

--At first, I was using the R "sem" package, but after a series of errors, I switched to "lavaan" (crying). --The sem function in lavaan was easier to set up. --For creating this file, refer to Try "First covariance structure analysis Amos path analysis" with lavaan. We received. --R has pre-installed packages "lavaan" and "semplot" to be used ――It seems that using this "semplot" will easily output a diagram of the estimation result of "lavaan". --This R program creates an SEM model, estimates it, and saves the path diagram as .png.

R(lavaan.R)


library(lavaan)
library(semPlot)

r <-  cor(dat)

model1 <- ('
  reach=~a1*Intermediate_test+a2*term_end_exam+a3*minitest
  skill=~b1*mock_examination1+b2*mock_examination2+b3*mock_examination3 
  reach~~skill
  ')

fit <- sem(model1, sample.cov=r, sample.nobs=284)


png("C:\\Users\\xxxxxxxx\\Documents\\Python Scripts\\python_R\\semplot.png ") 
semPaths(fit, "std", edge.label.cex = 1,line = 3, curvePivot = TRUE,
         sizeMan = 8, sizeInt = 1, sizeLat = 8)
dev.off()

Link python and R

--Let python read the data --This time, Analysis using R (SEM) of I converted "book usage data" into English and used it.

python


test = pd.read_csv("C:\Users\xxxxxxx\Documents\R\input\data\ch14sem.csv")

--Pass python data to R --Pass "test" to R as "dat".

python


r.assign("dat", test)

--Execute the R file "lavaan.R" with python

python


r("source(file='C:\\Users\\xxxxx\\Documents\\R\\input\\script\\lavaan.R')")

--r.get ("")can extract objects in R

python(Extraction of correlation matrix)


 r.get("r")

--It is possible to execute the R command on python on python with r ("") and output the result.

python(Estimated result display)


print r("summary(fit, standardized=TRUE, fit.measure=TRUE)")

Output path diagram

semplot.png

According to semPlot Examples | --Sacha Epskamp, this path diagram can be set in detail. (With this, I don't have to draw a path diagram with PowerPoint or draw.io. .)

Where it gets stuck

--If you don't indent on R, you will get an error on python even if you don't get an error on R. --Plot on R was not brought to python with r.get ("") , but .png was output. --Use // to specify the path.

The site that I used as a reference

Analysis using R (SEM)

http://mizumot.com/handbook/?page_id=625

Try "first covariance structure analysis path analysis by Amos" with lavaan

https://sites.google.com/site/officeoga/r/hajimeteno

Works with Python and R

http://qiita.com/ynakayama/items/f84dc659f1337d71dd9e

PypeR

http://www.webarray.org/softwares/PypeR/

R vs Python: Compare data analysis

http://postd.cc/r-vs-python-head-to-head-data-analysis/

semPlot Examples | - Sacha Epskamp

http://sachaepskamp.com/semPlot/examples

Recommended Posts

I want to output a path diagram of distributed covariance structure analysis (SEM) by linking Python and R.
I want to start a lot of processes from python
I want to know the features of Python and pip
I want to create a karaoke sound source by separating instruments and vocals using Python
I want to output the beginning of the next month with Python
I want to use both key and value of Python iterator
I want to clear up the question of the "__init__" method and the "self" argument of a Python class.
I want to build a Python environment
I want to color a part of an Excel string in Python
I tried to verify and analyze the acceleration of Python by Cython
Python: I want to measure the processing time of a function neatly
I want to make a web application using React and Python flask
I want to drop a file on tkinter and get its path [Tkinter DnD2]
I want to exe and distribute a program that resizes images Python3 + pyinstaller
I want to create a window in Python
I want to make a game with Python
I want to sell Mercari by scraping python
I want to write to a file with Python
I want to use a network defined by myself in PPO2 of Stable Baselines
I want to write a triple loop and conditional branch in one line in python
I tried to compare the processing speed with dplyr of R and pandas of Python
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to iterate a Python generator many times
I want to generate a UUID quickly (memorandum) ~ Python ~
I want to handle optimization with python and cplex
I want to write in Python! (2) Let's write a test
I want to randomly sample a file in Python
I want to work with a robot in python.
I want to install a package of Php Redis
[Python] I want to make a nested list a tuple
I want to use the R dataset in python
I want to run a quantum computer with Python
[Python] I want to make a 3D scatter plot of the epicenter with Cartopy + Matplotlib!
I made a class to get the analysis result by MeCab in ndarray with python
I want to find the intersection of a Bezier curve and a straight line (Bezier Clipping method)
I want to output a beautifully customized heat map of the correlation matrix. matplotlib edition
I want to make a voice changer using Python and SPTK with reference to a famous site
[Python] How to make a list of character strings character by character
[Python] I want to get a common set between numpy
NikuGan ~ I want to see a lot of delicious meat! !!
I want to send a message from Python to LINE Bot
Keras I want to get the output of any layer !!
I want Sphinx to be convenient and used by everyone
I want to make input () a nice complement in python
I wrote AWS Lambda, and I was a little addicted to the default value of Python arguments
Find the white Christmas rate by prefecture with Python and map it to a map of Japan
"Introduction to data analysis by Bayesian statistical modeling starting with R and Stan" implemented in Python
[Python scraping] Output the URL and title of the site containing a specific keyword to a text file
I want to extract the tag information (title and artist) of a music file (flac, wav).
Ported from R language of "Sazae-san's rock-paper-scissors data analysis" to Python
Recommended books and sources of data analysis programming (Python or R)
I want to record the execution time and keep a log.
I want to use a wildcard that I want to shell with Python remove
A simple data analysis of Bitcoin provided by CoinMetrics in Python
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
I tried to make a regular expression of "amount" using Python
Automatic acquisition of gene expression level data by python and R
I tried to make a regular expression of "time" using Python
Read the standard output of a subprocess line by line in Python
I tried to create a list of prime numbers with python