pbreheny / visreg

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

xtrans argument #31

Closed ecologist1234 closed 7 years ago

ecologist1234 commented 7 years ago

Hello, Visreg is wonderful! Thank you! I am trying to use the xtrans argument to back-transform standardized regression predictors for plotting using the raw values. The raw predictors were transformed by [x-mean(x)] / sd(x), so now I want the back-transformation to be [y*sd(x)]+mean(x).

I tried this:

visreg(model.name, "x.1.standardized", by="x.2.standardized", scale="response", overlay=T, xtrans = ((x.1.standardized*sd(x))+mean(x)))

When I do this I get an error "Error in getXY(fit, f, name, nn, cond.j, type, trans, xtrans, alpha, jitter, : object 'X.1.standardized' not found", but of course the variable is in the model object. What am doing wrong? I don't see any instructions for how to use xtrans in the documentation.

Also, am I correct that I it's fine to leave the second x-variable "x.2.standardized" in the "by" argument standardized? It is just there to provide breaks, and I assume that does not interfere with the transformation.

Thank you. Again, I have gotten a lot of mileage out of visreg, even as a slow-learner!

pbreheny commented 7 years ago

Glad to hear you're finding visreg useful! xtrans needs to be a function; you're supplying a vector of numbers. Here's a minimal example:

m <- mean(airquality$Wind, na.rm=TRUE)
s <- sd(airquality$Wind, na.rm=TRUE)
airquality$StdWind <- (airquality$Wind - m)/s
fit <- lm(Ozone ~ StdWind*Temp, airquality)
visreg(fit, 'StdWind', by='Temp', xtrans = function(x) {m + x*s})

Also, just as a suggestion, it's probably easier to use scale in the formula, then you don't need to worry about xtrans:

fit <- lm(Ozone ~ scale(Wind)*Temp, airquality)
visreg(fit, 'Wind', by='Temp')

This produces the exact same plot as the first chunk of code.