Plot Results of Goodness-of-Fit Test to Compare Two Samples
Source:R/plot.gofTwoSample.R
plot.gofTwoSample.RdPlot the results of calling the function gofTest to compare
two samples. gofTest returns an object of class "gofTwoSample"
when supplied with both the arguments y and x.
plot.gofTwoSample provides five different kinds of plots.
The function plot.gofTwoSample is automatically called by plot
when given an object of class "gofTwoSample". The names of other functions
associated with goodness-of-fit test are listed under Goodness-of-Fit Tests.
Usage
# S3 method for class 'gofTwoSample'
plot(x, plot.type = "Summary",
captions = list(PDFs = NULL, CDFs = NULL, QQ = NULL, MDQQ = NULL, Results = NULL),
x.labels = list(PDFs = NULL, CDFs = NULL, QQ = NULL, MDQQ = NULL),
y.labels = list(PDFs = NULL, CDFs = NULL, QQ = NULL, MDQQ = NULL),
same.window = FALSE, ask = same.window & plot.type == "All", x.points.col = "blue",
y.points.col = "black", points.pch = 1, jitter.points = TRUE, discrete = FALSE,
plot.pos.con = 0.375, x.ecdf.col = "blue", y.ecdf.col = "black",
x.ecdf.lwd = 3 * par("cex"), y.ecdf.lwd = 3 * par("cex"), x.ecdf.lty = 1,
y.ecdf.lty = 4, add.line = TRUE,
digits = ifelse(plot.type == "Summary", 2, .Options$digits), test.result.font = 1,
test.result.cex = ifelse(plot.type == "Summary", 0.9, 1) * par("cex"),
test.result.mar = c(0, 0, 3, 0) + 0.1,
cex.main = ifelse(plot.type == "Summary", 1.2, 1.5) * par("cex"),
cex.axis = ifelse(plot.type == "Summary", 0.9, 1) * par("cex"),
cex.lab = ifelse(plot.type == "Summary", 0.9, 1) * par("cex"),
main = NULL, xlab = NULL, ylab = NULL, xlim = NULL, ylim = NULL,
add.om.title = TRUE,
oma = if (plot.type == "Summary" & add.om.title) c(0, 0, 4, 0) else c(0, 0, 0, 0),
om.title = NULL, om.font = 2, om.cex.main = 1.5 * par("cex"), om.line = 0, ...)Arguments
- x
an object of class
"gof". Seegof.objectfor details.- plot.type
character string indicating what kind of plot to create. Only one particular plot type will be created, unless
plot.type="All", in which case all plots will be created sequentially. The possible values ofplot.typeare:"Summary"(the default),"PDFs: Observed","CDFs: Observed","Q-Q Plot","Tukey M-D Q-Q Plot","Test Results", and"All".
See the DETAILS section for more information.- captions
a list with 1 to 5 components with the names
"PDFs","CDFs","QQ","MDQQ", and/or"Results". Each component either has the valueNULLor else it is a character string containing the title for that particular kind of plot. When the component has the valueNULL(the default), a default title is used. This argument is useful when you are creating more than one kind of plot with a single call toplot.gofTwoSample(i.e., whenplot.type="Summary"orplot.type="All") and you want to specify titles different from the default ones. If you are creating only one kind of plot, then you can just use themainargument to specify a title different from the default one.- x.labels
a list of 1 to 4 components with the names
"PDFs","CDFs","QQ", and/or"MDQQ". Each component either has the valueNULLor else it is a character string containing the label for the \(x\)-axis for that particular kind of plot. When the component has the valueNULL(the default), a default \(x\)-axis label is used. This argument is useful when you are creating more than one kind of plot with a single call toplot.gofTwoSample(i.e., whenplot.type="Summary"orplot.type="All") and you want to specify \(x\)-axis labels different from the default ones. If you are creating only one plot, then you can just use thexlabargument to specify an \(x\)-axis label different from the default one.- y.labels
a list of 1 to 4 components with the names
"PDFs","CDFs","QQ", and/or"MDQQ". Each component either has the valueNULLor else it is a character string containing the label for the \(y\)-axis for that particular kind of plot. When the component has the valueNULL(the default), a default \(y\)-axis label is used. This argument is useful when you are creating more than one kind of plot with a single call toplot.gofTwoSample(i.e., whenplot.type="Summary"orplot.type="All") and you want to specify \(y\)-axis labels different from the default ones. If you are creating only one plot, then you can just use theylabargument to specify a \(y\)-axis label different from the default one.- same.window
logical scalar indicating whether to produce all plots in the same graphics window (
same.window=TRUE), or to create a new graphics window for each separate plot (same.window=FALSE; the default). The argument is relevant only whenplot.type="All".- ask
logical scalar supplied to the function
devAskNewPage, indicating whether to prompt the user before creating a new plot within a single graphics window. The default value isFALSEunlesssame.window=TRUEandplot.type == "All".
Arguments associated with plot.type="PDFs: Observed":
- x.points.col
a character string or numeric scalar determining the color of the plotting symbol used to display the distribution of the observed
xvalues that were supplied togofTest. The default value isx.points.col="blue". See the entry forcolin the R help file forparfor more information.- y.points.col
a character string or numeric scalar determining the color of the plotting symbol used to display the distribution of the observed
yvalues that were supplied togofTest. The default value isy.points.col="black". See the entry forcolin the R help file forparfor more information.- points.pch
a character string or numeric scalar determining the plotting symbol used to display the distribution of the observed
xandyvalues that were supplied togofTest. The default value ispoints.pch=1. See the entry forpchin the R help file forparfor more information.- jitter.points
logical scalar indicating whether to jitter the points in the strip chart. The default value is
jitter.points=TRUE.
Arguments associated with plot.type="CDFs: Observed":
- discrete
logical scalar indicating whether the two distributions are considered to be discrete (
discrete=TRUE) or not(discrete=FALSE; the default). Whendiscrete=TRUE, the empirical CDFs are plotted as step functions.- plot.pos.con
numeric scalar between 0 and 1 containing the value of the plotting position constant used to construct the observed (empirical) CDFs. The default value is
plot.pos.con=0.375. See the help files forecdfPlotandqqPlotfor more information and the motivation for this choice of values.NOTE: This argument is also used to determine the value of the plotting position constant for the Q-Q plot (
plot.type="Q-Q Plot"), or the Tukey Mean-Difference Q-Q plot (plot.type="Tukey M-D Q-Q Plot").- x.ecdf.col
a character string or numeric scalar determining the color of the line used to display the empirical CDF for the
xvalues that were supplied togofTest. The default value isx.ecdf.col="blue". See the entry forcolin the R help file forparfor more information.- y.ecdf.col
a character string or numeric scalar determining the color of the line used to display the empirical CDF for the
yvalues that were supplied togofTest. The default value isy.ecdf.col="black". See the entry forcolin the R help file forparfor more information.- x.ecdf.lwd
numeric scalar determining the width of the line used to display the empirical CDF for the
xvalues that were supplied togofTest. The default value isx.ecdf.lwd=3*par("cex"). See the entry forlwdin the R help file forparfor more information.- y.ecdf.lwd
numeric scalar determining the width of the line used to display the empirical CDF for the
yvalues that were supplied togofTest. The default value isy.ecdf.lwd=3*par("cex"). See the entry forlwdin the R help file forparfor more information.- x.ecdf.lty
numeric scalar determining the line type used to display the empirical CDF for the
xvalues that were supplied togofTest. The default value isx.ecdf.lty=1. See the entry forltyin the R help file forparfor more information.- y.ecdf.lty
numeric scalar determining the line type used to display the empirical CDF for the
yvalues that were supplied togofTest. The default value isy.ecdf.lty=4. See the entry forltyin the R help file forparfor more information.
Arguments associated with plot.type="Q-Q Plot" or plot.type="Tukey M-D Q-Q Plot":
As explained above, plot.pos.con is used for these plot types. Also:
- add.line
logical scalar indicating whether to add a line to the plot. If
add.line=TRUEandplot.type="Q-Q Plot", a 0-1 line is added to the plot. Ifadd.line=TRUEandplot.type="Tukey M-D Q-Q Plot", a horizontal line at \(y=0\) is added to the plot. The default value isadd.line=TRUE.
Arguments associated with plot.type="Test Results"
- digits
scalar indicating how many significant digits to print for the test results when
plot.type="Summary"orplot.type="Test Results". Ifplot.type == "Summary", the default value isdigits=2, otherwise it is.Options$digits(i.e., the current setting ofoptions("digits")).- test.result.font
numeric scalar indicating which font to use to print out the test results. The default value is
test.result.font=1. See the description of thefontargument in the help file forparfor more information. You may get better results if you use a font number that corresponds to a fixed font (e.g., courier).- test.result.cex
numeric scalar indicating the value of
cexto use to print out the test results. The default value is0.9*par("cex")whenplot.type="Summary", otherwise it ispar("cex"). See the description of thecexargument in the help file forparfor more information.- test.result.mar
numeric vector indicating the value of
marto use to print out the test results. The default value istest.result.mar=c(0, 0, 3, 0)+0.1. See the description of themarargument in the help file forparfor more information.
Arguments associated with plot.type="Summary"
- add.om.title
logical scalar indicating whether to add a title in the outer margin when
plot.type="Summary". The default value isadd.om.title=TRUE.- om.title
character string containing the outer margin title. The default value is
om.title=NULL, which will result in a default title.- om.font
numeric scalar indicating the font to use for the outer margin. The default value is
om.font=2.- om.cex.main
numeric scalar indicating the value of
cexfor the outer margin title. The default value is1.75 * par("cex").- om.line
numeric scalar indicating the line to place the outer margin title on. The default value is
om.line=0.5.
Graphics parameters:
- cex.main, cex.axis, cex.lab, main, xlab, ylab, xlim, ylim, oma, ...
additional graphics parameters. See the help file for
par.
Details
The function plot.gofTwoSample is a method for the generic function
plot for the class "gofTwoSample" (see gofTwoSample.object).
It can be invoked by calling plot and giving it an object of
class "gofTwoSample" as the first argument, or by calling
plot.gofTwoSample directly, regardless of the class of the object given
as the first argument to plot.gofTwoSample.
Plots associated with the goodness-of-fit test are produced on the current graphics device. These can be one or all of the following:
Observed distributions (
plot.type="PDFs: Observed").Observed CDFs (
plot.type="CDFs: Observed"). See the help file forcdfCompare.Q-Q Plot (
plot.type="Q-Q Plot"). See the help file forqqPlot.Tukey mean-difference Q-Q plot (
plot.type="Tukey M-D Q-Q Plot"). See the help file forqqPlot.Results of the goodness-of-fit test (
plot.type="Test Results"). See the help file forprint.gofTwoSample.
See the help file for gofTest for more information.
References
Chambers, J. M. and Hastie, T. J. (1992). Statistical Models in S. Wadsworth & Brooks/Cole.
Author
Steven P. Millard (EnvStats@ProbStatInfo.com)
Examples
# Create an object of class "gofTwoSample" then plot the results.
# (Note: the call to set.seed simply allows you to reproduce
# this example.)
set.seed(300)
dat1 <- rnorm(20, mean = 3, sd = 2)
dat2 <- rnorm(10, mean = 1, sd = 2)
gof.obj <- gofTest(x = dat1, y = dat2)
# Summary plot (the default)
#---------------------------
if (interactive()) dev.new()
plot(gof.obj)
# Make your own titles for the summary plot
#------------------------------------------
if (interactive()) dev.new()
plot(gof.obj, captions = list(PDFs = "Compare PDFs",
CDFs = "Compare CDFs", QQ = "Q-Q Plot", Results = "Results"),
om.title = "Summary Plot")
# Just the Q-Q Plot
#------------------
if (interactive()) dev.new()
plot(gof.obj, plot.type="Q-Q")
# Make your own title for the Q-Q Plot
#-------------------------------------
if (interactive()) dev.new()
plot(gof.obj, plot.type="Q-Q", main = "Q-Q Plot")
#==========
# Clean up
#---------
rm(dat1, dat2, gof.obj)
graphics.off()