Last data update: 2014.03.03

R: Specify plots to illustrate Normal and t Hypothesis Tests or...
NormalAndTplotR Documentation

Specify plots to illustrate Normal and t Hypothesis Tests or Confidence Intervals.

Description

Specify plots to illustrate Normal and t Hypothesis Tests or Confidence Intervals.

Usage

NormalAndTplot(mean0, ...)
## Default S3 method:
NormalAndTplot(mean0=0,
             mean1=NA,
             xbar=NA,
             df=Inf, n=1,
             sd=1,
             xlim=c(-3, 3)*sd/sqrt(n) + range(c(mean0, mean1, xbar), na.rm=TRUE),
             ylim, alpha.right=.05, alpha.left=0,
             float=TRUE, ntcolors="original",
             digits=4, digits.axis=digits, digits.float=digits,
             distribution.name=c("normal","z","t","binomial"),
             type=c("hypothesis", "confidence"),
             zaxis=FALSE, z1axis=FALSE,
             cex.z=.5, cex.prob=.6, cex.top.axis=1,
             main=NA, xlab, ylab,
             prob.labels=(type=="hypothesis"),
             xhalf.multiplier=1,
             yhalf.multiplier=1,
             cex.main=1,
             key.axis.padding=4.5,
             number.vars=1,
             sub=NULL,
             NTmethod="default",
             power=FALSE,
             beta=FALSE,
              ...)
## S3 method for class 'htest'
NormalAndTplot(mean0, type="hypothesis", xlim=NULL, mean1=NA, ...,
             xbar, sd, df, n, alpha.left, alpha.right, ## ignored
             distribution.name, sub ## these input arguments will be ignored
             )

Arguments

mean0

Null hypothesis μ_0. When graphing a confidence interval, mean0 will be used for xbar should xbar itself have the value NA.

mean1

Alternative hypothesis μ_1.

xbar

Observed ar{x}.

sd

Standard deviation in the data scale σ for normal-, or s for t-distribution.

df

Degrees of freedom for t-distribution.

n

Number of observations per group.

main, xlab, ylab, xlim, ylim, sub

Standard xyplot arguments. Default values are constructed if these arguments are missing. The input value main=NA forces a new constructed main instead of using the main coming in through the htest methods.

...

Additional xyplot arguments.

number.vars

Number of variables. 1 for a one-sample test, 2 for two-sample tests and paired tests.

alpha.left, alpha.right

For type="hypothesis", the sum of these two numbers is the probability of the Type I Error α. When both of these numbers are positive, there is a two-sided test. Note that it is not required that they be equal. If one of the numbers is 0, then it is a one-sided test. For type="confidence", 1 minus the sum of these two numbers is the confidence level.

float

Logical. If TRUE, then the probabilities α, β, power, and p-values or the confidence value are displayed on the graph. If FALSE, these values are not displayed.

ntcolors

Vector of colors used in the graph. The default value is "original", which implies the ten colors c(col.alpha = "blue", col.notalpha = "lightblue", col.beta = "red", col.power = "pink", col.pvalue = "green", col.pvaluetranslucent = green127, col.critical = "gray50", col.border = black127, col.text = "black", col.conf = "lightgreen"). An alternative is "stoplight" which implies the ten colors c(col.alpha = "red", col.notalpha = "honeydew2", col.beta = "orange", col.power = "pink", col.pvalue = "blue", col.pvaluetranslucent = blue127, col.critical = "gray50", col.border = black127, col.text = "black", col.conf = "lightgreen"). The partially transparent colors are: black127="#0000007F", green127="#00FF007F", blue127="#0000FF7F". The user can enter any color scheme by specifying a vector of ten named colors. The names are: col.alpha, col.notalpha, col.beta, col.power, col.pvalue, col.pvaluetranslucent, col.critical, col.border, col.text, col.conf.

digits.axis, digits.float, digits

digits.axis is the number of significant digits for the top axis. digits.float is the number of significant digits for the floating probability values on the graph. digits is a convenience argument to set both digits.axis and digits.float at the same time. These number is passed to the format function.

distribution.name

Name of distribution.

type

"hypothesis" for a Hypothesis Test graph, or "confidence" for a Confidence Interval graph.

zaxis, z1axis

Logical or list. Should the z-axis centered on μ_0, or the z_1-axis centered on μ_1, be displayed? The list version of the argument must have two components at and labels as specified in panel.axis.

