I want to fit a model y ~ b * exp(-exp(a) * x), but including a random effect, with this data:
# data:
data = structure(list(re = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 3L, 2L,
2L, 3L, 1L, 1L, 3L, 3L, 2L, 2L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L,
2L, 1L, 1L, 3L, 3L, 2L), levels = c("A", "B", "C"), class = "factor"),
x = c(0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3,
3, 6, 6, 6, 6, 6, 6, 8, 8, 8, 8, 8, 8), y = c(0.0899425978552549,
-1.85384333820306, -4.14514965495534, -1.81807449635766,
-1.35326955027327, -1.28825429472141, -0.209944981000945,
-2.55036342588007, -2.41329880502537, 1.60816564193517, 0.974291267956421,
-1.03569994133107, 0.259011545358077, 1.15202768590689, -1.11099110226214,
0.723503984838873, 1.0498337589214, 0.0331911544521152, -1.0534870983813,
-2.39106769323274, 0.145263217335939, -0.314587019845322,
1.27885667618699, -1.46171756397001, 0.69031827310659, 0.656834906437993,
0.0910321826159956, 1.59640958118439, 1.02260280171595, -0.0386924537729473
)), row.names = c(NA, -30L), class = c("tbl_df", "tbl", "data.frame"
))
Thus I would fit this model with nlme:
# define the model function
f = function(x, a, b, Asym = 0) Asym + (b - Asym) * exp(-exp(a) * x)
define the model:
model.1 <- nlme(y ~ f(x, a, b),
data = d_pub,
fixed = a + b ~ 1,
random = b ~ 1 | re,
start = c(b = -0.15, a = -0.7))
model:
summary(model.1)
# Nonlinear mixed-effects model fit by maximum likelihood
# Model: y ~ f(x, a, b)
# Data: d_pub
# AIC BIC logLik
# 102.6748 108.2796 -47.33741
#
# Random effects:
# Formula: b ~ 1 | re
# b Residual
# StdDev: 1.10461 1.095258
#
# Fixed effects: a + b ~ 1
# Value Std.Error DF t-value p-value
# a -0.6980492 0.6247593 26 -1.117309 0.2741
# b -1.4309434 0.7840392 26 -1.825092 0.0795
# Correlation:
# a
# b -0.215
#
# Standardized Within-Group Residuals:
# Min Q1 Med Q3 Max
# -2.0549776 -0.5476474 0.1779084 0.9287772 1.8802465
#
# Number of Observations: 30
# Number of Groups: 3
I would then also quickly look at the residuals:
plot(model.1)
qqnorm(resid(model.1)); qqline(resid(model.1))
If I look at the data (and the model prediction and the std error of the model parameters), I don't actually assume that this model is particularly good. In this particular case (although in other cases the model seems to fit quite well), it might even be possible that y doesn't depend on x at all. To test this, I would like to compare it to a "null-model" to see whether it performs better or not.
Intuitively I would have fitted a linear model: lme(y~1, data = data, random = ~1|re), but then I wouldn't know how compare these very different kinds of models!
Update: I have now fitted the null-model as following:
f2= function(x, a) a
model.0 = nlme(diff ~ f2(x, a),
data = d_pub,
fixed = a ~ 1,
random = a ~ 1|re,
start = c(a = mean(d_pub$diff)))
anova(model.1, model.0)
Is there any way to assess whether a nlme-model fits the data, some measure of gooness-of-fit? And are there practiacal ways to also test model assumptions for nlme-models?
regroups in the example data. If this dataset is representative, it may be better to fit treatreas a fixed effect. See also Why do random effects require a minimum # of levels? – dipetkov Mar 08 '24 at 21:32