runMI issue

261 views
Skip to first unread message

David Kaplan

unread,
Mar 27, 2019, 6:07:01 PM3/27/19
to lavaan
Greetings all,

I am running a multiple imputation in mice and then using the imputed data sets in runMI.  The mice chunk works fine, but when I run the analysis through runMI I get the following error (in Rmarkdown).  Note that mice.imp contains 20 imputed data sets.  

```{r,echo=TRUE}
fitmi <- runMI(bemmiss.model,estimator="ml", 
              data=mice.imp,
              fun="sem")
summary(fitmi)
anova(fitmi,indices=TRUE)

```
Error in FUN(X[[i]], ...) : subscript out of bounds

Mauricio Garnier-Villarreal

unread,
Mar 28, 2019, 3:19:01 AM3/28/19
to lavaan
David

could you shared the full code? I ran the example from the semtools helps page and works, see the example that works for me, and the sessioInfo.

Is mice.imp a list with the 20 complete data sets, or is it the mice object from imputation? In my example I save a list with the complete data sets, as this is the required data set by runMI

## Not run:
## impose missing data for example
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
'

HS.mice <- mice(HSMiss, m = 20)

imps_list <- list()
for(i in 1:20){
  imps_list[[i]] <- complete(HS.mice, i)
}

out2 <- runMI(HS.model, data = imps_list, fun="cfa",estimator="ml")
summary(out2)
anova(out2,indices=TRUE)
fitMeasures(out2)


