Multiple imputation issues again

441 views
Skip to first unread message

David Kaplan

unread,
Dec 16, 2017, 7:18:31 PM12/16/17
to lavaan
Hi all,

I have had this issue before, but for some reason I can't MI to work within lavaan.    Here's the code with some other stuff snipped.

### Multiple imputation using MICE ###

scimodel_imp<-mice(scimodel, m = 5, method="pmm")
mice.imp <- NULL
for(i in 1:5) mice.imp[[i]] <- complete(scimodel_imp, action=i, inc=FALSE)  

# run lavaan with previously imputed data using runMI  
# Note that MLR handles the non-normality

fitmi <- runMI(pathmodel,estimator="MLR", 
               data=mice.imp,
               fun="sem")
----------------------------------

The code produces the multiply imputed data sets, but here is the error message.

-------------------
Error in checkSlotAssignment(object, name, value) : 
  assignment of an object of class “lavModel” is not valid for slot ‘Model’ in an object of class “lavaanStar”; is(value, "Model") is not TRUE
------------------------------

In an earlier attempt, I ran the following code to get to the source file, but I'm getting a 404 message

install.packages("devtools")
install.packages("lavaan", repos = "http://www.da.ugent.be", type = "source")
devtools::install_github("simsem/semTools/semTools")

Any assistance is appreciated.  

David

Edward Rigdon

unread,
Dec 16, 2017, 10:28:36 PM12/16/17
to lav...@googlegroups.com
 Version problem?

--
You received this message because you are subscribed to the Google Groups "lavaan" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lavaan+unsubscribe@googlegroups.com.
To post to this group, send email to lav...@googlegroups.com.
Visit this group at https://groups.google.com/group/lavaan.
For more options, visit https://groups.google.com/d/optout.

Terrence Jorgensen

unread,
Dec 18, 2017, 4:35:24 AM12/18/17
to lavaan
Error in checkSlotAssignment(object, name, value) : 
  assignment of an object of class “lavModel” is not valid for slot ‘Model’ in an object of class “lavaanStar”; is(value, "Model") is not TRUE

"lavaanStar" is the old object created in the semTools currently on CRAN, which is not compatible with lavaan 0.6-*

In an earlier attempt, I ran the following code to get to the source file, but I'm getting a 404 message

install.packages("devtools")
install.packages("lavaan", repos = "http://www.da.ugent.be", type = "source")
devtools::install_github("simsem/semTools/semTools")

Which specific line gives you a 404 message?  I just ran these lines with the latest R (using RStudio), and did not get an error.

