1
$\begingroup$

Here is my original dataset from a voting activity. Each participant voted for one option (A,B,C,D,E,F) listed below. The numerical value is the total number of participants who voted for each option.

enter image description here

I want to bootstrap the voting for 1000 times (sample with replacement) and make a comparison between the pre-event and post-event voting for each category using independent sample t-test.

However, I don't know how to start with generating the 1000 bootstrapping samples...I have the "Boot" packages installed on R. Could anyone give me a hint what kind of steps I should take in order to generate 1000 bootstrap samples based on the original data I provide above?

$\endgroup$
3
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Hi Wilson, thank you very much for your answer! Could you tell me how to sample with replacement? I apologize for not stating this clearly in the original thread. $\endgroup$ – Ariana K. Sep 25 '15 at 22:46
  • 2
    $\begingroup$ To sample with replacement you call sample with the argument replace=TRUE. $\endgroup$ – Wilson Freitas Sep 26 '15 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.