R2MLwiN

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/
mekareXX
Posts: 6
Joined: Mon Jun 30, 2014 1:51 pm

R2MLwiN

Post by mekareXX »

Hi everyone,
I try to run R2MLwin (setting up a 3-level hierarchical multivariate normal model), but keep getting an error message, which I cannot figure out. A testset of the data is attached.
My R coding follows the MLwiN MCMC Manual Ch 18 and looks like this:

install.packages("foreign")
install.packages("car")
library(car)
install.packages("R2MLwiN")
library(R2MLwiN)
install.packages("plyr")
library(plyr)

# 1.Set the path for MLwiN
mlwin = "C:/Program Files (x86)/MLwiN v2.28/i386"
cor(data[,c("TnyADD_100sowD","TnyADD_100slswD","TnyADD_100wD")])
formula="c(TnyADD_100sowD,TnyADD_100slswD,TnyADD_100wD)~(0|cons)+(1|cons)+(2|cons)+(3|cons)"
# 2.Define 3 level hierarchical structure
levID = c("POSTNR_TXT","ID","obsid")

# IGLS -> Max likelihood ->
# estimates can be used as priors in the following bayesian
estoptions=list(EstM=0)
(mymodel=runMLwiN(formula,levID,D='Multivariate Normal',data,estoptions,MLwiNPath=mlwin))
# MCMC -> Bayesian
estoptions=list(EstM=1)
(mymodel=runMLwiN(formula,levID,D='Multivariate Normal',data,estoptions,MLwiNPath=mlwin))

When I run the "mymodel" either using IGLS or MCMC, I keep getting the following error:

Error in read.dta(IGLSfile) :
unable to open file: 'No such file or directory'

I hope someone can help me to proceed!
Best wishes,
Mette
Attachments
testdata.pdf
(41.3 KiB) Downloaded 442 times
ChrisCharlton
Posts: 1351
Joined: Mon Oct 19, 2009 10:34 am

Re: R2MLwiN

Post by ChrisCharlton »

One thing that I notice is that you refer to the variables "cons" and "ID" in your model specification, but these do not appear in your example data. Could you confirm that these do appear in your real data?
mekareXX
Posts: 6
Joined: Mon Jun 30, 2014 1:51 pm

Re: R2MLwiN

Post by mekareXX »

Yes, they are there in the real dataset. Sorry, I forgot the "cons" (=1 for all observations) and ID is at the second page in the attachment.
ChrisCharlton
Posts: 1351
Joined: Mon Oct 19, 2009 10:34 am

Re: R2MLwiN

Post by ChrisCharlton »

Another couple of potential issues are that your data are not sorted to match the model hierarchy (you need to sort by obsid within ID within POSTNR_TXT) and at least in your example data there appears to be only one ID unit within each POSTNR_TXT.
mekareXX
Posts: 6
Joined: Mon Jun 30, 2014 1:51 pm

Re: R2MLwiN

Post by mekareXX »

Thank you, I was not aware of that. I sorted the data now (postnr_txt-id-obsid), but I still get the same error message. I do not know if it makes more sense if I add the whole R output:

Some input variables held data in more precision than MLwiN supports, these have been rounded

error while obeying batch file C:/Users/memun/AppData/Local/Temp/RtmpU134Od/macrofile_1f00664e58f9.txt at line number 27:
SETV 4 'cons.TnyADD_100sowD'
Wrong parameters

error while obeying batch file C:/Users/memun/AppData/Local/Temp/RtmpU134Od/macrofile_1f00664e58f9.txt at line number 27:
SETV 4 'cons.TnyADD_100sowD'

…6.
Execution completed
Error in read.dta(IGLSfile) :
unable to open file: 'No such file or directory'


It refers to TnyADD_100sowD, line 27 (=0.304102747), which I do not see is particularly different than the other observations.
ChrisCharlton
Posts: 1351
Joined: Mon Oct 19, 2009 10:34 am

Re: R2MLwiN

Post by ChrisCharlton »

The "line number 27" is referring to a line in the generated script, which is attempting to issue the following command:

Code: Select all

SETV 4 'cons.TnyADD_100sowD'
This is the equivalent to clicking on "cons.TnyADD_100sowD" in the model->equations window and ticking the l(POSTNT_TXT_long) box.

Could you add debugmode=TRUE to your estoptions so that the model is opened in the MLwiN GUI, and then manually tick this box to see what message MLwiN gives?
mekareXX
Posts: 6
Joined: Mon Jun 30, 2014 1:51 pm

Re: R2MLwiN

Post by mekareXX »

When I add the debugmode=TRUE and enter MLwiN, I can see that the three levels are set up correctly. However, only the "Fixed Parameter" box is ticked (and none of the three levels, "POSTNR_TXT, chrnr, obsid"). When I tick these levels manually, the model can run in MLwiN.

I tried to set up a single level model (to reproduce a complete identical code to the MCMC Manual cp 18):

formula1="c(TnyADD_100sowD,TnyADD_100slswD)~(0|cons)+(1|cons)"
levID1='obsid'
estoptions1=list(EstM=0,debugmode=TRUE)
(mymodel=runMLwiN(formula1,levID1,D='Multivariate Normal',
Q1213_risk_red,estoptions1,MLwiNPath=mlwin))

Here, I get another error:

error while obeying batch file C:/Users/memun/AppData/Local/Temp/RtmpYfYjcl/macrofile_15e8bd0cc0.txt at line number 24:
SETV 2 'cons.TnyADD_100sowD'
Wrong parameters

