The data are from here
library(nlme)
dat0 <- read.table("aids.dat2",head=T)
dat1 <- dat0[dat0$day<=90, ] # use only first 90-day data
dat2 <- dat1[!apply(is.na(dat1),1,any),] # remove missing data
# Next, let's treat the data as longitudinal (or grouped) data
aids.dat <- groupedData(lgcopy ~ day | patid, data=dat2)
# A NLME model fit, with random effects on all 4 parameters
start <- c(10,0.5,6,0.005) # starting value
aids.dat$log10copy = log10(aids.dat$lgcopy)
nlme.fit <- nlme(log10copy ~ exp(p1-b1*day) + exp(p2-b2*day + 1),
fixed = list(p1 ~ 1, b1 ~ 1, p2 ~ 1, b2 ~ 1),
random = list(patid = pdDiag(list(p1 ~ 1, b1 ~ 1, p2 ~ 1, b2 ~ 1))),
data =aids.dat, start=c(start))
summary(nlme.fit)
Here I fit a nonlinear mixed effects model using nlme
in the nlme
package. The model has 4 fixed effects and 4 random effects. I specified a diagonal structure on the variance-covariance matrix, and each patid
forms a group.
library(lme4)
deriv_mod <- deriv( ~ exp(p1 - b1*t) + exp(p2 - b2*t + 1),
c("p1", "b1", "p2", "b2"), function(t, p1, b1, p2, b2){})
nlmer.fit <- nlmer(deriv_mod ~ list(p1 ~ 1, b1 ~ 1, p2 ~ 1, b2 ~ 1) +
list(p1 ~ 1, b1 ~ 1, p2 ~ 1, b2 ~ 1), data = aids.dat, start = c(start))
Here, I would like to fit the same model using the lme4
package. From the documentation it seems that the formula
for nlmer
must also have a gradient component, thus I used the deriv
function first. However, I am not sure how to specify the rest of the parameters? The
deriv_mod ~ list(p1 ~ 1, b1 ~ 1, p2 ~ 1, b2 ~ 1) +
list(p1 ~ 1, b1 ~ 1, p2 ~ 1, b2 ~ 1)
is to specify 4 fixed effects (in the first list object) and their corresponding 4 random effects (in the second list object). However, I am not quite sure how to specify a diagonal variance-covariance structure and make sure that the observations are grouped by patid
, like I had specified in random = list(patid = pdDiag(list(p1 ~ 1, b1 ~ 1, p2 ~ 1, b2 ~ 1)))
with nlme
.