-
-
Notifications
You must be signed in to change notification settings - Fork 23
Add simple vignette for tinyplot #571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| --- | ||
| title: "Plotting estimated marginal means with tinyplot" | ||
| output: rmarkdown::html_vignette | ||
| vignette: > | ||
| %\VignetteIndexEntry{Plotting estimated marginal means with tinyplot} | ||
| %\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", "tinyplot") | ||
| 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, 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. | ||
|
|
||
| ## 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) | ||
| library(tinyplot) | ||
|
|
||
| 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") |> plt() | ||
| ``` | ||
|
|
||
| ## 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") |> plt() | ||
| ``` | ||
|
|
||
| ## 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")) |> plt() | ||
| ``` | ||
|
|
||
| ## 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")) |> plt() | ||
| ``` | ||
|
|
||
| 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. | ||
|
strengejacke marked this conversation as resolved.
|
||
|
|
||
| ```{r} | ||
| estimate_means(m, c("barthtot", "c172code")) |> | ||
| plt(facet = ~c172code) | ||
|
|
||
| estimate_means(m, c("barthtot", "c172code")) |> | ||
| plt(palette = "okabe") | ||
| ``` | ||
|
|
||
| ## 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")) |> plt() | ||
| ``` | ||
|
|
||
| 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) |> plt() | ||
| ``` | ||
|
|
||
| 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") |> plt() | ||
| ``` | ||
|
|
||
| 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. | ||
|
strengejacke marked this conversation as resolved.
|
||
|
|
||
| ```{r} | ||
| estimate_means( | ||
| m, | ||
| c( | ||
| "c172code = c('low level of education', 'high level of education')", | ||
| "barthtot = c(30, 50, 80)" | ||
| ) | ||
| ) |> plt() | ||
|
|
||
| estimate_means(m, c("c172code", "barthtot = [fivenum]")) |> plt() | ||
| ``` | ||
|
|
||
| ## 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")) |> plt() | ||
| ``` | ||
|
|
||
| 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) |> plt() | ||
|
|
||
| estimate_means(m, c("c12hour", "barthtot", "c160age"), range = "grid") |> plt() | ||
| ``` | ||
|
|
||
| ## 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")) |> plt() | ||
| ``` | ||
|
|
||
| ## 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")) |> plt() | ||
| ``` | ||
|
|
||
| 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) |> plt() | ||
| ``` | ||
|
|
||
| ```{r echo=FALSE} | ||
| # reset options | ||
| options( | ||
| modelbased_join_dots = NULL, | ||
| modelbased_estimate = NULL, | ||
| modelbased_select = NULL | ||
| ) | ||
| ``` | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.