Creating samples
You can create samples using sample
and replicate
functions.
I create smp
with the first given row.
> smp <- c(rep('A', 8), rep('B', 4), rep('C', 2), rep('D', 11), rep('E', 21), rep('F', 0))
> table(smp)
smp
A B C D E
8 4 2 11 21
>
> samples <- replicate(1000, sample(smp)) # creating 1000 samples
> dim(samples) # 1000 samples of 46 votes
[1] 46 1000
>
> samples[,1:3]
[,1] [,2] [,3]
[1,] "E" "A" "E"
[2,] "D" "E" "E"
[3,] "E" "D" "C"
[4,] "D" "E" "E"
[5,] "A" "D" "D"
[6,] "B" "E" "E"
[7,] "E" "A" "D"
[8,] "B" "C" "D"
[9,] "B" "A" "E"
[10,] "E" "E" "E"
[11,] "A" "E" "E"
[12,] "D" "E" "D"
[13,] "D" "E" "B"
[14,] "D" "B" "E"
[15,] "E" "E" "D"
[16,] "E" "E" "E"
[17,] "E" "B" "B"
[18,] "A" "D" "E"
[19,] "E" "E" "E"
[20,] "E" "E" "C"
[21,] "E" "E" "E"
[22,] "E" "D" "A"
[23,] "D" "D" "B"
[24,] "E" "E" "E"
[25,] "D" "D" "A"
[26,] "E" "E" "D"
[27,] "C" "A" "E"
[28,] "D" "E" "D"
[29,] "D" "E" "E"
[30,] "D" "A" "E"
[31,] "A" "D" "E"
[32,] "E" "E" "A"
[33,] "E" "E" "D"
[34,] "E" "E" "A"
[35,] "A" "E" "E"
[36,] "D" "C" "A"
[37,] "A" "D" "A"
[38,] "E" "D" "D"
[39,] "E" "D" "E"
[40,] "A" "E" "A"
[41,] "B" "A" "A"
[42,] "C" "A" "D"
[43,] "A" "A" "D"
[44,] "E" "B" "E"
[45,] "E" "D" "B"
[46,] "E" "B" "E"
If all you need are samples, sample
solves your problem.
Bootstraping
To bootstrap you need to compute a statistic. For example, I compute the weighted mean for a table of votes.
I'm using boot::boot
to bootsprap.
You must pass the original sample and a handler which receives the original sample s
and a vector with the indexes shuffled (idx
).
This function returns a boot
object that shows the bootstrap statistics.
> boot.smp <- boot::boot(smp, function(s, idx) {
+ tt <- table(s[idx])
+ weighted.mean(tt, as.numeric(factor(names(tt), labels=1:6, levels=LETTERS[1:6])))
+ }, 1000)
> boot.smp
CASE RESAMPLING BOOTSTRAP FOR CENSORED DATA
Call:
boot::boot(data = smp, statistic = function(d, w) {
tt <- table(d[w])
weighted.mean(tt, as.numeric(factor(names(tt), labels = 1:6,
levels = LETTERS[1:6])))
}, R = 1000)
Bootstrap Statistics :
original bias std. error
t1* 11.4 0.4126077 1.274827
The boot
object has other methods to compute confidence interval and return much more information.