error with inlabru AR1 model with group

204 views
Skip to first unread message

Andrew Seaton

unread,
Nov 26, 2018, 10:11:05 AM11/26/18
to R-inla discussion group
Hi

I have a question about the inlabru package which I hope is covered by this mailing list as people may search here for inlabru related questions.
but if not I can repost as a github issue.

I'm encountering an error I can't resolve with the inlabru package and AR1 processes.

I define an AR1 model with a group argument:

fml <- y ~ Intercept + myar1(map = t, model = "ar1", group = ID, n = n)
fit <- bru(fml, family = "poisson", data = df_groups)

This returns the following error:

Error in INLA::f(xxx, ..., group = group, model = model) : 
  object 'ID' not found

Code is below to reproduce the error,  I define the model the usual INLA way which works and then with inlabru.
It works when no grouping, but fails when I include a group.
-----------------------


library(INLA)
library(inlabru)

# Simulate data - 3 groups with same AR1 param
n = 100
rho <- 0.8
x1 = arima.sim(n=n, model=list(ar=c(rho)))
x2 = arima.sim(n=n, model=list(ar=c(rho)))
x3 = arima.sim(n=n, model=list(ar=c(rho)))

# Poisson observations
y1 = rpois(n, lambda = exp(x1))
y2 = rpois(n, lambda = exp(x2))
y3 = rpois(n, lambda = exp(x3))

# First fit just to y1 with no groups, this works:
# ----------------------------------------------------------
df2 <- data.frame(y = y1, t = 1:n)

# Fit with INLA
fml <- y ~ f(t, model = "ar1")
result = inla(fml, family = "poisson", data = df2)
summary(result)

# Fit with inlabru
fml <- y ~ Intercept + myar1(map = t, model = "ar1", n = n) # now need to specify n, not sure why
fit <- bru(fml, family = "poisson", data = df2)
summary(fit)

# Now with groups, this works in INLA but not inlabru:
# ----------------------------------------------------------------------

df_groups <- data.frame(y = c(y1, y2, y3),
                 t = rep(1:n, 3),
                 ID = c(rep(1, n), rep(2, n), rep(3,n)))

# INLA
fml = y ~ f(t, model = "ar1", group = ID)
result = inla(fml, family = "poisson", data = df_groups)
summary(result)

# inlabru
fml <- y ~ Intercept + myar1(map = t, model = "ar1", group = ID, n = n)    # n = n within group or n total?
fit <- bru(fml, family = "poisson", data = df_groups)


I'm not sure why I need the n argument in inlabru or how to specify it when grouping, but the error occurs with no n, n = n_group, n = n_total

Andy
df_groups <- data.frame(y = c(y1, y2, y3),
t = rep(1:n, 3),
ID = c(rep(1, n), rep(2, n), rep(3,n)))

# INLA
fml = y ~ f(t, model = "ar1", group = ID)
result = inla(fml, family = "poisson", data = df_groups)
summary(result)

# inlabru
fml <- y ~ Intercept + myar1(map = t, model = "ar1", group = ID, n = n)
fit <- bru(fml, family = "poisson", data = df_groups)

Finn Lindgren

unread,
Nov 26, 2018, 10:15:29 AM11/26/18
to Andrew Seaton, R-inla discussion group
Hi,
Just as in plain inla, you need to specify the group model via control.group.
Your code just specifies that the basic model should be ar1 and doesn’t say what it should be “grouped” with.

Finn
--
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 post to this group, send email to r-inla-disc...@googlegroups.com.
Visit this group at https://groups.google.com/group/r-inla-discussion-group.
For more options, visit https://groups.google.com/d/optout.

Andrew Seaton

unread,
Nov 27, 2018, 10:09:46 AM11/27/18
to R-inla discussion group
Hi Finn,

Thanks for the quick response, I think I was confused about the difference between group and replicate.  I want three independent sets of observations of a single AR1 process.

Now hitting a different error.  The INLA model I want to fit with inlabru is:

# INLA
fml = y ~ f(t, model = "ar1", replicate = r )
result = inla(fml, family = "poisson", data = df_groups)

I'm trying to do this in inlabru as:

# inlabru
fml <- y ~ Intercept + myar1(map = t, model = "ar1", replicate = r, n = n)

