Free the modeler in you: Introduction to Nimble
Question Answer(s)
Do you always need to provide indices for vectors? Like if x is a vector of n elements would: y &lt;- 1 + x work or does only y &lt;- 1 + x[1:n] works? For security, it's better to provide the indices y[1:n] <- 1 + x[1:n]
NA but if the declarations in your code make the size of x and y clear, then y <- 1+x could also work. Some informations here:
NA Awesome, thx!
Hi Chloe et al. Is there any efficiency gain from specifying sigma (nimble) rather than tau &amp; sigma (jags &amp; nimble)? I don't think so. Just more comfortable I guess that having to use precision.
NA Thanks :slightly_smiling_face:
NA Actually, really nice to have both options available in nimble for compatibility.
So basically Nimble is Jags on steroids! :slightly_smiling_face: Love it! I think that's gonna be my argument as to why favor nimble over JAGS :rolling_on_the_floor_laughing::rolling_on_the_floor_laughing::rolling_on_the_floor_laughing:
Hi, can someone re-explain what a "chain" is? A sequence of values from your MCMC. You may run 2 or more chains from different values.
NA thanks
Hi, just a quick question - I was wondering can Nimble call in functions from other R package (like raster etc.) while running a Bayesian model? Yes. More details I think in chapter 11 of the Nimble manual . We'll ask the question to when he joins us.
NA Thank you!
NA Yes.
NA You can use `nimbleRcall` to call R code and `nimbleExternalCall` to call external compiled code such as your own C code.
NA Sounds great! Thanks for the reply!
Is there anything which JAGS can do which Nimble can not [yet?} do? apparently parallelization is not so easy yet :smiley:
NA Don't think so. Like just said, parallelization is a bit trickier than jagsUI, but see
NA Interpreting empty indices is the only thing that comes to mind. Additionally, an important thing to be aware of is that nimble sometimes needs more memory to run than JAGS (especially for large models). More on this in lecture 8 :wink:
Did you say you have to specify everything before the model in nimble? Can you do a mix i.e. state the thin and burnin in the model? You can specify these value n.iter =5000 n.burnin = 1000 n.chains = 2 n.thin = 1 directly in the nimbleMCMC function, I reckon
Can you get WAIC with Nimble? Yes, more in the next lecture :slightly_smiling_face:
NA :money_mouth_face:
I don't know why I can't install the package MCMCvis ? Can you tell us what pb you get specifically?
NA R say ERROR: configuration failed for package ‘V8’ and 'rstan'. I'm tried to install these two packages but it didn't work.
NA What about when you try: ```install.packages('devtools') devtools::install_github('caseyyoungflesh/MCMCvis', build_vignettes = TR UE)```
NA because the systeme of my computer is ubuntu so i need to install a system dependencie. It worked now. Thanks!
NA Good to know!
nimbleMCMC() with setSeed aregument has Rhat==NaN in MCMCvis::MCMCsummary I get the same
NA is it a bug? it can be worked around with setting seed and running nimbleMCMC continuously: set.seet(mySeed); mcmc.output <- nimbleMCMC(without setSeed)
I dont get the same initial values when setting set.seed, why? You neet to setSeed(mySeed) each time before running the initial value function.
NA > setSeed(mySeed) > initial.values() > setSeed(mySeed) > initial.values() should give you twice the same number.
NA Thanks but I thought that once that you have set the seed would be enough
NA The seed we set is just the starting seed. It sets the hidden seeds for all subsequent random draws. So if you set the seed, then do initial.values() twice, you will get two different numbers. This is important, because you still want different initial values for your different chains.
NA Setting the seed makes sure that next time you run your code from the start, you will get the same numbers again. Does that make sense?
NA Yes, thanks!
What does Nimble do better than Stan? What's your sales pitch? :wink: Good one. Ping ! (For a long time, there was a persistent rumour that stan struggles with latent state models. But I learned lately that this is not actually the case. So I'm interested in Perry's thoughts on this one too!)
NA Sales pitch? Cooler logo? :stuck_out_tongue:
NA I am sold with the logo!
NA Jokes aside, let's see what Perry thinks about this!
NA I would be keen to hear Perry's thoughts too. I understand that stan cannot estimate latent _integers_ (if that's the right terminology)?
NA Me too :slightly_smiling_face: yes, you cannot define a parameter as an integer in Stan.
NA Thanks . Nice to see you here - I hope you're well. I think your statement about an integer _parameter_ is more general and better than mine. Thanks again.
NA I think you need to marginalize over the latent states in Stan to deal with HMMs. In Nimble (and nimbleEcology) you can do both, the joint likelihood of parameters and latent states, or the marginalization, more in lectures 4 and 8.
NA Don't know Nimble that much but indeed a big advantage of JAGS/Nimble over Stan is the possibility of estimating discrete stochastic parameters (states in HMM ...). It does seems that with some more maths one can also get at it with Stan:
NA Here are some comments in relation to Stan • Hamiltonian Monte Carlo (Stan's method) is not universally always most efficient. It mixes well at high computational cost. Sometimes we see methods that mix less well but iterate much faster because of lower computational cost, ending up to be more efficient. It is problem-specific. • Yes, handling discrete latent states is not possible Stan, as I understand it. One needs to write out the marginal probabilities. • nimble is not just an MCMC engine. It is a hierarchical modeling programming system. We've seen and/or built methods like Monte Carlo Expectation Maximization (max. likelihood integrating over latent states), particle filtering, and Particle MCMC (particle filter + MCMC). • Within MCMC, nimble enables methods like reversible jump (for variable selection) and Bayesian non-parametric distributions, both of which rely on discrete latent states. • We are building support for automatic differentiation and hence HMC, Laplace approximation, and other methods that need derivatives to be efficient. • That's a quick early-morning (in California) reply!
NA Thank you . It is a convincing list. I expect to use nimble in place of / alongside jags in the future. I still code in stan because I like the idea of using HMC for "simple" hierarchical regression-like models. Do you have an estimated date for HMC in nimble?
NA That's hard to predict. A lot is working but there are many details in developing such a system.
NA I should be a little less vague. I really hope we release this in the next year. I hope we release some beta version earlier.
When I run the nimbleMCMC, I have an error message regarding my time zone. Do you know why ? Hi , that is rather odd. What does the error message say? I doubt this is a nimble problem, it's probably "deeper down"...
NA Yes, I guess... I'll look for it. Thank you
NA Let us know if you cannot find the problem. We're happy to help look :slightly_smiling_face:
NA I set the TZ. however now I have another weird error message : "Error: Failed to create the shared library."
NA I am trying to understand why but not quite here yet
NA every packages that I tried to install fails so I might have a problem with R itself.
NA Yes, sounds like the path to your R library may be set wrongly. The "shared library error" we encountered earlier too. Maybe can help when he is back from lunch!
NA ok thank you
NA Turn off your computer and start from scratch. That's my fix for these weird issues. Let us know if it works better.
NA ok. I'll try that :slightly_smiling_face:
NA It did not work ...
NA Dunno about that one. I guess you've googled it? Ping in case he's seen this issue before.
&gt; library(janitor)
&gt;
&gt; mcmc.output %&gt;%
+ as_tibble() %&gt;%
+ janitor::clean_names() %&gt;%
+ ggplot() +
+ geom_histogram(aes(x = chain1[,"phi"]), color = "white") +
+ labs(x = "survival probability")
Error in as_tibble(.) : could not find function "as_tibble"
Hi . Fancy meeting you again! Anyways, try library(tidyverse) first.
NA you need to have also library(tidyverse)
&gt; library(MCMCvis)
Warning message:
package ‘MCMCvis’ was built under R version 4.0.5
&gt; library(ggplot2)
Want to understand how all the pieces fit together? Read R for Data
Science: <https://r4ds.had.co.nz/>
&gt; library(tidyverse)
Error: package or namespace load failed for ‘tidyverse’ in loadNamespace(j &lt;- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]):
namespace ‘pillar’ 1.5.1 is already loaded, but &gt;= 1.6.0 is required
In addition: Warning message:
package ‘tidyverse’ was built under R version 4.0.5
&gt; library(janitor)