cex.z, cex.prob, cex.top.axis, cex.main

cex.z is the cex value for the z and z_1 axes on the plot. cex.prob is the cex value for the floating probabilities on the graph. cex.top.axis is the cex value for the top axis values. cex.main is the cex value for the main title.

key.axis.padding

tuning constant to create additional room above the graph for a larger cex.main to fit.

prob.labels

logical. If TRUE label the floating probability values with their name, such as α. If FALSE, then don't label them. The default is TRUE for type="hypothesis" and FALSE for type="confidence".

xhalf.multiplier, yhalf.multiplier

Numerical tuning constants to control the width and height of the floating probability values. Empirically, we need a smaller value for the shiny app then we need for direct writing onto a graphic device.

NTmethod

Character string used when shiny=TRUE. It is normally calculated by the methods. NTmethod tells shiny how to use or ignore the df and n sliders.

"htest" objects by default are interpreted as a single observation (n=1) of a t-statistic with df degrees of freedom. The slider will let the user change the df, but not the n.

"power.htest" objects are interpreted as a set of n obervations per group and df is calculated as (n-1) for single-sample tests and as 2(n-1) for two-sample tests. The slider will let the user change n and will calculate the revised df.

For the normal approximation to the binomial (distribution.name="binomial"), only n is meaningful. The df is always ignored.

For the default situation of t, determined by the initially specified sample size n>1, the degrees of freedom is calculated as (n-1) for single-sample tests and as 2(n-1) for two-sample tests. The default z, is initially specified by a sample size n=1.

In all cases except the "binomial", the user can change the interpretation of the n and df sliders. The interpretation when both n and df are under user control is not always obvious.

power, beta

Logical. If TRUE, then display that graph, else don't display it. Passed forward to powerplot.

Details

The graphs produced by this single function cover most of the first semester introductory Statistics course. The htest method plots the results of the stats::t.test function.

NormalAndTplot is built on xyplot. Most of the arguments detailed in xyplot documentation work to control the appearance of the plot.

Value

"trellis" object.

Note

This function is built on lattice and latticeExtra. It supersedes the similar function normal.and.t.dist built on base graphics that is used in many displays in the book by Erich Neuwirth and me: R through Excel, Springer (2009). http://www.springer.com/978-1-4419-0051-7. Many details, particularly the alternate color scheme and the concept of floating probability labels, grew out of discussions that Erich and I have had since the book was published. The method for "htest" objects incorporates ideas that Jay Kerns and I developed at the 2011 UseR! conference. This version incorporates some ideas suggested by Moritz Heene.

Author(s)

Richard M. Heiberger (rmh@temple.edu)

See Also

NTplot

Examples

   NTplot(mean0=0, mean1=2,  xbar=1.8,  xlim=c(-3, 5))
   NTplot(mean0=0, mean1=2,  xbar=1.8,  xlim=c(-3, 5), distribution.name="t", df=4)
   NTplot(mean0=100, sd=12, mean1=113,  xbar=105,  xlim=c(92, 120), n=20)
   NTplot(mean0=100, sd=12, mean1=113,  xbar=105,  xlim=c(92, 120), n=20,
          zaxis=TRUE, z1axis=TRUE)
   NTplot(mean0=100, sd=12,  xbar=105,  xlim=c(92, 108), n=20, ntcolors="stoplight")
   NTplot(xbar=95, sd=10, xlim=c(65, 125), type="confidence",
          alpha.left=.025, alpha.right=.025)


x <- rnorm(12, mean=.78)
x.t <- t.test(x)
NTplot(x.t)
NTplot(x.t, type="confidence")
x.tg <- t.test(x, alternative="greater")
NTplot(x.tg)

y <- rnorm(12, mean=-.05)
xy.t <- t.test(x, y)
NTplot(xy.t)
NTplot(xy.t, type="confidence")

## Not run: 
     NTplot(shiny=TRUE)  ## with any other arguments for initialization of the shiny app.

## End(Not run)

## Not run: 
printbook.colors <- c(  ## based on "original" colors
  col.alpha             = "blue",
  col.notalpha          = "lightcyan",  ## this value is nonstandard
  col.beta              = "red",
  col.power             = "pink",
  col.pvalue            = "green",
  col.pvaluetranslucent = HH:::ColorWithAlpha("springgreen"),  ## this value is nonstandard
  col.critical          = "gray50",
  col.border            = HH:::ColorWithAlpha("black"),
  col.text              = "black",
  col.conf              = "lightgreen"
)
NTplot(ntcolors=printbook.colors) ## different colors

