I have a dataset which has repeated measures for each subject. And the linear-mixed effect model doesn't work well for the dataset. I want to know if there is any smoothing methods (e.g. splines) that works for longitudinal data such that I can find the function to fit the data?
2 Answers
A fundamental method that can be used with any regression model without extra code is that of B-splines.
In these methods, a single covariate is expanded into higher dimensions such that a piece wise polynomial with max degree - 1 continuous derivatives at the knots. Typical usage is a cubic spline, meaning the first and second derivative at the knots are continuous, but the 3rd derivative is allowed to change.
To help illustrate the parameter expansion, I will use an overly simplified example. Suppose we had a covariate in which we wished to fit a piece-wise linear fit with a single knot at 1 for a covariate $x$. Then we could expand $x$ into two new variables; $x^-$ and $x^+$. If $x_i < 1$, then we can set $x_i^- = x_i$ and $x_i^+ = 0$. If $x_i \ge 1$, we set $x_i^- = 1$ and $x_i^+ = x_i - 1$. (Note: this is actually a simplification of what's really done; transformations are performed on the expanded variables to lead to more numeric stability. See De Boor's algorithm if you're really interested). Note that the new fitted model will be continuous in $x$ but allows for different slopes above and below 1.
And most relevantly to this question, note that we have done nothing to the actual regression model itself (i.e. no adding penalties to the likelihood, etc.). So you can expand your parameter space and then plug directly into your regression model.
In R
, it's really easy to use B-splines; you just call bs(x,...)
in the formula for any regression model. But I suggest reading up on the help file to understand all the options.
Here's a simple example using linear regression. We could easily switch out lm
for any other regression model.
library(splines)
x <- sort( rnorm(1000) )
# sorting just for plotting purposes
y <- sin(x) + rnorm(1000, sd = 0.1)
bspl_fit <- lm(y ~ bs(x, df = 10))
plot(x, y)
lines(x, sin(x), col = 'blue', lwd = 2)
lines(x, predict(bspl_fit), col = 'red', lwd = 2)
legend('topleft', c('True', 'Spline Fit'),
lwd = 2, col = c('blue', 'red'))
-
$\begingroup$ Thank you for your reply. The B-splines will work for longitudinal data as well? $\endgroup$– J. ZhuFeb 12, 2017 at 16:21
-
$\begingroup$ @J.Zhu: B-splines will work with any regression model. So if you are using a regression model for longitudinal data (i.e. random intercept and random slopes) you can expand the covariate space and use B-splines. $\endgroup$– Cliff ABFeb 12, 2017 at 16:24
-
$\begingroup$ So if I write this in R using
lmer
, I just need to change my covariates to the basis I have? $\endgroup$– J. ZhuFeb 12, 2017 at 16:27 -
$\begingroup$ Great post on splines, bu seems OP's question is repeated measure and longitudinal data. $\endgroup$ Feb 12, 2017 at 16:38
-
$\begingroup$ @hxd1011: the OP asked for smoothing methods that can be applied to longitudinal models. B-splines can be applied to any regression model, including longitudinal/repeated measure regression models (i.e. mixed effects models). $\endgroup$– Cliff ABFeb 12, 2017 at 16:55
It is several years later, but the R package sme developed by Maurice Berk can do a very good job. https://rdrr.io/cran/sme/f/inst/doc/Tutorial.pdf
Also the initial.lambda.mu and initial.lambda.v values should be correctly selected for your data.
devtools::install_github("mberk/sme")
library(sme)
data(MTB)
fit <- sme(MTB[MTB$variable==6031,c("y","tme","ind")],criteria="AIC")
plot(fit,type="model",xlab="Hour",ylab="Gene Expression")
Also look over Maurice Berk thesis for additional information.
mgcv
r package (with usesnlme
) orgamm4
(which useslme4
): both allow mixed-effect spline models $\endgroup$?mgcv::gam
) $\endgroup$mgcv
package. If you read the help files you will find references to papers. It would be good to know more about what you man when you say "doesn't work well for the dataset", and why you have jumped to splines. $\endgroup$