3

I am trying to supply a lavaan.mi object (a SEM modelling multiple-imputed data using runMI() from semTools 0.5-2.) to semPaths() (semPlot 1.1.2). Doing so returns the error:

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘semPlotModel_S4’ for signature ‘"lavaan.mi"’

This is flagged as an 'issue' on GitHub, but I'd be grateful for a suggested workaround. Here is an example:

# Libraries 
library(mice)
library(semTools)
library(lavaan)
library(semPlot)

# Create DF 
HSMiss <- HolzingerSwineford1939[,paste("x", 1:9, sep="")]
randomMiss <- rbinom(prod(dim(HSMiss)), 1, 0.1)
randomMiss <- matrix(as.logical(randomMiss), nrow=nrow(HSMiss))
HSMiss[randomMiss] <- NA

# Specify model
HS.model <- ' visual  =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed   =~ x7 + x8 + x9 '

# Fit the model 
model_fit <- runMI(HS.model, 
              data=HSMiss,
              m = 5, 
              miPackage="mice",
              fun="sem")

# Attempt to create SEM plot
semPaths(model_fit)
pienkowski
  • 61
  • 7

1 Answers1

1

So the 'solution' I used was to simply substitute the parameter estimates from the MI model into a 'semPlotModel' object created from a 'lavaan' object:

# Libraries 
library(mice)
library(semTools)
library(lavaan)
library(semPlot)

# Create DF 
HSMiss <- HolzingerSwineford1939[,paste("x", 1:9, sep="")]
randomMiss <- rbinom(prod(dim(HSMiss)), 1, 0.1)
randomMiss <- matrix(as.logical(randomMiss), nrow=nrow(HSMiss))
HSMiss[randomMiss] <- NA

# Specify model
HS.model <- ' visual  =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed   =~ x7 + x8 + x9 '

# Create a 'dummy' object, with the same model structure as we'll use in the MI method 
model_fit <- sem(HS.model,  data=HSMiss)

# Create a dummy semplot object 
SEMPLOT <- semPlot::semPlotModel(model_fit)

# Fit the actual model 
model_fit <- runMI(HS.model, 
                   data=HSMiss,
                   m = 5, 
                   miPackage="mice",
                   fun="sem")

# Extract the direct results from the SEM with MI data 
desired_output <- data.frame(standardizedsolution(model_fit))

# Subsitute the desired parameter estimates for the desired ones, in the semplot object 
SEMPLOT@Pars$std <- desired_output$est.std

# Create SEM plot
semPaths(SEMPLOT)

pienkowski
  • 61
  • 7
  • 2
    WARNING: The `standardizedsolution()` function is not for `lavaan.mi` objects. It will still work on a `lavaanList` object, from which the `lavaan.mi` class inherits, but that is not the standardized solution calculated from pooled results. Inside `standardizedsolution()`, I think it just grabs whatever estimates are stored in the `@ParTable` slot, as would be returned by `lavaan::parTable()`, which might be the average estimates or those from the first (imputed) data set. You should use `class?lavaan.mi` methods to get the standardized estimates: `summary(fit, standardized = "std.all")` – Terrence Mar 01 '23 at 08:58