## End(Not run)

## Not run: 
## mean1 and xbar
  NTplot(mean0=0, mean1=2,  xbar=1.8,  xlim=c(-3, 5))
  NTplot(mean0=0, mean1=-2, xbar=-1.8, xlim=c(-5, 3),
         alpha.left=.05,  alpha.right=0)
  NTplot(mean0=0, mean1=2,  xbar=2.1,  xlim=c(-3, 5),
         alpha.left=.025, alpha.right=.025)
  NTplot(mean0=0, mean1=-2, xbar=-2.1, xlim=c(-5, 3),
         alpha.left=.025, alpha.right=.025)

## mean1
  NTplot(mean0=0, mean1=2,  xbar=NA, xlim=c(-3, 5))
  NTplot(mean0=0, mean1=-2, xbar=NA, xlim=c(-5, 3),
         alpha.left=.05,  alpha.right=0)
  NTplot(mean0=0, mean1=2,  xbar=NA, xlim=c(-3, 5),
         alpha.left=.025, alpha.right=.025)
  NTplot(mean0=0, mean1=-2, xbar=NA, xlim=c(-5, 3),
         alpha.left=.025, alpha.right=.025)

## xbar
  NTplot(mean0=0, mean1=NA, xbar=1.8,  xlim=c(-3, 5))
  NTplot(mean0=0, mean1=NA, xbar=-1.8, xlim=c(-5, 3),
         alpha.left=.05,  alpha.right=0)
  NTplot(mean0=0, mean1=NA, xbar=2.1,  xlim=c(-3, 5),
         alpha.left=.025, alpha.right=.025)
  NTplot(mean0=0, mean1=NA, xbar=-2.1, xlim=c(-5, 3),
         alpha.left=.025, alpha.right=.025)

## t distribution
## mean1 and xbar
  NTplot(mean0=0, mean1=2,  xbar=1.8,  xlim=c(-3, 5),
         distribution.name="t", df=4)
  NTplot(mean0=0, mean1=-2, xbar=-1.8, xlim=c(-5, 3),
         alpha.left=.05,  alpha.right=0, distribution.name="t", df=4)
  NTplot(mean0=0, mean1=2,  xbar=2.1,  xlim=c(-3, 5),
         alpha.left=.025, alpha.right=.025, distribution.name="t", df=4)
  NTplot(mean0=0, mean1=-2, xbar=-2.1, xlim=c(-5, 3),
         alpha.left=.025, alpha.right=.025, distribution.name="t", df=4)

## mean1
  NTplot(mean0=0, mean1=2,  xbar=NA, xlim=c(-3, 5),
         distribution.name="t", df=4)
  NTplot(mean0=0, mean1=-2, xbar=NA, xlim=c(-5, 3),
         alpha.left=.05,  alpha.right=0, distribution.name="t", df=4)
  NTplot(mean0=0, mean1=2,  xbar=NA, xlim=c(-3, 5),
         alpha.left=.025, alpha.right=.025, distribution.name="t", df=4)
  NTplot(mean0=0, mean1=-2, xbar=NA, xlim=c(-5, 3),
         alpha.left=.025, alpha.right=.025, distribution.name="t", df=4)

## xbar
  NTplot(mean0=0, mean1=NA, xbar=1.8,  xlim=c(-3, 5),
         distribution.name="t", df=4)
  NTplot(mean0=0, mean1=NA, xbar=-1.8, xlim=c(-5, 3),
         alpha.left=.05,  alpha.right=0, distribution.name="t", df=4)
  NTplot(mean0=0, mean1=NA, xbar=2.1,  xlim=c(-3, 5),
         alpha.left=.025, alpha.right=.025, distribution.name="t", df=4)
  NTplot(mean0=0, mean1=NA, xbar=-2.1, xlim=c(-5, 3),
         alpha.left=.025, alpha.right=.025, distribution.name="t", df=4)

