cfa.mi function with multiply imputed data set

218 views
Skip to first unread message

fotin...@gmail.com

unread,
Jul 27, 2018, 10:18:32 AM7/27/18
to lavaan
Hi,

I am trying to run a model within the cfa.mi function with an already multiply imputed dataset:

> Model1a <- 'ERa =~ ER1 + ER2 + ER3 + ER4 + ER5 BRa =~ BR1 + BR2 + BR3 + BR5 + BR6 PAa =~ PA1 + PA2 + PAMV1 ERb =~ ER6 + ER7 + ER8 BRb =~ BR7 + BR8 + BR9 + BR10 PAb =~ PAMV2 + PAC + PAIB + PAJ + PATB + PAS + PAO + PA3'

> fitallM1a <- cfa.mi(Model1a, data = mice_imputesM1, estimator = "WLSMV")

but I got this error

> fitallM1a <- cfa.mi(Model1a, data = mice_imputesM1, estimator = "WLSMV")
Error in ~BR7 + BR8 + BR9 + BR10PAb = ~PAMV2 + PAC + PAIB + PAJ + PATB +  : 
  object 'BR7' not found


I then looked at this to try an understand the problem

> str(Model1)
 chr "ERa =~ ER1 + ER2 + ER3 + ER4 + ER5 BRa =~ BR1 + BR2 + BR3 + BR5 + BR6 PAa =~ PA1 + PA2 + PAMV1 ERb =~ ER6 + ER7"| __truncated__

So to try an understand the issue further I tried splitting the model in parts just looking at the first latent variable

>Model1ERa <- 'ERa =~ ER1 + ER2 + ER3 + ER4 + ER5'
> fitallM1ERa <- cfa.mi(Model1ERa, data = mice_imputesM1, estimator = "WLSMV")
 And I get this error