error while obeying batch file C:/Users/memun/AppData/Local/Temp/RtmpYfYjcl/macrofile_15e8bd0cc0.txt at line number 24:
SETV 2 'cons.TnyADD_100sowD'

`‚*.
Execution completed
Error in read.dta(IGLSfile) :
unable to open file: 'No such file or directory'

When I enter MLwiN manually (using the debugmode), I have the same problem: The j(obsid_long) box is not ticked.
ChrisCharlton
Posts: 1351
Joined: Mon Oct 19, 2009 10:34 am

Re: R2MLwiN

Post by ChrisCharlton »

I tried running the single level model with your example data in the current development version and it worked for me:

Code: Select all

-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- 
MLwiN multilevel model (Multivariate Normal) 
Estimation algorithm:  IGLS        Elapsed time : 0.41s 
Number of obs:  19 (from total 19 )
Deviance statistic:  162 
--------------------------------------------------------------------------------------------------- 
The model formula:
[1] "c(TnyADD_100sowD,TnyADD_100slswD)~(0|cons)+(1|cons)"
Level 1: obsid      
--------------------------------------------------------------------------------------------------- 
The fixed part estimates:  
                          Coef.   Std. Err.       z    Pr(>|z|)         [95% Conf.   Interval] 
cons_TnyADD_100sowD    -2.74589     0.73081   -3.76   0.0001717   ***     -4.17825    -1.31354 
cons_TnyADD_100slswD   -0.08054     0.29976   -0.27      0.7882           -0.66805     0.50697 
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1  
--------------------------------------------------------------------------------------------------- 
The random part estimates at the obsid level: 
                                                  Coef.   Std. Err. 
var_cons_TnyADD_100sowD                        10.14751     3.29229 
cov_cons_TnyADD_100sowD_cons_TnyADD_100slswD   -0.01536     0.95489 
var_cons_TnyADD_100slswD                        1.70723     0.55390 
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- 
The part of the script file to set up this model (which you can compare with the .txt file referred to in the error message you are getting) is:

Code: Select all

MVAR 1   'TnyADD_100sowD'
MVAR 1   'TnyADD_100slswD'
NOTE   Specify the level identifier(s)
IDEN 2    'obsid'
IDEN 1 'resp_indicator'
LFUN 0

ADDT    'cons'


NOTE   Specify random part covariate(s)
SETV  2   'cons.TnyADD_100sowD'
SETV  2   'cons.TnyADD_100slswD'
If you open the MLwiN data manipulation->names window then each of the quoted variables in this script should appear as variable names.

It might also be worth checking that you have the most recent R2MLwiN and MLwiN versions.
mekareXX
Posts: 6
Joined: Mon Jun 30, 2014 1:51 pm

Re: R2MLwiN

Post by mekareXX »

Great to hear that the code for the testdata works for you.
I upgraded my MLwiN version to v2.30 (from v2.28) and installed the R2MLwiN package and library in R before running the code, so I expect that to be the latest version.
Just to try to reproduce your results I continued working on the testdata. So I made a new constant column (cons=1) in R:
testdata$cons <- rep(NA,length(testdata$obsid))
testdata$cons <- '1'
And set up a single level model (as previous):
mlwin = "C:/Program Files (x86)/MLwiN v2.30/i386"
formula1="c(TnyADD_100sowD,TnyADD_100slswD)~(0|cons)+(1|cons)"
levID1='obsid'
estoptions1=list(EstM=0,debugmode=TRUE)
(mymodel=runMLwiN(formula1,levID1,D='Multivariate Normal',
testdata,estoptions1,MLwiNPath=mlwin))

and now get the following error message:

Some input variables held data in more precision than MLwiN supports, these have been rounded

error while obeying batch file C:/Users/memun/AppData/Local/Temp/Rtmpcf6eUv/macrofile_108036fe2987.txt at line number 24:
SETV 2 'cons.TnyADD_100sowD'
Wrong parameters

error while obeying batch file C:/Users/memun/AppData/Local/Temp/Rtmpcf6eUv/macrofile_108036fe2987.txt at line number 24:
SETV 2 'cons.TnyADD_100sowD'

¨1.
Execution completed
Error in read.dta(IGLSfile) :
unable to open file: 'No such file or directory'

Then I added the debugmode and went to MLwiN Names window (attached) as you suggested. What strikes me is, that the "cons" equals 0, but should actually be 1...Hence, the 'cons.TnyADD_100sowD' and 'cons.TnyADD_100slswD' are missing as well. I hope you can help me again to proceed.
Attachments
MLwiN_Names.pdf
(99.56 KiB) Downloaded 458 times
ChrisCharlton
Posts: 1351
Joined: Mon Oct 19, 2009 10:34 am

Re: R2MLwiN

Post by ChrisCharlton »

I believe that the issue you are seeing with cons is that you have quoted the one when assigning it. This causes it to be a string variable (which you can confirm with is.character(testdata$obsid)). When this is sent to MLwiN it is converted into a categorical variable with the numeric value zero and label '1'. To generate the variable as numeric you simply need to remove the quotes, i.e.:

Code: Select all

testdata$cons <- rep(NA,length(testdata$obsid)) 
testdata$cons <- 1 
or more simply

Code: Select all

testdata$cons <- rep(1,nrow(testdata))
You can then test that it is indeed numeric with is.numeric(testdata$cons)
Post Reply