# Very confused by residual degrees of freedom in REML

I am trying to estimate a LMEM model with three fixed effects (two regressors and their interaction) and one random effect (a by-subject random intercept). Both regressors are dichotomous.

I'm using R's lme4 command, and I'm just having some problems understanding the residual degrees of freedom.

The model is below. Per my training, I estimated p-values using a Type III Wald F test with Kenward-Rogers degrees of freedom.

conflict1 <- lmer(neurosynth_conflict ~ contrast*Teen_vs_Adult + (1 | participant_id),dCon)

car::Anova(conflict1, type = 3, test = 'F')

The model converges just fine and the output mostly aligns with my expectations, but I'm just really thrown by the residual degrees of freedom. I get that residual degrees of freedom will almost never be integers under REML, but I don't understand why the values are so different across my regressors? Can anyone help me wrap my head around this?

                             F Df Df.res    Pr(>F)
(Intercept)            20.0400  1 377.20 1.006e-05 ***
contrast               10.5006  1 496.74  0.001273 **
contrast:Teen_vs_Adult  5.9632  1 499.22  0.014953 *

$$$$


More generally, the residual df will depend a lot on whether the information for a particular comparison comes from comparisons within participant or between participants. Very roughly, the residual df is a sort of $$n-p$$ number where $$n$$ is the number of observations and $$p$$ is the number of parameters you've estimated. If you have a variable that is constant within individuals, the relevant $$n$$ is going to be the number of individuals; if the variable varies within individuals as well, the relevant $$n$$ will be larger and might be the total number of observations.
Perhaps it's due to the contrasts used. I had the same issue (type3 and F test) and I had normal df.res once I ran this code before running the models: options(contrasts=c("contr.sum","contr.poly"))`