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
sampledraws without replacement by default.replace = TRUEandprobenable bootstrap and weights.replicaterepeats a draw to build a sampling distribution.
Keywords and builtins used here
cmeanrepreplicatesamplesd
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.
Step 3 of 3 in Describing, step 3 of 11 in Statistics.