Change code from Stata to R

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
jeroendebont
Posts: 2
Joined: Thu Feb 07, 2019 10:46 am

Change code from Stata to R

Post by jeroendebont » Thu Feb 07, 2019 11:20 am

Dear all,

I have been using "runmlwin" on STATA but I changed to R, and I have some doubts about changing the code to R. I have been reading the forum and the R2MLwiN manual and I think I have been able to adapt it to R properly but I would kindly ask if you could confirm this code in R is correctly.
The model:

*Aim: individual weight growth trajectories with repeated measurements during childhood.
*Statistical method: linear spline model with 3 knot points.
*Model includes: two random levels: 1:) level2 - individual level random effect 2:) level 1- individual-specific occasion-level residuals. Also, I am modelling the complex level 1 variation adding a constant to it.
* I specified a diagonal matrix for the random effects variance-covariance matrix.

My code in stata would be:

runmlwin weight cons s1 s2 s3 s4, ///
level2 (id: cons s1 s2 s3 s4, reset(none) residuals(res_model,var) ) ///
level1 (occ: cons, reset(none) diag) ///
nopause maxiter (150) batch


My variables are: weight (repeated weight measures), cons (constant necessary to develop the model), s1-s4 are the spline variables [s1 (age between birth and 3 months), s2 (age between 3 and 12 months), s3 (age between 12 and 36 months), s4 (age between 36 and 60 month)]

My model in R would be:
# prepare the diagonal covariance matrix
smat <- matrix(, nrow=2, ncol=1)
smat[1, 1] <- 2 # Level of covariance matrix
smat[2, 1] <- 1 # Set matrix type to diagonal


# prepare formula
model1 <- weight ~ 1 + s1 + s2 + s3 + s4 + (1 + s1 + s2 + s3 + s4 | id) + (cons| occ)

# execute the model
run_model1 <- runMLwiN(Formula = model1, data = mydb, estoption = list(resi.store = TRUE, smat = smat))

I appreciate much your time. I am mainly worried about the diagonal covariance matrix.

Thank you.

All the best,

Jeroen de Bont.

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

Re: Change code from Stata to R

Post by ChrisCharlton » Fri Feb 08, 2019 10:15 am

The converted code looks correct to me, however I believe that it is missing an equivalent for the reset and maxiter options. To add these you just need to add:

Code: Select all

reset=c(2, 2)
and

Code: Select all

maxiter=150
into the estoptions list. You could also replace:

Code: Select all

(cons | occ)
with:

Code: Select all

(1 | occ)
to avoid having to create a variable to represent the intercept.

Looking at our documentation for smat I think that the description is incorrect as the matrix is assumed to have two rows and you need to add a column for each level that you wish to set it at. Your syntax seems seems to use the correct version however.

To check that the model has been set up correctly (which we would suggest is a good idea while becoming familiar with the package) you can remove the nopause option in Stata or add the debugmode=TRUE option into the estoptions list in R. This will open the MLwiN GUI and display the model before it is run. You can then click "resume macro" to run the model, and again to close MLwiN. If you want to compare the syntax of the generated script between runmlwin and R2MLwiN to check that the specification is the same you can do so by adding the viewfullmacro in runmlwin and adding the show.file=TRUE option into the estoptions list for R2MLwiN. In both cases this will open a text window displaying the code that is sent to MLwiN.

jeroendebont
Posts: 2
Joined: Thu Feb 07, 2019 10:46 am

Re: Change code from Stata to R

Post by jeroendebont » Mon Feb 11, 2019 7:59 am

Dear Chris,

Thank you for your reply. I will have check with the debugmode = TRUE option.

All the best,

Jeroen.

Post Reply