Learn R Programming

asbio (version 0.3-1)

samp.dist: Animated representations of a statistic's sampling distribution

Description

Samples from a parent distribution without replacement with sample size = s.size R times. At each iteration a statistic requested in stat is calculated. Thus a distribution of R statistic estimates is created. This distribution is shown as an animated relative frequency histogram. Sampling distributions for up to four different statistics utilizing two different parent distributions are possible. Sampling distributions can be combined in various ways by specifying a function in func (see below).

Usage

samp.dist(parent, parent2 = parent, s.size = 1, s.size2 = s.size, 
n.seq = seq(1, 30),R = 1000, breaks = 30, stat = mean, stat2 = NULL, 
stat3 = NULL, stat4 = NULL, fix.n = TRUE, xlab = expression(bar(x)), 
ylab = "Relative frequency", ylim = NULL, func = NULL, show.n = TRUE, 
show.SE = FALSE, est.density = TRUE, col.density = 4, lwd.density = 2, 
est.ylim = TRUE, anim = TRUE, interval = 0.01, col.anim = "rainbow", 
digits = 3,...)

samp.dist.fixn(parent, parent2 = parent, s.size = 1, s.size2 = s.size, 
R = 1000, breaks = 30, stat = mean, stat2 = NULL, stat3 = NULL, 
stat4 = NULL, xlab = expression(bar(x)), ylab = "Relative frequency", 
func = NULL, show.n = TRUE, show.SE = FALSE, anim = TRUE, 
interval = 0.01, col.anim = "rainbow", digits = 3,...)

samp.dist.n(parent, R = 500, n.seq = seq(1, 30), stat = mean, 
xlab = expression(bar(x)), ylab = "Relative frequency", breaks = 30, 
func = NULL, show.n = TRUE, show.SE = FALSE, est.density = TRUE, 
col.density = 4, lwd.density = 2, est.ylim = TRUE, ylim = NULL, 
anim = TRUE, interval = 0.5, col.anim = NULL, digits = 3,...)

samp.dist.tck()

Arguments

parent
A vector containing observations from a parental distribution. For computational efficiency datasets exceeding 100000 observations are not recommended.
parent2
An optional second parental distribution, useful for the construction of sampling distributions of test statistics.
s.size
Sample size to be taken at each of R iterations from the parental distribution.
s.size2
An optional second sample size if a second statistic is to be calculated.
n.seq
A two element vector specifying the smallest and largest in a range of demonstrated sample sizes. Requires fix.n=FALSE.
R
The number of samples to be taken from parent distribution(s).
breaks
Number of breaks in the histogram.
stat
The statistic whose sampling distribution is to be represented. Will work for any summary statistic; e.g. mean, var, median
stat2
An optional second statistic. Useful for conceptualizing sampling distributions of test statistics. Calculated from sampling parent2.
stat3
An optional third statistic. The sampling distribution is created from the same sample data used for stat.
stat4
An optional fourth statistic. The sampling distribution is created from the same sample data used for stat2
fix.n
Logical indicating whether or not sample size should be held constant in demonstrations (see below).
xlab
X-axis label.
ylab
Y-axis label.
ylim
Limits for Y-axis. Specify using a two element vector.
func
An optional function used to manipulate a sampling distribution or to combine the sampling distributions of two or more statistics. The function must have only sampling distributions, i.e. s.dist1, s.dist2, s.dist3,
show.n
A logical command, TRUE indicates that sample size for parent will be displayed.
show.SE
A logical command, TRUE indicates that bootstrap standard error for the statistic will be displayed.
est.density
A logical command, if TRUE then a density line is plotted over the histogram. Only used if fix.n = true.
col.density
The color of the density line. See est.density above.
lwd.density
The width of the density line. See est.density above.
est.ylim
Logical. If TRUE Y-axis limits are estimated logically for the animation. Consistent Y-axis limits make animations easier to visualize. Only used if fix.n = TRUE.
anim
A logical command indicating whether or not animation should be used.
interval
Animation speed. Decreasing interval increases speed.
col.anim
Color to be used in animation. Three changing color palettes: rainbow, gray, heat.colors, or "fixed" color
digits
The number of digits to be displayed in the bootstrap standard error.
...
Additional arguments from plot.histogram.

Value

  • Returns a representation of a statistic's sampling distribution in the form of a histogram.

Details

