I wanted to use predict
on some averaged models which were built in nlme
to plot confidence intervals of modelled relationships. But, I've found this is not possible using nlme
and MuMIn::model.avg
. Instead, I plan to use glmmTMB
, as suggested here. However, I'm struggling to work out how to set the correlation structure in glmmTMB
.
The following is a small subset of my data, and the model specification in nlme
. The data are an incomplete time series, and the random structure is the test position in a sequence for a given ID, nested within ID.
library(nlme)
library(glmmTMB)
mydata <- structure(list(id = c("F530", "F530", "F530", "F530", "F530", "M391", "M391", "M391", "M391", "M391", "M391", "M391"),testforid = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("1", "2"), class = c("ordered", "factor")), time = c(12.043, 60.308, 156.439, 900.427, 1844.542, 42.095, 61.028, 130.627, 194.893, 238.893, 905.282, 1859.534), a = c(35.5786398928957, 35.4973671656257, 36.7414694383557, 37.4316029157078, 36.0805603474457, 38.892219234833, 37.081136308003, 37.339272893363, 36.744902161663, 36.741897283613, 38.158072893363, 38.946697283613), b = c(0.0079975108148372, 0.0151689857479705, 0.0275942757878888, 0.0125676102827941, 0.0352227834243443, 0.0195902976534779, 0.0118588484445401, 0.0069799148425349, 0.00723445099500534, 0.00787758751826021, 0.0162518412492866, 0.0127526068249484), c = c(1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0)), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L), class = "data.frame")
model.lme <- lme(a ~ b + c,
random = list(id = ~1, testforid = ~1),
correlation = corExp(metric = "maximum", nugget = TRUE),
method = "ML",
data = mydata)
I tried following the instructions in this vignette, converting time to a factor with unit spaced time points as levels (in this case milliseconds), and setting a single grouping factor:
mydata$times <- factor(mydata$time,
levels = seq(from = min(mydata$time),
to = max(mydata$time),
by = 0.001))
mydata$group <- 1
I then guessed at my model structure being (not sure this is correct):
model.glmmTMB <- glmmTMB(a ~ b + c + exp(times + 0 | group) + (1|id/testforid), data = mydata)
And got the following error:
Error in parseNumLevels(reTrms$cnms[[i]]) :
Failed to parse numeric levels: times12.043times42.095times60.308times61.028times130.627times156.439times194.893times238.893times900.427times905.282times1844.542times1859.534
In addition: There were 12 warnings (use warnings() to see them)
> warnings()
Warning messages:
1: In lapply(strsplit(tmp, ","), as.numeric) : NAs introduced by coercion
My guess is the problem is that the time series is incomplete, but I'm not sure.
Any thoughts/suggestions on if/how I can properly convert the model from nlme
to glmmTMB
, or failing that on how I might bootstrap confidence intervals from averaged nlme
models (averaged using MuMIn::model.avg
) would be greatly appreciated. Thanks!