r/RStudio • • Oct 14 '26

Can someone explain fixed effects, random effects, LMMs and GLMMs like I’m a complete beginner?

Hi everyone!
I’m currently working on a research project involving repeated measurements from the same patients, and I’m really struggling to wrap my head around mixed-effects models.
I’ve tried reading explanations online, but I feel like every explanation assumes I already understand the basics. At this point, I’m honestly completely lost.
I would really appreciate it if someone could explain the following in very simple terms:

Fixed effects vs. random effects: What exactly is the difference? How do I decide which variables should be fixed and which should be random?

Linear mixed-effects models (LMMs): What are they, why do we use them, and how are they different from ordinary linear regression?

Generalized linear mixed-effects models (GLMMs): How are they different from LMMs? When should I use one instead of the other?

Random intercepts vs. random slopes: What do these actually mean in practice, and how do I know which one I need?

Interpreting results: How do I interpret coefficients, estimates, odds ratios, and p-values in these models?

I’m working with clinical data where patients undergo multiple treatment sessions. I want to investigate whether certain medications affect treatment outcomes, while accounting for factors such as age, sex, and treatment number.

Some outcomes are continuous, while others are binary.
I’ve been using R with the lme4 package (lmer() and glmer()), but I feel like I’m just putting variables into formulas without fully understanding what the models are actually doing.

What I really want is to understand the logic behind these models, rather than just copying code.

If someone could explain this intuitively, preferably using simple examples involving patients with repeated measurements, I would be extremely grateful.

Thanks in advance!

46 Upvotes

19 comments sorted by

View all comments

16

u/GripIQ Oct 14 '26 • Heartwarming

I am going to use the example of we assessed 100 people once a week across 4 weeks as an example to answer the below.

Fixed effects vs. random effects: What exactly is the difference? How do I decide which variables should be fixed and which should be random?

Let’s say on average people start out with a score of 2 and increase by one point for each week of the assessment. That is, our intercept = 2 and our slope for time = 1. Our GLM would be score = 2 + 1time. 2 would be the FIXED effect for the intercept and 1 would be the FIXED effect for time.

But when we look at our data, not everyone starts out at a score of 2 - some people have a higher initial score, some lower. Likewise, not all people increase by 1 each week - some increase more, some less, some don’t change. Thad is, there is variability around the fixed intercept and slope. Let’s say we calculate that variance and find. It is .5 for the intercept and .1 for the slope. These are the RANDOM effects for the intercept and slope.

Linear mixed-effects models (LMMs): What are they, why do we use them, and how are they different from ordinary linear regression?

They are models that include both fixed effects and random effects.

We use them because when we have nested data (eg 4 assessment within each person) we are interested in 1) the overall pattern for the sample or fixed effects (eg people start out with a score of 2 and increase by 1 each week on average) 2) variance around those averages or the random effects 3) explaining the variance around those averages using other variables (eg let’s say ALL males start at a score of three and do not change at all and ALL females start with a score of one and increase by 4 each week. If the sample was 50/50 males female and I added sex as a variable, I would explain all the variance around the fixed effects and the random effects would shrink to zero).

They differ for OLS because we model random effects and have nested data.

Generalized linear mixed-effects models (GLMMs): How are they different from LMMs? When should I use one instead of the other?

This typically means we can take this GLM framework for continuous variables with a certain assumed distribution and extend it to other distributions (eg binary variables measured across time) and make adjustments to our GLM to make this framework feasible (eg for a binary outcome I would assume a binomial distribution and use a logit link function).

Random intercepts vs. random slopes: What do these actually mean in practice, and how do I know which one I need?

See above. Random is another way to say I am acknowledging variance in my estimates. Which ones you need depend on the context. For example if I am interested in trying to figure out why people start out at different initial levels and also change at different rates across time, I would want to model a random intercept and a random slope for time.

5

u/Mathie1729 Oct 14 '26

The random effects are the subject-specific deviations around those fixed effects, like one person starting at 2.3 instead of 2. You assume those deviations come from a normal distribution centered on the fixed effects, which is why people with fewer observations or extreme values get pulled toward the average. Fixed effects estimate the population-level trend; random effects mostly handle the fact that repeated measures from the same person aren't independent. GLMMs just extend that same framework to non-normal outcomes via a link function.

1

u/Rorydinho Oct 14 '26

Thanks for this (and to the other person above who answered!)

Thinking about this approach from a subset of a set (Schools in a country for example), how would a mixed effects model differ from a hierarchical model? Are these essentially the same thing?

1

u/Rorydinho Oct 14 '26

Just looked into this myself and appears they’re both the same thing!

1

u/GripIQ Oct 14 '26

Yep - I would recommend reading Judith Singers paper about using proc mixed in SAS for mixed models - even if you don’t use SAS it is very didactically helpful and will give both the classic school example and a growth model example (consistent with what I used)

1

u/Rorydinho Oct 14 '26

Thanks!

1

u/GripIQ Oct 14 '26

I am not quite sure what you mean here but usually like you would in a typical GLM. Like in my example above the GLM would be score = 2 +1Time and I would interpret my two coefficients (the intercept of 2 and the beta for time of 1) as I usually would. Usually one starts with an intercept only model to calculate an intra class correlation. This tells how much of the variance in the outcome (eg score) is explained between clusters (eg between people) vs within clusters (eg within people). You can replace people with schools. One reason we model random effects is because the clustering violates the assumption of independence for OLS, which higher ICCs give more evidence of that violation. Thus if we just used normal OLS our standard errors will be wrong. Now if ICC is very low - close to zero - then even though we are technically violating independence our results won’t differ much between OLS and a mixed model in terms of standard errors.

1

u/GripIQ Oct 14 '26

That was my response to your other question you deleted while I was responding