Learn R Programming

HH (version 3.1-47)

CIplot: Illustration of the meaning of confidence levels.

Description

Illustration of the meaning of confidence levels. Generate sets of confidence intervals for independent randomly generated sets of normally distributed numbers. Low confidence levels give narrow intervals that are less likely to bracket the true value. Higher confidence levels increase the probability of bracketing the true value, and are also much wider and therefore less precise. The shiny app can animate how the increase in confidence level and width leads to a consequent decrease in precision.

Usage

CIplot(n.intervals = 100,
       n.per.row = 40,
       pop.mean = 0,
       pop.sd = 1,
       conf.level = 0.95,
       ...)

confintervaldata(n.intervals = 100, n.per.row = 40, pop.mean = 0, pop.sd = 1, conf.level = 0.95, seed, ...)

confinterval.matrix(x, conf.level = attr(x, "conf.level"), ...)

confintervalplot(x.ci, n.intervals = nrow(x.ci), pop.mean = attr(x.ci, "pop.mean"), pop.sd = attr(x.ci, "pop.sd"), n.per.row = attr(x.ci, "n.per.row"), xlim, ylim, ...)

shiny.CIplot(height = "auto")

Arguments

n.intervals

Number of sets of observations to generate. Each set leads to one confidence interval on the plot.

n.per.row

Number of observations in each set.

pop.mean, pop.sd

Population mean and standard deviation for generated set of n.per.row independent normally distributed random numbers.

conf.level

Confidence level of each of the n.per.row confidence intervals calculated from the generated datasets.

seed

Standard argument to rnorm.

x

Output matrix from confintervaldata.

x.ci

Output data.frame from confinterval.matrix.

xlim, ylim

Standard xyplot arguments.

height

Height of graph on web page in pixels.

Additional arguments. For CIplot, seed will be forwarded to confintervaldata, and xlim and ylim will be forwarded to confintervalplot. Any other additional arguments will be ignored.

Value

CIplot and confintervalplot return a "trellis" plot containing a plot of Confidence Intervals.

confintervaldata returns a matrix of n.intervals rows by n.per.row columns of independent normally distributed random numbers. The matrix has a set of attributes recording the arguments to the function.

confinterval.matrix returns a data.frame of n.intervals with three columns containing the lower bound, center, and upper bound of the confidence interval for each row of its input matrix. The data.frame has a set of attributes recording the arguments to the function.

shiny.CIplot returns a shiny app object which, when printed, runs a shiny app displaying the Confidence Interval plot and several slider controls.

Details

The shiny app has sliders for the n.intervals, n.per.row, pop.mean, pop.sd, and conf.level. Changes in the conf.level slider, either manually by animation, use the same set of generated data to show how increasing the confidence level increases the width of the confidence interval and consequently decreases the precision of the interval estimator.

Examples

Run this code
# NOT RUN {
## A. from the console

## example 1
CIplot()

## example 2
# }
# NOT RUN {
CIplot(n.intervals=100,
       n.per.row=40,
       pop.mean=0,
       pop.sd=1,
       conf.level=.95)
# }
# NOT RUN {
## example 3
# }
# NOT RUN {
tmp.data <- confintervaldata()
tmp.ci <- confinterval.matrix(tmp.data)
confintervalplot(tmp.ci)
# }
# NOT RUN {
## example 4
# }
# NOT RUN {
tmp.data <- confintervaldata(n.intervals=100,
                             n.per.row=40,
                             pop.mean=0,
                             pop.sd=1,
                             conf.level=.95)
tmp.ci <- confinterval.matrix(tmp.data)
confintervalplot(tmp.ci)
# }
# NOT RUN {

## B. shiny, initiated from the console

## example 5
# }
# NOT RUN {
  if (interactive())
    shiny.CIplot()
# }
# NOT RUN {
## example 6
# }
# NOT RUN {
  if (interactive())
    shiny.CIplot(height=800)  ## px
  ## take control of the height of the graph in the web page
# }

Run the code above in your browser using DataLab