> sessionInfo()
R version 3.5.3 (2019-03-11)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.2 LTS

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.2.20.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8  
 [6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C           
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C      

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

other attached packages:
[1] mice_3.4.0         lattice_0.20-38    semTools_0.5-1.906 lavaan_0.6-3     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.1       pillar_1.3.1     compiler_3.5.3   nloptr_1.2.1     tools_3.5.3      rpart_4.1-13     boot_1.3-20      lme4_1.1-21    
 [9] tibble_2.1.1     nlme_3.1-137     pkgconfig_2.0.2  rlang_0.3.2      Matrix_1.2-17    rstudioapi_0.10  parallel_3.5.3   pbivnorm_0.6.0 
[17] dplyr_0.8.0.1    generics_0.0.2   nnet_7.3-12      stats4_3.5.3     grid_3.5.3       tidyselect_0.2.5 glue_1.3.1       R6_2.4.0       
[25] survival_2.43-3  mitml_0.3-7      minqa_1.2.4      purrr_0.3.2      tidyr_0.8.3      magrittr_1.5     backports_1.1.3  MASS_7.3-51.1  
[33] splines_3.5.3    assertthat_0.2.1 mnormt_1.5-5     broom_0.5.1      crayon_1.3.4     pan_1.6          jomo_2.6-7 

David Kaplan

unread,
Mar 28, 2019, 4:08:17 PM3/28/19
to lavaan
Hi Mauricio,

Here is the full input and output


library(mice)
library(lavaan)
library(semTools)

bemmiss <- read.csv("~/desktop/bem.csv",header=T)




bemmiss$assert[bemmiss$assert == 1] <- NA
bemmiss$softspok[bemmiss$softspok == 7] <- NA
bemmiss$stand[bemmiss$stand >=6] <- NA

bemmiss.model <- ' masculine =~ assert+forceful+dominant+stand
feminine =~ softspok+warm+truthful+tender+gentle '

bemmiss_imp <- mice(bemmiss, m = 20, method="pmm")
mice.imp <- NULL
for(i in 1:20) { mice.imp[[i]] <- complete(bemmiss_imp, action=i, inc=FALSE)  
}

# Run lavaan with previously imputed data using runMI  

fitmi <- runMI(bemmiss.model,estimator="ml", 
               data=mice.imp,
               fun="sem")
summary(fitmi)
--------------------------------------------------
> library(mice)
> library(lavaan)
> library(semTools)
> bemmiss <- read.csv("~/desktop/bem.csv",header=T)
> bemmiss$assert[bemmiss$assert == 1] <- NA
> bemmiss$softspok[bemmiss$softspok == 7] <- NA
> bemmiss$stand[bemmiss$stand >=6] <- NA
> bemmiss.model <- ' masculine =~ assert+forceful+dominant+stand
+ feminine =~ softspok+warm+truthful+tender+gentle '
> bemmiss_imp <- mice(bemmiss, m = 20, method="pmm")

 iter imp variable
  1   1  assert  stand  softspok
  1   2  assert  stand  softspok
  1   3  assert  stand  softspok
  1   4  assert  stand  softspok
  1   5  assert  stand  softspok
  1   6  assert  stand  softspok
  1   7  assert  stand  softspok
  1   8  assert  stand  softspok
  1   9  assert  stand  softspok
  1   10  assert  stand  softspok
  1   11  assert  stand  softspok
  1   12  assert  stand  softspok
  1   13  assert  stand  softspok
  1   14  assert  stand  softspok
  1   15  assert  stand  softspok
  1   16  assert  stand  softspok
  1   17  assert  stand  softspok
  1   18  assert  stand  softspok
  1   19  assert  stand  softspok
  1   20  assert  stand  softspok
  2   1  assert  stand  softspok
  2   2  assert  stand  softspok
  2   3  assert  stand  softspok
  2   4  assert  stand  softspok
  2   5  assert  stand  softspok
  2   6  assert  stand  softspok
  2   7  assert  stand  softspok
  2   8  assert  stand  softspok
  2   9  assert  stand  softspok
  2   10  assert  stand  softspok
  2   11  assert  stand  softspok
  2   12  assert  stand  softspok
  2   13  assert  stand  softspok
  2   14  assert  stand  softspok
  2   15  assert  stand  softspok
  2   16  assert  stand  softspok
  2   17  assert  stand  softspok
  2   18  assert  stand  softspok
  2   19  assert  stand  softspok
  2   20  assert  stand  softspok
  3   1  assert  stand  softspok
  3   2  assert  stand  softspok
  3   3  assert  stand  softspok
  3   4  assert  stand  softspok
  3   5  assert  stand  softspok
  3   6  assert  stand  softspok
  3   7  assert  stand  softspok
  3   8  assert  stand  softspok
  3   9  assert  stand  softspok
  3   10  assert  stand  softspok
  3   11  assert  stand  softspok
  3   12  assert  stand  softspok
  3   13  assert  stand  softspok
  3   14  assert  stand  softspok
  3   15  assert  stand  softspok
  3   16  assert  stand  softspok
  3   17  assert  stand  softspok
  3   18  assert  stand  softspok
  3   19  assert  stand  softspok
  3   20  assert  stand  softspok
  4   1  assert  stand  softspok
  4   2  assert  stand  softspok
  4   3  assert  stand  softspok
  4   4  assert  stand  softspok
  4   5  assert  stand  softspok
  4   6  assert  stand  softspok
  4   7  assert  stand  softspok
  4   8  assert  stand  softspok
  4   9  assert  stand  softspok
  4   10  assert  stand  softspok
  4   11  assert  stand  softspok
  4   12  assert  stand  softspok
  4   13  assert  stand  softspok
  4   14  assert  stand  softspok
  4   15  assert  stand  softspok
  4   16  assert  stand  softspok
  4   17  assert  stand  softspok
  4   18  assert  stand  softspok
  4   19  assert  stand  softspok
  4   20  assert  stand  softspok
  5   1  assert  stand  softspok
  5   2  assert  stand  softspok
  5   3  assert  stand  softspok
  5   4  assert  stand  softspok
  5   5  assert  stand  softspok
  5   6  assert  stand  softspok
  5   7  assert  stand  softspok
  5   8  assert  stand  softspok
  5   9  assert  stand  softspok
  5   10  assert  stand  softspok
  5   11  assert  stand  softspok
  5   12  assert  stand  softspok
  5   13  assert  stand  softspok
  5   14  assert  stand  softspok
  5   15  assert  stand  softspok
  5   16  assert  stand  softspok
  5   17  assert  stand  softspok
  5   18  assert  stand  softspok
  5   19  assert  stand  softspok
  5   20  assert  stand  softspok
> mice.imp <- NULL
> for(i in 1:20) { mice.imp[[i]] <- complete(bemmiss_imp, action=i, inc=FALSE)  
+ }
> # Run lavaan with previously imputed data using runMI  
> fitmi <- runMI(bemmiss.model,estimator="ml", 
+                data=mice.imp,
+                fun="sem")
Error in FUN(X[[i]], ...) : subscript out of bounds
> summary(fitmi)
lavaan 0.6-3 did not run (perhaps do.fit = FALSE)?
** WARNING ** Estimates below are simply the starting values

  Optimization method                           NLMINB
  Number of free parameters                          0

  Number of observations                          7361

  Estimator                                         ML
  Model Fit Test Statistic                     756.510
  Degrees of freedom                                10
  P-value (Chi-square)                           0.000

Parameter Estimates:


Regressions:
                   Estimate
  sciach ~                 
    scigrade10        0.884
  scigrade10 ~             
    scigrade6         0.850
    ses               0.174
    certsci          -0.058
    understand        0.171
    challenge        -0.026
  challenge ~              
    understand        0.318
  understand ~             
    certsci          -0.030

Variances:
                   Estimate
   .sciach           22.264
   .scigrade10        2.671
   .challenge         1.250
   .understand        1.858

Terrence Jorgensen

unread,
Mar 29, 2019, 7:12:22 AM3/29/19
to lavaan
Hard to know which bug caused this error, but it is resolved now.

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


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

Reply all
Reply to author
Forward
0 new messages