Last active
October 27, 2022 14:35
-
-
Save EvaMaeRey/d1a8a837795c0245971970c06f7e21d8 to your computer and use it in GitHub Desktop.
plot continuous and dummy w interaction term
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
| compute_panel_ols_ind <- function(data, scales) { | |
| model <- lm(y ~ x*indicator, | |
| data = data) | |
| data.frame(x = data$x, | |
| y = model$fitted.values, | |
| indicator = data$indicator) | |
| } | |
| StatLminteraction <- ggplot2::ggproto("StatLminteraction", | |
| ggplot2::Stat, | |
| compute_panel = compute_panel_ols_ind, | |
| required_aes = c("x", "y", "indicator"), | |
| default_aes = aes(group = after_stat(indicator)) | |
| ) | |
| geom_lm_interaction <- function(mapping = NULL, data = NULL, | |
| position = "identity", na.rm = FALSE, | |
| show.legend = NA, | |
| inherit.aes = TRUE, ...) { | |
| ggplot2::layer( | |
| stat = StatLminteraction, # proto object from Step 2 | |
| geom = ggplot2::GeomLine, # inherit other behavior | |
| data = data, | |
| mapping = mapping, | |
| position = position, | |
| show.legend = show.legend, | |
| inherit.aes = inherit.aes, | |
| params = list(na.rm = na.rm, ...) | |
| ) | |
| } | |
| geom_lm_interaction_fitted <- function(mapping = NULL, data = NULL, | |
| position = "identity", na.rm = FALSE, | |
| show.legend = NA, | |
| inherit.aes = TRUE, ...) { | |
| ggplot2::layer( | |
| stat = StatLminteraction, # proto object from Step 2 | |
| geom = ggplot2::GeomPoint, # inherit other behavior | |
| data = data, | |
| mapping = mapping, | |
| position = position, | |
| show.legend = show.legend, | |
| inherit.aes = inherit.aes, | |
| params = list(na.rm = na.rm, ...) | |
| ) | |
| } | |
| compute_panel_ols_int_label <- function(data, scales) { | |
| model <- lm(y ~ x*indicator, | |
| data = data) | |
| data.frame(names = model[[1]] %>% names(), | |
| coeff = model[[1]]) %>% | |
| tibble() %>% | |
| slice(-1, -2) %>% | |
| mutate(equation = paste0(coeff %>% good_digits(), "*", names)) %>% | |
| pull(equation) %>% | |
| paste(collapse = " + ") -> | |
| dummies | |
| data.frame(x = mean(data$x), | |
| y = mean(data$y), | |
| label = paste0("y = ", | |
| model$coefficients[2] %>% good_digits(), | |
| "x + ", | |
| dummies, | |
| " + ", | |
| model$coefficients[1] %>% good_digits() %>% str_wrap(50) | |
| )) | |
| } | |
| StatOlsintformula <- ggplot2::ggproto("StatOlsintformula", | |
| ggplot2::Stat, | |
| compute_panel = compute_panel_ols_int_label, | |
| required_aes = c("x", "y", "indicator") | |
| ) | |
| geom_lm_interaction_formula <- function(mapping = NULL, data = NULL, | |
| position = "identity", na.rm = FALSE, show.legend = NA, | |
| inherit.aes = TRUE, ...) { | |
| ggplot2::layer( | |
| stat = StatOlsintformula, geom = ggplot2::GeomLabel, data = data, mapping = mapping, | |
| position = position, show.legend = show.legend, inherit.aes = inherit.aes, | |
| params = list(na.rm = na.rm, ...) | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment