R2MLwiN and multilevel multiple imputation

Welcome to the forum for R2MLwiN users. Feel free to post your question about R2MLwiN here. The Centre for Multilevel Modelling take no responsibility for the accuracy of these posts, we are unable to monitor them closely. Do go ahead and post your question and thank you in advance if you find the time to post any answers!

Go to R2MLwiN: Running MLwiN from within R >> http://www.bris.ac.uk/cmm/software/r2mlwin/
tjsduq64
Posts: 31
Joined: Mon Jul 15, 2019 10:04 pm

R2MLwiN and multilevel multiple imputation

Post by tjsduq64 »

Hi,

I have a simple question, but didn't want to dive in before I know the answer to... what's the ideal approach to conduct multiple imputations of multilevel data in R using R2MLwiN? I know there are options like REALCOM and Stat-JR but I do not have access to these software. Also, it would be much simpler if I can do this in R.

Can R2MLwiN work with a package mice? I will run a two-level random slope model, and my parameters of interest are random effect parameters. Specifically, I am interested in the level 2 variance estimate as a function of a predictor. If not, are there other packages that work well with R2MLwiN?

If not, do you suggest using lme4, instead of R2MLwiN?

Thanks,

Sun
ChrisCharlton
Posts: 1348
Joined: Mon Oct 19, 2009 10:34 am

Re: R2MLwiN and multilevel multiple imputation

Post by ChrisCharlton »

The Realcom-Impute software is free to download, however converting the data to the right formats is easiest if you have MLwiN or Stata.

R2MLwiN has worked in the past with mice (see https://www.cmm.bristol.ac.uk/forum/vie ... php?t=2404), however I believe that recent versions rely on the broom package to extract the parameters, which the currently released version of R2MLwiN does not link to. I do have a local development version of R2MLwiN which does output broom-compatible objects.

In terms of using R2MLwiN to impute the missing data see chapter 18 of the MCMC guide, along with the R replication example on http://bristol.ac.uk/cmm/software/r2mlwin/examples/.
tjsduq64
Posts: 31
Joined: Mon Jul 15, 2019 10:04 pm

Re: R2MLwiN and multilevel multiple imputation

Post by tjsduq64 »

Thanks Chris.

It seems like, if I want to use R2MLwiN, my options are running a RIGLS model with Realcom or with mice. Does MCMC work with Realcom or mice? It seems like Realcom won't work, and I am not sure if mice takes mlwinMCMC object. I may need to use MCMC since my outcome is a binary variable.

Sun
ChrisCharlton
Posts: 1348
Joined: Mon Oct 19, 2009 10:34 am

Re: R2MLwiN and multilevel multiple imputation

Post by ChrisCharlton »

Could you please try running the following example to check that this works for you? If not then I can provide a development version package. Of course you could substitute your own preferred packages for the imputation and/or model of interest analysis.

Code: Select all

library(R2MLwiN)
library(mitools)
library(mice)
data(hungary1)

# Impute responses at iterations 0, 1000, 2000, 3000, 4000 and 5000
(impmodel <- runMLwiN(c(es_core, biol_core, biol_r3, biol_r4, phys_core, phys_r2) ~ 1 + female + (1 | school) + (1 | student),
 D = "Multivariate Normal", estoptions = list(EstM = 1, mcmcMeth = list(dami = c(0, 1000, 2000, 3000, 4000, 
  5000))), data = hungary1))

# Create complete datasets as imputationLIst
mi <- imputationList(impmodel@imputations)

# Fit a model to each imputated dataset
moimodels <- with(mi, runMLwiN(Formula = es_core ~ 1 + biol_core + biol_r3 + biol_r4 + phys_core + phys_r2 + femaleFemale + (1 | school) + (1 | student) ))

# Summarise model results 
summary(pool(moimodels))
tjsduq64
Posts: 31
Joined: Mon Jul 15, 2019 10:04 pm

Re: R2MLwiN and multilevel multiple imputation

Post by tjsduq64 »

1. Ah, I see that MLwiN uses multivariate model to impute missing data, very cool. The manual says imputing an ordered categorical variable is under research. What about binary variables and multinomial (unordered) variables? If these work, I will definitely use this option since using mice seems a bit of work of writing broom functions.

2. In the meantime, I tried using mice. Indeed, as you said, using mice gave me an error

Code: Select all

No glance method for objects of class mlwinfitIGLS
But, after some codes, I really do not know what happened, mice started working without giving this error. During this time, I tried to load an older version of mice but failed so I don't think this is the reason it's working now. I confirmed by sessionInfo() and redownloading the new mice version. It still works. Anyhow, my question now is this. Have you ever seen a bug where a model runs fine when I just run it on one dataset but fails when I run it through a list of datasets using lapply? The model runs fine if I use my original dataset for a complete case analysis but fails when looping through multiply imputed datasets. This is only happening to the complex model (complex level 2 variation model); the simpler ones (without complex level 2 variation) work fine. I received this error after a while (usually the model doesn't take this long). I get if a complex model fails but what is confusing is it only fails when I loop through multiple datasets.

Code: Select all

v gone -ve definite for block 0 iteration aborted(fixed pass)
To provide a context:
I am running a complex level 2 variation model. I applied weight and allowed negative variances at level 1 and 2. Sorry, I do not have reproducible codes because I think the problem is specific to the dataset and the choice of a model.

Code: Select all

# adjusted complex level 2 model
sample <- mywvs6
options <- list(debugmode = F, Meth = 0, resi.store = T, residoptions='standardised',
                weighting=list(weightvar=c(NA,"weight"), standardised=T), reset=c(2,2))
model <-  life_satisfaction ~ 1 + sex + age + age_2 + income + education + socialclass +
  maritalstatus + gdp + gini +
  (1 + gini| country) + (1| id)
fullRS_ls_gini <- runMLwiN(Formula = model, data = sample, estoption = options) # complete case analysis
summary(fullRS_ls_gini)

mi_fullRS_ls_gini <- lapply(mi_complete, function(x){ # analysis accounting for missing data (this is where the model does not run)
  runMLwiN(Formula = model, data = x, estoption = options)
})
combined_fit <- pool(mi_fullRS_ls_gini)
summary(combined_fit)
tjsduq64
Posts: 31
Joined: Mon Jul 15, 2019 10:04 pm

Re: R2MLwiN and multilevel multiple imputation

Post by tjsduq64 »

Not sure if this helps, if I use debugmode=TRUE and see what is happening in MLwiN, I find that MLwiN turns "not responding" before I get this error.

Code: Select all

v gone -ve definite for block 0 iteration aborted(fixed pass)
ChrisCharlton
Posts: 1348
Joined: Mon Oct 19, 2009 10:34 am

Re: R2MLwiN and multilevel multiple imputation

Post by ChrisCharlton »

1) MLwiN should handle mixtures of binary (probit) and normal responses when running in MCMC. The research into categorical variables and responses at higher levels was implemented in Realcom and Stat-JR, although if you have MLwiN you should at least in theory also have access to these too.

2) You might have a bit more luck using the attached development version of R2MLwiN, as this contains initial support for some of the broom functions. I would suggest examining your imputed datasets to check that they seem plausible, as well as running the model on a few of them individually. It is likely that some aspect of the data is causing the problem as this should be the only thing that changes between the complete and imputed versions.
Attachments
R2MLwiN_0.8-7.tar.gz
(340.31 KiB) Downloaded 451 times
tjsduq64
Posts: 31
Joined: Mon Jul 15, 2019 10:04 pm

