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/
Post Reply
tjsduq64
Posts: 14
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: 14
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: 1144
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: 1144
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.

Post Reply