socialscientist
socialscientist

Reputation: 4272

R: is it possible to draw a diagram with SemPlot without fitting a model?

The documentation for the SemPlot package doesn't appear to show any way to build a path diagram without passing a model object and the model objects appear to only be the output of fit models in e.g. lavaan. Any way to specify nodes and paths similar to Diagrammar without fitting a model to data?

Upvotes: 6

Views: 190

Answers (2)

Sinval
Sinval

Reputation: 1417

You definitely can:

library(lavaan)
#> This is lavaan 0.6-19
#> lavaan is FREE software! Please report any bugs.
library(semPlot)

model <- "
visual  =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed   =~ x7 + x8 + x9 
"

plot_model <- semPlotModel_lavaanModel(model,auto.var = TRUE,
                                       auto.fix.first = F,
                                       auto.cov.lv.x = TRUE)


diagram_plot <- semPlot::semPaths(object = plot_model,
                                  thresholds = F,
                                  whatLabels = "none",
                                  intercepts = F,
                                  exoCov = T,
                                  style = "ram",
                                  curvePivot=F,
                                  edge.color="black",
                                  DoNotPlot = T,
                                  residuals = F,
                                  sizeMan = 8)

plot(diagram_plot)

Created on 2025-02-23 with reprex v2.1.1

Upvotes: 0

Julian Karch
Julian Karch

Reputation: 492

It should be possible, but it does not seem to be the intended use-case of the package. semPlot extracts the things it needs to plot into an object of class semPlotModel, see the semPlotModel function and ?semPlotModel-class. You could create a semPlotModel programmatically from scratch, but this will likely be cumbersome. See the below code where I modify the model extracted from lavaan for inspiration.

HS.model <- 'visual  =~ x1 + x2 + x3'
fit <- cfa(HS.model, data = HolzingerSwineford1939)
model <- semPlotModel(fit)
model@Pars$lhs[1] <- "x1"
model@Pars$rhs[1] <- "visual"
semPaths(model)

Upvotes: 1

Related Questions