nimble-dev / nimble

The base NIMBLE package for R
http://R-nimble.org
BSD 3-Clause "New" or "Revised" License
156 stars 23 forks source link

Documentation 'calcNodes' not up to date? #1347

Closed BertvanderVeen closed 4 months ago

BertvanderVeen commented 11 months ago

When going through the example of creating a nimbleFunctionList of calcNodes the function errs. This gives the impression that the documentation for calcNodes might not be up to date.

For example:

library(nimble)
#> nimble version 1.0.1 is loaded.
#> For more information on NIMBLE and a User Manual,
#> please visit https://R-nimble.org.
#> 
#> Note for advanced users who have written their own MCMC samplers:
#>   As of version 0.13.0, NIMBLE's protocol for handling posterior
#>   predictive nodes has changed in a way that could affect user-defined
#>   samplers in some situations. Please see Section 15.5.1 of the User Manual.
#> 
#> Attaching package: 'nimble'
#> The following object is masked from 'package:stats':
#> 
#>     simulate
#################################
######### simulate data #########
#################################

beta1 <- rnorm(2, 0, 1)
set.seed(1);X <- matrix(rnorm(n = 2000),ncol = 2)
eta <-  X%*%beta1
lambda <- exp(eta)
set.seed(1);y <- rpois(1000, lambda=lambda) 

#################################
########### BUGS code ###########
#################################

pois.glm <- nimbleCode({
  ## Likelihood
  for(i in 1:N){
    y[i] ~ dpois(lambda[i])
    log(lambda[i]) <- eta[i]
    eta[i] <- inprod(beta[1:k],X[i,])
  }
  for(l in 1:k){
    beta[l] ~ dnorm(0,1)
  }
})

#################################
############# fit it ############
#################################

dat <- list(X = X,
            y = y)
const <- list(
  N = 1000,
  k = 2)
# construct model object
model <- nimbleModel(pois.glm, const, dat)
#> Defining model
#> Building model
#> Setting data and initial values
#> Running calculate on model
#>   [Note] Any error reports that follow may simply reflect missing values in model variables.
#> Checking model sizes and dimensions
#>   [Note] This model is not fully initialized. This is not an error.
#>          To see which variables are not initialized, use model$initializeInfo().
#>          For more information on model initialization, see help(modelInitialization).
mod <- nimble::compileNimble(model)
#> Compiling
#>   [Note] This may take a minute.
#>   [Note] Use 'showCompilerOutput = TRUE' to see C++ compilation details.

#################################
## try nimble function list it ##
#################################
myCalcs <- nimbleFunctionList(calcNodes)
myCalcs[[1]] <- calcNodes(model, "beta[1]") # errs
#> Error: An element being put in a nimbleFunctionList is not valid. It may not have the right contains (base class)

Created on 2023-10-11 with reprex v2.0.2

paciorek commented 11 months ago

@perrydv I believe @BertvanderVeen is looking at help(calcNodes), which suggests running nimbleFunctionList(calcNodes), which of course doesn't work as calcNodes is a nimbleFunction.

I tried to modify the example code to be:

baseClass <- nimbleFunctionVirtual(run = function() {returnType(double())})
myCalcs <- nimbleFunctionList(baseClass)
myCalcs[[1]] <- calcNodes(model, "beta[1]")
# Error: An element being put in a nimbleFunctionList is not valid. It may not have the right contains (base class)

but that fails because the calcNodes nimbleFunction does not contain baseClass.

Can you look and see what you may have had in mind here?

danielturek commented 11 months ago

Noting this code dates back to the initial commit of the nimble package (>9 years ago), and hence the code itself actually predates that commit.

This is clearly very old code (and documentation) from an earlier era. I wasn't aware of the calcNodes nimbleFunction, and it might be a safe guess that the original motivation (and syntax) are long-since out of date (and no longer valid).

danielturek commented 11 months ago

Also includes relics such as identityMatrix: https://github.com/nimble-dev/nimble/blob/02cffc9fc107a5a1c8f2fd5d938d7558b0c0c184/packages/nimble/R/NF_utils.R#L187

paciorek commented 4 months ago

I've removed the non-working example code embedded in the roxygen. One could use calcNodes() but not as part of a nimbleFunctionList.