Simulated sampling distribution of mean or other statistic

Share:

Description

Simulates the sample distribution of the specified statistic, for samples of the size(s) specified in numINsamp. Additionally a with replacement) sample is drawn from the specified population.

Usage

1
2
3
simulateSampDist(rpop = rnorm, numsamp = 100, numINsamp = c(4, 16),
                 FUN = mean, seed=NULL
      )

Arguments

rpop

Either a function that generates random samples from the specified distribution, or a vector of values that define the population (i.e., an empirical distribution)

numsamp

Number of samples that should be taken. For close approximation of the asymptotic distribution (e.g., for the mean) this number should be large

numINsamp

Size(s) of each of the numsamp sample(s)

FUN

Function to calculate the statistic whose sampling distribution is to be simulated

seed

Optional seed for random number generation

Value

List, with elements values, numINsamp and FUN

values

Matrix, with dimensions numsamp by numINsamp + 1. The first column has a random with replacement sample from the population, while the remaining length(numINsamp) columns hold simulated values from sampling distributions with samples of the specified size(s)

numINsamp

Input value of numINsamp

numsamp

Input value of numsamp

Author(s)

John Maindonald

References

Maindonald, J.H. and Braun, W.J. (2nd edn, 2006) Data Analysis and Graphics Using R, 2nd edn, Section 4.1

See Also

help(plotSampDist)

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
## By default, sample from normal population
simAvs <- simulateSampDist()
par(pty="s")
plotSampDist(simAvs)
## Sample from empirical distribution
simAvs <- simulateSampDist(rpop=rivers)
plotSampDist(simAvs)


## The function is currently defined as
function(rpop=rnorm, numsamp=100, numINsamp=c(4,16), FUN=mean,
seed=NULL){
    if(!is.null(seed))set.seed(seed)
    funtxt <- deparse(substitute(FUN))
    nDists <- length(numINsamp)+1
    values <- matrix(0, nrow=numsamp, ncol=nDists)
    if(!is.function(rpop)) {
      x <- rpop
      rpop <- function(n)sample(x, n, replace=TRUE)
    }
    values[,1] <- rpop(numsamp)
    for(j in 2:nDists){
      n <- numINsamp[j-1]
      for(i in 1:numsamp)values[i, j] <- FUN(rpop(n))
    }
    colnames(values) <- paste("Size", c(1, numINsamp))
    invisible(list(values=values, numINsamp=numINsamp, FUN=funtxt))
  }