Bonjour,
je me suis posé la même question, en me demandant comment coder cette interaction sous R.
Comme le dit Eric tu peux repartir de la manière dont sont codées les choses.
Une possibilité qui peut-être longue à force :
Code : Tout sélectionner
#### un jeu de données test :
set.seed(100)
# coefficients for each group
# coefficients for each group
cf <- structure(c(58.8007098743483, 68.9526514961022, 75.7517805503469,
68.2111807884739, 79.0803042994813, 75.2743397284317, 29.8661527230426,
32.7502759832602, 30.7439702116961), .Dim = c(3L, 3L), .Dimnames = list(
c("gr1", "gr2", "gr3"), c("Asym_mean", "xmid_mean", "scal_mean"
)))
nl <- c(10, 10, 10)
Time <- 1:130
tab <- expand.grid(Individual = 1:sum(nl), Time = Time)
tab <- tab[do.call(order, tab),]
tab$Li <- numeric(nrow(tab))
tab$group <- factor(rep(c("gr1", "gr2", "gr3"), nl*130))
for (i in 1:sum(nl)) {
auxi <- tab$Individu %in% i
sec <- unique(tab$group[auxi])
Asym1 <- rnorm(1, cf[sec, "Asym_mean"], 13)
xmid1 <- rnorm(1, cf[sec, "xmid_mean"], 15)
scal1 <- rnorm(1, cf[sec, "scal_mean"], 4.6)
crois <- sort(SSlogis(Time, Asym1, xmid1, scal1) + rnorm(130, 0, 0.3))
tab$Li[auxi] <- crois
}
# un nls simple
nls(Li ~ SSlogis(Time, p1, p2, p3), data = tab)
# équivalent à
nls(Li ~ (p1)/(1+exp(-(Time-(p2))/(p3))), start = list(p1=60, p2=74, p3=32), data = tab)
# ici trois groupes
# crée les indicatrices pour les groupes 2 et 3
tab$gr2 <- (tab$group=="gr2")*1
tab$gr3 <- (tab$group=="gr3")*1
# le modèle
# pour chaque paramètre on ajoute le produit du coefficient * indicatrice
nls(Li ~ (p1+a2*gr2+a3*gr3)/(1+exp(-(Time-(p2+b2*gr2+b3*gr3))/(p3+c2*gr2+c3*gr3))),
start = list(p1=60, p2=74, p3=32, a2=0, a3=0, b2=0, b3=0, c2=0, c3=0), data = tab)
# ou encore :
tab$gri <- model.matrix(~group, tab)
nls(Li ~ (gri%*%p1)/(1+exp(-(Time-(gri%*%p2))/(gri%*%p3))),
start = list(p1=matrix(c(60,0,0), 3), p2=matrix(c(74,0,0),3), p3=matrix(c(32, 0, 0),3)), data = tab)
# avec nlme
# fonction nlsList
library(nlme)
nlsList(Li ~ SSlogis(Time, Asym, xmid, scal) | group, data = tab)
Cordialement,
Maxime