R2MLwiN of mixture models

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/
Post Reply
MannyGomez
Posts: 17
Joined: Mon Feb 17, 2014 4:55 pm

R2MLwiN of mixture models

Post by MannyGomez » Mon Feb 17, 2014 5:45 pm

Dear all,
I have two quick questions:
1. As I understood from reading page 36 from the paper 'R2MLwiN: A Package to Run MLwiN From Within R' to be published on the J. Stat. Soft., MLwiN (and R2MLwiN) can only fit multivariate mixture models which combine Gaussian and binomial responses. Notheless, I noticed some advancements with this type of models in Stat-JR (I was particularly interested in implementing a mixture of Gaussian, binomial and ordered categorical outcomes). Are you planning to incorporate these in MLwiN at some point or these new features will only be available through Stat-JR?

2. I frequently have to run statistical simulations, and using R2MLwiN provides me the flexibility to embed all analyses within R platform. As I understand from pages 36-41 of the Advanced User Guide to Stat-JR the interoperability between Stat-JR and R may be limited to a few packages. If I had to perform a simulation study requiring the use of Stat-JR, could you please advise on alternative strategies to implement that (is it possible to interact with R to conduct these)?

Many thanks,
Manny

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

Re: R2MLwiN of mixture models

Post by ChrisCharlton » Mon Feb 17, 2014 6:06 pm

  1. We currently have no plans to extend MLwiN with the mixed-response model functionality implemented in Stat-JR.
  2. Are you referring to calling R from Stat-JR or Stat-JR from R? If it is the former you can create a basic template to call any R function using the "R_script" package. For an example of this see the PlotsViaR template. If you would like Stat-JR to be able extract the results you would need to write a package file (see R_glm as an example) that knows how to interpret the output of your R commands. If it is the latter then we currently have some very rudimentary functionality to allow calling Stat-JR from a command line, which could be wrapped in R. This is currently most useful if you're running models with MCMC and want to extract the parameter chains. We hope to extend this functionality in the future.

MannyGomez
Posts: 17
Joined: Mon Feb 17, 2014 4:55 pm

Re: R2MLwiN of mixture models

Post by MannyGomez » Tue Feb 18, 2014 3:33 pm

Hi Chris,
Thanks! Yes, I was alluding to the latter. Do you have any examples about how to call Stat-JR from a command line in R? I was particularly interested in fitting a multilevel mixture model (a mixture of continuous, binary and ordinal outcomes) using either MCMC or RIGLS.
Cheers,
Manuel

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

Re: R2MLwiN of mixture models

Post by ChrisCharlton » Tue Feb 18, 2014 4:41 pm

As we have not yet done much development on calling Stat-JR from other packages the interface is likely to change, however here is an example of using the functionality in the current release to run the regression template and retrieve the MCMC chains:

Code: Select all

# Load required packages
require(coda)
require(foreign)

# Load dataset of interest
mydata <- read.dta("http://www.bristol.ac.uk/cmm/media/runmlwin/tutorial.dta")

# Select template
template <- "Regression1"

# Set template inputs
inputs <- "'y': 'normexam', 'x': 'cons,standlrt'"

# Set estimation options
estoptions <- "'burnin': '500', 'defaultsv': 'Yes', 'thinning': '1', 'nchains': '3', 'defaultalg': 'Yes', 'iterations': '2000', 'outdata': 'out', 'seed': '1', 'makepred': 'No'"

# The following is generic code to run the template with the selected inputs
dtafile <- tempfile(fileext=".dta")
chainfile <- tempfile(fileext=".dta")
resfile <- tempfile(fileext=".txt")
write.dta(mydata, dtafile)
command <- paste0("cd /d C:/StatJR/src/apps/scripting && runStatJR --template ", template, " --invars \"", inputs, "\" --estoptions \"", estoptions, "\" --datafile \"", dtafile, "\" --chainfile \"", chainfile, "\" --resultfile \"", resfile, "\"")
shell(command)

