"NA's in argument 'Ntrials' are not allowed" ERROR

14 views
Skip to first unread message

Borizook

unread,
Nov 18, 2025, 3:31:18 AM (9 days ago) Nov 18
to R-inla discussion group
Hello, 

Thanks for the answer for all of questions.

I faced some error, which I dont understand why it happens.

I am buildling a joint likelihood model using "Poisson" and "Binomial" distirbution.

Regarding Ntrials, I have put 1 values for data stack for Poisson and associated values for data stack for Binomial. 
I have checked Ntrials has no NA value.

But, INLA says 

"NA's in argument 'Ntrials' are not allowed" 

Could you please see my code below?

Thanks a lot!

Best
Jun-Sik
----------------------------------------

### Data A for Poisson distribution

mydata = ssubset_db_pk_env_dem


num = mydata$num
st.group = mydata$mon
m.time = st.group %>% max


spde_spatial_index <- inla.spde.make.index("spatial",
                                           n.spde = spde$n.spde,
                                           n.group = m.time
)


coords = mydata[c("x", "y")] %>% as.matrix
A_inci <- inla.spde.make.A(mesh = mesh_new, loc = coords,  group = st.group )



stack_A <- inla.stack(
  data = list(y = cbind(num, NA),
              , Ntrials = cbind(rep(1, length(num)),1),
  A = list(A_inci, 1),
  effects = list(
    list(spatial_A = spde_spatial_index$spatial,
         spatial_A.group = spde_spatial_index$spatial.group),
    list(intercept_A = rep(1, length(num)))
  ),
  tag = "A"
)






### Data B for binomial distribution
st.group = mydata2$mon

n.group = mydata2 %>% pull(mon) %>% unique %>%length



coords = mydata2[c("X", "Y")] %>% as.matrix

A_sero <- inla.spde.make.A(mesh = mesh_new,
                           loc = coords,  
                           group = st.group)


num = mydata2 %>% pull(num)
tot_num = mydata2 %>% pull(tot_num)







stack_B <- inla.stack(
  data = list(y = cbind(NA, num), Ntrials = cbind(NA, tot_num)),
  A = list(A_sero, 1),
  effects = list(
    list(spatial_B = spde_spatial_index$spatial,
         spatial_B.group = spde_spatial_index$spatial.group),
    list(intercept_B = rep(1, length(num)))
  ),
  tag = "B"
)



formula = y ~ -1 +
  intercept_A +
  intercept_B +
  f(spatial_A, model=spde,
    group=spatial_A.group,
    control.group=list(model="ar1")) +
  f(spatial_B, copy = " spatial_A",
    hyper = list(beta = list(fixed = FALSE)),
    group = spatial_B.group  )

stack_AB= inla.stack(stack_A, stack_B)


dat <- inla.stack.data(stack_AB)


model_joint <- inla(
  formula,
  data    = dat,
  Ntrials = dat$Ntrials
  family  = c("poisson", "binomial"),
  control.predictor = list(A = inla.stack.A(stack_AB), compute = TRUE),
  control.compute   = list(
    dic  = TRUE,
    waic = TRUE,
    cpo  = TRUE,
    return.marginals.predictor = TRUE,
    config = TRUE
  ),
  control.inla = list(
    lincomb.derived.correlation.matrix = TRUE,
    strategy     = "gaussian",
    int.strategy = "eb"
  ),
  control.fixed = list(
    correlation.matrix = TRUE
  )
)

Håvard Rue

unread,
Nov 18, 2025, 3:34:40 AM (9 days ago) Nov 18
to Borizook, R-inla discussion group

can you make a simple example which I can rerun here?
> --
> You received this message because you are subscribed to the Google Groups "R-
> inla discussion group" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to r-inla-discussion...@googlegroups.com.
> To view this discussion, visit
> https://groups.google.com/d/msgid/r-inla-discussion-group/8a5f249b-5be4-4542-b436-b729f7d56d1dn%40googlegroups.com
> .

--
Håvard Rue
hr...@r-inla.org

Borizook

unread,
Nov 18, 2025, 3:39:25 AM (9 days ago) Nov 18
to R-inla discussion group

Here's sample code


library(INLA)
set.seed(1)

## 1. Simulate spatial locations and time index ---------------------------

n.loc  <- 30      # number of spatial locations
n.time <- 8       # number of time points

coords <- matrix(runif(2 * n.loc), ncol = 2)  

mesh <- inla.mesh.2d(
  loc      = coords,
  max.edge = c(0.2, 0.5)
)

spde <- inla.spde2.pcmatern(
  mesh        = mesh,
  prior.range = c(0.5, 0.01),  # P(range < 0.5) = 0.01
  prior.sigma = c(1.0, 0.01)   # P(sigma > 1)   = 0.01
)

df <- expand.grid(
  loc  = 1:n.loc,
  time = 1:n.time
)
df$x <- coords[df$loc, 1]
df$y <- coords[df$loc, 2]
n   <- nrow(df)



## 2. Simulate “true” latent effect and responses ------------------------

## For simplicity, simulate an arbitrary space-time effect (not from SPDE)
eta_st <- rnorm(n, sd = 0.5)

## Poisson outcome
beta_inci  <- -1
lambda     <- exp(beta_inci + eta_st)
y_pois     <- rpois(n, lambda)

## Binomial outcome, sharing (copying) part of the latent effect
m_binom      <- 20
beta_sero_0  <- -2
beta_copy    <- 0.7

linpred_sero <- beta_sero_0 + beta_copy * eta_st
p             <- plogis(linpred_sero)
y_binom       <- rbinom(n, size = m_binom, prob = p)

## 3. SPDE index and A matrix with group (time) --------------------------

group_time <- df$time

spde_index <- inla.spde.make.index(
  name    = "spatial",
  n.spde  = spde$n.spde,
  n.group = n.time
)

A_st <- inla.spde.make.A(
  mesh  = mesh,
  loc   = as.matrix(df[, c("x", "y")]),
  group = group_time
)

## 4. Build INLA stacks for both likelihoods -----------------------------

## Stack 1: Poisson (incidence)
stack_inci <- inla.stack(
  data = list(
    y       = cbind(y_pois, NA),              # Poisson, Binomial
    Ntrials = cbind(rep(1, n), 1)            # only relevant for Poisson
  ),
  A = list(A_st, 1),
  effects = list(
    list(
      spatial_inci       = spde_index$spatial,
      spatial_inci.group = spde_index$spatial.group
    ),
    list(
      intercept_inci = rep(1, n)
    )
  ),
  tag = "inci"
)

## Stack 2: Binomial (sero)
stack_sero <- inla.stack(
  data = list(
    y       = cbind(NA, y_binom),             # Poisson, Binomial
    Ntrials = cbind(1, rep(m_binom, n))      # only relevant for Binomial
  ),
  A = list(A_st, 1),
  effects = list(
    list(
      spatial_sero       = spde_index$spatial,
      spatial_sero.group = spde_index$spatial.group
    ),
    list(
      intercept_sero = rep(1, n)
    )
  ),
  tag = "sero"
)

## Combine stacks
stack_all <- inla.stack(stack_inci, stack_sero)
dat       <- inla.stack.data(stack_all)

## 5. Joint model: Poisson + Binomial with copy of SPDE field -----------

formula <- y ~ -1 +
  intercept_inci +
  intercept_sero +
  f(spatial_inci,
    model        = spde,
    group        = spatial_inci.group,
    control.group = list(model = "ar1")) +
  f(spatial_sero,
    copy  = "spatial_inci",                   # copy of the first f()
    group = spatial_sero.group,
    hyper = list(beta = list(fixed = FALSE))) # estimate copy coefficient

## 6. Fit model ----------------------------------------------------------


model_joint <- inla(
  formula,
  data    = dat,
  family  = c("poisson", "binomial"),
  Ntrials = dat$Ntrials,
  control.predictor = list(
    A       = inla.stack.A(stack_all),
    compute = TRUE
  ),

  control.compute = list(
    dic  = TRUE,
    waic = TRUE,
    cpo  = TRUE
  ),
  control.inla = list(

    strategy     = "gaussian",
    int.strategy = "eb"
  )
)

Finn Lindgren

unread,
Nov 18, 2025, 3:44:59 AM (9 days ago) Nov 18
to Borizook, R-inla discussion group
Ntrials should be a vector, not a matrix.
Just use
  Ntrials=rep(1, length(num))
In the inla.stack() call.

Finn

On 18 Nov 2025, at 08:39, Borizook <sgj...@gmail.com> wrote:


You received this message because you are subscribed to the Google Groups "R-inla discussion group" group.

To unsubscribe from this group and stop receiving emails from it, send an email to r-inla-discussion...@googlegroups.com.

Borizook

unread,
Nov 18, 2025, 8:13:37 PM (8 days ago) Nov 18
to R-inla discussion group
It works!
Thanks a lot


Beset
Jun-Sik

Reply all
Reply to author
Forward
0 new messages