R version 3.4.3 (2017-11-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale
:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached
base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base    

loaded via a
namespace (and not attached):
[1] compiler_3.4.3 tools_3.4.3    yaml_2.1.16  


If something weird is happening with install_github(), you can try installing a source I recently built for a semTools contributor:

install.packages("https://github.com/simsem/semTools/blob/master/builds/semTools_0.4-15.912.tar.gz",
                 repos
= NULL, type = "source")

Terrence D. Jorgensen
Postdoctoral Researcher, Methods and Statistics
Research Institute for Child Development and Education, the University of Amsterdam


David Kaplan

unread,
Dec 18, 2017, 1:31:47 PM12/18/17
to lavaan
Terrance,

I get the following error with the install instructions you provided.

Error in getOctD(x, offset, len) : invalid octal digit
Warning in install.packages :
  installation of package ‘/var/folders/hl/nzbg75k56w70l6__mjbd3cnm0000gp/T//RtmpUyWHtD/downloaded_packages/semTools_0.4-15.912.tar.gz’ had non-zero exit status

David Kaplan

unread,
Dec 18, 2017, 1:33:44 PM12/18/17
to lavaan
The version of lavaan I'm running is  0.6-1.1179
I am running this in RStudio version  Version 1.0.143

David

 Version problem?

To unsubscribe from this group and stop receiving emails from it, send an email to lavaan+un...@googlegroups.com.

David Kaplan

unread,
Dec 18, 2017, 1:43:17 PM12/18/17
to lavaan
I'm running semTools version 

0.4-14


I tried all of this in R rather than RStudio with the same error messages.  

Terrence Jorgensen

unread,
Dec 19, 2017, 6:54:03 AM12/19/17
to lavaan
Error in getOctD(x, offset, len) : invalid octal digit

Sorry for the trouble, David.  I didn't realize R throws an error when it sees an extra number after "0.4-15" indicating a development version.  I attached a source file that I installed without error in R 3.4.3.  Just download it to your working directory and run

install.packages("semTools_0.4-15.tar.gz", repos = NULL, type = "source")

I think there are still several MSEM kinks to work out before lavaan 0.6 can go to CRAN, at which point a new semTools will go to CRAN as well.
semTools_0.4-15.tar.gz

David Kaplan

unread,
Dec 19, 2017, 11:31:59 AM12/19/17
to lavaan
Hi Terrence,

That did the trick, except I am getting the following warning:

** WARNING ** lavaan (0.5-23.1097) model has NOT been fitted
** WARNING ** Estimates below are simply the starting values

The problem is that I am not getting standard errors, but I do get the chi-square.  The model runs perfectly well in every other context.

Thanks

David

Terrence Jorgensen

unread,
Dec 20, 2017, 3:42:31 AM12/20/17
to lavaan
That did the trick, except I am getting the following warning:
** WARNING ** lavaan (0.5-23.1097) model has NOT been fitted

I'm surprised you could install semTools 0.4-15 when you still have lavaan 0.5-23 on your machine, since semTools requires at least lavaan 0.6

install.packages("lavaan", repos="http://www.da.ugent.be", type="source")

David Kaplan

unread,
Dec 20, 2017, 10:09:06 AM12/20/17
to lavaan
Hi Terrence,

Thanks.  That did the trick.  It seems that I can only use "ml" as my estimator.  When I try using "mlr", I get the error message

Error in if (attr(x, "information") %in% c("expected", "first.order") ||  : 
  missing value where TRUE/FALSE needed

David

Terrence Jorgensen

unread,
Dec 23, 2017, 6:53:09 AM12/23/17
to lavaan
It seems that I can only use "ml" as my estimator.  When I try using "mlr", I get the error message

Error in if (attr(x, "information") %in% c("expected", "first.order") ||  : 
  missing value where TRUE/FALSE needed

Hi David, sorry for the delay.  Just finished final-exams week.

I wasn't expecting that error, so thanks for finding it.  I'll figure out the cause and put in a check when I have some time for programming in February.  

In the meantime, you can just use estimator = "mlm", which is asymptotically equivalent but requires complete data (which is the case for all your imputed data sets).  That summary() output is returned without the error in the example from the help page

HSMiss <- HolzingerSwineford1939[ , c(paste("x", 1:9, sep = ""),
                                     
"ageyr","agemo","school")]
set.seed(12345)
HSMiss$x5 <- ifelse(HSMiss$x5 <= quantile(HSMiss$x5, .3), NA, HSMiss$x5)
age
<- HSMiss$ageyr + HSMiss$agemo/12
HSMiss$x9 <- ifelse(age <= quantile(age, .3), NA, HSMiss$x9)

## specify CFA model from lavaan's ?cfa help page
HS
.model <- '
visual  =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed   =~ x7 + x8 + x9
'


## impute data within runMI...
out1
<- cfa.mi(HS.model, data = HSMiss, m = 20, seed = 12345,
               miArgs
= list(noms = "school"))
out2
<- cfa.mi(HS.model, data = HSMiss, m = 20, seed = 12345,
               miArgs
= list(noms = "school"), estimator = "mlm")
out3
<- cfa.mi(HS.model, data = HSMiss, m = 20, seed = 12345,
               miArgs
= list(noms = "school"), estimator = "mlr")

summary
(out1) # ML : no error
summary
(out2) # MLM: no error
summary
(out3) # MLR: error


But please let me know if you run into any other troubles with MLM.
Message has been deleted

David Kaplan

unread,
Dec 31, 2017, 3:09:46 PM12/31/17
to lavaan
Hi Terrence,

Something is still not right.  I am running SemTools version 0.4-15.tar.gz, lavaan version .6-1.1183, and R version 3.43.  I get the following error messages when I run the code below
There were 20 warnings (use warnings() to see them)
> anova(fitmi, indices=TRUE)
Error in anova(fitmi, indices = TRUE) : 
  trying to get slot "imputed" from an object of a basic class ("logical") with no slots
> summary(fitmi)
** WARNING ** lavaan (0.6-1.1183) model has NOT been fitted
** WARNING ** Estimates below are simply the starting values

The 20 warnings are all the same and say
1: In if (class(model) == "formula") { ... :
  the condition has length > 1 and only the first element will be used

I appreciate your patience, but I am planning to cover multiple imputation via lavaan and semTools in my upcoming SEM course.

David


-----------
require(semTools)
require(lavaan)
require(mice)

scimodel <- read.csv(file.choose(),header=T)


## Generate some missing values ####
scimodel$ses[scimodel$ses >=.9] <- NA
scimodel$sciach[scimodel$sciach >=30.00] <- NA
scimodel$scigrade6[scimodel$scigrade6 == 1] <- NA


## Science Achievement Example
## Kaplan (2009, pg. 15)
pathmodel <- ' 

# regressions
sciach ~ scigrade10
scigrade10 ~ scigrade6+ses+certsci+understand+challenge
challenge~understand
understand ~ certsci

'

### Multiple imputation using MICE ###

scimodel_imp<-mice(scimodel, m = 5, method="pmm")
mice.imp <- NULL
for(i in 1:5) mice.imp[[i]] <- complete(scimodel_imp, action=i, inc=FALSE)  

# run lavaan with previously imputed data using runMI  
# Note that MLR handles the non-normality

fitmi <- runMI(pathmodel,estimator="ml", 
               data=mice.imp,
               fun="sem")
anova(fitmi, indices=TRUE)   # Fit statistics are not provided with the 'anova' command #
summary(fitmi)
-------------

Maria Usacheva

unread,
Jan 1, 2018, 8:31:21 PM1/1/18
to lavaan
Hi,

sorry to jump on the older thread, but I'll try my luck, as I also have issues with multiple imputation with lavaan and semTools.

I have installed the following versions of lavaan and semTools (I was kindly directed to another post in this group by one of the stackoverflow members):

install.packages("lavaan", repos = "http://www.da.ugent.be", type = "source")
# if necessary:    install.packages("devtools")
devtools::install_github("simsem/semTools/semTools")

Then I run my model:
model5 <- 'zceadiff ~ mompa + zcdpea + momabhx
mompa ~ b1*zpeadiff + c*momabhx + b2*zmommhpsi
zpeadiff ~ a1*momabhx
zcdpea ~ momabhx
zmommhpsi ~ a2*momabhx
zpeadiff ~~ zcdpea
direct := c
indirect1 := a1 * b1
indirect1 := a2 * b2
total    := c + (a1 * b1) + (a2 * b2)'

fit5 <- runMI(model5, data = imputedData, fun="sem", ordered = "mompa", estimator="WLSMV")
summary(fit5, standardized = TRUE, ci = T)

It runs fine, except if I add "chi" = "mplus" to the runMI statement. Then it returns:   lavaan ERROR: unknown argument 'chi'
I've looked at semTools documentation and tried all over possible combinations to get fit indices for my model, from lavInspect or lavListInspect to fitMeasuresMX, with no result.

It does work if I use the solution posted in the end of this thread, anova(fit5, indices=T), but is there a way to get the original "chi=mplus" statement to work and get the model fit measures printed in a fashion similar to lavaan?

Thank you!!
Best,
Maria

Maria Usacheva

unread,
Jan 1, 2018, 8:33:53 PM1/1/18
to lavaan
Also, if I run miPowerFit it also returns "unknown 'what' argument in inspect function 'mi'...


On Saturday, December 16, 2017 at 4:18:31 PM UTC-8, David Kaplan wrote:

Terrence Jorgensen

unread,
Jan 5, 2018, 8:03:56 AM1/5/18
to lavaan
I am running SemTools version 0.4-15.tar.gz, lavaan version .6-1.1183, and R version 3.43.  I get the following error messages when I run the code below
There were 20 warnings (use warnings() to see them)
> anova(fitmi, indices=TRUE)
Error in anova(fitmi, indices = TRUE) : 
  trying to get slot "imputed" from an object of a basic class ("logical") with no slots
> summary(fitmi)
** WARNING ** lavaan (0.6-1.1183) model has NOT been fitted
** WARNING ** Estimates below are simply the starting values

These warnings are consistent with semTools 0.4-14, not 0.4-15.  Can you post your sessionInfo() output to verify?  Make sure to install semTools in a fresh R session, not after semTools has already been loaded.

Terrence Jorgensen

unread,
Jan 5, 2018, 8:41:46 AM1/5/18
to lavaan
It runs fine, except if I add "chi" = "mplus" to the runMI statement. Then it returns:   lavaan ERROR: unknown argument 'chi'

As the message indicates, "chi" is not an argument to (new version of) the runMI() function.  See the ?runMI help page for a list of accepted arguments.

Choosing a particular chi-squared value is now accomplished in the "test=" argument to the anova() function.  See the class?lavaan.mi help page for details.

It does work if I use the solution posted in the end of this thread, anova(fit5, indices=T), but is there a way to get the original "chi=mplus" statement to work and get the model fit measures printed in a fashion similar to lavaan?

Yes:

anova(fit5, test = "mplus", indices = TRUE)

Terrence Jorgensen

unread,
Jan 5, 2018, 8:50:47 AM1/5/18
to lavaan
Also, if I run miPowerFit it also returns "unknown 'what' argument in inspect function 'mi'...

That function is only for lavaan objects.  It is not designed to work with multiple imputations.  Later this year, I hope to work out how modification indices can be calculated with multiple imputations, at which point I could consider adapting miPowerFit() to work with lavaan.mi objects too.

Terrence Jorgensen

unread,
Apr 17, 2018, 6:48:56 PM4/17/18
to lavaan
It seems that I can only use "ml" as my estimator.  When I try using "mlr", I get the error message

Error in if (attr(x, "information") %in% c("expected", "first.order") ||  : 
  missing value where TRUE/FALSE needed

David

summary() was silently returning the data.frame of results but only failed to print it when add.attributes = TRUE.  I fixed this error, so the results now display correctly with MLR estimation. 

install.packages("lavaan", repos="http://www.da.ugent.be", type="source")

devtools
::install_github("simsem/semTools/semTools")

Leslie Rutkowski

unread,
Apr 19, 2018, 2:49:14 PM4/19/18
to lavaan
Hi Terrence and David,

I'm also running into similar problems. I am using cfa.mi with and without estimator = "MLR". Without, the results are fine. With, I run into trouble.

lavaan::anova(out.mi, test = "D2", indices = TRUE) produces the error 

Error in baseOut[["chisq.shift.parameters"]] : subscript out of bounds

And the summary function on the out.mi object gives:

Error in if (attr(x, "information") %in% c("expected", "first.order") ||  : 
  missing value where TRUE/FALSE needed

To replicate, I am including my full script below:

set.seed(20)
# Simulating 4 correlated variables
Sigma <- matrix(c(1, .40, .40, .45, .40, 1, .35, .50, .40, .35, 1, .40, .45, .50, .40, 1), 4, 4)
cfadat
<- as.data.frame(mvrnorm(100, rep(0, 4), Sigma))
names
<- colnames(cfadat) <- c("v1", "v2", "v3", "v4")
round
(cor(cfadat), 3)


# MCAR - some are missing v1 and v2, some are missing v3 and v4
# Some are missing all
#set.seed(20)
prop
.m <- .20
mcar1
<- runif(100, min = 0, max = 1)
mcar2
<- runif(100, min = 0, max = 1)
cfadat$v1
.mcar <- ifelse(mcar1 < prop.m, NA, cfadat$v1)
cfadat$v2
.mcar <- ifelse(mcar1 < prop.m, NA, cfadat$v2)
cfadat$v3
.mcar <- ifelse(mcar2 < prop.m, NA, cfadat$v3)
cfadat$v4
.mcar <- ifelse(mcar2 < prop.m, NA, cfadat$v4)
head
(round(cfadat, 4), 20)

cfadat.miss <- cfadat[, 5:8]
head(cfadat.miss)

# CFA with multiple imputations-------
m1 <- 'f1 =~ v1.mcar + v2.mcar + v3.mcar + v4.mcar'

# Using semTools
out.mi1 <- cfa.mi(m1, data = cfadat.miss, m = 10, estimator = "MLR", seed = 20)
out.mi2 <- cfa.mi(m1, data = cfadat.miss, m = 10, seed = 20)

# To get fit measures
lavaan::anova(out.mi1, test = "D2", indices = TRUE)
lavaan::anova(out.mi2, test = "D2", indices = TRUE)


summary(out.mi1)
summary(out.mi2)

Thanks,
Leslie

Leslie Rutkowski

unread,
Apr 19, 2018, 2:53:04 PM4/19/18
to lavaan
Also, it bears saying that I ran the installation script below before running the analysis. 

install.packages("lavaan", repos="http://www.da.ugent.be", type="source")

devtools
::install_github("simsem/semTools/semTools")

Terrence Jorgensen

unread,
Apr 20, 2018, 10:04:39 AM4/20/18
to lavaan
 
lavaan::anova(out.mi, test = "D2", indices = TRUE)

Not that this is the cause of the problem, but why are you putting lavaan:: in front of anova()out.mi is an object of class lavaan.mi, created in the semTools package, not lavaan.  So if anything, you could put semTools::anova(), but that should not be necessary. The whole point of generic functions like anova() is that they first check what class the first argument is, then selects the appropriate method provided in the corresponding package.

produces the error 
Error in baseOut[["chisq.shift.parameters"]] : subscript out of bounds

This was resolved.  I don't know when you installed these last, but try again.  I ran your script using lavaan 0.6-1.1213 and semTools 0.4-15.920, without any errors.

Leslie Rutkowski

unread,
Apr 23, 2018, 10:57:15 AM4/23/18
to lavaan
Hi Terrence,

Thanks for your help with this. It was a bit of a bear to update semTools on my machine. I kept getting an error of the sort:
ERROR: failed to lock directory 'C:/Users/lrutkows/Documents/R/R-3.4.3/library' for modifying
Try removing 'C:/Users/lrutkows/Documents/R/R-3.4.3/library/00LOCK-semTools'
In R CMD INSTALL
Installation failed: Command failed (3)

This fixed it: 
unlink("C:/Users/lrutkows/Documents/R/R-3.4.3/library/00LOCK-semTools", recursive = TRUE)

And now things are running along fine with MLR. 

Best,
Leslie
Reply all
Reply to author
Forward
0 new messages