fit <- bru(fml, family = "poisson", data = df_groups)


And getting the following error message:

Error in (function (data, model, stackmaker, n = 10, result = NULL, family,  :
  INLA returned message: length(replicate) != length(xx) 300 != 401
In addition: Warning message:
In (function (formula, family = "gaussian", contrasts = NULL, data,  :
  length(replicate) != length(xx) 300 != 401


Code for this example is below.
I tweaked the replicate example from the R-INLA website, it's almost identical except a Poisson likelihood and no difference in mean between replicates:

# Simulate data - 3 groups with same AR1 param
n = 100
rho <- 0.8
x1 = arima.sim(n=n, model=list(ar=c(rho)))
x2 = arima.sim(n=n, model=list(ar=c(rho)))
x3 = arima.sim(n=n, model=list(ar=c(rho)))

# Poisson observations
y1 = rpois(n, lambda = exp(x1))
y2 = rpois(n, lambda = exp(x2))
y3 = rpois(n, lambda = exp(x3))

df_groups <- data.frame(y = c(y1, y2, y3),
                        t = rep(1:n, 3))
r <- rep(1:3, each = n)

# INLA
fml = y ~ f(t, model = "ar1", replicate = r )

result = inla(fml, family = "poisson", data = df_groups)
summary(result)

# inlabru
fml <- y ~ Intercept + myar1(map = t, model = "ar1", replicate = r, n = n)

fit <- bru(fml, family = "poisson", data = df_groups)



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

Finn Lindgren

unread,
Nov 27, 2018, 10:13:23 AM11/27/18
to Andrew Seaton, R-inla discussion group
Sadly, I think the replicate features wasn’t implemented in the released version. Please try the backend branch if you don’t want to wait...

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

Andrew Seaton

unread,
Nov 28, 2018, 6:54:45 AM11/28/18
to R-inla discussion group
Hi Finn,

I tried the backend branch devtools::install_github("fbachl/inlabru@backend")

Now getting a different message:

> fit <- bru(fml, family = "poisson", data = df_groups)
 Error in effect.character("myar1", map = t, model = "ar1", replicate = r,  :
  Effect 'myar1' (type 'ar1') requires argument 'values'. Check out f() for additional information on this argument.

Here is the traceback:
11.
stop(sprintf(miss.msg, effect$label, model.type, "values"))
10.
effect.character("myar1", map = t, model = "ar1", replicate = r,
    n = n)
9.
effect("myar1", map = t, model = "ar1", replicate = r, n = n) at <text>#1
8.
eval(x, envir = environment(formula))
7.
eval(x, envir = environment(formula))
6.
FUN(X[[i]], ...)
5.
lapply(parsed, function(x) eval(x, envir = environment(formula)))
4.
effect.formula(components)
3.
effect(components)
2.
make.model(components)
1.

bru(fml, family = "poisson", data = df_groups)


From the help file for f():

values   
An optional vector giving all values assumed by the covariate for which we want estimated the effect. It must be a numeric vector, a vector of factors or NULL.

I'm not sure what this means although it should default to NULL.  Maybe when the internal inlabru g() is converted to f() this does not happen?

Andy

Finn Lindgren

unread,
Nov 28, 2018, 6:59:23 AM11/28/18
to Andrew Seaton, R-inla discussion group
Hi Andy,
Inlabru is more picky about these things than inla, since it needs to know how to map between the input and output, so for now you have to specify more things explicitly instead of relying on inla investing something for you. Of course, you can try to supply values=NULlL, but inlabru will likely complain that you didn’t supply anything useful.

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

Andrew Seaton

unread,
Dec 12, 2018, 4:21:56 AM12/12/18
to R-inla discussion group
Hi Finn,

Hope your computer is back up and running!  It would be great if we could pick up on this replicate topic again.

I'm trying to get this simple ar1 example working.  Setting values = NULL receives the following error:

Error in INLA::inla.mesh.1d(sort(unique(fvals$values))) : 
  All 'loc' must be >= interval[1].
In addition: Warning messages:
1: In is.na(x) : is.na() applied to non-(list or vector) of type 'NULL'
2: In min(x, na.rm = na.rm) :
  no non-missing arguments to min; returning Inf
3: In max(x, na.rm = na.rm) :
  no non-missing arguments to max; returning -Inf

I'm not sure what the values argument is usually used for.  
I'm working with inlabru backend branch and INLA version ............: 18.06.19  which is what is installed after running install.packages("INLA", repos="https://inla.r-inla-download.org/R/testing")

I note that effect.character() documentation lists various arguments as experimental (such as group argument) but replicate is not included in that list.

Full code to get the error:

library(inlabru)
library(INLA)
 
# Simulate data - 3 groups with same AR1 param
n = 100
rho <- 0.8
x1 = arima.sim(n=n, model=list(ar=c(rho)))
x2 = arima.sim(n=n, model=list(ar=c(rho)))
x3 = arima.sim(n=n, model=list(ar=c(rho)))

# Poisson observations
y1 = rpois(n, lambda = exp(x1))
y2 = rpois(n, lambda = exp(x2))
y3 = rpois(n, lambda = exp(x3))

df_groups <- data.frame(y = c(y1, y2, y3),
                        t = rep(1:n, 3))
r <- rep(1:3, each = n)

# INLA
fml = y ~ f(t, model = "ar1", replicate = r)
result = inla(fml, family = "poisson", data = df_groups)
summary(result)

# inlabru
fml <- y ~ Intercept + myar1(map = t, model = "ar1", replicate = r, n = n, values = NULL)

fit <- bru(fml, family = "poisson", data = df_groups)

Andy

Andrew Seaton

unread,
Dec 12, 2018, 7:31:12 AM12/12/18
to R-inla discussion group
Moving this conversation to github issue:

Minho Shin

unread,
Mar 5, 2020, 12:16:34 AM3/5/20
to R-inla discussion group
Hi, Finn.

I think I am currently getting the same error as what Andrew got when using 'replicate' feature for inlabru version 2.1.12.
Is there any update so far?

Minho

2018년 11월 28일 수요일 오전 12시 13분 23초 UTC+9, Finn Lindgren 님의 말:
Sadly, I think the replicate features wasn’t implemented in the released version. Please try the backend branch if you don’t want to wait...

Finn

Finn Lindgren

unread,
Mar 5, 2020, 5:15:54 PM3/5/20
to R-inla discussion group
Hi,
no, full support for replicate&group turned out to be a bit more
complex than anticipated, and not easily fixed without the new backend
code that has been delayed by other duties. A new version that aims to
support a much more complete INLA feature set is underway, with
tentative release this summer. Sorry for the delay.

Finn
>>> To unsubscribe from this group and stop receiving emails from it, send an email to r-inla-discussion...@googlegroups.com.
>>> To post to this group, send email to r-inla-disc...@googlegroups.com.
>>> Visit this group at https://groups.google.com/group/r-inla-discussion-group.
>>> For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> 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 post to this group, send email to r-inla-disc...@googlegroups.com.
>> Visit this group at https://groups.google.com/group/r-inla-discussion-group.
>> For more options, visit https://groups.google.com/d/optout.
>
> --
> 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 on the web, visit https://groups.google.com/d/msgid/r-inla-discussion-group/f79f43b1-d0e1-4297-8961-28eebc427a31%40googlegroups.com.



--
Finn Lindgren
email: finn.l...@gmail.com

Minho Shin

unread,
Mar 5, 2020, 10:11:39 PM3/5/20
to R-inla discussion group
Dear Finn,

Always thanks for the quick response.
Meanwhile, I will try implementing my model in INLA then.

I look forward to further update in inlabru!
Thank you for developing the awesome package.

Minho


2020년 3월 6일 금요일 오전 7시 15분 54초 UTC+9, Finn Lindgren 님의 말:
>>> To unsubscribe from this group and stop receiving emails from it, send an email to r-inla-discussion-group+unsub...@googlegroups.com.
>>> To post to this group, send email to r-inla-disc...@googlegroups.com.
>>> Visit this group at https://groups.google.com/group/r-inla-discussion-group.
>>> For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> 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-group+unsub...@googlegroups.com.
>> To post to this group, send email to r-inla-disc...@googlegroups.com.
>> Visit this group at https://groups.google.com/group/r-inla-discussion-group.
>> For more options, visit https://groups.google.com/d/optout.
>
> --
> 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-group+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages