Closed
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:
- apply the same transformation to the data when plotting as used in the model specification
- include a
transform
inestimate_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)