pbreheny / visreg

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

Allow a custom X reference for the contrast plot #32

Closed jealie closed 7 years ago

jealie commented 7 years ago

Sometimes, the meaningful "reference" for a contrast plot is not the average value or the first factor, and the user might want to set a custom reference for the variable.

In the Air Quality dataset, for example, the right reference for the discretized heat could be the "Mild" instead of "Cool"; and the absence of wind (speed of 0) might make more sense for some people.

# Same example as in the vignette
data('airquality')
airquality$Heat = cut(airquality$Temp, 3, labels=c("Cool", "Mild", "Hot"))
fit.heat = lm(Ozone ~ Solar.R + Wind + Heat, data=airquality)
par(mfrow=c(2,2))
visreg(fit.heat, "Heat", type="contrast", main='default reference (first factor)')
visreg(fit.heat, "Wind", type="contrast", main='default reference (mean)')
visreg(fit.heat, "Heat", type="contrast", main='custom reference (second factor)', cond=list(Heat=2))
visreg(fit.heat, "Wind", type="contrast", main='custom reference (Wind speed: 0)', cond=list(Wind=0))

example

I have written the few lines of code to handle these cases and I will send a pull request shortly (in case you think this is a worthwhile addition).

Thanks for the package!

pbreheny commented 7 years ago

Sorry it took a while for me to merge your pull request, but I was busy with other projects. This is great -- thanks for the improvement! visreg_2.4.1, incorporating this change, is now headed to CRAN.