Page 1 of 2

presenting MCMC visuals on rmarkdown

Posted: Thu Mar 26, 2020 2:57 am
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

Re: presenting MCMC visuals on rmarkdown

Posted: Thu Mar 26, 2020 3:15 am
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.

Re: presenting MCMC visuals on rmarkdown

Posted: Thu Mar 26, 2020 12:37 pm
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.

Re: presenting MCMC visuals on rmarkdown

Posted: Thu Mar 26, 2020 1:11 pm
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.

Re: presenting MCMC visuals on rmarkdown

Posted: Tue Mar 31, 2020 11:40 am
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

Re: presenting MCMC visuals on rmarkdown

Posted: Tue Mar 31, 2020 11:48 am
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.

Re: presenting MCMC visuals on rmarkdown

Posted: Tue Mar 31, 2020 2:40 pm
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

Re: presenting MCMC visuals on rmarkdown

Posted: Tue Mar 31, 2020 2:49 pm
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.

Re: presenting MCMC visuals on rmarkdown

Posted: Tue Mar 31, 2020 3:08 pm
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

Re: presenting MCMC visuals on rmarkdown

Posted: Tue Mar 31, 2020 3:16 pm
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?