# Degrees of freedom in a linear mixed model (or ANOVA) - Categorical factors versus continuous numbers

I have data from 20 individuals. In general, each individual has measurements (i.e., called Value below) from the Left and Right arm taken at different months in intervals of 6 months, from 6 to 60. Note that not everyone has all conditions filled in (e.g., SubjID 8 is missing months 12 an 18, SubjID 11 is missing Right arm values at month 24, etc).

SubjID  Arm  Month  Value
1       L    6       3
1       L    12      3.2
...     ...  ...     ...
1       L    60      6.1
1       R    6       2.1
1       R    12      8.1
...     ...  ...     ...
1       R    60      3.9
...     ...  ...     ...
...     ...  ...     ...
20      R    60      3.1


I am using a mixed linear model in R to test for main effects of Arm and Month on the Value observed. SubjID and Arm are factors, whereas Month and Value are numbers (i.e., non-factors). My fixed effects are Month, Arm, and their interaction, and my random effect is an intercept difference for each SubjID.

eq = Value~Month*Arm+(1|SubjID)
fit = lmer(eq)
anova(fit)


When I run the model my degrees of freedom for all variables are just 1. I found that if I convert Month to a factor instead of a number, then my degrees of freedom for the variables are all correct (e.g., non-1, except Arm). Also, the F-values are different in either case.

What is the best approach here? Should Month be a factor or a number? And why would that change the degrees of freedom for testing the main effect of Arm?

I simulated data and did not get degree of freedom of arm = 2.

> set.seed(7634)
> id <- rep(c(1:20),each=20)
> arm <- rep(c("L","R"),each=10,times=20)
> m <- rep(c(6,12,18,24,30,36,42,48,54,60),times=40)
> y <- rep(rnorm(20,0,1),each=20) + rnorm(400,100,2)
> final <- data.frame(id,arm,m,y)
> fit1 <- lmer(y~m*arm + (1|id), data=final)
> anova(fit1)
Analysis of Variance Table
Df Sum Sq Mean Sq F value
m      1 0.0682  0.0682  0.0170
arm    1 9.9837  9.9837  2.4900
m:arm  1 0.2495  0.2495  0.0622
> fit2 <- lmer(y~factor(m)*arm + (1|id), data=final)
> anova(fit2)
Analysis of Variance Table
Df Sum Sq Mean Sq F value
factor(m)      9 28.228  3.1365  0.7883
arm            1  9.984  9.9837  2.5093
factor(m):arm  9 47.369  5.2633  1.3229


For month being a factor or a number, you can compare two models by

> anova(fit2,fit1)
refitting model(s) with ML (instead of REML)
Data: final
Models:
fit1: y ~ m * arm + (1 | id)
fit2: y ~ factor(m) * arm + (1 | id)
Df    AIC    BIC logLik deviance  Chisq Chi Df Pr(>Chisq)
fit1  6 1736.0 1760.0 -862.0   1724.0
fit2 22 1748.6 1836.4 -852.3   1704.6 19.413     16     0.2478


If p value is small, it means the relationship between month and $$Y$$ is not linear, and month should be a factor. Otherwise, the linear relationship between month and $$Y$$ cannot be rejected, so keep month as a number.

Degree of freedom = # of regression coefficients. When month is continuous, the model is:

$$Y=\beta_0 + \beta_1 X_1 + \beta_2 X_2 + \beta_3 X_1X_2+\epsilon$$ where $$X_1$$ is arm and $$X_2$$ is month. Each covariate and interaction has one regression coefficient, so their df = 1.

When month are categorical, 9 dummy variable are generated, because there are 10 levels and one of them is reference. The model is:

$$Y=\beta_0 + \beta_1 X_1 + \beta_{21} X_{21}+...+\beta_{29}X_{29} + \beta_{31} X_1X_{21} +...+\beta_{39}X_1X_{39}+\epsilon$$ The month has 9 regression parameters $$\beta_{21},...,\beta_{29}$$, so its df is 9. Same as the interaction between arm and month.

• Thanks so much for your reply. I wrote the question slightly incorrectly, I apologize for the typo. What I meant is that, when Month is a factor (instead of a number), then all the degrees of freedom for all the variables are 1. Why does that single factor affect the degrees of freedom for all variables? Dec 19, 2018 at 18:17
• See what I added in Answer. Dec 19, 2018 at 19:02
• @user158565 can age be used without converting it to category like some groups?
– kcm
Jun 27, 2022 at 20:09