# How to plot the lmer result by group with straight lines [closed]

I use "R" to do some clinical research.

I got a lognitudinal data such as:

id outcome groups age edu sex years
1 12 0 65 12 M 1
1 14 0 66 12 M 2
1 15 0 67 12 M 3
2 10 1 80 12 F 1
2 15 1 81 12 F 2
2 17 1 82 12 F 3
2 19 1 83 12 F 4

... Every "id" is a unique participant. "years" is the visit time from the baseline(0)

I use lmer/lme to make lognitudinal analysis by the f:

lm1 <- lmer(outcome ~ age + edu + sex + group + years + group:years + (years|id), data=mydata)

Now I want to make the result visulizable.

So I code:

lmplot <- ggplot() +
geom_line(aes(years,predict(lm1),color=group),data=mydata) +
geom_point(aes(years,outcome,color=group,data=mydata)


The R give me the plot:

However, I am looking for the Plot with two straight lines for group 0 & 1 in the same panel to show the difference of the slope by years.

something like :

It seems that the predict function did not work.

I have search for solutions like: Demetri Pananos answered the similar question before. That really help with simple model, But not for mixed fitted model (R: "Error in eval(predvars, data, env) : object 'age' not found").

Is there anyone who can help me ?

Thank you very much!

New contributor
Yann is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.