Search code examples
rvisualizationr-lavaansemplot

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?


Solution

  • 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)