## confidence intervals

  NTplot(mean0=0, xlim=c(-3, 4), type="confidence")
  NTplot(xbar=01, xlim=c(-3, 4), type="confidence")
  NTplot(mean0=0, xlim=c(-4, 3), type="confidence",
         alpha.left=.05,  alpha.right=0)
  NTplot(mean0=0, xlim=c(-3, 3), type="confidence",
         alpha.left=.025, alpha.right=.025)
  NTplot(mean0=95, sd=10, xlim=c(65, 125), type="confidence",
         alpha.left=.025, alpha.right=.025)
  NTplot(mean0=95, sd=10, xlim=c(65, 125), type="confidence",
         alpha.left=.025, alpha.right=.025,
         distribution="t", df=10)


## End(Not run)

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(HH)
Loading required package: lattice
Loading required package: grid
Loading required package: latticeExtra
Loading required package: RColorBrewer
Loading required package: multcomp
Loading required package: mvtnorm
Loading required package: survival
Loading required package: TH.data
Loading required package: MASS

Attaching package: 'TH.data'

The following object is masked from 'package:MASS':

    geyser

Loading required package: gridExtra
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/HH/NormalAndTplot.Rd_%03d_medium.png", width=480, height=480)
> ### Name: NormalAndTplot
> ### Title: Specify plots to illustrate Normal and t Hypothesis Tests or
> ###   Confidence Intervals.
> ### Aliases: NormalAndTplot NormalAndTplot.default NormalAndTplot.htest
> ### Keywords: hplot
> 
> ### ** Examples
> 
>    NTplot(mean0=0, mean1=2,  xbar=1.8,  xlim=c(-3, 5))
>    NTplot(mean0=0, mean1=2,  xbar=1.8,  xlim=c(-3, 5), distribution.name="t", df=4)
>    NTplot(mean0=100, sd=12, mean1=113,  xbar=105,  xlim=c(92, 120), n=20)
>    NTplot(mean0=100, sd=12, mean1=113,  xbar=105,  xlim=c(92, 120), n=20,
+           zaxis=TRUE, z1axis=TRUE)
>    NTplot(mean0=100, sd=12,  xbar=105,  xlim=c(92, 108), n=20, ntcolors="stoplight")
>    NTplot(xbar=95, sd=10, xlim=c(65, 125), type="confidence",
+           alpha.left=.025, alpha.right=.025)
> 
> 
> x <- rnorm(12, mean=.78)
> x.t <- t.test(x)
> NTplot(x.t)
> NTplot(x.t, type="confidence")
> x.tg <- t.test(x, alternative="greater")
> NTplot(x.tg)
> 
> y <- rnorm(12, mean=-.05)
> xy.t <- t.test(x, y)
> NTplot(xy.t)
> NTplot(xy.t, type="confidence")
> 
> ## Not run: 
> ##D      NTplot(shiny=TRUE)  ## with any other arguments for initialization of the shiny app.
> ## End(Not run)
> 
> ## Not run: 
> ##D printbook.colors <- c(  ## based on "original" colors
> ##D   col.alpha             = "blue",
> ##D   col.notalpha          = "lightcyan",  ## this value is nonstandard
> ##D   col.beta              = "red",
> ##D   col.power             = "pink",
> ##D   col.pvalue            = "green",
> ##D   col.pvaluetranslucent = HH:::ColorWithAlpha("springgreen"),  ## this value is nonstandard
> ##D   col.critical          = "gray50",
> ##D   col.border            = HH:::ColorWithAlpha("black"),
> ##D   col.text              = "black",
> ##D   col.conf              = "lightgreen"
> ##D )
> ##D NTplot(ntcolors=printbook.colors) ## different colors
> ## End(Not run)
> 
> ## Not run: 
> ##D ## mean1 and xbar
> ##D   NTplot(mean0=0, mean1=2,  xbar=1.8,  xlim=c(-3, 5))
> ##D   NTplot(mean0=0, mean1=-2, xbar=-1.8, xlim=c(-5, 3),
> ##D          alpha.left=.05,  alpha.right=0)
> ##D   NTplot(mean0=0, mean1=2,  xbar=2.1,  xlim=c(-3, 5),
> ##D          alpha.left=.025, alpha.right=.025)
> ##D   NTplot(mean0=0, mean1=-2, xbar=-2.1, xlim=c(-5, 3),
> ##D          alpha.left=.025, alpha.right=.025)
> ##D 
> ##D ## mean1
> ##D   NTplot(mean0=0, mean1=2,  xbar=NA, xlim=c(-3, 5))
> ##D   NTplot(mean0=0, mean1=-2, xbar=NA, xlim=c(-5, 3),
> ##D          alpha.left=.05,  alpha.right=0)
> ##D   NTplot(mean0=0, mean1=2,  xbar=NA, xlim=c(-3, 5),
> ##D          alpha.left=.025, alpha.right=.025)
> ##D   NTplot(mean0=0, mean1=-2, xbar=NA, xlim=c(-5, 3),
> ##D          alpha.left=.025, alpha.right=.025)
> ##D 
> ##D ## xbar
> ##D   NTplot(mean0=0, mean1=NA, xbar=1.8,  xlim=c(-3, 5))
> ##D   NTplot(mean0=0, mean1=NA, xbar=-1.8, xlim=c(-5, 3),
> ##D          alpha.left=.05,  alpha.right=0)
> ##D   NTplot(mean0=0, mean1=NA, xbar=2.1,  xlim=c(-3, 5),
> ##D          alpha.left=.025, alpha.right=.025)
> ##D   NTplot(mean0=0, mean1=NA, xbar=-2.1, xlim=c(-5, 3),
> ##D          alpha.left=.025, alpha.right=.025)
> ##D 
> ##D ## t distribution
> ##D ## mean1 and xbar
> ##D   NTplot(mean0=0, mean1=2,  xbar=1.8,  xlim=c(-3, 5),
> ##D          distribution.name="t", df=4)
> ##D   NTplot(mean0=0, mean1=-2, xbar=-1.8, xlim=c(-5, 3),
> ##D          alpha.left=.05,  alpha.right=0, distribution.name="t", df=4)
> ##D   NTplot(mean0=0, mean1=2,  xbar=2.1,  xlim=c(-3, 5),
> ##D          alpha.left=.025, alpha.right=.025, distribution.name="t", df=4)
> ##D   NTplot(mean0=0, mean1=-2, xbar=-2.1, xlim=c(-5, 3),
> ##D          alpha.left=.025, alpha.right=.025, distribution.name="t", df=4)
> ##D 
> ##D ## mean1
> ##D   NTplot(mean0=0, mean1=2,  xbar=NA, xlim=c(-3, 5),
> ##D          distribution.name="t", df=4)
> ##D   NTplot(mean0=0, mean1=-2, xbar=NA, xlim=c(-5, 3),
> ##D          alpha.left=.05,  alpha.right=0, distribution.name="t", df=4)
> ##D   NTplot(mean0=0, mean1=2,  xbar=NA, xlim=c(-3, 5),
> ##D          alpha.left=.025, alpha.right=.025, distribution.name="t", df=4)
> ##D   NTplot(mean0=0, mean1=-2, xbar=NA, xlim=c(-5, 3),
> ##D          alpha.left=.025, alpha.right=.025, distribution.name="t", df=4)
> ##D 
> ##D ## xbar
> ##D   NTplot(mean0=0, mean1=NA, xbar=1.8,  xlim=c(-3, 5),
> ##D          distribution.name="t", df=4)
> ##D   NTplot(mean0=0, mean1=NA, xbar=-1.8, xlim=c(-5, 3),
> ##D          alpha.left=.05,  alpha.right=0, distribution.name="t", df=4)
> ##D   NTplot(mean0=0, mean1=NA, xbar=2.1,  xlim=c(-3, 5),
> ##D          alpha.left=.025, alpha.right=.025, distribution.name="t", df=4)
> ##D   NTplot(mean0=0, mean1=NA, xbar=-2.1, xlim=c(-5, 3),
> ##D          alpha.left=.025, alpha.right=.025, distribution.name="t", df=4)
> ##D 
> ##D ## confidence intervals
> ##D 
> ##D   NTplot(mean0=0, xlim=c(-3, 4), type="confidence")
> ##D   NTplot(xbar=01, xlim=c(-3, 4), type="confidence")
> ##D   NTplot(mean0=0, xlim=c(-4, 3), type="confidence",
> ##D          alpha.left=.05,  alpha.right=0)
> ##D   NTplot(mean0=0, xlim=c(-3, 3), type="confidence",
> ##D          alpha.left=.025, alpha.right=.025)
> ##D   NTplot(mean0=95, sd=10, xlim=c(65, 125), type="confidence",
> ##D          alpha.left=.025, alpha.right=.025)
> ##D   NTplot(mean0=95, sd=10, xlim=c(65, 125), type="confidence",
> ##D          alpha.left=.025, alpha.right=.025,
> ##D          distribution="t", df=10)
> ##D 
> ## End(Not run)
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>