Smoothing (GAM) using brms

part of the result.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)

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.

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