Learn R Programming

nimble (version 1.2.1)

simNodesMV: Basic nimbleFunctions for using a NIMBLE model with sets of stored values

Description

simulate, calculate, or get the existing log probabilities for values in a modelValues object using a NIMBLE model

Usage

simNodesMV(model, mv, nodes)

calcNodesMV(model, mv, nodes)

getLogProbNodesMV(model, mv, nodes)

Value

from simNodesMV: NULL. from calcNodesMV and getLogProbMV: a vector of the sum of log probabilities (densities) from any stochastic nodes in nodes.

Arguments

model

A nimble model.

mv

A modelValues object in which multiple sets of model variables and their corresponding logProb values are or will be saved. mv must include the nodes provided

nodes

A set of nodes. If none are provided, default is all model$getNodeNames()

Run time arguments

  • m. (simNodesMV only). Number of simulations requested.

  • saveLP. (calcNodesMVonly). Whether to save the logProb values in mv. Should be given as TRUE unless there is a good reason not to.

Author

Clifford Anderson-Bergman

Details

simNodesMV simulates values in the given nodes and saves them in mv. calcNodesMV calculates these nodes for each row of mv and returns a vector of the total log probabilities (densities) for each row. getLogProbNodesMV is like calcNodesMV without actually doing the calculations.

Each of these will expand variables or index blocks and topologically sort them so that each node's parent nodes are processed before itself.

getLogProbMV should be used carefully. It is generally for situations where the logProb values are guaranteed to have already been calculated, and all that is needed is to query them. The risk is that a program may have changed the values in the nodes, in which case getLogProbMV would collect logProb values that are out of date with the node values.

Examples

Run this code
code <- nimbleCode({
for(i in 1:5)
x[i] ~ dnorm(0,1)
})

myModel <- nimbleModel(code)
myMV <- modelValues(myModel)

Rsim <- simNodesMV(myModel, myMV)
Rcalc <- calcNodesMV(myModel, myMV)
Rglp <- getLogProbNodesMV(myModel, myMV)
if (FALSE) {
  cModel <- compileNimble(myModel)
  Csim <- compileNimble(Rsim, project = myModel)
  Ccalc <- compileNimble(Rcalc, project = myModel)
  Cglp <- compileNimble(Rglp, project = myModel)
  Csim$run(10)
  Ccalc$run(saveLP = TRUE)
  Cglp$run()	#Gives identical answers to Ccalc because logProbs were saved
  Csim$run(10)
  Ccalc$run(saveLP = FALSE)
  Cglp$run()	  #Gives wrong answers because logProbs were not saved
  result <- as.matrix(Csim$mv)
}

Run the code above in your browser using DataLab