I would like do create a mixed linear model for an unbalanced dataset (different number of events per subject and a few missing values for some time points). I am using R version 3.2.1 (2015-06-18)
, package: nlme_3.1-120
.
Here comes simulated data:
library(nlme)
set.seed(1)
subject <- factor(rep(c(1, 1, 2, 3, 4, 4, 4, 5, 6, 7, 7, 8, 9, 9, 10,
11, 11, 11, 12, 13), 10))
event <- factor(rep(1:20, 10))
timepoint <- rep(1:10, each = 20)
measure <- rnorm(length(timepoint)) + timepoint*0.3
timepoint <- factor(timepoint)
measure[sample(1:length(measure), rpois(5,4))] <- NA
data <- data.frame(subject=subject, event=event, timepoint=timepoint,
measure=measure)
str(data)
The model should predict the variable “measure” over different time points as fixed effect and for subjects and events as random effects.
base <- lme(measure ~ 1, data=data, random= ~ 1|subject,
na.action=na.exclude, method="ML")
intercept <- lme(measure ~ timepoint, data=data, random= ~ 1|subject,
na.action=na.exclude, method="ML")
nested <- lme(measure ~ timepoint, data=data, random= ~ 1|subject/event,
na.action=na.exclude, method="ML")
anova(base, intercept, nested)
I would like to fit random intercept and slope, because intercept and slope can vary among subjects and events. However when I add the random slope effect, the model does not converge. It does not through any error message, but it runs to infinity. What can I do create a model with random slope that converges?
cave model runs endless
slope <- lme(measure ~ timepoint, data=data, random= ~ timepoint|subject,
na.action=na.exclude, method="ML")
I tried also this
cave model runs endless
slope2 <- lme(measure ~ timepoint, data=data, random= ~ timepoint|subject,
na.action=na.exclude, method="ML", control=list(opt="optim"))
cave some models may run endless
slope3 <- lme(measure ~ timepoint, data=data, random= ~ timepoint|subject/event,
na.action=na.exclude, method="ML", control = list(opt="optim"))
covariance <- lme(measure ~ timepoint, data=data, random= ~ timepoint|subject,
correlation=corAR1(),na.action = na.exclude, method="ML")
covariance2 <- lme(measure ~ timepoint, data=data, random= ~ timepoint|subject,
correlation=corAR1(0), na.action=na.exclude, method="ML",
control=list(opt="optim"))
covariance3 <- lme(measure ~ timepoint, data=data, random= ~ timepoint|subject,
correlation=corAR1(0), na.action=na.exclude, method="ML",
control=list(maxlter=1000))
timepoint
as categorical. That means that fitting a term liketimepoint|subject
will require fitting a full 10x10 variance-covariance matrix (55 distinct parameters) across 13 subjects. It would be much more practical to fittimepoint
as a continuous covariate ... $\endgroup$