Question

I have the example data and model

x<-rep(seq(0,100,by=1),10)
y<-15+2*rnorm(1010,10,4)*x+rnorm(1010,20,100)
id<-NULL
for (i in 1:10){
  id<-c(id, rep(i,101))}
dtfr<-data.frame(x=x,y=y, id=id)
library(nlme)
with (dtfr, summary(lme((y)~x,random=~1+x|id, na.action=na.omit  )))
model.mx<-with (dtfr, (lme((y)~x,random=~1+x|id, na.action=na.omit  )))
pd<-predict(model.mx, newdata=data.frame(x=0:100),level=0)
with (dtfr, plot(x, y))
lines(0:100,predict(model.mx, newdata=data.frame(x=0:100),level=0), col="darkred", lwd=7)

How can I extract the modelled intercept and slope of each individual ID and plot the individual trajectories of each ID?

Was it helpful?

Solution

Not sure what you want to do because all your coefficients are almost identical:

> coef(model.mx)
   (Intercept)        x
1     54.88302 19.18001
2     54.88298 19.18000
3     54.88299 19.18000
4     54.88299 19.18000
5     54.88302 19.18001
6     54.88300 19.18000
7     54.88301 19.18000
8     54.88300 19.18000
9     54.88299 19.18000
10    54.88300 19.18000

Maybe your real data gives you more different results. If it's the case, I would use abline inside a mapply call:

with (dtfr, plot(x, y))
mapply(abline,a=coef(model.mx)[,1],b=coef(model.mx)[,2], col=1:10)

Here's the result. Since all coeffcients are almost the same, the lines are plotted on top of each other. You only see the last one. enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top