--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.
--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')
--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()
--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)")
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. .)
--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.
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