library(visreg)
knitr::opts_knit$set(aliases=c(h = 'fig.height', w = 'fig.width'))
knitr::opts_chunk$set(comment='#', collapse=TRUE, cache=FALSE, tidy=FALSE)
knitr::knit_hooks$set(small.mar = function(before, options, envir) {
  if (before) par(mar = c(4, 4, .1, .1))
})

As noted in getting started, the default behavior of visreg when constructing a conditional plot is to fill in the other variables with either the median (for continuous variables) or the most common category (for categorical variables). This can be modified using the cond argument, which offers more explicit control over what to condition on. Note that this has no bearing on contrast plots (at least, in the absence of interactions), which do not depend on the other terms in the model.

The cond argument must be provided as a named list. Each element of that list specifies the value for one of the terms in the model; any elements left unspecified are filled in with the median/most common category. For example, let's construct the a plot of wind vs. ozone, but condition on three different values for temperature: (1) a cold temperature of 50 degrees (2) the default median temperature of r median(airquality$Tem) degrees and (3) a hot temperature of 100 degrees.

fit <- lm(Ozone ~ Solar.R + Wind + Temp, data=airquality)
par(mfrow=c(1,3))
visreg(fit, "Wind", cond=list(Temp=50))
visreg(fit, "Wind")
visreg(fit, "Wind", cond=list(Temp=100))

A few observations/remarks:

Finally, recall that this model had three explanatory variables; in the above example, visreg calculated the conditional response by filling in solar radiation with its median value, as it was not specified otherwise in the cond argument.



pbreheny/visreg documentation built on March 20, 2024, 1:07 a.m.