3

I am trying to plot a path diagram of a Structural Equation Model(SEM) in R. I was able to plot it using semPlot::semPaths(). The output is similar to enter image description here The SEM was modeled using lavaan package.

I want a plot similar to enter image description here. with estimates and p values. Can anyone help me out?

Oli
  • 9,766
  • 5
  • 25
  • 46
Stat009
  • 45
  • 1
  • 5

2 Answers2

1

My suggestion would be lavaanPlot (see more of it in the author's personal website):

library(lavaan)
library(lavaanPlot)
# path model
model <- 'mpg ~ cyl + disp + hp
          qsec ~ disp + hp + wt'

fit1 <- sem(model, data = mtcars)
labels1 <- list(mpg = "Miles Per Gallon", cyl = "Cylinders", disp = "Displacement", hp = "Horsepower", qsec = "Speed", wt = "Weight") #define labels
lavaanPlot(model = fit1, labels = labels1, coefs = TRUE, stand = TRUE, sig = 0.05) #standardized regression paths, showing only paths with p<= .05

enter image description here

Sinval
  • 1,315
  • 1
  • 16
  • 25
  • When I tried your sample code in my system, it is working fine. But with my model it is giving me an error "Error in as.character.default(new("lavaan", version = "0.6.8", call = lavaan::lavaan(model = model1, : no method for coercing this S4 class to a vector". – Stat009 Mar 15 '21 at 09:02
  • My model, sem_protein <- sem(model1, data = data1,estimator="DWLS"). This model is working without any error. summary(sem_protein) lavaan 0.6-8 ended normally after 231 iterations Estimator DWLS Optimization method NLMINB Number of free parameters 48 Used Total Number of observations 151977 224714 – Stat009 Mar 15 '21 at 09:06
  • Just try: `lavaanPlot(model = fit_your_model)` just use this with your own model. If you want to use labels, you must define them accordingly. – Sinval Mar 15 '21 at 09:13
  • That Worked. I have many exogenous categorical variables with more than 2 categories. I replaced each of them k-1 dummy variables and got the estimates. However, in the lavaanPlot, I have k-1 rectangles for k-1 dummy variables. Since I have many variables to dummy code plot is not looking good. Is there any way to work with this? – Stat009 Mar 17 '21 at 10:59
  • Here is the sample code; `WI=rep(c("WI1","WI2","WI3","WI4","WI5"),10) ED=c(rep("ED1",15 ), rep("ED2",7), rep("ED3",8), rep("ED4",20)) Price=rnorm(50,70,5) Dem=rnorm(50,120,5) Status=round(runif(50,0,1),0) df=data.frame(WI,ED,Price,Status,Dem) df_1 = cbind(df, dummy.code(df$WI)) df_1 = cbind(df_1, dummy.code(df$ED)) # path model model <- 'Price ~ WI2+WI3+WI4+WI5 Dem ~ ED2+ED3+ED4 Status ~ Price +Dem ' fit1 <- sem(model, data = df_1) lavaanPlot(model = fit1,coefs = TRUE)` – Stat009 Mar 17 '21 at 11:02
  • If if solves your problem, please mark it as solved. `lavaanPlot(model = fit1, graph_options = list(layout="dot",rankdir = "LR"), coefs=T, stand=T)` – Sinval Mar 17 '21 at 11:06
  • Is there any way to subgraph few nodes and form clusters (for example, all the dummies of a categorical variable) like in DiagrammeR. – Stat009 Mar 18 '21 at 09:02
  • Hmmm I do not think that it is possible. But you can always ask the original author if it is possible to group particular variables. Here (https://github.com/alishinski/lavaanPlot) – Sinval Mar 18 '21 at 10:56
0

check this example, it might be helpful https://rstudio-pubs-static.s3.amazonaws.com/78926_5aa94ae32fae49f3a384ce885744ef4a.html

CleanCoder
  • 332
  • 3
  • 14