Learn R Programming

condvis (version 0.2-2)

ceplot: Interactive conditional expectation plot

Description

Creates an interactive conditional expectation plot, which consists of two main parts. One part is a single plot depicting a section through a fitted model surface, or conditional expectation. The other part shows small data summaries which give the current condition, which can be altered by clicking with the mouse.

Usage

ceplot(data, model, response = NULL, S = NULL, C = NULL, sigma = NULL, 
    distance = "euclidean", type = "default", cex.axis = NULL, cex.lab = NULL, 
    tck = NULL, view3d = FALSE, Corder = "default", selectortype = "minimal", 
    conf = FALSE, select.colour = "blue", select.cex = 1)

Arguments

data
a dataframe containing the training data for the model(s) in model
model
either a single model object, or a list containing model objects.
response
integer index of the response variable in data (character variable name is also acceptable)
S
integer index of the 'free' variable(s) to be used in the expectation plot (character variable name(s) also acceptable). Defaults to the first variable in data that is not the response.
C
a list, each element of which is either an integer index of data or a vector of integer indices of length 2 (character variable names are also acceptable). Defaults to the list produced by
sigma
tuning parameter for colouring data by proximity to current conditioning, passed to visualweight.
distance
distance metric to be used in visualweight, choice of: "euclidean" (default) or "maxnorm"
type
type of interactive plot. "default" plots everything on one device. "separate" plots the condition selector plots on a separate device. "shiny" creates and opens a Shiny web application.
cex.axis
scaling parameter for axis text. Default is 0.7.
cex.lab
scaling parameter for axis labels. Default is 0.8.
tck
parameter for axis ticks. Default is -0.05.
view3d
logical. If TRUE, displays the model section as a prespective plot if possible.
Corder
method of arranging variables in C if C is not supplied. See arrangeC
selectortype
only used if type = "separate". "minimal" is the default behaviour, simply calling plotxc on the variables given by C. "pcp" gives a parallel coo
conf
if TRUE, approximate 95% confidence intervals are plotted where possible
select.colour
colour for highlighting current section in condition selector plots
select.cex
scaling used for plot symbols in condition selector plots

Value

  • Returns no value, but produces an interactive plot as a side-effect. The current Rsession is busy until the interactive session is ended by the user, or the graphics device is closed. If using the Shiny option, "Esc" can be pressed to free up the Rsession.

See Also

getGraphicsEvent

Examples

Run this code
## Example 1: Multivariate regression, xs one continuous predictor

mtcars$cyl <- as.factor(mtcars$cyl)

library(mgcv)
model1 <- list(
    quadratic = lm(mpg ~ cyl + hp + wt + I(wt^2), data = mtcars),
    additive = mgcv::gam(mpg ~ cyl + hp + s(wt), data = mtcars))

conditionvars1 <- list(c("cyl", "hp"))

ceplot(data = mtcars, model = model1, response = "mpg", S = "wt", 
    C = conditionvars1, sigma = 0.3, conf = T)

## Example 2: Binary classification, xs one categorical predictor

mtcars$cyl <- as.factor(mtcars$cyl)
mtcars$am <- as.factor(mtcars$am)

library(e1071)
model2 <- list(
    svm = svm(am ~ mpg + wt + cyl, data = mtcars, family = "binomial"),
    glm = glm(am ~ mpg + wt + cyl, data = mtcars, family = "binomial"))

ceplot(data = mtcars, model = model2, S = "wt", sigma = 1, type = "shiny")

## Example 3: Multivariate regression, xs both continuous

mtcars$cyl <- as.factor(mtcars$cyl)
mtcars$gear <- as.factor(mtcars$gear)

library(e1071)
model3 <- list(svm(mpg ~ wt + qsec + cyl + hp + gear, 
    data = mtcars, family = "binomial"))

conditionvars3 <- list(c("cyl","gear"), "hp")

ceplot(data = mtcars, model = model3, S = c("wt", "qsec"), 
    sigma = 1, C = conditionvars3)

ceplot(data = mtcars, model = model3, S = c("wt", "qsec"), 
    sigma = 1, type = "separate", view3d = T)

## Example 4: Multi-class classification, xs both categorical

mtcars$cyl <- as.factor(mtcars$cyl)
mtcars$vs <- as.factor(mtcars$vs)
mtcars$am <- as.factor(mtcars$am)
mtcars$gear <- as.factor(mtcars$gear)
mtcars$carb <- as.factor(mtcars$carb)

library(e1071)
model4 <- list(svm(carb ~ ., data = mtcars, family = "binomial"))

ceplot(data = mtcars, model = model4, S = c("cyl", "gear"), sigma = 3)

## Example 5: Multi-class classification, xs both continuous

data(wine)
wine$Class <- as.factor(wine$Class)
library(e1071)

model5 <- list(svm(Class ~ ., data = wine))

ceplot(data = wine, model = model5, S = c("Hue", "Flavanoids"), sigma = 3)

ceplot(data = wine, model = model5, S = c("Hue", "Flavanoids"), sigma = 3, 
    type = "separate")

ceplot(data = wine, model = model5, S = c("Hue", "Flavanoids"), sigma = 3, 
    type = "separate", selectortype = "pcp")

## Example 6: Multi-class classification, xs with one categorical predictor,
##            and one continuous predictor.

mtcars$cyl <- as.factor(mtcars$cyl)
mtcars$carb <- as.factor(mtcars$carb)

library(e1071)
model6 <- list(svm(cyl ~ carb + wt + hp, data = mtcars, family = "binomial"))

ceplot(data = mtcars, model = model6, sigma = 1, S = c("carb", "wt"), C = "hp")

Run the code above in your browser using DataLab