[R-sig-ME] glmmTMB- fitting splines

Ben Bolker bbolker @ending from gm@il@com
Tue May 22 02:09:09 CEST 2018


  I don't know of an example offhand, but

https://stats.stackexchange.com/questions/301666/using-splines-in-r-lme4glmer-scale-issues

  gives an example of using splines::ns().  Basically, you can use ns()
as a drop-in term within a formula; unlike the magical s() function in
mgcv, you have to specify the number of knots/degrees of freedom
yourself (splines::ns fits regression splines, mgcv::s fits *penalized*
regression splines).

  Perhaps not known to everyone, mgcv can handle some forms of
zero-inflation (although I think it does ZIP but not ZINB), so
https://www.fromthebottomoftheheap.net/2017/05/04/compare-mgcv-with-glmmTMB/

might also be useful.

Here's an example. It is in principle possible to use
(ns(Days,5)|Subject) as the random effect (i.e. let curves vary among
individuals), but it didn't work in this case -- too complex for this
medium-size data set.

library(glmmTMB)
data(sleepstudy,package="lme4")

library(splines)
m1 <- glmmTMB(Reaction~ns(Days,5)+(1|Subject), data=sleepstudy)
sleepstudy$pred <- predict(m1)

library(ggplot2)
ggplot(sleepstudy,aes(x=Days))+geom_point(aes(y=Reaction))+geom_line(aes(y=pred,group=Subject))





On 2018-05-21 07:36 PM, dani wrote:
> Hello everyone,
> 
> 
> I am working with a glmmTMB model with two random effects. Some of my
> covariates have non-parametric associations with my dependent
> variable so I would like to fit splines for them. I am not sure how
> my code should look like.
> 
> 
> Could someone point me towards an example using glmmTMB with splines?
> I am not really sure how to interpret such a model.
> 
> 
> Thanks!
> 
> Best regards,
> 
> Dani
> 
> 
> 
> <http://aka.ms/weboutlook>
> 
> [[alternative HTML version deleted]]
> 
> _______________________________________________ 
> R-sig-mixed-models at r-project.org mailing list 
> https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models
>



More information about the R-sig-mixed-models mailing list