TeachingDemos (version 2.10)

TkPredict: Plot predicted values from a model against one of the predictors for a given value of the othe predictors

Description

These functions create a plot of predicted values vs. one of the predictors for given values of the other predictors. TkPredict further creates a Tk gui to allow you to change the values of the other predictors.

Usage

Predict.Plot(model, pred.var, ..., type='response', add=FALSE,
plot.args=list(), n.points=100, ref.val, ref.col='green', ref.lty=1,
data)
TkPredict(model, data, pred.var, ...)

Arguments

model

A model of class 'lm' or 'glm' (or possibly others) from which to plot predictions.

pred.var

A character string indicating which predictor variable to put on the x-axis of the plot.

...

for Predict.Plot The predictor variables and their values for the predictions. See below for detail.

type

The type value passed on to the predict function.

add

Whether to add a line to the existing plot or start a new plot.

plot.args

A list of additional options passed on to the plotting function.

n.points

The number of points to use in the approximation of the curve.

ref.val

A reference value for the pred.var, a reference line will be drawn at this value to the corresponding predicted value.

ref.col, ref.lty

The color and line type of the reference line if plotted.

data

The data frame or environment where the variables that the model was fit to are found. If missing, the model will be examined for an attempt find the needed data.

Value

These functions are run for their side effects of creating plots and do not return anything.

Details

These functions plot the predicted values from a regression model (lm or glm) against one of the predictor variables for given values of the other predictors. The values of the other predictors are passed as the ... argument to Predict.Plot or are set using gui controls in TkPredict (initial values are the medians).

If the variable for the x axis (name put in pred.var) is not included with the ... variables, then the range will be computed from the data argument or the data component of the model argument.

If the variable passed as pred.var is also included in the ... arguments and contains a single value, then this value will be used as the ref.val argument.

If it contains 2 or more values, then the range of these values will be used as the x-limits for the predictions.

When running TkPredict you can click on the "Print Call" button to print out the call of Predict.Plot that will recreate the same plot. Doing this for different combinations of predictor values and editing the plot.args and add arguments will give you a script that will create a static version of the predictions.

See Also

tkrplot, tkexamp, predict

Examples

Run this code
# NOT RUN {
library(splines)

fit.lm1 <- lm( Sepal.Width ~ ns(Petal.Width,3)*ns(Petal.Length,3)+Species,
	data=iris)

Predict.Plot(fit.lm1, pred.var = "Petal.Width", Petal.Width = 1.22,
      Petal.Length = 4.3, Species = "versicolor",
 	plot.args = list(ylim=range(iris$Sepal.Width), col='blue'),
      type = "response")
Predict.Plot(fit.lm1, pred.var = "Petal.Width", Petal.Width = 1.22,
      Petal.Length = 4.3, Species = "virginica",
	plot.args = list(col='red'),
      type = "response", add=TRUE)
Predict.Plot(fit.lm1, pred.var = "Petal.Width", Petal.Width = 1.22,
      Petal.Length = 4.4, Species = "virginica",
	plot.args = list(col='purple'),
      type = "response", add=TRUE)

fit.glm1 <- glm( Species=='virginica' ~ Sepal.Width+Sepal.Length,
	data=iris, family=binomial)

Predict.Plot(fit.glm1, pred.var = "Sepal.Length", Sepal.Width = 1.99,
      Sepal.Length = 6.34, plot.args = list(ylim=c(0,1), col='blue'),
 	type = "response")
Predict.Plot(fit.glm1, pred.var = "Sepal.Length", Sepal.Width = 4.39,
      Sepal.Length = 6.34, plot.args = list(col='red'),
	type = "response", add=TRUE)




if(interactive()){
TkPredict(fit.lm1)

TkPredict(fit.glm1)
}

# }

Run the code above in your browser using DataLab