Grouped splines

148 views
Skip to first unread message

Kyle Manning

unread,
Nov 15, 2022, 6:38:55 PM11/15/22
to R-inla discussion group
Hi all, I was wondering about whether it is possible to use a "grouped spline", which I am imagining would be similar in setup to random slopes where we use 

... + f(index_var, continuous_var, ...)

I was hoping this would work as with other spline models, but using

... + f(index_var, continuous_var, model="rw2") 

only gives a random effect for the index variable. is there a way to do this in INLA?

alternatively, perhaps I could I create separate columns with different segments set to NA to simulate this?

Finn Lindgren

unread,
Nov 15, 2022, 7:40:34 PM11/15/22
to Kyle Manning, R-inla discussion group
The f(value, weights, …) syntax means
  Effect[i] = weights[i] * latent(value[i])
In the model=“iid” case that gives you “random slopes”, and in the rw2 case you get “splines” (really an integrated random walk Gaussian process, which isn’t exactly the same thing as a spline, which specifically refers to the corresponding penalty minimiser) multiplied by the weighs, sometimes referred to as varying coefficient models.

It sounds like you’re looking for a model with a rw2 process of one variable, replicated across another, index, variable?
Then you want
f(continuous_var, model=“rw2”, replicate = index)
?
If you spell out the model you want in equations it should be easier to figure out precisely what model you want; there is no universal terminology across all application fields, and the same vague description based on code examples can refer to very different actual models, so describing them in unambiguous maths is often necessary.

Finn

On 15 Nov 2022, at 23:38, Kyle Manning <manning...@gmail.com> wrote:

Hi all, I was wondering about whether it is possible to use a "grouped spline", which I am imagining would be similar in setup to random slopes where we use 
--
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/97fcc442-d54e-4518-8112-3c6226ea8aadn%40googlegroups.com.

Tim Meehan

unread,
Dec 9, 2022, 6:09:39 PM12/9/22
to R-inla discussion group
Check out the code below. If you are looking for something analogous to the "by" 
argument in mgcv, try the "replicate" argument to inla that Finn mentioned. If you 
are looking for something more like "fs" smooths in mgcv, try the "group"
argument in INLA. In both cases you get similar results with the two methods, 
despite being quite different under the hood.

Best,
Tim

# libs
library(mgcv)
library(INLA)
library(ggplot2)
library(dplyr)

# data
d1 <- gamSim(eg=4, n=400, dist="normal", scale=2) %>%
  select(x2, fac, y) %>%
  mutate(x2_group=inla.group(x2), fac_idx=as.integer(fac)) %>%
  ungroup()

# gam with by
g1 <- gam(y ~ 1 + fac + s(x2, by=fac), data=d1)

# inla with replicate
i1 <- inla(y ~ 1 + fac + f(x2_group, model="rw2", replicate=fac_idx), data=d1)

# plot them, gam is dashed line
ggplot() +
  geom_line(data=data.frame(fac=d1$fac, x2=d1$x2, pg=predict(g1)),
             aes(x=x2, y=pg, col=fac), lty=2, lwd=1) +
  geom_line(data=data.frame(fac=d1$fac, x2=d1$x2_group,
                        pi=i1$summary.fitted$mean),
                        aes(x=x2, y=pi, col=fac), lwd=1)

# gam with fs
g2 <- gam(y ~ fac + s(x2, fac, bs="fs"), data=d1)

# inla with group
i2 <- inla(y ~ fac + f(x2_group, model="rw2", group=fac_idx,
                 control.group=list(model="iid")), data=d1)

# plot them, gam is dashed line
ggplot() +
  geom_line(data=data.frame(fac=d1$fac, x2=d1$x2, pg=predict(g2)),
            aes(x=x2, y=pg, col=fac), lty=2, lwd=1) +
  geom_line(data=data.frame(fac=d1$fac, x2=d1$x2_group,
                            pi=i2$summary.fitted$mean),
            aes(x=x2, y=pi, col=fac), lwd=1)

Sylvan Benaksas

unread,
Dec 15, 2022, 10:39:11 AM12/15/22
to R-inla discussion group
Hi Tim,

How could this rw2 effect work when you are also fitting a prediction stack?

I tried this a long time ago but gave up.

All the Best,
Sylvan

Finn Lindgren

unread,
Dec 15, 2022, 10:52:36 AM12/15/22
to Sylvan Benaksas, R-inla discussion group
Hi Sylvan,

It depends on what data is missing. In order for rw2 prediction to make sense, you need at least some observations within each replicate (and this isn’t related to the inla method, but rather a basic property of any replicated intrinsic stationary model). A prediction stack is just a fancy way of specifying that one wants predictions for some specific missing values.

Finn

On 15 Dec 2022, at 15:39, Sylvan Benaksas <sylva...@gmail.com> wrote:

Hi Tim,
Reply all
Reply to author
Forward
0 new messages