Re: R2MLwiN and multilevel multiple imputation

Post by tjsduq64 »

EDIT: for the multiply imputed datasets, I confirmed that there is nothing wrong with each dataset. I ran the model in each dataset and it works fine. I think running it through a loop is causing an issue..? This is related to my new post, it's the same error, one that opens MLwiN and shows nothing for a while before it gives me an error, ve gone negative definite, except in the new post, it is a different situation.

Hi, this could be rather elementary question, but I am having trouble installing the file manually... could you please give me a quick guide?

I've tried,

Code: Select all

install.packages("C:/Users/My Name/Desktop/Research/R2MLwiN_0.8-7.tar.gz", repos = NULL, type="source")
Installing package into ‘C:/Users/My Name/Documents/R/win-library/3.6’
(as ‘lib’ is unspecified)
* installing *source* package 'R2MLwiN' ...
** using staged installation
** R
** data
** demo
** inst
** byte-compile and prepare package for lazy loading
Fatal error: cannot open file 'C:\Users\My': No such file or directory

ERROR: lazy loading failed for package 'R2MLwiN'
* removing 'C:/Users/My Name/Documents/R/win-library/3.6/R2MLwiN'
* restoring previous 'C:/Users/My Name/Documents/R/win-library/3.6/R2MLwiN'
Warning in install.packages :
  installation of package ‘C:/Users/My Name/Desktop/Research/R2MLwiN_0.8-7.tar.gz’ had non-zero exit status
In the error where it says "Fatal error", it seems like the issue is due to having a space in one of my document because it ends with "My" and doesn't register "My Name". But I cannot figure out how to rename my user folder name.

Do you have any idea? Sorry asking about a non-mlwin issue.
ChrisCharlton
Posts: 1348
Joined: Mon Oct 19, 2009 10:34 am

Re: R2MLwiN and multilevel multiple imputation

Post by ChrisCharlton »

If you start R as an administrator and then attempt to install the package then it should put it into the system library path. Alternatively you can use the .libPaths function (https://stat.ethz.ch/R-manual/R-patched ... Paths.html) to manually set where R stores and searches for installed libraries.
tjsduq64
Posts: 31
Joined: Mon Jul 15, 2019 10:04 pm

Re: R2MLwiN and multilevel multiple imputation

Post by tjsduq64 »

I changed the paths for the R2MLwiN file and the package library so that it doesn't have to go through "My Name" folder that includes a space. But as you can see in the Fatal error below, it still tries to open file "C:\Users\My"...

Code: Select all

> install.packages("C:/Program Files/R/R2MLwiN_0.8-7.tar.gz", repos = NULL, type = "source", lib="C:/Program Files/R/R-3.6.2/library")
* installing *source* package 'R2MLwiN' ...
** using staged installation
** R
** data
** demo
** inst
** byte-compile and prepare package for lazy loading
Fatal error: cannot open file 'C:\Users\My': No such file or directory

ERROR: lazy loading failed for package 'R2MLwiN'
* removing 'C:/Program Files/R/R-3.6.2/library/R2MLwiN'
Warning in install.packages :
  installation of package ‘C:/PROGRA~1/R/R2MLwiN_0.8-7.tar.gz’ had non-zero exit status
Post Reply