presenting MCMC visuals on rmarkdown

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

presenting MCMC visuals on rmarkdown

Post by tjsduq64 »

Hi,

I am using R2MLwiN to run a model via MCMC and presenting the diagnostics by knitting a R markdown file into a html document.

This code on rmarkdown (i.e., open a rmarkdown and run this code) creates "test.Rmd" that I want to knit.

Code: Select all

```{r}
## Read tutorial data
data(tutorial, package = "R2MLwiN")

mymodel0 <- runMLwiN(normexam ~ 1 + standlrt + (1 | school) + (1 | student), estoptions = list(EstM = 1), data = tutorial)
summary(mymodel0)

trajectories(mymodel0)

sixway(mymodel0@chains[, "RP2_var_Intercept", drop = FALSE], "RP2_var_Intercept")

```
This code on R script (i.e., open a R script and run this code) knits the "test.Rmd" into a html file.

Then, in my final document (I cannot figure out a way to attach my html document), I get everything fine except the visuals. In this particular example, the output of trajectories function is presented correctly, but the output of sixway is not presented. Instead, on RStudio, a new window pops up and presents sixway.

But with my actual codes for my data analysis, I only get sixway in the document and trajectories on pop up windows. It seems like whatever is presented in a new window does not get included in the document. Another problem is that in my actual codes, the outputs of sixway gets inserted in incorrect positions. They do not get inserted right after the code but after a few lines of codes.

My two questions are
1) why doesn't my final document include all visuals? It includes sometimes trajectories, other times sixway. How can I fix this?
2) why does my sixway output get inserted in a lagged fashion (i.e., after a few lines of codes)?

Thanks,

Sun
tjsduq64
Posts: 31
Joined: Mon Jul 15, 2019 10:04 pm

Re: presenting MCMC visuals on rmarkdown

Post by tjsduq64 »

Additional concern: I also noticed that what gets included in the final document changes every time I knit the document, even with the same codes. So, when I run a single document of codes multiple times, sometimes trajectories get included and other times, sixway.
ChrisCharlton
Posts: 1348
Joined: Mon Oct 19, 2009 10:34 am

Re: presenting MCMC visuals on rmarkdown

Post by ChrisCharlton »

It seems that rmarkdown is not working happily with our use of dev.new() in the sixway function (line 119 at https://github.com/rforge/r2mlwin/blob/ ... R/sixway.R). Removing this line seems to make the plots appear correctly for me. Unfortunately doing this also means that the previous graph is not cleared when you run the function in the console, so this is not a general solution. We will have to investigate a way that works in both cases, however in the meantime a workaround would be to copy this function into your own version that doesn't have the line and use that from the markdown instead.
ChrisCharlton
Posts: 1348
Joined: Mon Oct 19, 2009 10:34 am

Re: presenting MCMC visuals on rmarkdown

Post by ChrisCharlton »

It looks like setting the background to white instead of the dev.new() makes it work in both cases, so I'll make this change in the new version of the package.
tjsduq64
Posts: 31
Joined: Mon Jul 15, 2019 10:04 pm

Re: presenting MCMC visuals on rmarkdown

Post by tjsduq64 »

Hi Chris, thank you for your help and sorry for the late reply. That's good news that the new version of the package will fix this.

I have a question about your suggestion. I don't see dev.new anywhere in the link you sent me. I may just be not understanding the programming part. I nonetheless deleted the line 119 as you suggested, but I get error "Error in plot.new(): figure margins too large" and "Error in graphics::par(mypar): object 'mypar' not found".

Did I removed the correct line?

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

Re: presenting MCMC visuals on rmarkdown

Post by ChrisCharlton »

Sorry, the link that I provided always points to the current version of the file, so if you look at it now it will include the fix. The original version of the file can be found at https://github.com/rforge/r2mlwin/blob/ ... R/sixway.R. If you now use the file from the original link I provided without making any changes then it should work.
tjsduq64
Posts: 31
Joined: Mon Jul 15, 2019 10:04 pm

Re: presenting MCMC visuals on rmarkdown

Post by tjsduq64 »

When I used the new version of sixway (removing the line 119 from the older version), I only get the first four plots out of the six (first two rows of 2 by 3 plots) + summary statistics, and also get the error,

Code: Select all

Error in MCSE(flatchain, rho, ll = 0.5, ul = 20) : 
  could not find function "MCSE"
In addition: Warning message:
In par(new = TRUE) : calling par(new=TRUE) with no plot
The warning message appears for some codes, but not all.

This is indeed an improvement since the outputs are consistent. Not a huge issue at the moment but I figured you would want to know these errors. Thank you for your help.

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

Re: presenting MCMC visuals on rmarkdown

Post by ChrisCharlton »

Thanks for the update. The MCSE function is another one provided with R2MLwiN (https://github.com/rforge/r2mlwin/blob/ ... N/R/MCSE.R), so I am not sure why it isn't picking this up. You could try including the above function definition manually and seeing whether that fixes things.
tjsduq64
Posts: 31
Joined: Mon Jul 15, 2019 10:04 pm

Re: presenting MCMC visuals on rmarkdown

Post by tjsduq64 »

Ah, gotcha. I added MCSE manually and now the next function BD is not being picked up. Probably something wrong with my current setup in the environment. Thank you.

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

Re: presenting MCMC visuals on rmarkdown

Post by ChrisCharlton »

You can find BD here: https://github.com/rforge/r2mlwin/blob/ ... wiN/R/BD.R. I think that's the last of our functions that sixway uses. These should all be available once you have loaded the R2MLwiN package, so it's a bit odd that you it can't find them. Are they available if you run the functions directly?
Post Reply