Question

I did a linear regression with random effects using R and the lme function :

ana1 <- lme(Y ~ 1+X,data,random = list(group=pdDiag(~1+X)))

where Y are my observations, X the vector of regressors and group a grouping vector (each element of group is a number between 1 and N which indicates to which subject the observation belongs).

After doing the linear regression, I can obtain fitted values using :

fit <- fitted(ana1)

What I would like to do is : create a function which takes as arguments ana1 and k, a number between 1 and N, and plot (on the same plot) the observations for subject k and the fitted values (with different colors).

I don't know how to do this. I have the feeling that this can be done using predict.

Was it helpful?

Solution

plot(Y~X,data) #To plot all data
points(data$X[k],predict(ana1,data[k,]),col='red') #add a predicted point in red
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top