19
$\begingroup$

Say we're working on a random effects model of some count data over time, and we want to control for some trends. Normally, you'd do something like:

lmer(counts ~ dependent_variable + (1+t+I(t^2)|ID), family="poisson")

to include a quadratic shape for t. Is it possible to use some more sophisticated smoothing techniques like a LOESS smoother or splines to model that relationship?

$\endgroup$
25
$\begingroup$

If what you show works for a lmer formula for a random effects term then you should be able to use functions from the splines package that comes with R to set up the relevant basis functions.

require("lme4")
require("splines")
lmer(counts ~ dependent_variable + (bs(t) | ID), family="poisson")

Depending on what you want to do, you should also look at the gamm4 package and the mgcv package. The former is essentially formalising the bs() bit in the lmer() call above and allows smoothness selection to be performed as part of the analysis. The latter with function gam() allows for some degree of flexibility in fitting models like this (if I understand what you are trying to do). It looks like you want separate trends within ID? A more fixed effects approach would be something like:

gam(counts ~ dependent_variable + ID + s(t, by = ID) , family="poisson")

Random effects can be included in gam() models using the s(foo, bs = "re") type terms where foo would be ID in your example. Whether it makes sense to combine the by term idea with a random effect is something to think about and not something I am qualified to comment on.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.