In lav_data_full(data = data, group = group, cluster = cluster,  :
  lavaan WARNING: unordered factor(s) with more than 2 levels detected in data: ER1 ER2 ER3 ER4 ER5

I tried this

I also realised at the time that some factors were not ordered when they should be so I tried this

> fitallM1ERa <- cfa.mi(Model1, data = mice_imputesM1, estimator = "WLSMV", ordered=c("ER1","ER2","ER3","ER4","ER5"))

and I got this error
Error in DATA[, ord.idx, drop = FALSE] : incorrect number of dimensions

I then changed them individually and then checked for unordered factors but they have been ordered (see below)

> sapply(mice_imputesM1, class)

and it returns this

$data
[1] "data.frame"

$imp
[1] "list"

$m
[1] "numeric"

$where
[1] "matrix"

$blocks
[1] "list"

$call
[1] "call"

$nmis
[1] "integer"

$method
[1] "character"

$predictorMatrix
[1] "matrix"

$visitSequence
[1] "character"

$formulas
[1] "list"

$post
[1] "character"

$blots
[1] "list"

$seed
[1] "logical"

$iteration
[1] "numeric"

$lastSeedValue
[1] "integer"

$chainMean
[1] "array"

$chainVar
[1] "array"

$loggedEvents
[1] "data.frame"

$version
[1] "package_version" "numeric_version"

$date
[1] "Date"

$ER1
[1] "ordered" "factor" 

$ER2
[1] "ordered" "factor" 

$ER3
[1] "ordered" "factor" 

$ER4
[1] "ordered" "factor" 

$ER5
[1] "ordered" "factor" 

$BR1
[1] "ordered" "factor" 

$BR2
[1] "ordered" "factor" 

$BR3
[1] "ordered" "factor" 

$BR4
[1] "ordered" "factor" 

$BR5
[1] "ordered" "factor" 

$BR6
[1] "ordered" "factor" 

$BR7
[1] "ordered" "factor" 

$BR8
[1] "ordered" "factor" 

$BR9
[1] "ordered" "factor" 

$BR10
[1] "ordered" "factor" 

$ER6
[1] "ordered" "factor" 

$ER7
[1] "ordered" "factor" 

$ER8
[1] "ordered" "factor" 

$PA1
[1] "numeric"

$PA2
[1] "numeric"

$PA3
[1] "numeric"


The data being imputed is this:

>mice_imputesM1 = mice(MCS_Latent_Variables_Model_1_SR_PA_HC_MD, m=5, maxit = 40)

I tried running everything again but it is still recognising them as unordered in my second model OR still not recognising BR7 in my first model.  I have tried dropping BR7 but then it moves onto the next factor in the model that it doesn't recognise (i.e. BR8).

Thanks so much for any help!!

Mauricio Garnier-Villarreal

unread,
Jul 29, 2018, 7:17:44 PM7/29/18
to lavaan
if you change the type of a columns, mice would use the default method for it, for example

dat$X1 <- as.ordered(dat$X1)

would change thw type of X1 and mice would use the polr for its imputation.

You could also tell mice that all of them are continuous, and mice would use the nonoparametric imputation method Predictive Mean Matching, which has shown to behvae well even with categorical data

Now, a separate issue is how to give the imputations to cf.mi. The .mi functions take a list of imputed data sets as data, instead of the whole mice object, so you need to set the imputations in a list like this, and give the new list object to the .mi functions

mi_list <- list()
for(i in 1:5){ ## using 5 according to your code, it would be the number of imputations
mi_list[[i]] <- cimplete(mice_object, i)
}

this loop would set each complete imputed data set in a separate object of the list

Fotini Vasilopoulos

unread,
Jul 31, 2018, 8:09:16 AM7/31/18
to lav...@googlegroups.com
I have changed it to be ordered and also created a list and tried to run cfa.mi the following ways with the following errors:

> fitallM1a <- cfa.mi(Model1a, data = mi_list, estimator = "WLSMV")
Error in (function (model = NULL, dataList = NULL, dataFunction = NULL,  : 
  lavaan ERROR: (generated) data is not a data.frame (or a matrix)

> fitallM1a <- cfa.mi(Model1a, datalist = mi_list, estimator = "WLSMV")
Error in runMI(model = model, data = data, fun = "cfa", ..., m = m, miArgs = miArgs,  : 
  argument "data" is missing, with no default

> fitallM1a <- cfa.mi(Model1a, data = Imputed_dataM1, datalist = mi_list, estimator = "WLSMV")
Error in runMI(model = model, data = data, fun = "cfa", ..., m = m, miArgs = miArgs,  : 
  argument "m" is missing, with no default

> fitallM1a <- cfa.mi(Model1a, data = Imputed_dataM1, list(mi_list), estimator = "WLSMV")
Error in runMI(model = model, data = data, fun = "cfa", ..., m = m, miArgs = miArgs,  : 
  argument "m" is missing, with no default

I thought it should be a list not data frame (error first in the list) and I thought that if it is a list of imputed data sets that m is ignored.

Can anyone help?

Fotini Vasilopoulos
Yoga Teacher (RYT 200 RCYT Yoga Nidra )

--
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,
Jul 31, 2018, 8:40:34 AM7/31/18
to lavaan
> fitallM1a <- cfa.mi(Model1a, data = mi_list, estimator = "WLSMV")
Error in (function (model = NULL, dataList = NULL, dataFunction = NULL,  : 
  lavaan ERROR: (generated) data is not a data.frame (or a matrix)

Does mi_list contain data frames?  Check:

sapply(mi_list, class) # should all be "data.frame"

There was a typo in the suggestion above:  cimplete() should be complete().  So mi_list might not contain anything at all.

> fitallM1a <- cfa.mi(Model1a, datalist = mi_list, estimator = "WLSMV")
Error in runMI(model = model, data = data, fun = "cfa", ..., m = m, miArgs = miArgs,  : 
  argument "data" is missing, with no default

The message tells you the problem: you used the wrong argument name (highlighted above).

> fitallM1a <- cfa.mi(Model1a, data = Imputed_dataM1, datalist = mi_list, estimator = "WLSMV")
Error in runMI(model = model, data = data, fun = "cfa", ..., m = m, miArgs = miArgs,  : 
  argument "m" is missing, with no default

What kind of object is Imputed_dataM1?  As described on the ?runMI help page, it should be a list of imputed data sets.  Again, "datalist=" is not an argument for this function.

> fitallM1a <- cfa.mi(Model1a, data = Imputed_dataM1, list(mi_list), estimator = "WLSMV")

The highlighted part makes no sense.

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

Reply all
Reply to author
Forward
0 new messages