Custom distribution error: Failed to create the shared library

79 views
Skip to first unread message

Stephanie Cunningham

unread,
Apr 28, 2023, 10:22:19 AM4/28/23
to nimble-users
Hi all,

I am a new user for Nimble, and am looking to build a custom distribution (Conway-Maxwell Poisson for under dispersed count data). I think I'm close, but when I try to compile the model, I get an error:
"Error: Failed to create the shared library. Run 'printErrors()' to see the compilation errors.

R version: 4.2.3, Nimble version: 0.13.1

library(COMPoissonReg)
library(nimble)

dCOMP <- nimbleFunction(
  run = function(x=integer(), lambda=double(0), nu=double(0), log=logical(0, default=0)) {
    returnType(double(0))
    logProb <- (1 - nu) * (log(lambda) * x - log(factorial(x))) - lambda - log(sum(exp((1-nu)*(log(lambda)*x - log(factorial(x))) - lambda)))
    if (log) return(logProb)
    else return(exp(logProb))
    })

demoCode <- nimbleCode({
  for (i in 1:4) {
    x[i] ~ dCOMP(2.8,1.1)
  }
})
demoModel <- nimbleModel(demoCode, inits = list(x = rcmp(4, 2.8, 1.2)),
                         check = FALSE, calculate = FALSE)
CdemoModel <- compileNimble(demoModel)

The output from printErrors():
P_1_demoCode_MID_1_nfCode.cpp: In function 'double rcFun_R_GlobalEnv5(int, double, double, bool)': P_1_demoCode_MID_1_nfCode.cpp:71:107: error: request for member 'sum' in 'exp(((((double)1 - ARG3_nu_) * ((log(ARG2_lambda_) * (double)ARG1_x_) - log(factorial((double)ARG1_x_)))) - ARG2_lambda_))', which is of non-class type 'double' 71 | Interm_3 = (exp((1 - ARG3_nu_) * (log(ARG2_lambda_) * ARG1_x_ - log(factorial(ARG1_x_))) - ARG2_lambda_)).sum(); | ^~~ P_1_demoCode_MID_1_nfCode.cpp: In member function 'virtual void x_L2_UID_4::simulate(const indexedNodeInfo&) const': P_1_demoCode_MID_1_nfCode.cpp:113:72: error: cannot bind non-const lvalue reference of type 'NimArr<1, int>&' to an rvalue of type 'NimArr<1, int>' 113 | (**model_x)[(ARG1_INDEXEDNODEINFO__.info[0]) - 1] = rcFun_R_GlobalEnv4(1, 2.8, 1.1); | ^ In file included from C:\Users\stcunnin\AppData\Local\R\win-library\4.2\nimble\include/nimble/RcppNimbleUtils.h:25, from C:\Users\stcunnin\AppData\Local\R\win-library\4.2\nimble\include/nimble/NamedObjects.h:28, from C:\Users\stcunnin\AppData\Local\R\win-library\4.2\nimble\include/nimble/EigenTypedefs.h:26, from P_1_demoCode_MID_1_nfCode.cpp:6: C:\Users\stcunnin\AppData\Local\R\win-library\4.2\nimble\include/nimble/NimArr.h:276:3: note: after user-defined conversion: 'NimArr<1, T>::NimArr(int) [with T = int]' 276 | NimArr<1, T>(int is1) : NimArrBase<T>() { setSize(is1); } | ^~~~~~~~~~~~ P_1_demoCode_MID_1_nfCode.cpp:15:58: note: initializing argument 1 of 'NimArr<1, double> rcFun_R_GlobalEnv4(NimArr<1, int>&, double, double)' 15 | NimArr<1, double> rcFun_R_GlobalEnv4 ( NimArr<1, int> & ARG1_n_, double ARG2_lambda_, double ARG3_nu_ ) { | ~~~~~~~~~~~~~~~~~^~~~~~~ make: *** [C:/PROGRA~1/R/R-42~1.3/etc/x64/Makeconf:260: P_1_demoCode_MID_1_nfCode.o] Error 1

Any guidance would be much appreciated!

All the best,
Stephanie



Perry de Valpine

unread,
Apr 28, 2023, 11:18:38 AM4/28/23
to Stephanie Cunningham, nimble-users
Hi Stephanie,
Great to see this and thanks for the question.
All model quantities are doubles, even if in fact their values will be integers. For that reason, please try using "x = double()".
Also, if x is large, it will be better to use lgamma(x + 1) instead of log(factoria(x)).
HTH!
Perry


--
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/4ccd02f9-d226-4fb5-82cf-81bc606fe542n%40googlegroups.com.

Stephanie Cunningham

unread,
Apr 28, 2023, 3:41:46 PM4/28/23
to nimble-users
Hi Perry,

Thank you for the quick response! I've changed all of the values to be doubles, but am still getting the same error. I am wondering if perhaps it is related to trying to call an R function as the `r` function for the distribution? 

dCOMP <- nimbleFunction(
  run = function(x=double(), lambda=double(0), nu=double(0), log=double(0, default=0)) {
    returnType(double(0))
    logProb <- (1 - nu) * (log(lambda) * x - lgamma(x + 1)) - lambda - log(sum(exp((1-nu)*(log(lambda)*x - lgamma(x + 1)) - lambda)))

    if (log) return(logProb)
    else return(exp(logProb))
    })

rCOMP<- nimbleRcall(
  function(n=double(1), lambda=double(0), nu=double(0)){},
  Rfun = 'rcmp',
  returnType=double())

Alternatively, when I've attempted to write out the `r` function like is shown in the Nimble manual (using nimbleFunction), I receive this error (when I run nimbleModel()):

[Warning] CheckDistributionFunctions: parameter arguments not the same amongst density and simulation functions for dCOMP. Continuing anyway based on arguments to the density function; algorithms using the simulation function are unlikely to function properly. Error in FUN(X[[i]], ...) : prepareDistributionInfo: types/dimensions of parameters are not the same in density and simulation functions: 'dCOMP' and 'rCOMP'.

Thanks,
Stephanie

Perry de Valpine

unread,
Apr 28, 2023, 4:11:57 PM4/28/23
to Stephanie Cunningham, nimble-users
In rCOMP, please try `n = double(0)` or `n = integer(0)`.  The "(1)" will make it a vector, which shouldn't be what you want.

However, the problem I see is that in sum(exp((1-nu)*(log(lambda)*x - log(factorial(x))) - lambda)), there isn't a vector being summed over. The inner expression is entirely scalar. That should not have caused a nimble glitch, but it did when I tried it, so that's one issue.  But also it made me look up the rcmp distribution and from the RCOMPoissonReg vignette, it looks like there needs to be some kind of summation to get the (normalizing constant of the) density correct? I am not very familiar with this, but it looks like maybe something is not right? If I remove the "sum" to get it to work, all the results can be seen to be 0.  In uncompiled execution (demoModel$calculate(); demoModel$logProb_x), we see the same thing.

Perry


Reply all
Reply to author
Forward
0 new messages