# Read and display the model/parameter estimates
cat(readChar(resfile, file.info(resfile)$size))

# Read and store the MCMC chains
chaindata <- read.dta(chainfile)
chains <- mcmc.list(by(chaindata, chaindata$chain, function(x) mcmc(x)))
summary(chains)
This assumes that Stat-JR is installed in the default location. Obviously this could be wrapped into a more user-friendly function if several models needed to be run. To generate the input strings required for this command it is probably easiest to run an example model through the point-and-click interface and then copy the necessary parts from the displayed string that starts with Command:.

MannyGomez
Posts: 17
Joined: Mon Feb 17, 2014 4:55 pm

Re: R2MLwiN of mixture models

Post by MannyGomez » Wed Feb 26, 2014 9:42 pm

Thanks Chris, I will have a go at it.
On a different issue, but within the same matter.
When running a mixture multilevel model (continuous and binary) using R2MLwiN, is it possible to store the residuals?

I have included in the R code the option 'resi.store=TRUE', but receive the following error:

error while obeying batch file C:/Users/mgomes/AppData/Local/Temp/RtmpoLn2CB/macrofile_5283782864.txt at line number 88:
RESI

wrong number of output columns

error while obeying batch file C:/Users/mgomes/AppData/Local/Temp/RtmpoLn2CB/macrofile_5283782864.txt at line number 88:
RESI

ÐÉ.
Execution completed
Error in read.dta(resifile) :
unable to open file: 'No such file or directory


Any idea what might have gone wrong?
Much appreciated,
Manuel

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

Re: R2MLwiN of mixture models

Post by ChrisCharlton » Wed Feb 26, 2014 10:22 pm

This should work, but it's possible that the script isn't being generated correctly. We'll look into this and let you know what we find.

MannyGomez
Posts: 17
Joined: Mon Feb 17, 2014 4:55 pm

Re: R2MLwiN of mixture models

Post by MannyGomez » Mon Mar 03, 2014 8:18 pm

Thanks Chris, that would be great.
I have another question on running mixture models using R2MLwiN. I have modelled 1continuous and 1 binary, and multiple continuous and 1 binary and 'runMLwiN' worked fine.
However, when I tried to model 1 normal with multiple binary it gave me the following error:

Error in if (D[[i + 1]] == "Poisson" || D[[i + 1]] == "Negbinom") { :
missing value where TRUE/FALSE needed


I'm using the following code ('walk' is continuous, 'death' and 'NYHA_bin' are binary):

model.mix<- runMLwiN(Formula = "c(walk, probit(death,denomb1), probit(NYHA_bin,denomb2)) ~ (0s|cons+CRT+SEX+CRTMALE) + (1s|cons.walk1) + (2s|cons)",
D=c("Mixed","Normal","Binomial","Binomial"),
levID = c("STUDYID", "USUBJID"), indata = data,
estoptions=list(x64=TRUE, Meth=2), MLwiNPath = 'C:/Program Files (x86)/MLwiN v2.29')


Am I doing anything silly?
Thanks,
Manuel

richardparker
Posts: 57
Joined: Fri Oct 23, 2009 1:49 pm

Re: R2MLwiN of mixture models

Post by richardparker » Thu Mar 06, 2014 4:49 pm

Hi Manuel,

Thanks for alerting us to this; on further investigation it appears that a bug in the R2MLwiN package is responsible for this problem, so we'll look into possible solutions and get back to you.

Best wishes,

Richard

richardparker
Posts: 57
Joined: Fri Oct 23, 2009 1:49 pm

Re: R2MLwiN of mixture models

Post by richardparker » Fri Mar 28, 2014 1:48 pm

Hi - the latest version of R2MLwiN (0.2.0), released today, has bug fixes for this.

Thanks again for reporting the issue.

Best wishes,

Richard

Post Reply