-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathplotting.Rmd
178 lines (129 loc) · 6.63 KB
/
plotting.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
---
title: "Plotting estimated marginal means"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Plotting estimated marginal means}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
bibliography: bibliography.bib
---
```{r set-options, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
dev = "png",
out.width = "100%",
fig.width = 7,
fig.height = 4,
dpi = 300,
message = FALSE,
warning = FALSE,
package.startup.message = FALSE
)
options(modelbased_join_dots = FALSE)
options(modelbased_select = "minimal")
pkgs <- c("marginaleffects", "ggplot2", "see")
if (!all(insight::check_if_installed(pkgs, quietly = TRUE))) {
knitr::opts_chunk$set(eval = FALSE)
}
if (getRversion() < "4.1.0") {
knitr::opts_chunk$set(eval = FALSE)
}
```
This vignette provides a quick overview with different examples that show how to plot estimated marginal means.
In summary, you can use the `length` and `range` arguments in `estimate_means()` (which are passed to [`insight::get_datagrid()`](https://easystats.github.io/insight/reference/get_datagrid.html)), as well as directly specifying meaningful values in the `by` argument, which are also used to create a data grid, to control the plot-appearance. See also the [vignette on data grids](https://easystats.github.io/modelbased/articles/visualisation_matrix.html).
Although the **modelbased** package does not focus on publication-ready plots, the default plots can already be used directly. Furthermore, a few modifications are already applies, like a percentage-scale for logistic regression models, or using variable labels for _labelled data_.
## One predictor - categorical
The simplest case is possibly plotting one categorical predictor. Predicted values for each level and its confidence intervals are shown.
```{r}
library(modelbased)
data(efc, package = "modelbased")
efc <- datawizard::to_factor(efc, c("e16sex", "c172code", "e42dep"))
m <- lm(neg_c_7 ~ e16sex + c172code + barthtot, data = efc)
estimate_means(m, "c172code") |> plot()
```
## One predictor - numeric
For numeric predictors, the range of predictions at different values of the focal predictor are plotted, the uncertainty is displayed as confidence band.
```{r}
estimate_means(m, "barthtot") |> plot()
```
## Two predictors - categorical
For two categorical predictors, the first focal predictors is plotted along the x-axis, while the levels of the second predictor are mapped to different colors.
```{r}
m <- lm(neg_c_7 ~ e16sex * c172code + e42dep, data = efc)
estimate_means(m, c("e16sex", "c172code")) |> plot()
```
## Two predictors - numeric * categorical
For two predictors, where the first is numeric and the second categorical, range of predictions including confidence bands are shown, with the different levels of the second (categorical) predictor mapped to colors again.
```{r}
m <- lm(neg_c_7 ~ barthtot * c172code + e42dep, data = efc)
estimate_means(m, c("barthtot", "c172code")) |> plot()
```
In general, plots can be further modified using functions from the **ggplot2** package. Thereby, other themes, color scales, faceting and so on, can be applies.
```{r}
library(ggplot2)
estimate_means(m, c("barthtot", "c172code")) |>
plot() +
see::theme_modern(show.ticks = TRUE)
estimate_means(m, c("barthtot", "c172code")) |>
plot() +
facet_grid(~c172code)
estimate_means(m, c("barthtot", "c172code")) |>
plot() +
scale_color_brewer(palette = "Dark2") +
scale_fill_brewer(palette = "Dark2")
```
## Two predictors - categorical * numeric
If the numeric predictor is the _second_ focal term, its values are still mapped to colors, however, by default to a continuous (gradient) scale, because a range of representative values for that numeric predictor is used by default.
Focal predictors specified in `estimate_means()` are passed to `insight::get_datagrid()`. If not specified otherwise, representative values for numeric predictors are evenly distributed from the minimum to the maximum, with a total number of `length` values covering that range.
I.e., by default, arguments `range = "range"` and `length = 10` in `insight::get_datagrid()`, and thus for numeric predictors, a _range_ of _length_ values is used to estimate predictions.
```{r}
# by default, `range = "range"` and `length = 10`
estimate_means(m, c("c172code", "barthtot")) |> plot()
```
That means that the `length` argument can be used to control how many values (lines) for the numeric predictors are chosen.
```{r}
estimate_means(m, c("c172code", "barthtot"), length = 20) |> plot()
```
Another option would be to use `range = "grid"`, in which case the mean and +/- one standard deviation around the mean are chosen as representative values for numeric predictors.
```{r}
estimate_means(m, c("c172code", "barthtot"), range = "grid") |> plot()
```
It is also possible to specify representative values, at which the estimated marginal means of the outcome should be plotted. Again, consult the documentation at `?ìnsight::get_datagrid` for further details.
```{r}
estimate_means(
m,
c(
"c172code = c('low level of education', 'high level of education')",
"barthtot = c(30, 50, 80)"
)
) |> plot()
estimate_means(m, c("c172code", "barthtot = [fivenum]")) |> plot()
```
## Three numeric predictors
The default plot-setting for three numeric predictors can be rather confusing.
```{r}
m <- lm(neg_c_7 ~ c12hour * barthtot * c160age, data = efc)
estimate_means(m, c("c12hour", "barthtot", "c160age")) |> plot()
```
Instead, it is recommended to use `length`, create a "reference grid", or again specify meaningful values directly in the `by` argument.
```{r}
estimate_means(m, c("c12hour", "barthtot", "c160age"), length = 2) |> plot()
estimate_means(m, c("c12hour", "barthtot", "c160age"), range = "grid") |> plot()
```
## Three categorical predictors
Multiple categorical predictors are usually less problematic, since discrete color scales and faceting are used to distinguish between factor levels.
```{r}
m <- lm(neg_c_7 ~ e16sex * c172code * e42dep, data = efc)
estimate_means(m, c("e16sex", "c172code", "e42dep")) |> plot()
```
## Smooth plots
Remember that by default a range of ten values is chosen for numeric focal predictors. While this mostly works well for plotting linear relationships, plots may look less smooth for certain models that involve quadratic or cubic terms, or splines, or for instance if you have GAMs.
```{r}
m <- lm(neg_c_7 ~ e16sex * c12hour + e16sex * I(c12hour^2), data = efc)
estimate_means(m, c("c12hour", "e16sex")) |> plot()
```
In this case, simply increase the number of representative values by setting `length` to a higher number.
```{r}
estimate_means(m, c("c12hour", "e16sex"), length = 200) |> plot()
```