r/RStudio • u/Tiny_Athlete_3637 • 1d ago
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!
14
u/GripIQ 1d ago
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.
4
u/Mathie1729 1d ago
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 1d ago
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 1d ago
Just looked into this myself and appears they’re both the same thing!
1
u/GripIQ 23h ago
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 23h ago
Thanks!
1
u/GripIQ 23h ago
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.
3
7
u/seanv507 1d ago
wrong sub. this is about a r programming environment. ask r/statistics or r/AskStatistics
-11
u/Tiny_Athlete_3637 1d ago
I’m working with Rstudio
7
u/seanv507 1d ago
You can use R in a code editor. Doesnt mean you should ask r questions or statistics questions in a sub about the code editor
4
2
u/Confident_Bee8187 1d ago
I've been saying this several times that RStudio is just an IDE, and there are several IDEs out there. Go to the sub the parent comment gave to you.
2
u/dr_canak 1d ago
Honestly, this is the kind of question that is perfect for AI, even the free models. These can be complex models, that require more explanation than you're going to get here. And likely some sort of ongoing dialog if you have more questions. Your idea of "intuitive" will likely differ from anyone explaining it to you, which is why dialogue matters. LLMs have their place, and this is one of them.
1
u/UsualFamiliar8777 7h ago edited 7h ago
This is truth.
With my background it makes so much more sense as a linear algebra problem Ax=b whereas most explanations go right into weird abstractions (for me). I asked Gemini to explain it in terms of a linear equation Ax=b and it gave immediate clarity.
Imagine you want to do a regression of weight vs height from people in different countries. Let's say you have weight = [150 170 140 220 110...] in kg and height = [124 135 117 177 133...] in cm.
The linear model is A * x = b where A = [1 1 1 1 1...; 150 170 140 220 110...] and b = [124 135 117 177 133...]. (Imagine there are thousands of points and multiple countries in the dataset.)
One approach is to solve Ax=b using all the data from all countries together (pooled). However countries e.g. USA and CHINA may have very different intercepts and/or slopes. This inflates the variance of x.
Another approach is to model each country separately (unpooled).
A_US * x_US = b_US
A_CH * x_CH = b_CH
This also inflates the variance of the estimated x since some countries may have fewer points, and loses the sense of a global trend of height vs weight.
The mixed effects approach solves the pooled and unpooled equations simultaneously.
[A A_US A_CH] * [x; dx_US; dx_CH] = b
Here we have the pooled equation Ax=b and the unpooled equations and additional variables dx_US and dx_CH. As it stands, it's worse than the other approaches because we have tripled the number of unknowns (x).
The trick is to regularize dx_US and dx_CH to be close to zero. Conceptually just like Tikhonov regularization.
[A A_US A_CH; 0 J11 J12; 0 J12 J22] * [x; dx_US; dx_CH] = [b; 0; 0]
This adds constraints, e.g. J11 * dx_US = 0, that pull the country specific terms close to zero. The precise way it does that is somewhat technical but inversely related to the estimated variance of x. If the estimated variance of x(1) (intercept) is high then the associated element of J11 will be "small" to allow more individual variation, and vice versa.
In a statistical sense, I think what it does is force the country-specific terms (dx_US and dx_CH) to have a zero mean Gaussian distribution with specified variance. That's basically mixed-effects in a nutshell for a linear algebraist!
23
u/Minimum_Database_979 1d ago
Hi, i realize you asked a question in the wrong sub, but since i’m reading this anyway, here’s the video that helped my wrap my head around it. All of the examples are also in Rstudio: https://youtu.be/5tOifM51ZOk?is=9DdSUdn3KBgr3Yb1
Hope it helps!