typestar

Sampling and bootstrapping in R

Drawing samples, with and without replacement.

set.seed(1)
population <- 1:100

simple <- sample(population, size = 10)
with_replacement <- sample(population, size = 10, replace = TRUE)
shuffled <- sample(population)

weights <- rep(c(0.1, 0.9), length.out = 100)
weighted <- sample(population, 5, prob = weights)

boots <- replicate(200, mean(sample(population, 30, replace = TRUE)))
se <- sd(boots)

How it works

  1. sample draws without replacement by default.
  2. replace = TRUE and prob enable bootstrap and weights.
  3. replicate repeats a draw to build a sampling distribution.

Keywords and builtins used here

The run, in numbers

Lines
12
Characters to type
353
Tokens
92
Three-star pace
90 tpm

At the three-star pace of 90 tokens a minute, this run takes about 61 seconds.

Type this snippet

Step 3 of 3 in Describing, step 3 of 11 in Statistics.

← Previous Next →