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.
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)
These functions are run solely for the side effect of plotting the intervals, there is no meaningfull return value.
The mean of the population.
The standard deviation of the population.
The sample size for each sample.
The number of samples/intervals to create.
The confidence level of the intervals.
'z', 't', or 'both', should the intervals be based on the normal, the t, or both distributions.
Quantile for lower confidence bound.
Quantile for upper confidence bound.
The seed to use for the random number generation.
Greg Snow 538280@gmail.com
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.
z.test
, t.test
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