Skip to content

Instantly share code, notes, and snippets.

@strengejacke
Last active July 22, 2025 14:54
Show Gist options
  • Save strengejacke/4ccb47dab2c97d3bb48b7805122b1e88 to your computer and use it in GitHub Desktop.
Save strengejacke/4ccb47dab2c97d3bb48b7805122b1e88 to your computer and use it in GitHub Desktop.
`tinyplot` methods for _modelbased_
Replicates plots from this vignette:
https://easystats.github.io/modelbased/articles/plotting.html
Not working
m <- lm(neg_c_7 ~ c12hour * barthtot * c160age, data = efc)
# gpplot2 version
estimate_means(m, c("c12hour", "barthtot", "c160age")) |> plot()
## FIXME not working yet
estimate_means(m, c("c12hour", "barthtot", "c160age")) |> plt()
## TODO
states <- as.data.frame(state.x77)
states$HSGrad <- states$`HS Grad`
m_mod <- lm(Income ~ HSGrad + Murder * Illiteracy, data = states)
slopes <- estimate_slopes(m_mod, "Murder", by = "Illiteracy", length = 200)
plt(slopes)
@strengejacke
Copy link
Author

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()

estimate_means(m, "barthtot") |> plt()

m <- lm(neg_c_7 ~ e16sex * c172code + e42dep, data = efc)
estimate_means(m, c("e16sex", "c172code")) |> plt()

m <- lm(neg_c_7 ~ barthtot * c172code + e42dep, data = efc)
estimate_means(m, c("barthtot", "c172code")) |> plt()

estimate_means(m, c("barthtot", "c172code")) |> plt(facet = ~c172code)

estimate_means(m, c("c172code", "barthtot")) |> plt()

estimate_means(m, c("c172code", "barthtot"), length = 20) |> plt()

estimate_means(m, c("c172code", "barthtot"), range = "grid") |> plt()

estimate_means(m, c("c172code", "barthtot = [fivenum]")) |> plt()

m <- lm(neg_c_7 ~ c12hour * barthtot * c160age, data = efc)
# gpplot2 version
estimate_means(m, c("c12hour", "barthtot", "c160age")) |> plot()

## FIXME not working yet
estimate_means(m, c("c12hour", "barthtot", "c160age")) |> plt()
#> Warning in tinyplot.default(x = x, y = y, by = by, facet = facet, facet.args = facet.args, : 
#> Continuous legends not supported for this plot type. Reverting to discrete legend.

estimate_means(m, c("c12hour", "barthtot", "c160age"), range = "grid") |> plt()

m <- lm(neg_c_7 ~ e16sex * c172code * e42dep, data = efc)
estimate_means(m, c("e16sex", "c172code", "e42dep")) |> plt()

m <- lm(neg_c_7 ~ e16sex * c12hour + e16sex * I(c12hour^2), data = efc)
estimate_means(m, c("c12hour", "e16sex")) |> plt()

estimate_means(m, c("c12hour", "e16sex"), length = 200) |> plt()

## TODO
states <- as.data.frame(state.x77)
states$HSGrad <- states$`HS Grad`
m_mod <- lm(Income ~ HSGrad + Murder * Illiteracy, data = states)
slopes <- estimate_slopes(m_mod, "Murder", by = "Illiteracy", length = 200)
plt(slopes)
#> Warning in min(x, na.rm = na.rm): no non-missing arguments to min; returning
#> Inf
#> Warning in max(x, na.rm = na.rm): no non-missing arguments to max; returning
#> -Inf

#> Error in plot.window(xlim = c(1L, 8L), ylim = c(Inf, -Inf), asp = NA, : need finite 'ylim' values

Created on 2025-07-22 with reprex v2.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment