Skip to content

visualisation_recipe with transformed responses  #151

Closed
@bwiernik

Description

Currently, the default plot produced by estimate_expected() with a transformed response variable isn't correct. The data points are in the raw metric, but the predicted line is in the transformed metric.

We should either:

  1. apply the same transformation to the data when plotting as used in the model specification
  2. include a transform in estimate_expectation() to allow back-transforming of predictions
library(modelbased)
library(ggplot2)

m_inline_trans <- lm(log(dist) ~ speed, data = cars)

estimate_expectation(m_inline_trans) |> 
  plot()

estimate_expectation(m_inline_trans) |> 
  dplyr::mutate(
    Predicted = exp(Predicted),
    SE = SE * exp(Predicted),
    CI_low = exp(CI_low),
    CI_high = exp(CI_high)
  ) |> 
  plot()

cars$log_dist <- log(cars$dist)
m_pre_trans <- lm(log_dist ~ speed, data = cars)

estimate_expectation(m_pre_trans) |>
  plot()

estimate_expectation(m_pre_trans) |>
  plot() +
  scale_y_continuous(trans = "exp")
#> Warning in self$trans$inverse(limits): NaNs produced
#> Error in if (zero_range(as.numeric(limits))) {: missing value where TRUE/FALSE needed

estimate_expectation(m_pre_trans) |>
  ggplot() +
  aes(x = speed) +
  geom_point(
    aes(y = log_dist), data = insight::get_data(m_pre_trans)
  ) +
  geom_ribbon(
    aes(ymin = CI_low, ymax = CI_high),
    alpha = .5
  ) +
  geom_line(
    aes(y = Predicted)
  ) 

Created on 2021-10-08 by the reprex package (v2.0.1)

Metadata

Assignees

No one assigned

    Labels

    Enhancement 💥Implemented features can be improved or revisedFeature idea 🔥New feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions