Learn R Programming

DAAG (version 1.25.6)

simulateSampDist: Simulated sampling distribution of mean or other statistic

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

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

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

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

Author

John Maindonald

References

Maindonald, J.H. and Braun, W.J. (3rd edn, 2010) Data Analysis and Graphics Using R, 3rd edn, Sections 3.3 and 3.4

See Also

help(plotSampDist)

Examples

Run this code
## 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))
  }

Run the code above in your browser using DataLab