compileNimble() Error: Problem with type of arg1 in sizeBinaryCwise

7 views
Skip to first unread message

Abigail Keller

unread,
May 29, 2023, 5:07:31 PM5/29/23
to nimble-users
Hi,

I'm trying to write a model with a vectorized normal distribution, and I'm getting the following error after compileNimble(): "Error: Problem with type of arg1 in sizeBinaryCwise." 

The error doesn't occur when I use dnorm() with a for loop, so I'm assuming the error is related to the following functions and parts of the model code:

#function for vectorized normal distribution -- all means = 0
dnorm_vector <- nimbleFunction(
  run = function( x = double(1),
                  sd = double(1),
                  nsite = double(0),
                  log = integer(0, default = 0)
  ) {
    returnType(double(0))
    logProb <- sum(dnorm(x, mean = rep(0,nsite), sd = sd, log = TRUE))
    if(log) return(logProb) else return(exp(logProb))
  })

#row sums function
rowsum <- nimbleFunction(
  run = function( matrix = double(2),
                  nsite = double(0))
  {
    returnType(double(1))
    out <- rep(NA,nsite)
    for(i in 1:nsite){
      out[i] <- sum(matrix[i,])
    }
    return(out)
  }
)

model_code <- nimbleCode({

   #process error
    for(t in 1:ntime){
     proc_error[t,1:nsite] ~ dnorm_vector(sd=rowsum(N[t,1:nsite,1:nsize], nsite) * proc_CV, nsite=nsite)

}
})


Does anyone have any suggestions for what could be causing this error during C++ compilation?

Thanks!
Abby K.

Daniel Turek

unread,
May 29, 2023, 5:18:08 PM5/29/23
to Abigail Keller, nimble-users
Abigail, thanks for the question.  I haven't tried this yet, I'll sketch it on the fly, but I think you'll have to add up the result of nsite calls to dnorm(), each with scalar x and sd arguments.  See modified code below, and forgive any possible typos:

dnorm_vector <- nimbleFunction(
  run = function( x = double(1),
                  sd = double(1),
                  nsite = double(0),
                  log = integer(0, default = 0)
  ) {
    returnType(double(0))
    logProb <- 0   ##sum(dnorm(x, mean = rep(0,nsite), sd = sd, log = TRUE))
    for(i in 1:nsite) {
        logProb <- logProb + dnorm(x[i], 0, sd = sd[i], log = TRUE)
    }
    if(log) return(logProb) else return(exp(logProb))
  })


Let me know if this works for you.

Cheers,
Daniel


--
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/6d43ed04-d298-4151-b151-d2a273c2c5b8n%40googlegroups.com.

Perry de Valpine

unread,
May 29, 2023, 7:37:20 PM5/29/23
to Daniel Turek, Abigail Keller, nimble-users
Abby,
To follow up on Daniel's response, when I use a name different from "rowsum", it works on my machine. I think the mechanism to find rowsum and attempt to access its types if it is a nimbleFunction is failing because it is finding the base R function rowsum. This is something we'll have to look into, but in the meantime I'd suggest using a different name as a workaround.
HTH!
Perry
 

Abigail Keller

unread,
May 30, 2023, 1:15:42 PM5/30/23
to Perry de Valpine, Daniel Turek, nimble-users
Thanks so much to you both for such quick responses! I really appreciate it.

Daniel -- is there a benefit to looping through with scalar arguments, as opposed to using a vector in the function? I had been drawing from this example, but I see that it is a few years old.

Perry -- thanks! That helped. I also hadn't registered the distributions (some of my other custom distributions are discrete), so I think that was also causing problems.

Thanks so much again.
Abby
--
Abby Keller
PhD Student
Environmental Science, Policy, and Management
(she/her)

Daniel Turek

unread,
May 30, 2023, 1:59:18 PM5/30/23
to Abigail Keller, Perry de Valpine, nimble-users
No, I don't think it would make any difference in compiled execution, and my apologies for the red herring.
Reply all
Reply to author
Forward
0 new messages