modelCode <- nimbleCode({ #### DP # for mean for(i in 1:(H-1)){ v[i] ~ dbeta(1, alpha) } w[1:H] <- stick_breaking(v[1:(H-1)]) #for phi for(i in 1:(HH-1)){ vv[i] ~ dbeta(1, alpha2) } ww[1:HH] <- stick_breaking(vv[1:(HH-1)]) ### sample from G0 # mean for(h in 1:H){ a0[h] ~ dnorm(0.0,1.5) a1[h] ~ ddexp(0.0,1.5) a2[h] ~ ddexp(0.0,1.5) a3[h] ~ ddexp(0.0,1.5) a4[h] ~ ddexp(0.0,1.5) } # phi for(hh in 1:HH){ b0[hh] ~ dnorm(0.0,1.5) b1[hh] ~ ddexp(0.0,1.5) b2[hh] ~ ddexp(0.0,1.5) b3[hh] ~ ddexp(0.0,1.5) b4[hh] ~ ddexp(0.0,1.5) } # likelihood for(g in 1:G){ # sample category: zeta[g] in [1......H], and zeta2[g] in [1...HH] zeta[g] ~ dcat(w[1:H]) zeta2[g] ~ dcat(ww[1:HH]) for(t in 1:T){ # sample gene expression Y[g,t] ~ dnegbin(p[g,t],phi[g,t]) # construct value p[g,t] in terms of mu[g,t] and phi[g,t] p[g,t] <- phi[g,t]/(scale_factors[t]*exp(logmu[g,t])+phi[g,t]) phi[g,t] <- exp(logphi[g,t]) # we need to define logmu[g,t] logmu[g,t] <- a0[zeta[g]] + a1[zeta[g]] * X1[t]+ a2[zeta[g]] * X2[t] + a3[zeta[g]]* X3[t]+ a4[zeta[g]]* X4[t] # define phi[g,t] logphi[g,t] <- b0[zeta2[g]] + b1[zeta2[g]] * X1[t] + b2[zeta2[g]] * X2[t] + b3[zeta2[g]] * X3[t] + b4[zeta2[g]] * X4[t] } }})
constants <- list(G = G, H = n_clusters_mean, HH = n_clusters_phi, T = T, alpha = concentration_mean, alpha2 = concentration_phi )
data <- list(Y = Y, X1 = X1, X2 = X2, X3 = X3, X4 = X4, scale_factors = scale_factors )
inits <- list(a0 = rnorm(constants$H, 0, 1), a1 = rnorm(constants$H, 0, 1), a2 = rnorm(constants$H, 0, 1), a3 = rnorm(constants$H, 0, 1), a4 = rnorm(constants$H, 0, 1), b0 = rnorm(constants$HH, 0, 1), b1 = rnorm(constants$HH, 0, 1), b2 = rnorm(constants$HH, 0, 1), b3 = rnorm(constants$HH, 0, 1), b4 = rnorm(constants$HH, 0, 1), v = rbeta(constants$H, 1, 1), vv = rbeta(constants$HH, 1, 1) )
# this is R object, not C++ objectRmodel <- nimbleModel(modelCode,constants,data,inits)
# lets monitor just cluster assignments z, we can change samplers heremonitors <- c("zeta","zeta2")RmodelConf <- configureMCMC(Rmodel, monitors = monitors,thin=lag)
# create mcmc objectRmcmc <- buildMCMC(RmodelConf)
print("R objects created")
# compile everything in C++Cmodel <- compileNimble(Rmodel,showCompilerOutput = TRUE)Cmcmc <- compileNimble(Rmcmc,project = Cmodel,showCompilerOutput = TRUE)
print("C++ objects created")
runMCMC_samples <- runMCMC(Cmcmc, nburnin = burnin, niter = n_steps)--
You received this message because you are subscribed to the Google Groups "nimble-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/03185087-8187-4a8a-ac75-d99eb60383efo%40googlegroups.com.
Hi Evgeny,Thanks for the message and I'm very interested to see this application.Let me point you to my message of June 26 on this thread:https://groups.google.com/forum/#!topic/nimble-users/8vaTHQoJz2MI commented on why nimble encounters memory problems (which we hope to improve). Then I gave a strategy for keeping large objects out of the model. That strategy might work well for your problem.Looking at your model, I wonder if the following would work: Vectorize logmu and logphi over t (since any change in a parameter will affect all t). And use a vector of dummy data values to push calculation of phi[g, 1:T], p[g, 1:T] and log probabilities of y[g, 1:T] to R (or C++) using the trick I showed in the June 26 message. Your model code would have something like:dummy[g] ~ dCustom(logmu[g, 1:T], logphi[g, 1:T], g ) # do the phi, p and y steps all togetherAll the actual values of y would live in dCustom.This could also be done for logmu and/or logphi at a couple of levels. You could havelogmu[g, 1:T] <- calc_logmu(a0[zeta[g]], a1[zeta[g]], etc.)All of the values of X would live in calc_logmu.Going further, you could store logmu itself outside the model, using another dummy variable to serve as a low-dimensional node to establish the graph relationships in the model. This would look something likelogmu_dummy[g] <- calc_logmu(g, a0[zeta[g]], a1[zeta[g]], etc.)where calc_logmu would have to return a value to logmu_dummy[g] that wouldn't really matter. The actual values of logmu would be stored in R (or C++) in the same scope from where they'd be used by dCustom.This is all something of a workaround to the scaling issues, but it is fairly straightforward to set up once you see how the pieces fit together and allows nimble to manage the model graph and MCMC system. I'm happy to help sort it out if it's not clear how it all can fit together.A couple of final thoughts on your model: Sampling for dcat can be inefficient because on every iteration every possible value is tried. It would be easy to set up a sampler that for example tries one alternative or a random subset of alternatives. Computational cost vs. mixing is hard to predict and would depend on the model. The dcat case is a tradeoff I've been curious about. Also, you may be good with what you've written, but nimble does have Bayesian non-parametric distributions (see blog posts linked from our documentation page at r-nimble.org) and they will be improved in an upcoming release.HTH and let me know how it goes.-Perry
To unsubscribe from this group and stop receiving emails from it, send an email to nimble...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/f2afb677-391e-4ff4-b798-5285d6c5ff69o%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/f2afb677-391e-4ff4-b798-5285d6c5ff69o%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/55f6e41a-43c5-4ae7-846a-efdcc2883754o%40googlegroups.com.
makeRfuns <- function(keepInR){ # get samples X1 <- keepInR$X1 X2 <- keepInR$X2 X3 <- keepInR$X3 # I can use X1,X2,X3 in all functions here # generic function, can be used for both logmu and logphi calc_log <- function(c0,c1,c2,c3){ return(c0 + c1 * X1 + c2 * X2 + c3 * X3) } return(list(calc_log = calc_log))}
####### multivariate negative binomial distribution# densitydNBVector <- nimbleFunction( run = function(x = double(1), logmu = double(1), logphi = double(1), log = integer(0, default = 0)) { ## default should be zero returnType(double(0)) # compute p,r out of logmu and logphi r = exp(logphi) p = r/(exp(logmu)+r) logProb = sum(dnbinom(x,r,p,log=TRUE)) if(log) return(logProb) else return(exp(logProb)) })# random number generationrNBVector<-nimbleFunction( run = function(n = integer(0), logmu = double(1), logphi = double(1)) { ## default should be zero returnType(double(1)) # compute p,r out of logmu and logphi r = exp(logphi) p = r/(exp(logmu)+r) return(rnbinom(n,r,p)) })
##### obtain Y,X1,X2,X3 somehow
# create a function that computes logmu/logphikeepInR <- list(X1=X1,X2=X2,X3=X3)
# obtain pointer to calc_log functionRfuns <- makeRfuns(keepInR)calc_log_internal <- Rfuns$calc_log
# now need to register with NIMBLEcalc_log <- nimbleRcall(function(c0=double(0),c1=double(0),c2=double(0),c3=double(0)){}, returnType = double(1), Rfun = "calc_log_internal" )
#### create nimble modelmodelCode <- nimbleCode({ #### DP # for mean for(i in 1:(H-1)){ v[i] ~ dbeta(1, alpha) } w[1:H] <- stick_breaking(v[1:(H-1)]) #for phi for(i in 1:(HH-1)){ vv[i] ~ dbeta(1, alpha2) } ww[1:HH] <- stick_breaking(vv[1:(HH-1)]) ### sample from G0 # mean for(h in 1:H){ a0[h] ~ dnorm(0.0,1.5) a1[h] ~ dnorm(0.0,1.5) a2[h] ~ dnorm(0.0,1.5) a3[h] ~ dnorm(0.0,1.5) } # phi for(hh in 1:HH){ b0[hh] ~ dnorm(0.0,1.5) b1[hh] ~ dnorm(0.0,1.5) b2[hh] ~ dnorm(0.0,1.5) b3[hh] ~ dnorm(0.0,1.5) } for(g in 1:G){ # sample category: zeta[g] in [1......H], and zeta2[g] in [1...HH] zeta[g] ~ dcat(w[1:H]) zeta2[g] ~ dcat(ww[1:HH]) # try to use our function logmu[g,1:T] <- calc_log(a0[zeta[g]],a1[zeta[g]],a2[zeta[g]],a3[zeta[g]]) logphi[g,1:T] <- calc_log(b0[zeta2[g]],b1[zeta2[g]],b2[zeta2[g]],b3[zeta2[g]]) # likelihood Y[g,1:T] ~ dNBVector(logmu[g,1:T],logphi[g,1:T]) }})To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/55f6e41a-43c5-4ae7-846a-efdcc2883754o%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/222a5526-84a6-4570-bf49-953e692a2c17o%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/222a5526-84a6-4570-bf49-953e692a2c17o%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/96622458-5a19-4425-9735-13e37e101adao%40googlegroups.com.
Hi Evgeny,I think something went wrong with your installation from the devel branch, but I'm not sure what if it does show that it is version 0.10.0.That error message is the old one from version 0.9.1, and the message is not in the current code in our devel branch. You might try uninstalling your current nimble andinstalling again using the instructions Claudia provided.Alternatively, if you are familiar with Git you might clone our repo from github.com/nimble-dev/nimble, then change to the directory "nimble/packages" and then do:R CMD build nimbleR CMD INSTALL nimble_0.10.0.tar.gzto install nimble.-chris
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/96622458-5a19-4425-9735-13e37e101adao%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/44ba2783-31cc-4ee7-b8ce-10f945acbcefo%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/44ba2783-31cc-4ee7-b8ce-10f945acbcefo%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/222a5526-84a6-4570-bf49-953e692a2c17o%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/222a5526-84a6-4570-bf49-953e692a2c17o%40googlegroups.com.
Hi Evgeny,
On the question of the error when you use the CRP approach, there was
a difference between NIMBLE's behavior on R 3.6 and R 4.0 with regard
to
setting up the MCMC sampler for the CRP node, and I suspect you are using R 4.0.
I've fixed that discrepancy, so if you re-install from the devel
branch, I think it should be fine for you now.
-chris
> To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/1b309a05-501f-4836-a5e8-a738bef7b834o%40googlegroups.com.
Hi Perry!
Thank you for detailed explanation. I think I understood what needs to be done. I created a code and it works.I have few concerns/points to mention.
- When I declared a proxy_Y node, I had to make it vector-valued(i.e. proxy_Y[g,1:T] instead of suggested proxy_Y[g]). Otherwise, the c++ compilation fails.
- I tested 2 instances of the code: one where I keep matrix Y in R and one where I supply matrix Y to nimble. The second case surprisingly runs a bit faster. Is it because of C++ <-> R data passing overhead or what can be the reason?
- In both versions I had to provide not just a density function for multivariate Negative Binomial distribution (dNBVector) but also a random number generating function (rNBVector). In 2 different situations (provide/not provide Y to nimble) the dNBVector function is coded slightly differently. I wonder if that can contribute to performance issues as well?
In fact, I will share both versions (that is whether or not Y is supplied to nimble) and, if you have some time/desire, you can take a look.I have to say, this is very kind of you and other team members of NIMBLE to help me. You are really helping me with my project and I appreciate that a lot!
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/1b309a05-501f-4836-a5e8-a738bef7b834o%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/CABeTmqherSWXeKzYxMD4nx7B-Xk%2B-CFi1n62AfLf9sYUmssZXw%40mail.gmail.com.
Hi Evgeny,I'm glad everyhting is working now, please let us know if you have further questions.Best,Claudia
On Fri, Jul 17, 2020 at 9:45 AM Perry de Valpine <pdeva...@berkeley.edu> wrote:
Thanks for reporting on some experimental results.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/1b309a05-501f-4836-a5e8-a738bef7b834o%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "nimble-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/CABeTmqherSWXeKzYxMD4nx7B-Xk%2B-CFi1n62AfLf9sYUmssZXw%40mail.gmail.com.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/a4069848-ad2b-482c-a8b5-7ccc4915237co%40googlegroups.com.