Sampling distributions of individual statistics can be created, or the function can be used in more sophisticated ways, e.g. to create sampling distributions of ratios of statistics, i.e. t*, F* etc. (see examples below). To provide pedagogical clarity animation for figures is provided. Two general uses of the function are possible. 1) One can demonstrate the accumulation of statistics for a single size with or without animation. This is useful because as more and more statistics are acquired the frequentist paradigm associated with sampling distributions becomes better represented (i.e the number of estimates is closer to infinity). This is elucidated by allowing the default fix.n = TRUE. Animation will be provided with the default anim = TRUE. This approach also allows specification of up two parent distributions, up to two sample sizes, and up to four distinct statistics (i.e. four distinct sampling distributions, representing four distinct estimators can be created). The arguments stat and stat3 will be drawn from parent, while stat3 and stat4 will be drawn from parent2. These distributions can be manipulated and combined in an infinite number of ways with an auxiliary function called in the argument func (see examples below). This allows depiction of sampling distributions made up of multiple estimators, e.g. test statistics. 2) One can provide a seamless animated demonstration of the effect of varying sample size on a sampling distribution by specifying n.fixed = FALSE. If n.fixed = FALSE is used, then a range of sample sizes (integers) must also be specified as a two element vector in n.seq. Note if n.fixed = FALSE then the arguments s.size and s.size2 will be superfluous. In addition, multiple statistics and parent populations are not supported if n.fixed = FALSE, although auxilary functions can still be called with func. Loading the package tcltk allows use of the function samp.dist.tck which provides an interactive GUI that runs samp.dist.

See Also

plot.histogram, hist, bootstrap.

Examples

Run this code
##Central limit theorem
#Four sample sizes, one at a time
exp.parent<-rexp(100000)
samp.dist(parent=exp.parent, s.size=1, R=1000) ## n = 1
samp.dist(parent=exp.parent, s.size=5, R=1000) ## n = 5
samp.dist(parent=exp.parent, s.size=10, R=1000) ## n = 10
samp.dist(parent=exp.parent, s.size=50, R=1000)## n = 50 

#All four at once
par(mfrow=c(2,2),mar=c(4.4,4.5,1,0.5))
samp.dist(parent=exp.parent, s.size=1, R=300,anim=FALSE) ## n = 1
samp.dist(parent=exp.parent, s.size=5, R=300,anim=FALSE) ## n = 5
samp.dist(parent=exp.parent, s.size=10, R=300,anim=FALSE) ## n = 10
samp.dist(parent=exp.parent, s.size=50, R=300,anim=FALSE) ## n = 50 

##n not fixed -- sample mean
exp.parent<-rexp(10000)
samp.dist(parent=exp.parent, col.anim="heat.colors",fix.n=FALSE,interval=.3)

##n not fixed -- sample mean and sample median (both are consistent and unbiased,
# but which is more efficient for mu?).  
# This will take a few seconds.
parent<-rnorm(10000,sd=3)
dev.new()
samp.dist(parent, R=1000,col.anim="heat.colors",fix.n=FALSE,interval=.1,
n.seq=seq(1,100),breaks=50,show.SE=TRUE)
dev.new()
samp.dist(parent, R=1000,col.anim="heat.colors",fix.n=FALSE,interval=.1,stat=median,
n.seq=seq(1,100),xlab="Median",show.SE=TRUE,breaks=50)

#How do the efficiency of the median and mean compare in a distribution with 10% 
#contamination?
parent<-c(rnorm(9000),rnorm(1000,mean=10))
samp.dist(parent, col.anim="heat.colors",fix.n=FALSE,interval=.3,
breaks=50,show.SE=TRUE)
dev.new()
samp.dist(parent, col.anim="heat.colors",fix.n=FALSE,interval=.3,stat=median,
xlab="Median",est.ylim=TRUE,show.SE=TRUE,breaks=50)


##Distribution of t-statistics under valid and invalid assumptions
#valid
parent<-rnorm(100000)
t.star<-function(s.dist1,s.dist2,s.dist3,s.dist4,s.size=6,s.size2=s.size){
MSE<-(((s.size-1)*s.dist3)+((s.size2-1)*s.dist4))/(s.size+s.size2-2)
func.res<-(s.dist1-s.dist2)/(sqrt(MSE)*sqrt((1/s.size)+(1/s.size2)))
func.res}

samp.dist(parent, parent2=parent, s.size=6, R=1000, breaks=35,stat=mean,stat2=mean,
stat3=var,stat4=var,xlab="t*", ylab="Relative frequency",func=t.star,show.n=FALSE)

curve(dt(x,10),from=-6,to=6,add=TRUE,lwd=2)
legend("topleft",lwd=2,col=1,legend="t(10)")

#invalid; same means (null true) but different variances and other distributional 
#characteristics.
parent<-runif(100000, min=0,max=2)
parent2<-rexp(100000)

samp.dist(parent, parent2=parent2, s.size=6, R=1000, breaks=35,stat=mean,stat2=mean,
stat3=var,stat4=var,xlab="t*", ylab="Relative frequency",func=t.star,show.n=FALSE)
curve(dt(x,10),from=-6,to=6,add=TRUE,lwd=2)
legend("topleft",lwd=2,col=1,legend="t(10)")

#Interactive GUI, require package 'tcltk'
samp.dist.tck()

Run the code above in your browser using DataLab