Attaching package: ‘janitor’

The following objects are masked from ‘package:stats’:

chisq.test, fisher.test

Warning message:
package ‘janitor’ was built under R version 4.0.5
&gt; library(pillar)
you may need to update the R version on your machine
NA and update also the tidyverse package
NA thanks! somehow, loading dplyr helped!
&gt;
&gt; library(MCMCvis)
&gt; library(tidyverse)
Error: package or namespace load failed for ‘tidyverse’ in loadNamespace(j &lt;- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]):
namespace ‘pillar’ 1.5.1 is already loaded, but &gt;= 1.6.0 is required
&gt; mcmc.output %&gt;%
+ as_tibble() %&gt;%
+ janitor::clean_names() %&gt;%
+ ggplot() +
+ geom_histogram(aes(x = chain1[,"phi"]), color = "white") +
+ labs(x = "survival probability")
Error in mcmc.output %&gt;% as_tibble() %&gt;% janitor::clean_names() %&gt;% ggplot() :
could not find function "%&gt;%"
Maybe quit R or Rstudio and re-install the tidyverse package to have a clean install.
NA It looks like you also need to update/re-install the "pillar" packages. At the moment, your tidyverse does not load because "pillar" is out of date.
NA Then do as suggests and restart R(studio). Hopefully that will fix it.
Just by chance my distribution has this bimodal peak! Re-running is fast and fixes this. However, if I had a model that takes a while to run.... can I keep these results? Would that be considered okay? This is no problem at all. Remember there are only 5000 iterations here. If you wanted to publish this, you'd probably run for a bit longer, and also with a 3rd or maybe 4th chain. Also, I would not call this "bimodal". It's just a bit "wiggly".
NA It's just the defaults of the kernel density estimate (the continuous histogram) that make your dataviz a bit wiggly, no problem here.
NA Great, thanks!
NA Can I use "wiggly" in a publication? :smile:
NA surprisingly yes :wink: used in Methods Eco Evo
NA I encourage more "diversity" in language use in papers! My answer is "yes", as long as you don't use passive voice with it :stuck_out_tongue:
Hi all...I'm curious what I should expect in terms of the time it takes to define a model when using Nimble if I have a large data set and complex model? I have a working model tested with simulated data and have followed your intro to nimble video using real data and model, data, constants, etc. set up. I did not attempt parallelization...I have a capture history matrix with about 3400 rows and 140 occasions and a multistate model with 5 states and 4 obs types...RStudio has continued with "defining model...." for a couple of hours now?? My computer memory is definitely ramped up so I feel like things are happening, but not really sure if this is normal? Thanks for any insight! Yes it can be a problem for large models. There are some ways to reduce the number of nodes (model declarations) that need to be managed. A first step is to not use `nimleMCMC` and start with `nimbleModel` and use `calculate=FALSE`.
NA Okay, I will try that. Thank you!
NA Hmmm...made those changes and let it run even longer this time, and still same issue.
NA I suspect the large dataset makes it difficult to fit your rather complex multistate model. Marginalization may help, as well as grouping individuals with the same encounter history. We will go through such an example in lecture 8. If you're too eager to try on your own, check out the section 'Marginalization and weighted likelihood with nimbleEcology' in
NA Thank you so much! The pooled matrix now is reduced to just over 300 rows now. I followed along with the demo and then received the following error message: defining model... Registering the following user-provided distributions: dHMMweighted Error in FUN(X[[i]], ...) : prepareDistributionInfo: types/dimensions of parameters are not the same in density and simulation functions: 'dHMMweighted' and 'rHMMweighted'. In addition: Warning message: In FUN(X[[i]], ...) : checkDistributionFunctions: parameter arguments not the same amongst density and simulation functions for dHMMweighted. Continuing anyway based on arguments to the density function; algorithms using the simulation function are unlikely to function properly. Timing stopped at: 0.19 0.03 0.22
NA I see now that you've posted the videos...I will take a look and see if I can troubleshoot...
NA 300 rows, look promising! Can you run the live demo with the geese example without problem?
NA If you send me your code/data, I'm happy to help :wink:
NA I am going through the video now and will test/run the geese example next. And yes! I am happy to share my code/data. Thank you!
NA I've emailed you my code/data...thanks!