Learn R Programming

TeachingDemos (version 2.13)

ci.examp: Plot examples of Confidence Intervals

Description

Generate reps samples from a normal distribution then compute and plot confidence intervals for each sample along with information about the population to demonstrate confidence intervals. Optionally change the confidence level using a Tk slider.

Usage

ci.examp(mean.sim = 100, sd = 10, n = 25, reps = 50, conf.level = 0.95,
  method = "z", lower.conf = (1 - conf.level)/2,
  upper.conf = 1 - (1 - conf.level)/2)
run.ci.examp(reps = 100, seed, method="z", n=25)

Value

These functions are run solely for the side effect of plotting the intervals, there is no meaningfull return value.

Arguments

mean.sim

The mean of the population.

sd

The standard deviation of the population.

n

The sample size for each sample.

reps

The number of samples/intervals to create.

conf.level

The confidence level of the intervals.

method

'z', 't', or 'both', should the intervals be based on the normal, the t, or both distributions.

lower.conf

Quantile for lower confidence bound.

upper.conf

Quantile for upper confidence bound.

seed

The seed to use for the random number generation.

Author

Greg Snow 538280@gmail.com

Details

These functions demonstrate the concept of confidence intervals by taking multiple samples from a known normal distribution and calculating a confidence interval for each sample and plotting the interval relative to the true mean. Intervals that contain the true mean will be plotted in black and those that do not include the true mean will be plotted in different colors.

The method argument determines the type of interval: 'z' will use the normal distribution and the known population standard deviation, 't' will use the t distribution and the sample standard deviations, 'both' will compute both for each sample for easy comparison (it is best to reduce reps to about 25 when using 'both').

The optional arguments lower.conf and upper.conf can be used to plot non-symmetric or 1 sided confidence intervals.

The function run.ci.examp also creates a Tk slider that will allow you to interactively change the confidence level and replot the intervals to show how the interval widths change with the confidence level.

See Also

z.test, t.test

Examples

Run this code
ci.examp()

if(interactive()) {
  run.ci.examp()
}

# 1 sided confidence intervals
ci.examp(lower.conf=0, upper.conf=0.95)

# non-symmetric intervals
ci.examp(lower.conf=0.02, upper.conf=0.97)

Run the code above in your browser using DataLab