|
| 1 | +--- |
| 2 | +title: "Plotting estimated marginal means with tinyplot" |
| 3 | +output: rmarkdown::html_vignette |
| 4 | +vignette: > |
| 5 | + %\VignetteIndexEntry{Plotting estimated marginal means with tinyplot} |
| 6 | + %\VignetteEngine{knitr::rmarkdown} |
| 7 | + %\VignetteEncoding{UTF-8} |
| 8 | +bibliography: bibliography.bib |
| 9 | +--- |
| 10 | + |
| 11 | +```{r set-options, echo = FALSE} |
| 12 | +knitr::opts_chunk$set( |
| 13 | + collapse = TRUE, |
| 14 | + comment = "#>", |
| 15 | + dev = "png", |
| 16 | + out.width = "100%", |
| 17 | + fig.width = 7, |
| 18 | + fig.height = 4, |
| 19 | + dpi = 300, |
| 20 | + message = FALSE, |
| 21 | + warning = FALSE, |
| 22 | + package.startup.message = FALSE |
| 23 | +) |
| 24 | +
|
| 25 | +options(modelbased_join_dots = FALSE) |
| 26 | +options(modelbased_select = "minimal") |
| 27 | +
|
| 28 | +pkgs <- c("marginaleffects", "tinyplot") |
| 29 | +if (!all(insight::check_if_installed(pkgs, quietly = TRUE))) { |
| 30 | + knitr::opts_chunk$set(eval = FALSE) |
| 31 | +} |
| 32 | +if (getRversion() < "4.1.0") { |
| 33 | + knitr::opts_chunk$set(eval = FALSE) |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +This vignette provides a quick overview with different examples that show how to plot estimated marginal means, like [in this vignette](https://easystats.github.io/modelbased/articles/plotting.html), however, here we use the [`{tinyplot}`](https://grantmcdermott.com/tinyplot/) package instead of `{ggplot2}` to create the plots. |
| 38 | + |
| 39 | +## One predictor - categorical |
| 40 | + |
| 41 | +The simplest case is possibly plotting one categorical predictor. Predicted values for each level and its confidence intervals are shown. |
| 42 | + |
| 43 | +```{r} |
| 44 | +library(modelbased) |
| 45 | +library(tinyplot) |
| 46 | +
|
| 47 | +data(efc, package = "modelbased") |
| 48 | +efc <- datawizard::to_factor(efc, c("e16sex", "c172code", "e42dep")) |
| 49 | +
|
| 50 | +m <- lm(neg_c_7 ~ e16sex + c172code + barthtot, data = efc) |
| 51 | +estimate_means(m, "c172code") |> plt() |
| 52 | +``` |
| 53 | + |
| 54 | +## One predictor - numeric |
| 55 | + |
| 56 | +For numeric predictors, the range of predictions at different values of the focal predictor are plotted, the uncertainty is displayed as confidence band. |
| 57 | + |
| 58 | +```{r} |
| 59 | +estimate_means(m, "barthtot") |> plt() |
| 60 | +``` |
| 61 | + |
| 62 | +## Two predictors - categorical |
| 63 | + |
| 64 | +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. |
| 65 | + |
| 66 | +```{r} |
| 67 | +m <- lm(neg_c_7 ~ e16sex * c172code + e42dep, data = efc) |
| 68 | +estimate_means(m, c("e16sex", "c172code")) |> plt() |
| 69 | +``` |
| 70 | + |
| 71 | +## Two predictors - numeric * categorical |
| 72 | + |
| 73 | +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. |
| 74 | + |
| 75 | +```{r} |
| 76 | +m <- lm(neg_c_7 ~ barthtot * c172code + e42dep, data = efc) |
| 77 | +estimate_means(m, c("barthtot", "c172code")) |> plt() |
| 78 | +``` |
| 79 | + |
| 80 | +In general, plots can be further modified using functions or arguments from the **tinyplot** package. Thereby, other themes, color scales, faceting and so on, can be applied. |
| 81 | + |
| 82 | +```{r} |
| 83 | +estimate_means(m, c("barthtot", "c172code")) |> |
| 84 | + plt(facet = ~c172code) |
| 85 | +
|
| 86 | +estimate_means(m, c("barthtot", "c172code")) |> |
| 87 | + plt(palette = "okabe") |
| 88 | +``` |
| 89 | + |
| 90 | +## Two predictors - categorical * numeric |
| 91 | + |
| 92 | +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. |
| 93 | + |
| 94 | +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. |
| 95 | + |
| 96 | +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. |
| 97 | + |
| 98 | +```{r} |
| 99 | +# by default, `range = "range"` and `length = 10` |
| 100 | +estimate_means(m, c("c172code", "barthtot")) |> plt() |
| 101 | +``` |
| 102 | + |
| 103 | +That means that the `length` argument can be used to control how many values (lines) for the numeric predictors are chosen. |
| 104 | + |
| 105 | +```{r} |
| 106 | +estimate_means(m, c("c172code", "barthtot"), length = 20) |> plt() |
| 107 | +``` |
| 108 | + |
| 109 | +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. |
| 110 | + |
| 111 | +```{r} |
| 112 | +estimate_means(m, c("c172code", "barthtot"), range = "grid") |> plt() |
| 113 | +``` |
| 114 | + |
| 115 | +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 `?insight::get_datagrid` for further details. |
| 116 | + |
| 117 | +```{r} |
| 118 | +estimate_means( |
| 119 | + m, |
| 120 | + c( |
| 121 | + "c172code = c('low level of education', 'high level of education')", |
| 122 | + "barthtot = c(30, 50, 80)" |
| 123 | + ) |
| 124 | +) |> plt() |
| 125 | +
|
| 126 | +estimate_means(m, c("c172code", "barthtot = [fivenum]")) |> plt() |
| 127 | +``` |
| 128 | + |
| 129 | +## Three numeric predictors |
| 130 | + |
| 131 | +The default plot-setting for three numeric predictors can be rather confusing. |
| 132 | + |
| 133 | +```{r} |
| 134 | +m <- lm(neg_c_7 ~ c12hour * barthtot * c160age, data = efc) |
| 135 | +estimate_means(m, c("c12hour", "barthtot", "c160age")) |> plt() |
| 136 | +``` |
| 137 | + |
| 138 | +Instead, it is recommended to use `length`, create a "reference grid", or again specify meaningful values directly in the `by` argument. |
| 139 | + |
| 140 | +```{r} |
| 141 | +estimate_means(m, c("c12hour", "barthtot", "c160age"), length = 2) |> plt() |
| 142 | +
|
| 143 | +estimate_means(m, c("c12hour", "barthtot", "c160age"), range = "grid") |> plt() |
| 144 | +``` |
| 145 | + |
| 146 | +## Three categorical predictors |
| 147 | + |
| 148 | +Multiple categorical predictors are usually less problematic, since discrete color scales and faceting are used to distinguish between factor levels. |
| 149 | + |
| 150 | +```{r} |
| 151 | +m <- lm(neg_c_7 ~ e16sex * c172code * e42dep, data = efc) |
| 152 | +estimate_means(m, c("e16sex", "c172code", "e42dep")) |> plt() |
| 153 | +``` |
| 154 | + |
| 155 | +## Smooth plots |
| 156 | + |
| 157 | +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. |
| 158 | + |
| 159 | +```{r} |
| 160 | +m <- lm(neg_c_7 ~ e16sex * c12hour + e16sex * I(c12hour^2), data = efc) |
| 161 | +estimate_means(m, c("c12hour", "e16sex")) |> plt() |
| 162 | +``` |
| 163 | + |
| 164 | +In this case, simply increase the number of representative values by setting `length` to a higher number. |
| 165 | + |
| 166 | +```{r} |
| 167 | +estimate_means(m, c("c12hour", "e16sex"), length = 200) |> plt() |
| 168 | +``` |
| 169 | + |
| 170 | +```{r echo=FALSE} |
| 171 | +# reset options |
| 172 | +options( |
| 173 | + modelbased_join_dots = NULL, |
| 174 | + modelbased_estimate = NULL, |
| 175 | + modelbased_select = NULL |
| 176 | +) |
| 177 | +``` |
0 commit comments