'Interpret output of lm with ordinal factor
I have something similar to the following example:
library(tidyverse)
library(yardstick)
#> For binary classification, the first factor level is assumed to be the event.
#> Use the argument `event_level = "second"` to alter this as needed.
#>
#> Attaching package: 'yardstick'
#> The following object is masked from 'package:readr':
#>
#> spec
data <- tibble(y = c(rnorm(30), rnorm(30,0.5), rnorm(30,1)),
x = c(rep("a", 30), rep("b", 30), rep("c", 30)),
covar = rnorm(90,0.1)) %>%
mutate(x = factor(x, levels = c("a", "b", "c"), ordered = TRUE))
lm(y ~ x + covar, data = data) %>%
tidy()
#> # A tibble: 4 × 5
#> term estimate std.error statistic p.value
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 (Intercept) 0.584 0.101 5.79 0.000000114
#> 2 x.L 0.522 0.175 2.99 0.00369
#> 3 x.Q -0.108 0.176 -0.615 0.540
#> 4 covar -0.0128 0.102 -0.125 0.901
Created on 2022-03-09 by the reprex package (v2.0.1)
Where I'd like to know if y depends on x, but I also like to consider a covariate covar.
How do I interpret the output of the lm model? What are x.L and x.Q? I couldn't find this in the documentation for the function.
Solution 1:[1]
You have defined x as and ordered factor. Evidently this is more than a categorical variable, it is a variable of ordinal level. This means that there is information in the order of the levels.
In this ordinal case, lm defaults to polynomial contrasts: it will check for a linear (L), quadratic (Q), cubic (C), and so on ... effects. lm will fit "number of levels minus 1" polynomial contrasts. In your case, x has 3 levels, hence x.L and x.Q were present in the output.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | KoenV |
