Passing variable names into inla.posterior.sample.eval()?

14 views
Skip to first unread message

Bob O'Hara

unread,
Nov 7, 2025, 4:39:40 AMNov 7
to R-inla discussion group
I'm trying to write a function that will work with inla.posterior.sample.eval() where the variables are chosen within the function: I want to pass the names of the variables (or something that can be converted to them) into the function and then calculate some stats on those variables. I also want to use the function for different data sets with different variable names and numbers of variables.

A toy version of the problem is below (the real problem also has hyperparameters, which creates another issue I'll ignore for now).

Help?

Bob

set.seed(42)
N <- 100
Data <- data.frame(x1=rnorm(N), x2=rnorm(N), x3=rnorm(N), y=rnorm(N))

in1 <- inla(y ~ x1 + x2, data=Data, control.compute=list(config = TRUE))
in2 <- inla(y ~ x1 + x2 + x3, data=Data, control.compute=list(config = TRUE))

Samp1 <- inla.posterior.sample(n = 5, result=in1)
Samp2 <- inla.posterior.sample(n = 5, result=in2)

CalcStat <- function(beta) {
  betas <- paste0("x", beta)
  #  b1 <- get(betas[1]) # works, but not flexible enough
  #  b1 <- mget(betas) # gives errors, but parallelisation means I can't trace the error
  #  for(i in 1:length(betas)) {    b1[i] <- get(betas[i])  } # errors
  b1 <- sapply(betas, get) # works doesn't give 2 values
  c(sum(b1), b1)
}

Stat1 <- inla.posterior.sample.eval(CalcStat, Samp1, beta=1:2)
Stat1[,1] # gives 201 values, not 3 values

# this gives an error
Stat2 <- inla.posterior.sample.eval(CalcStat, Samp2, beta=1:3)

Helpdesk (Haavard Rue)

unread,
Nov 7, 2025, 5:53:06 AMNov 7
to Bob O'Hara, R-inla discussion group
something like this?


n <- 100
x <- rnorm(n)
xx <- rnorm(n)
xx <- rnorm(n)
xxx <- rnorm(n)
y <- rnorm(n)

r <- inla(y ~ 1 + x + xx + xxx,
data = data.frame(y, x, xx, xxx),
family = "stdnormal",
control.compute = list(config = TRUE))

do.sample <- function(n, r, nm) {
s <- inla.posterior.sample(n, r)
ss <- inla.posterior.sample.eval(nm, s)
rownames(ss) <- nm
return (ss)
}

do.sample(5, r, "x")
do.sample(5, r, c("x", "xx"))
do.sample(5, r, c("x", "xx", "xxx"))
> --
> 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/83cf474e-7b84-4185-a1f9-b2a93a04a1e2n%40googlegroups.com
> .

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

Finn Lindgren

unread,
Nov 7, 2025, 5:56:00 AMNov 7
to Bob O'Hara, R-inla discussion group
This will soon be much easier via the inlabru generate()/predict() system, as it will support tidy-eval features, such as “pronouns” for latent variables and effects, as well as making those available as data objects; example:
generate(…, ~ .effect.[[variable_containing_a_name]])
The code is in a feature branch at the moment, but will soon be merged into the main devel branch.
Finn

On 7 Nov 2025, at 09:39, Bob O'Hara <rni...@gmail.com> wrote:


--
You received this message because you are subscribed to the Google Groups "R-inla discussion group" group.
Reply all
Reply to author
Forward
0 new messages