Error when viewing output from testEstimates function
Closed this issue · 2 comments
This may or may not be an error related to your package, but I receive the error: "Error in fmt[isLarge] <- sub(paste0(postfix, "$"), "e", fmt[isLarge]) : NAs are not allowed in subscripted assignments" when running the final line of code in this example to undertake mediation analysis on multiply imputed datasets. Are you able to clarify for me whether it is an issue with the package?
I think the issue is that the extracted parameters (qhat
) in this example include non-free parameters that shouldn't really be pooled. This produces NaN
values that weren't formatted correctly, hence the error. I've uploaded a fix, so the example should run fine now.
However, notice that mitml
supports lavaan
natively since version 0.4-3, so you can use the simplified code below to run your model and pool the results. This automatically omits non-free parameters (unless they are user-defined).
library(mice)
library(miceadds)
library(mitml)
data <- airquality
# create missing data
data[80:81, 3] <- rep(NA, 2)
data[4:15, 3] <- rep(NA, 12)
data[1:5, 2] <- rep(NA, 5)
# remove categorical variables
data <- data[-c(5,6)]
summary(data)
# impute
imp <- mice(data, m = 5, maxit = 10, meth = 'pmm', seed = 500)
summary(imp)
# create list of imputed data sets
implist <- mids2mitml.list(imp)
# create mediation model
mediation <- '
# DV
Temp ~ cprime*Ozone + b*Solar.R
# mediator
Solar.R ~ a*Ozone
# indirect effect
indirect := a*b
direct := cprime
total := direct + indirect
'
# fit model
mod2 <- with(implist, lavaan::sem(mediation), include.data = TRUE)
# pool results
res2 <- mitml::testEstimates(model = mod2)
res2
# Call:
#
# mitml::testEstimates(model = mod2)
#
# Final parameter estimates and inferences obtained from 5 imputed data sets.
#
# Estimate Std.Error t.value df P(>|t|) RIV FMI
# Temp~Ozone (cprime) 0.188 0.024 7.683 24.974 0.000 0.667 0.443
# Temp~Solar.R (b) 0.011 0.008 1.309 32.891 0.200 0.535 0.385
# Solar.R~Ozone (a) 0.957 0.254 3.767 62.484 0.000 0.339 0.276
# Temp~~Temp 48.800 5.709 8.547 2006.556 0.000 0.047 0.046
# Solar.R~~Solar.R 7406.932 897.781 8.250 334.923 0.000 0.123 0.115
# indirect:=a*b (indirect) 0.010 0.008 1.257 51.989 0.215 0.384 0.304
# direct:=cprime (direct) 0.188 0.024 7.683 24.974 0.000 0.667 0.443
# total:=direct+indirect (total) 0.198 0.022 8.876 33.198 0.000 0.532 0.383
#
# Unadjusted hypothesis test as appropriate in larger samples.