pbreheny / visreg

Visualization of regression functions
http://pbreheny.github.io/visreg/
61 stars 18 forks source link

Arguments `xtrans` and `xlim` are incompatible #81

Closed dschlaep closed 4 years ago

dschlaep commented 4 years ago

visreg:::setupV deals with an xlim argument (on the predictor-scale) and creates a correct v object. However, visreg passes this same xlim argument to plot.visreg which is then overriding the internally correctly calculated values by visreg:::visregPlot, visreg:::visregOverlayPlot, etc. from the data with if (is.factor(xx)) c(0, 1) else range(xx).

packageVersion("visreg")
# 2.6.0

# This works as expected
fit1 <- lm(Ozone ~ Solar.R + log(Wind) + Temp, data = airquality)
visreg::visreg(fit1, "Wind")
visreg::visreg(fit1, "Wind", xlim = c(10, 15))

# Passing a x-transformation via `xtrans` doesn't work together with `xlim`
#  - `xlim` should be on the predictor-scale for function `visreg:::setupV`
#  - and, simultaneously, `xlim` should be on the transformed-scale for the plotting functions
airquality2 <- airquality
airquality2[, "logWind"] <- log(airquality[, "Wind"])

fit2 <- lm(Ozone ~ Solar.R + logWind + Temp, data = airquality2)
visreg::visreg(fit2, "logWind", xtrans = function(x) exp(x))
visreg::visreg(fit2, "logWind", xtrans = function(x) exp(x), xlim = log(c(10, 15)))
visreg::visreg(fit2, "logWind", xtrans = function(x) exp(x), xlim = c(10, 15))

# Separating the `visreg` call from the `plot.visreg` call works correctly
v2 <- visreg::visreg(fit2, "logWind",
  xtrans = function(x) exp(x),
  xlim = log(c(10, 15)), # predictor-scale
  plot = FALSE
)

plot(v2) # `xlim` internally calculated
plot(
  v2,
  xlim = c(10, 15) # transformed scale
)

I see a few options to handle this issue (and I am sure there are others):

Many thanks for this great package!

pbreheny commented 4 years ago

Thanks for letting me know! This is fixed now.

FYI, my original motivation for using xlim in setting up x was to avoid unnecessary calculations that aren't going to end up plotted anyway, but as you show, this is pretty fragile and prone to interact badly with other options.