# Smoothing (GAM) using brms

**URL:** <https://discourse.mc-stan.org/t/smoothing-gam-using-brms/10137>\
**Category:** brms\
**Created:** [August 11, 2019, 2:07am UTC](https://discourse.mc-stan.org/t/smoothing-gam-using-brms/10137 "2019-08-11T02:07:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![belay](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/belay/32/4288_2.png) [@belay](https://discourse.mc-stan.org/u/belay)\
**Post date:** [August 11, 2019, 2:07am UTC](https://discourse.mc-stan.org/t/smoothing-gam-using-brms/10137/1 "2019-08-11T02:07:56Z")

</div>

[part of the result.txt](https://discourse.mc-stan.org/uploads/short-url/qvSU7Ip1TmQfdPxeA3ooXtANywX.txt) (1.3 KB) Please also provide the following information in addition to your question:

- brms Version:2.9.0

Hi @paul.buerkner et al,

I was trying to fit a zero-inflated semiparametric model using thinplate spline for a longitudinal data. Below is the code with simulated data. When I request for marginal\_smooths (fit), the dimension as well as values of the time effect (where the spline is applied) is different from the input. The input time effect is a length of 30 equally spaced values between 0 and 1 while the returned one are a length of 100 equally spaced values between 0 and 1. Any help?

```
library(brms)
#=====================================================================================
# Generate data
#=====================================================================================
rm(list=ls())
nyy<-30
n.sub<-100
ti<-seq(from=0,to=1,length=nyy)
# treatment indicator
nn<-n.sub/2
z<-c(rep(0,nn),rep(1,nn))

Liner_comb_zero<-p_zero<-data_HPN<-data_ZIHPN<-matrix(0,n.sub,nyy)
beta<-c(1.7,0.7)
sig_cluster<-0.5
sig_zro<-0.5
#p_zero<-0.2
tru_fun<-(1/3)*dbeta(ti, 20, 5)+(1/3)*dbeta(ti, 12, 12)+(1/3)*dbeta(ti, 7, 30)

#plot(ti, exp(1+0.5+tru_fun), type="l")

a.i1<-rnorm(n.sub,0,sig_cluster)
a.i3<-rnorm(n.sub,0,sig_zro)

for(i in 1:n.sub)
{
  Liner_comb_zero[i,]<-1.8+0.7*z[i]+a.i3[i]
  p_zero[i,]<-1/(1+exp(Liner_comb_zero[i,]))
  data_HPN[i,]<-rpois(nyy,lambda =exp(beta[1]+beta[2]*z[i]+tru_fun+a.i1[i])) 
  data_ZIHPN<-data_HPN; data_ZIHPN[i,rbinom(nyy, size=1, prob=p_zero[i,]) == 1 ] = 0
  
}

#=====================================================================================
# Prapare data in longitudinal Data format
#=====================================================================================

y4<-c(data_ZIHPN[1,]) # responce
for (i in 2:n.sub){y4=c(y4,data_ZIHPN[i,])}

time<-c(rep(ti,n.sub))
group<-rep(z,each=nyy)
id<-rep(1:n.sub,each=nyy)
data_sim<-data.frame(id=id, time=time,group,y4)
data_sim$group<-factor(data_sim$group)

# fit brms models 
ZIP<-brm(bf(y4 ~ 1 + group+ s(time,bs="tp") + (1| id), 
            zi ~ 1 + group + (1| id)), family = zero_inflated_poisson(),
         prior = c( set_prior("normal(0, 1)", class = "b"), set_prior("cauchy(0, 2)", class = "sd")),chains,data = data_sim)

```

```

```

---

<div class="post-metadata">

**Author:** ![paul.buerkner](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul.buerkner/32/3303_2.png) [@paul.buerkner](https://discourse.mc-stan.org/u/paul.buerkner)\
**Post date:** [August 12, 2019, 6:44am UTC](https://discourse.mc-stan.org/t/smoothing-gam-using-brms/10137/2 "2019-08-12T06:44:01Z")

</div>

brms uses 100 observations for continuous predictors to make the plot appear smooth. What’s the problem with this approach?

You can manually obtain predictions by using argument newdata in the fitted and predict methods.

---

<div class="post-metadata">

**Author:** ![belay](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/belay/32/4288_2.png) [@belay](https://discourse.mc-stan.org/u/belay)\
**Post date:** [August 12, 2019, 8:17am UTC](https://discourse.mc-stan.org/t/smoothing-gam-using-brms/10137/3 "2019-08-12T08:17:51Z")

</div>

Thank you Paul! Yes, I can do the prediction with newdata.
