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)