# Compare longitudinal data in control and group 1 in R

I am new to this so please be nice.

I have longitudinal data for my control and group 1. Within control and group 1 I have more groups. e.g person id number. Essentially I want to know the best stats test to compare longitudinal data in my control and my response group.

I've managed to do LMER analysis for my control and group 1 and have found the best model for each of them using ANOVA. Now I have plotted their best models onto a same plot. But I want to analyse whether the plots of group 1 is significantly different to control group. I've again used ANOVA, but I am missing data for this and its not lining up.

now should I be comparing the two plots in a different way? or do something else?

Control

ID Week Response

Group 1

ID Week Response

Some weeks having missing data, and the groups do not have the same number of participants.

Apologies in advance if you specifically want to compare plots, because I don't know about that. But, I assume you have a continuous dependent variable and either continuous or factorial predictor or maybe several predictors. In this case I would compare the two groups statistically by putting them into the same lmer model - e.g. (with one continuous predictor):

model<-lmer(outcome ~ (1|person_id) + group*predictor, data=data) #edited to remove code redundancy

"group" being a factorial variable with two levels, group1 and control. Then, if the group*predictor interaction is significant, you have evidence that outcome ~ predictor relationship is statistically different for group1 vs. control. Then, you can run the simple models of outcome ~ predictor + (1|person_id) separately for each group and report the fixed main effects separately for each group, and also plot the 2 slopes into the same figure.

Edited to respond to your comment: I think you got an output from R looking like this:

Linear mixed model fit by REML. t-tests use Satterthwaite's
method [lmerModLmerTest]
Formula: outcome ~ (1 | id) + group * predictor
Data: df

REML criterion at convergence: 851.8

Scaled residuals:
Min       1Q   Median       3Q      Max
-2.05211 -0.66622 -0.02345  0.67188  2.56591

Random effects:
Groups   Name        Variance Std.Dev.
id       (Intercept) 0.004382 0.0662
Residual             0.967369 0.9835
Number of obs: 300, groups:  id, 60

Fixed effects:
Estimate Std. Error        df t value
(Intercept)        4.34483    0.32619 295.67804  13.320
group1            -0.84478    0.44085 295.96649  -1.916
predictor         -0.08481    0.07839 287.76043  -1.082
group1:predictor   0.19004    0.10647 288.70985   1.785
Pr(>|t|)
(Intercept)        <2e-16 ***
group1             0.0563 .
predictor          0.2802
group1:predictor   0.0753 .

(this is made based on simulated data with 60 participants and 5 observations per participant. I have no significant effects there, but never mind that).

So, if you look at the "Fixed effects" part, "group1" effect is an effect comparing group 1 to the control group (estimate for "predictor" is the continuous predictor main effect, here non-significant and negative). Because "group" is a factorial effect, it compares one group to another. "Control" is the default to which "group1" is compared to. That's why it is not shown in the output. With a factorial predictor, you always see in the output the number of levels of the predictor minus 1 level - because that's the level the other levels are compared to.

In my mock data, the results suggest that outcome was lower in group1 than in control group, because the estimate is negative, but not significantly so. If your "group1" effect was significant and positive, it suggests that outcome was significantly higher for people in group1 than in control, and vice versa if it was significant and negative. You get estimated marginal means for "control" and "group1" from emmeans package:

library(emmeans)
em<-emmeans(model, specs = pairwise ~ group)

Or, you can of course check and plot raw means by group too.

Of course, if the interaction is significant, you should focus on that more than on "group" main effect. You can for instance plot separate slopes of predictor for each group by using:

library(ggplot2)
library(ggeffects)

preds<-ggpredict(model, c("predictor", "group"))
plot1<-ggplot(preds)+geom_line(aes(x=x, y=predicted, color=group))
plot2<-plot1+scale_color_discrete(name="Group", labels=c("Control", "Group1"))
plot3<-plot2+labs(x="Predictor", y="Outcome", title="title")
• Hi, thank you so much for your reply, it means a lot. Oct 21, 2022 at 20:54
• Technically predictor + group + group*predictor is redundant (but harmless); group*predictor expands to group + predictor + group:predictor (where : denotes the interaction term) Oct 22, 2022 at 19:40
• Oops, thank you! I'll edit my answer. Oct 23, 2022 at 16:40
• Thank you so much, I'm new to stats and didn't really understand. Should the factor variables be Control and Group 1, or binary numbers such as 0 and 1? For my results, I am only seeing groupControl*Predictor, is the group 1 just inferred or have I done something wrong? I grouped control first and then group 1. Oct 23, 2022 at 17:41
• Sorry I only see group1*Predictor and not control. Is this inferred or an error? Oct 23, 2022 at 18:16